-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[system] Add modularCssLayers theme flag to split styles into multiple CSS layers
#46001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Netlify deploy previewBundle size report@mui/material parsed: 🔺+1.72KB(+0.32%) gzip: 🔺+482B(+0.31%) DetailsShow details for 100 more bundles (86 more not shown)@mui/material/CssBaseline parsed: 🔺+8KB(+12.66%) gzip: 🔺+3.66KB(+16.68%) |
…i into feat/mui-css-layer
…i into feat/mui-css-layer
experimental_modularCssLayers theme flag to split styles into multiple CSS layersmodularCssLayers theme flag to split styles into multiple CSS layers
mapache-salvaje
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @siriwatknp ! Very cool feature!
Co-authored-by: mapache-salvaje <71297412+mapache-salvaje@users.noreply.github.com> Co-authored-by: Marija Najdova <mnajdova@gmail.com> Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
mapache-salvaje
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more suggestions here to sharpen the Caveats section but otherwise I think the doc's ready to go!
Co-authored-by: mapache-salvaje <71297412+mapache-salvaje@users.noreply.github.com> Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
…ple CSS layers (mui#46001) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
closes #33012
closes #28911
closes #30536
Docs: https://deploy-preview-46001--material-ui.netlify.app/material-ui/customization/css-layers/
Playground: https://stackblitz.com/edit/6a5ghxfh?file=src%2Findex.tsx
Motivation
By splitting styles into different CSS layers. It eliminates the specificity issue when theming or with
sxprop.For example, customize the padding of the
DialogContentusingsxprop does not work because the styles of thesxprop cannot beat the default styles fromDialogdue to this selector.MuiDialogTitle-root+.css-1foygqf-MuiDialogContent-root:User will need to increase the specificity in order to override. So if styles are split into different CSS layers
@layer, the issue above is gone.With the same code as above, the generated styles will be:
Side benefits
Independent of styles injection order. With modern framework like Next.js evolves, the style injection order might not be straightforward which could produce styles inconsistency for Emotion.
By moving to CSS layers, we don't need to worry about the injection order because CSS will take care of it. Just need to make sure that the layer order is always at the top of the head.
Implementation
modularCssLayers) enabled, Material UI serialize incoming styles (turns object, array, function) to a string and wrap them with@layer layerName. Since the serialized styles come from Emotion, this works perfectly fine.The styles are split into 5 layers:
@layer global: for styles coming fromGlobalStylescomponent (CssBaselinetoo)@layer default: the base styles coming from Material UI components.@layer theme: the component styles fromcreateTheme().@layer custom: styles fromstyled()that does not haveMuiname prefix.@layer sx: styles from thesxprop.There is no performance downside because the serialization has to be done anyway. When Emotion receive the serialized styles, it will skip the serialization and go through injection process.
modularCssLayersis enabled: