Skip to content
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

Merged
merged 16 commits into from
Nov 4, 2021

Conversation

alexfauquette
Copy link
Member

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))

@mui-pr-bot
Copy link

mui-pr-bot commented Nov 1, 2021

Details of bundle changes

Generated by 🚫 dangerJS against b863e1d

@siriwatknp
Copy link
Member

I think it should be a deprecation, not a breaking change.

@alexfauquette
Copy link
Member Author

@siriwatknp I imitated the @deprecated comments from what I found in the codebase. Is there something else to do in order to deprecate a function?

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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?

Copy link
Member

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?

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right.

Copy link
Member

@oliviertassinari oliviertassinari Nov 4, 2021

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.

Copy link
Member

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 😁.

Copy link
Member Author

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

Comment on lines 68 to 88
### 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.

Copy link
Member

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.

@m4theushw m4theushw removed their request for review November 2, 2021 11:36
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 }),
});
Copy link
Member Author

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),
Copy link
Member Author

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

Copy link
Member

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.

Copy link
Member

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

Comment on lines 15 to 18
/*
* Enable to use color-scheme property to set dark mode
*/
enableColorScheme?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*
* 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) => {
Copy link
Member

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?

@@ -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 }) {
Copy link
Member

@mnajdova mnajdova Nov 3, 2021

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.

Suggested change
function CssBaseline({ enableColorScheme = false, ...inProps }) {
function CssBaseline(inProps) {

Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { children } = props;
const { children, enableColorScheme = false } = props;

@@ -229,15 +228,6 @@ export function ThemeProvider(props) {
spacing,
},
dense ? highDensity : null,
Copy link
Member

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.
Copy link
Member

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:

Copy link
Member

@mnajdova mnajdova left a 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 :)

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@alexfauquette alexfauquette merged commit 5b886d5 into mui:master Nov 4, 2021
@alexfauquette alexfauquette deleted the use-color-schem branch November 4, 2021 10:38
@oliviertassinari oliviertassinari changed the title [core] use color-scheme property to deal with dark mode [core] Use color-scheme property to deal with dark mode Nov 4, 2021
@mnajdova mnajdova changed the title [core] Use color-scheme property to deal with dark mode [CSSBaseline] Add enableColorScheme prop so enable using color-scheme property to deal with dark mode Nov 8, 2021
@oliviertassinari oliviertassinari added the component: CssBaseline The React component label Aug 26, 2022
@oliviertassinari oliviertassinari changed the title [CSSBaseline] Add enableColorScheme prop so enable using color-scheme property to deal with dark mode [CssBaseline] Use enableColorScheme to set color-scheme for dark mode Aug 26, 2022
@oliviertassinari oliviertassinari changed the title [CssBaseline] Use enableColorScheme to set color-scheme for dark mode [CssBaseline] Use enableColorScheme to set color-scheme for dark mode Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: CssBaseline The React component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Calendar icon is almost invisible when in dark mode [theme] Improve darkScrollbar helper
5 participants