Skip to content

[react] Support transpiled library for globalCss and keyframes#155

Merged
siriwatknp merged 2 commits intomasterfrom
fix/transpiled-lib
Jun 21, 2024
Merged

[react] Support transpiled library for globalCss and keyframes#155
siriwatknp merged 2 commits intomasterfrom
fix/transpiled-lib

Conversation

@siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Jun 21, 2024

Found this bug from mui/material-ui#42640

Root cause

The code of CssBaseline looks like this:

const GlobalStyles = globalCss(({ theme }) => styles(theme));

However, after building Material UI as a package and Next.js import it, the code before passing to Pigment is:

const GlobalStyles = globalCss(_c = ({
  theme
}) => styles(theme));
_c1 = GlobalStyles;
var c1;

Notice that the value passed to globalCss is a variable instead of a function. Pigment CSS thinks that it's not a function, that's why the CSS does not generated (with Emotion error):

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}`

The fix is simple, we check the type after the evaluation and then pass the theme if it's a function.


@siriwatknp siriwatknp added the type: bug It doesn't behave as expected. label Jun 21, 2024
@siriwatknp siriwatknp requested a review from brijeshb42 June 21, 2024 05:01
Comment on lines +28 to +30
html {
color: red;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the change, this will not be generated.

@siriwatknp siriwatknp merged commit 659ed43 into master Jun 21, 2024
@siriwatknp siriwatknp deleted the fix/transpiled-lib branch June 21, 2024 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants