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
[system] Fix color-scheme implementation #34639
Conversation
@material-ui/system: parsed: -0.54% 😍, gzip: -0.47% 😍 |
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.
👍 for the isolation of different React trees that this change brings. From what I understand, we can now natively nest light and dark themes inside the same React tree with Joy UI, as we can with Material UI. I can't think of a better alternative, for example, if we had CssVarsProviders create a div to host the style, then it wouldn't work with the portaled components.
test/regressions/fixtures/CssBaseline/MaterialScopedCssBaseline.js
Outdated
Show resolved
Hide resolved
Signed-off-by: Siriwat K <siriwatkunaporn@gmail.com>
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.
Great, I left few small comments. I was missing something like this in my preview for MD3 to be able to see at the same time light & dark mode :)
|
||
## Global reset | ||
|
||
You might be familiar with [normalize.css](https://github.com/necolas/normalize.css), a collection of HTML element and attribute style-normalizations. |
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.
We probably should specify that it is used, this is why we are mentioning 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.
I think it is mentioned to give the reader a clue that it is a similar purpose. I checked normalize.css
and we are not using the same global reset.
( | ||
Object.entries(theme.colorSchemes) as Array<[DefaultColorScheme, ColorSystem]> | ||
).forEach(([key, scheme]) => { | ||
colorSchemeStyles[theme.getColorSchemeSelector(key).replace(/\s*&/, '')] = { |
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.
Can you clarify what is this doing?
theme.getColorSchemeSelector(key).replace(/\s*&/, '')
We have it in many places, maybe we should extract the logic?
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.
theme.getColorSchemeSelector(key)
returns a string, [data-joy-color-scheme="$key"] &
, with an &
at the end so that developers can use it to write custom styles in styled
or sx
like this:
styled('div')(({ theme }) => ({
[theme.getColorSchemeSelector('dark')]: { ... }
// easier than writing
// [`${theme.getColorSchemeSelector('dark')} &`]: { ... }
}))
However, the CssBaseline define the selector as global so the .replace(/\s*&/, '')
is required to remove &
at the end:
theme.getColorSchemeSelector(key).replace(/\s*&/, '') // '[data-joy-color-scheme="dark"]'
I think this case would be rare but I could provide the attribute
from the theme to be used like:
[`[${theme.attribute}="dark"]`]: { ... }
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.
Thinking again, I rather go with theme.getColorSchemeSelector(key).replace(/\s*&/, '')
for now because I don't want to add a new property to the theme. Let's wait to see how the community would like to use the API.
…al-ui into system/fix-server-mode
https://deploy-preview-34639--material-ui.netlify.app/
Breaking changes
The
enableColorScheme
prop has been removed fromCssVarsProvider
andgetInitColorScheme
(both Material UI and Joy UI). Migration:<CssBaseline enableColorScheme />
.<CssBaseline />
, see the docs.Preview:
Docs:
Motivation
From @oliviertassinari's comment, applying inline
color-scheme: light | dark
to the html tag creates a global effect for complex application (our website is one of the examples where some pages useCssVarsProvider
but some are not).With the current implementation (inline style), the homepage improvement requires several workarounds to override the inline color-scheme.
To fix this, the
color-scheme
should be moved toCssBaseline
so thatChanges
enableColorScheme
flag toCssBaseline
.CssBaseline
andScopedCssBaseline
to Joy UI because theenableColorScheme
is removed from theCssVarsProvider
.How it works
The logic lives in
CssBaseline
which generates style sheets for each color scheme (to prevent flashing) when enabled:For example, if the theme has three color schemes:
light, dark, and orange(custom)
. Each color scheme must contain the mode to be used (eitherlight
ordark
). Material UI already contains the mode in the default light and dark palette, so this PR does the same for Joy UI.Developers can configure the mode like this:
Benefits
ScopedCssBaseline
.color-scheme
is not attached as an inline style anymore, it is in the stylesheet. This work with any element thatdata-color-scheme="..."
CssVarsProvider
andgetInitColorSchemeScript
.Tested that it works well in #33545
Test
Added visual regression to make sure that CssBaseline works for both Material UI and Joy UI. https://app.argos-ci.com/mui/material-ui/builds/5852