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
[CssVarsProvider][Material UI] Exclude dark mode variables from :root
stylesheet
#34131
Conversation
@material-ui/core: parsed: +0.05% , gzip: +0.07% |
import { expect } from 'chai'; | ||
import excludeVariablesFromRoot from './excludeVariablesFromRoot'; | ||
|
||
describe('excludeVariablesFromRoot', () => { |
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.
Could we add test in the CssVarsProvider
about this logic? Also, how do we make sure we remove the variables only in light mode? Adding tests for both cases would be great.
I would argue that the alternative approach is easier to learn and apply rather than adding new API, and at this moment bundle size doesn't really concerns me that much.
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.
Could we add test in the CssVarsProvider about this logic? Also, how do we make sure we remove the variables only in light mode? Adding tests for both cases would be great.
Added 2 regression tests
- Test the default dark mode to ensure that the variables exist in dark mode.
- The default mode is dark and then switches to light to ensure that those variables does not exist in light mode.
I would argue that the alternative approach is easier to learn and apply rather than adding new API, and at this moment bundle size doesn't really concerns me that much.
It might create confusion for developers and be prone to errors:
- Introducing the
unknown
variables (do we need to discuss the name?) - Need to document the
unknown
variables - Developer could accidentally remove the variables in the light mode which will cause unexpected result.
- Bundle size will grow if you have more than
light
and `dark.
I rather have this done internally, so developers do not need to worry about these edge cases.
/** | ||
* @internal These variables should not appear in the :root stylesheet when the `defaultMode="dark"` | ||
*/ | ||
const excludeVariablesFromRoot = (cssVar: string) => |
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.
I am wondering if people will ever need this as feature on their side. If yes, we could export this so that they can extend/override it.
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.
Left some comments for consideration in the future.
…rial-ui/fallback-vars
…rial-ui/fallback-vars
close #34084
Before: https://codesandbox.io/s/material-ui-forked-84f6p7?file=/index.tsx
After: https://codesandbox.io/s/create-react-app-with-typescript-forked-nqvqnu?file=/src/index.tsx
Root cause
There are CSS variables that are intended to be used in dark mode only:
overlays-{number}
palette-AppBar-darkBg
palette-AppBar-darkColor
Currently, the application works perfectly if the default mode is
light
because the above variables exist only in dark stylesheetHowever, if the
defaultMode="dark"
the variables are applied to the:root
stylesheet as well which causes thePaper
andAppBar
that consume those variables to behave differentlyChanges
This PR introduces an internal option in MUI System which lets Material UI excludes those variables from being generated to the
:root
stylesheet.Alternative
The other way to solve this is to provide those variables in light mode with
unknown
values like this:The downside is that it increases the bundle size (+26 variables) which does not sound right to me. Excluding them from the
:root
stylesheet sounds like a better solution.