-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[material-ui][pigment-css] How to toggle between color modes #43655
Comments
I battled this issue this morning... What I did to resolve in the interim:
- cssVariables: true,
+ cssVariables: {
+ colorSchemeSelector: "class"
+ },
PS |
This at least gets it to toggle but it loses system preference. Would really like to have an opinionated way to allow system preference, but then to select an override if desired. And like other frameworks, be able to avoid the first time load flicker while not causing all routes to become dynamic by using async in the root layout. |
@shjacobs303 Pigment does not have a built-in way to handle system preferences yet. Meanwhile, you can use https://github.com/pacocoursey/next-themes for Next.js |
next-themes looks to be ideal, I can see you're planning to add some examples using V6. I can wait and follow your pattern, looking forward to it. |
As @samuelgoldenbaum has stated, if you are using const matcher = window.matchMedia('(prefers-color-scheme: dark)');
if (matcher.matches) {
document.documentElement.classList.remove('light');
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
document.documentElement.classList.add('light');
}
matcher.addEventListener('change', event => {
const newColorScheme = event.matches ? "dark" : "light";
document.documentElement.classList.remove('light', 'dark');
document.documentElement.classList.add(newColorScheme);
}); |
Steps to reproduce
Link to live example: (required) codesandbox
Steps:
.theme-${colorScheme}
: ":root" to the theme fileCurrent behavior
adds class="theme-light" or "theme-dark"Color scheme does not change after button click
Expected behavior
adds class="theme-light" or "theme-dark"Color scheme changes on button click
Context
I'm trying to use mui v6 with pigment css with the ability to select a particular color scheme, overriding the system defaults.
You'll also notice I had to comment out a Grid component in the codesandbox example. The nextjs pigment css example does not currently render with Grid/> without changing to a client component.
Your environment
npx @mui/envinfo
Search keywords: pigment-css v6
The text was updated successfully, but these errors were encountered: