question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Functions that are interpolated in css calls will be stringified.

See original GitHub issue
  • emotion version: 10.0.2
  • react version: 16.3

Relevant code.

// @flow
import { type SerializedStyles } from '@emotion/utils';
import themeData from './theme';

// Use this function in your components to provide a fallback theme in case
// no ThemeProvider is available
const withFallback = (fn: Object => SerializedStyles) => ({
  theme,
  ...props
}: {
  theme: Object,
}) => {
  const t = Object.keys(theme).length ? theme : themeData.light;
  return fn({ theme: t, ...props });
};

export default withFallback;

What you did:

I created this function to be used in my UI library to make sure a theme is always defined even if no ThemeProvider is available.

Usage:

css`
  color: ${withFallback(props => props.theme.primary)};
`

What happened:

When I run it, in some contexts, I get this error:

console.error node_modules/@emotion/serialize/dist/serialize.cjs.dev.js:135
      Functions that are interpolated in css calls will be stringified.
      If you want to have a css call based on props, create a function that returns a css call like this
      let dynamicStyle = (props) => css`color: ${props.color}`
      It can be called directly with props or interpolated in a styled call like this
      let SomeComponent = styled('div')`${dynamicStyle}`

and the resulting CSS, in fact, is the stringified function.

Reproduction:

https://github.com/FezVrasta/emotion-interpolation-repro

Just run the tests to see the error

Problem description:

My withFallback function seems to work fine if used as:

css`
  color: ${withFallback(props => props.theme.primary)};
`

but throws error if used as:


const textStyles = () => css`
  color: ${withFallback(props => props.theme.primary)};
`;

css`
  ${textStyles()}
`;

(I also tried:

const textStyles = () => () => css`
  color: ${withFallback(props => props.theme.primary)};
`;

with the same result)

Suggested solution:

Not sure

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
littlehome-eugenecommented, Oct 14, 2019

what about conditionals such as

 const disableStyle = props => css`
 color: ${props => (props.disabled ? props.theme.colors.disabled : null)};
8reactions
WNemenchacommented, Jul 31, 2019

This issue still ranks high on search engines, so here’s a solution.

// @flow
import styled from '@emotion/styled';
import { css } from '@emotion/core';

- const color = css`
+ const color = props => css`
-  color: ${props => props.color};
+  color: ${props.color};
`;

const App = styled.div`
  ${color};
`;

export default App;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing functions into css prop in emotion js - Stack Overflow
index.js:2177 Functions that are interpolated in css calls will be stringified. If you want to have a css call based on props, ...
Read more >
The css Prop - Emotion
The function returns an object containing the computed name and flattened styles. The returned object is understood by emotion at a low level...
Read more >
Thank Me Later: Use Styled Components's css helper ...
Interpolated functions work as they do with Styled Components because the styled object's members are tagged templates that provide that ...
Read more >
UNPKG - @emotion/css
The CDN for @emotion/css. ... @emotion/css/dist/emotion-css.umd.min.js.map. Version: ... 'Functions that are interpolated in css calls will be stringified.
Read more >
@emotion/css | Yarn - Package Manager
0.0 (2018-10-27). Emotion 10 is a big change that we're really excited about with improvements to the css prop, a Global component for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found