-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
SX and Styled can't override applyStyles css #44488
Comments
This is expected as a default behavior. However, you can customize const theme = createTheme({
cssVariables: {
colorSchemeSelector: '.mode-%s',
},
colorSchemes: {
dark: {},
light: {},
},
applyStyles: function (key: string, styles: any) {
if (typeof styles === 'string') {
return `*:where(.mode-${key}:not(:has(.mode-light))) & {${styles}}`;
}
return {
[`*:where(.mode-${key}:not(:has(.mode-light &))) & `]: styles,
};
},
}); |
I mark this as an enhancement to make the And to add a docs about customizing |
It's good to know that you can override the applyStyles function, but that's not the crux of this issue. The issue is that there is a breaking change when switching to the new system of using applyStyles and there is seemingly no workaround for it. This code creates green text in both themes
But that's not possible with the new system. This results in green text in light mode and red text in dark mode.
It's a problem with sx and styled modifying the base styles of a component and so the dark theme styles always overwrite those. Honestly I'm not sure how this is even solvable with out a major change like moving styles into layers so that sx/styled always override. |
That's true. I will add docs for this but I don't see a perfect transition, the Anyway, the next step is to add option to enable CSS |
Steps to reproduce
Repro link
Steps:
theme.applyStyles
to apply dark theme styles to componentCurrent behavior
Sx and Styled theme behavior breaks with this new paradigm. They can't override the styles if the base component is using the new applyStyles helper since it creates a css selector with higher specificity
Expected behavior
Sx and Styled should be able to override styles like they used to with
theme.palette.mode
conditional logicContext
There are multiple issues with the new css variables system.
theme.applyStyles
due to specifictytheme.applyStyles
doesn't support template literal syntax so the css selector has to either be extracted to a var or hardcodedtheme.applyStyles
, then the actual code files say to not use spread syntax. ie. Docs, Code.mode-light
to a component won't apply those styles since.mode-dark
has higher specificity. Refer to link for exampleThe biggest issue is the breaking change with sx/styled behavior
Your environment
npx @mui/envinfo
Search keywords: css variables, css vars, applyStyles, sx, styled
The text was updated successfully, but these errors were encountered: