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

[system] Add defaultMode to InitColorSchemeScript #44139

Merged
merged 2 commits into from
Oct 21, 2024

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Oct 17, 2024

closes #43972

Root cause

InitColorSchemeScript has a hard-coded default mode as system when the user first visit to the site, however this contradict the flow if the project has <ThemeProvider defaultMode="light"> on user's dark environment.

Solution

  • Add defaultMode to InitColorSchemeScript.
  • Update docs to make sure that both places are defined for custom defaultMode.

Preview: https://deploy-preview-44139--material-ui.netlify.app/material-ui/customization/dark-mode/#initcolorschemescript-component

@siriwatknp siriwatknp added package: system Specific to @mui/system v6.x labels Oct 17, 2024
@mui-bot
Copy link

mui-bot commented Oct 17, 2024

Netlify deploy preview

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against ab80c08

@@ -146,6 +146,14 @@ To set a different default mode, pass the `defaultMode` prop to the ThemeProvide
The `defaultMode` value can be `'light'`, `'dark'`, or `'system'`.
:::

### InitColorSchemeScript component
Copy link
Member

@oliviertassinari oliviertassinari Oct 17, 2024

Choose a reason for hiding this comment

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

Should we inverse the docs order? It looks like if <InitColorSchemeScript defaultMode="dark"> is used, <ThemeProvider defaultMode="dark"> is a no-op.

It would make sense to document this as well?

Now, developers need to set both because the initial load might not have InitColorSchemeScript or System based components while the next page has. If all pages have InitColorSchemeScript, then I guess it's a 100% no-op.


Actually, It seems that the whole page is wrong. This is MUI System docs no? so it shouldn't be documented in Material UI.


We are also missing the API table page for InitColorSchemeScript.

* The default mode when the storage is empty (user's first visit)
* @default 'system'
*/
defaultMode?: 'system' | 'light' | 'dark';
Copy link
Member

@oliviertassinari oliviertassinari Oct 17, 2024

Choose a reason for hiding this comment

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

I'm not sure about the prop name: "default"? It sounds more like "initial" as there is a notion of session. Should we rename it?

@@ -8,6 +8,11 @@ export const DEFAULT_COLOR_SCHEME_STORAGE_KEY = 'color-scheme';
export const DEFAULT_ATTRIBUTE = 'data-color-scheme';

export interface InitColorSchemeScriptProps {
/**
* The default mode when the storage is empty (user's first visit)
Copy link
Member

@oliviertassinari oliviertassinari Oct 17, 2024

Choose a reason for hiding this comment

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

Suggested change
* The default mode when the storage is empty (user's first visit)
* The default mode when the storage is empty (user's first visit).

x al the others on this file.

Edit. Fixed in #44187.

@siriwatknp siriwatknp merged commit 1a018b7 into mui:master Oct 21, 2024
24 checks passed
@oliviertassinari
Copy link
Member

@siriwatknp what do you think about my previous comments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: system Specific to @mui/system v6.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MUI v6] Setting default mode flickers the light/dark theme at first render
4 participants