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

[Mui v6] Set default mode at first render #43622

Closed
mtr1990 opened this issue Sep 5, 2024 · 12 comments · Fixed by #43951
Closed

[Mui v6] Set default mode at first render #43622

mtr1990 opened this issue Sep 5, 2024 · 12 comments · Fixed by #43951
Assignees
Labels
enhancement This is not a bug, nor a new feature package: system Specific to @mui/system

Comments

@mtr1990
Copy link

mtr1990 commented Sep 5, 2024

Steps to reproduce

In the process of migrating from MUI v5 to MUI v6

In v5 I can specify "dark" or "light" as default but in v6 the default is always "system"

x-9

Here is the code that worked in v5. But in v6 the defaultMode flags have been removed

  <InitColorSchemeScript
    defaultMode={"light}
    modeStorageKey={schemeConfig.modeStorageKey}
  />
  
 <AppRouterCacheProvider options={{ key: 'css' }}>
<CssVarsProvider
  theme={theme}
  defaultMode={"light"}
  modeStorageKey={schemeConfig.modeStorageKey}
>
  <CssBaseline />
  <RTL direction={settings.direction}>{children}</RTL>
</CssVarsProvider>
</AppRouterCacheProvider>

Current behavior

Default mode is always "system" at first render

Expected behavior

Maybe specify "light" / "dark" / "system" to default instead of always defaulting to "system" at first render

Context

No response

Your environment

No response

Search keywords: Set default mode

@mtr1990 mtr1990 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 5, 2024
@DiegoAndai DiegoAndai moved this to Backlog in Material UI Sep 5, 2024
@oliviertassinari oliviertassinari added the package: system Specific to @mui/system label Sep 5, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 5, 2024

@mtr1990 It sounds like you are looking for https://mui.com/material-ui/customization/css-theme-variables/configuration/#toggling-dark-mode-manually. Does it solve your problem?

Now, the docs structure confuses me a bit. I would assume that the CSS variables about dark mode should be into https://mui.com/material-ui/customization/dark-mode/.

@mtr1990
Copy link
Author

mtr1990 commented Sep 6, 2024

Thanks for your response but it seems this document you provided doesn't work for me.

As far as I understand createTheme creates a default mode of light.

But mode and systemMode from useColorScheme will always take the priority value from the system.

Currently my computer system is dark state but I want my web application to always be light by default (without configuration from the computer system). Can we specify light,dark,system modes instead of default always beingsystem?

x-5

@siriwatknp siriwatknp added enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Sep 6, 2024
@siriwatknp
Copy link
Member

siriwatknp commented Sep 6, 2024

I'd say this is an edge case. The reason is why do you want your app to be light by default when the user prefers dark?

Another question, do you need to set the default as light but still letting user toggle to other modes? or do you want to force the page to always be light.

@siriwatknp siriwatknp added the status: waiting for author Issue with insufficient information label Sep 6, 2024
@mtr1990
Copy link
Author

mtr1990 commented Sep 6, 2024

Hi @siriwatknp

The reason is why do you want your app to be light by default when the user prefers dark?

Because for my application the color palette in light mode is more important. It highlights the UI components I built and dark mode in my app is just a secondary option.

So I want to set the light mode by default so that first time users get the best impression of my app.

Users may prefer to set their own default mode rather than being forced to follow a pre-set mode and our customers (users) are also developers (they also have the need to customize things) not end users

Another question, do you need to set the default as light but still letting user toggle to other modes? or do you want to force the page to always be light.

The default is light mode and users can switch to dark or system.

In my app there is a setting option for light and dark. So if the default mode is not customizable (default is always system) then the defaultSettings we provide is meaningless.

I just thought v6 had support for this feature and I didn't find the documentation to customize it. But according to your answer, this feature is currently unavailable.

Can you help me provide a solution to this problem? (The default can be: light, dark or system and they can switch modes )

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Sep 6, 2024
@GBvanDam
Copy link

GBvanDam commented Sep 6, 2024

@siriwatknp maybe the other way around makes more sense. We have an application that users will use at night. Most of the users have their devices on light mode, but at night they want to switch to dark mode in our application. Right now, they would have to switch to dark mode for every night they need our application, as it will time and again default to system.

Agree with (The default can be: light, dark or system and they can switch modes)

@siriwatknp siriwatknp removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 11, 2024
@siriwatknp
Copy link
Member

Can you help me provide a solution to this problem? (The default can be: light, dark or system and they can switch modes )

Thanks for your feedback, I am working on this.

@MartinXPN
Copy link

I'm facing the same issues as @mtr1990
Would be great if we could provide the default color scheme.
Have there been any updates on this?

@ggascoigne
Copy link

My use case is similar, I want to make sure that the default starts as light mode, but I want to allow the user to change it - after which, it is OK to use whatever they last chose. Whilst personally I love having system be the default, my PMs specifically want light mode to be the default to allow users to opt in to the other options and not suddenly get a changed ui.

@siriwatknp
Copy link
Member

@mtr1990 Can you try this?

{
  "@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/e07eebfd/@mui/material"
}

@mtr1990
Copy link
Author

mtr1990 commented Oct 1, 2024

@mtr1990 Can you try this?

{
  "@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/e07eebfd/@mui/material"
}

Thanks @siriwatknp
It works 👍 . I will come back if there are any other problems.

Share settings for anyone who needs it.

export const schemeConfig = {
  modeStorageKey: 'theme-mode',
  defaultMode: 'light
};
import { ThemeProvider as ThemeVarsProvider } from '@mui/material/styles';

<AppRouterCacheProvider options={{ key: 'css' }}>
<ThemeVarsProvider
  theme={theme}
  modeStorageKey={schemeConfig.modeStorageKey}
  defaultMode={schemeConfig.defaultMode}
>
  <CssBaseline />
</ThemeVarsProvider>
</AppRouterCacheProvider>
import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';

<InitColorSchemeScript attribute="data" modeStorageKey={schemeConfig.modeStorageKey} />

@github-project-automation github-project-automation bot moved this from Selected to Done in Material UI Oct 1, 2024
Copy link

github-actions bot commented Oct 1, 2024

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

We value your feedback @mtr1990! How was your experience with our support team?
We'd love to hear your thoughts in this brief Support Satisfaction survey. Your insights help us improve!

@MartinXPN
Copy link

MartinXPN commented Oct 2, 2024

The theme flickers on the first render.
If the system default color mode is set to dark, and the defaultMode of the Theme is set to light, the app still renders the UI in dark theme, and then switches to the light mode on the client side.
This causes flickering of the whole page on the first render.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature package: system Specific to @mui/system
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

6 participants