Skip to content

How to avoid color flash when using 'system' media query? #2377

Answered by freddydumont
freddydumont asked this question in Q&A
Discussion options

You must be logged in to vote

If anyone has this issue, I fixed it by injecting this script in the document head:

if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  document.documentElement.classList.add('theme-ui-dark');
} else {
  document.documentElement.classList.add('theme-ui-__default');
}

window
  .matchMedia('(prefers-color-scheme: dark)')
  .addEventListener('change', (event) => {
    if (event.matches) {
      //dark mode
      document.documentElement.classList.remove('theme-ui-__default');
      document.documentElement.classList.add('theme-ui-dark');
    } else {
      //light mode
      document.documentElement.classList.remove('theme-ui-dark');
      document.documentElement.classList.add(

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hasparus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant