-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
[CssBaseline] Use enableColorScheme
to set color-scheme
for dark mode
#29454
Conversation
I think it should be a deprecation, not a breaking change. |
@siriwatknp I imitated the |
@@ -172,6 +172,9 @@ export * from './Container'; | |||
export { default as CssBaseline } from './CssBaseline'; | |||
export * from './CssBaseline'; | |||
|
|||
/** | |||
* @deprecated use color-scheme in CssBaseline or ScopedCssBaseline to modify scrollbar color |
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.
* @deprecated use color-scheme in CssBaseline or ScopedCssBaseline to modify scrollbar color | |
* @deprecated the dark scrollbar is automatically added by the `CSSBaseline` and `ScopeCSSBaseline` using the color-scheme attribute |
And seeing this, it is a breaking change. Could we maybe add a prop to enable this? Something like setColorScheme
. cc @siriwatknp what do you think?
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 maybe add a prop to enable this
a prop to which component?
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.
Ah, I got it. a prop to CssBaseline
. Yes, that make sense.
<CssBaseline enableColorScheme />
@mnajdova @alexfauquette What do you think?
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.
So if I resume, enableColorScheme
is by default set to false
such that the behavior does not change when updating the minor version. Dev can set it to true
in order to use color-scheme
instead of darkScrollbar
.
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.
Yes, you are right.
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.
What's the use case for the opt-in prop? Setting the right color sheme by default sounds like a bug fix to me.
If it's about playing it safe because we are unsure, then I would argue for trying out the path that will maximize our learning: be greedy by default, and take one step down if necessary.
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.
Regarding the deprecation, we only deprecated it with TypeScript, not with the runtime, is it expected?
At some point I was wondering about turning the helper to be a generic high level scrollbar color customization tool. But I'm not sure there is a market for it, it might be overkill 😁.
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.
On my side, I have been confused, between the code of the CssBaseline
and the code of the website.
The idea was that replacing darkScrollbar
with color-scheme
could lead to style modification for users. But it is not the case, because darkScrollbar
is not set by default in the theme.
So if we set colorScheme: theme.palette.mode
as the default behavior of the CssBaseline the following should happen:
- For projects using
darkScrollbar
as proposed in the doc, the scollbar will still be override - For projects not using
darkScrollbar
scrollbar colors will be override (which is an improvement)
Then, it can be seen as a clean bug fix replacing a hack
### Scrollbars | ||
|
||
The colors of the scrollbars can be customized to improve the contrast (especially on Windows). Add this code to your theme (for dark mode). | ||
|
||
```jsx | ||
import darkScrollbar from '@mui/material/darkScrollbar'; | ||
|
||
const theme = createTheme({ | ||
components: { | ||
MuiCssBaseline: { | ||
styleOverrides: { | ||
body: theme.palette.mode === 'dark' ? darkScrollbar() : null, | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
This website uses `darkScrollbar` when dark mode is enabled. | ||
Be aware, however, that using this utility (and customizing `-webkit-scrollbar`) forces MacOS to always show the scrollbar. | ||
|
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 suggest to preserve this section and adding deprecate note after ### Scroll bars
and also add a link to color-scheme
example.
WebkitFontSmoothing: 'antialiased', // Antialiasing. | ||
MozOsxFontSmoothing: 'grayscale', // Antialiasing. | ||
// Change from `box-sizing: content-box` so that `width` | ||
// is not affected by `padding` or `border`. | ||
boxSizing: 'border-box', | ||
// Fix font resize problem in iOS | ||
WebkitTextSizeAdjust: '100%', | ||
colorScheme: theme.palette.mode, | ||
...(enableColorScheme && { colorScheme: theme.palette.mode }), | ||
}); |
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 assume these functions are exported to be used by ScopedCssBaseline
, but not used by dev. I do not found any use of them somewhere else. That's why I modified their signature.
})(({ theme }) => ({ | ||
...html, | ||
})(({ theme, enableColorScheme }) => ({ | ||
...html(theme, enableColorScheme), |
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 do not found how to pass enableColorScheme
to this component, and at the same time avoid the following warning
Warning: React does not recognize the
enableColorScheme
prop on a DOM
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.
index f67c9d661d..c833821012 100644
--- a/packages/mui-material/src/ScopedCssBaseline/ScopedCssBaseline.js
+++ b/packages/mui-material/src/ScopedCssBaseline/ScopedCssBaseline.js
@@ -34,7 +34,7 @@ const ScopedCssBaselineRoot = styled('div', {
const ScopedCssBaseline = React.forwardRef(function ScopedCssBaseline(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiScopedCssBaseline' });
- const { className, component = 'div', ...other } = props;
+ const { className, component = 'div', enableColorScheme = false, ...other } = props;
const ownerState = {
...props,
This should help, we don't want to spread it on the DOM.
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.
And note that you can access it in the styles via ownerProps.enableColorScheme
/* | ||
* Enable to use color-scheme property to set dark mode | ||
*/ | ||
enableColorScheme?: boolean; |
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.
/* | |
* Enable to use color-scheme property to set dark mode | |
*/ | |
enableColorScheme?: boolean; | |
/* | |
* Enable `color-scheme` css property to use `theme.palette.mode`. | |
* For more details, check out https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme | |
* For browser support, check out https://caniuse.com/?search=color-scheme | |
*/ | |
enableColorScheme?: boolean; |
@@ -24,9 +24,9 @@ export const body = (theme) => ({ | |||
}, | |||
}); | |||
|
|||
export const styles = (theme) => { | |||
export const styles = (enableColorScheme) => (theme) => { |
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.
Why not this
export const styles = (theme, enableColorScheme) => {
}
?
I assume that styles
is public?
packages/mui-material/src/ScopedCssBaseline/ScopedCssBaseline.d.ts
Outdated
Show resolved
Hide resolved
@@ -54,12 +55,12 @@ export const styles = (theme) => { | |||
/** | |||
* Kickstart an elegant, consistent, and simple baseline to build upon. | |||
*/ | |||
function CssBaseline(inProps) { | |||
function CssBaseline({ enableColorScheme = false, ...inProps }) { |
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.
Oops, with the current implementation we would not be able to pull these from the theme's defaultProps
. Please destructure this on line 60 after you have the props from the theme as well.
function CssBaseline({ enableColorScheme = false, ...inProps }) { | |
function CssBaseline(inProps) { |
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.
@@ -54,12 +55,12 @@ export const styles = (theme) => { | |||
/** | |||
* Kickstart an elegant, consistent, and simple baseline to build upon. | |||
*/ | |||
function CssBaseline(inProps) { | |||
function CssBaseline({ enableColorScheme = false, ...inProps }) { | |||
const props = useThemeProps({ props: inProps, name: 'MuiCssBaseline' }); | |||
const { children } = props; |
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.
const { children } = props; | |
const { children, enableColorScheme = false } = props; |
@@ -229,15 +228,6 @@ export function ThemeProvider(props) { | |||
spacing, | |||
}, | |||
dense ? highDensity : null, |
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.
Actually, instead of removing this we could do:
{
components: {
MuiCssBaseline: {
defaultProps: {
enableColorScheme: true,
},
},
},
},
With this, you shouldn't need the changes in the previous three files. For this to work you need to fix https://github.com/mui-org/material-ui/pull/29454/files#r742040199 :)
Be aware, however, that using this utility (and customizing `-webkit-scrollbar`) forces MacOS to always show the scrollbar. | ||
|
||
This is a deprecated functionality. |
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.
https://deploy-preview-29454--material-ui.netlify.app/components/css-baseline/#scrollbars
The text is hard to notice that it is deprecated. I suggest to change to something like this.
### Scrollbars
> This API is deprecated, consider using [color-scheme](#color-scheme) instead.
The colors of the scrollbars can be customized to improve the contrast (especially on Windows). Add this code to your theme (for dark mode).
### Color scheme
This API is introduced in @mui/material (v5.0.7) for switching between "light" and "dark" modes of native components such as scrollbar, using the `color-scheme` css property. To enable it, you can set `enableColorScheme=true` as follow:
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.
Looks great 👌 Now we won't have anymore mismatch in different design systems as we would depend on the native implementation of the browser :)
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.
👍
color-scheme
property to deal with dark mode color-scheme
property to deal with dark mode
color-scheme
property to deal with dark mode enableColorScheme
prop so enable using color-scheme
property to deal with dark mode
enableColorScheme
prop so enable using color-scheme
property to deal with dark modecolor-scheme
for dark mode
color-scheme
for dark modeenableColorScheme
to set color-scheme
for dark mode
Fix #25016 and fix mui/mui-x#2855
This PR use
color-scheme
to set dark/light mode to the browser such that native components have correct colors (#25016 (comment))