Light Theme mode always renders for some seconds before switching to dark mode
See original GitHub issueIn Chrome and Safari, I have noticed that your website always takes some seconds to switch to dark mode when the user has the system preferences set to dark.
I found this query takes a while to resolve: https://github.com/mui-org/material-ui/blob/b17e6856c33e96e1b6c5c5dfac327127cf4be705/docs/src/modules/components/ThemeContext.js#L151
It takes a while to get the results due to the browser appending it some page renders later.
However, prefers light resolves immediately, either true or false, eliminating the “initial flicker”:
const prefersLightMode = useMediaQuery('(prefers-color-scheme: light)');
const preferredType = prefersLightMode ? 'light' : 'dark' ;
the documentation for useMediaQuery will need update if this change is made: https://material-ui.com/components/use-media-query/#usemediaquery
Loving your framework, keep rocking!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Light Theme mode always renders for some seconds before ...
In Chrome and Safari, I have noticed that your website always takes some seconds to switch to dark mode when the user has...
Read more >Thinking on ways to solve a DARK/LIGHT THEME SWITCH
In today's GUI Challenge, @AdamArgyleInk shares thinking on a way to build a theme switch component. Often a website will allow switching ......
Read more >Light/dark mode: avoid flickering on reload
This can result in a flicker when users visit your website: they picked the dark mode, but when loading the website, for a...
Read more >A Dark Mode Toggle with React and ThemeProvider
In this tutorial, we're going to build a toggle that allows users to switch between light and dark modes, using a <ThemeProvider wrapper ......
Read more >Fixing the dark mode flash issue on server rendered websites
This blog post is a follow up to Switching off the lights - Adding dark mode to your React app that I wrote...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You probably want to use
NoSsr: true
in your useMediaQuery.No worries. This is definitely a hard issue and I would prefer we wouldn’t put media queries into JavaScript anyway. Having feedback that this is easy to get wrong helps.
To be honest I wouldn’t mind making
NoSsr
required so that you have decide how your component is going to be used. Hydration quirks should definitely be visible in your codebase.