use-dark-mode icon indicating copy to clipboard operation
use-dark-mode copied to clipboard

System query causes initial mode to ignore initialState value

Open abbott opened this issue 5 years ago • 1 comments

System query causes initial mode to ignore initialState value — is this a feature or a bug?

If a site prioritizes a particular mode by setting initialState then system preference would be ignored. The user would have to manually change the site preference, otherwise if initialState is null, then the system preference is used.

abbott avatar Jun 25 '20 16:06 abbott

Hi, if I'm understanding you correctly you can solve this using the matchMedia API. Here's how I did it:

https://github.com/NorfolkDev/norfolkdevelopers-website/blob/39c988376c38976f9eaa2dfac3638c6fd938ec14/src/components/DarkModeToggle.tsx#L7-L10

const darkMode = useDarkMode(
  window.matchMedia('(prefers-color-scheme: dark)').matches
);

tom-sherman avatar Mar 29 '21 10:03 tom-sherman