Open RickyRoller opened 1 day ago
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
This is expected by design. When CSS variables is enable, the applyStyles
create a new stylesheet for overriding styles. The result correspond to the code:
const BaseButton = styled(Button)(({ theme }) => ({
background: 'red',
color: '#fff',
...theme.applyStyles('dark', {
background: 'steelblue',
color: '#fff',
}),
}));
// css
.button-hash {
background: 'red',
color: '#fff',
}
:where(.mode-dark) .button-hash {
background: 'steelblue',
color: '#fff',
}
Based on your code, if you want to use .mode-light
to change the styles, you must change the component implementation to use CSS variables as values instead of raw values.
const BaseButton = styled(Button)(({ theme }) => ({
background: theme.vars.palette.*,
color: theme.vars.palette.*,
}));
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
``` System: OS: macOS 14.1.1 Binaries: Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node npm: 10.8.3 - ~/.nvm/versions/node/v20.12.2/bin/npm pnpm: 9.13.0 - ~/.nvm/versions/node/v20.12.2/bin/pnpm Browsers: Chrome: 131.0.6778.71 Edge: Not Found Safari: 17.1 ```npx @mui/envinfo
Search keywords: css variables, css vars, applyStyles, sx, styled