question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Light Theme mode always renders for some seconds before switching to dark mode

See original GitHub issue

In 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
eps1loncommented, Apr 19, 2020

You probably want to use NoSsr: true in your useMediaQuery.

1reaction
eps1loncommented, Apr 19, 2020

Thank you so much! I’m sorry for taking so much of your time.

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found