Functions that are interpolated in css calls will be stringified.
See original GitHub issueemotion
version: 10.0.2react
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:
- Created 5 years ago
- Comments:11 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
what about conditionals such as
This issue still ranks high on search engines, so here’s a solution.