DEV Community

Matt ✌️
Matt ✌️

Posted on

#help window.matchMedia Javascript to remember user selection on page change

Hi, I'm having trouble trying to make my own dark-mode-toggle using javascript. So far I have the site working with a custom toggle, but when a user first lands on the site I want my javascript to be able to detect whether or not the user has an OS theme pref (white/black) and then if the user chooses to toggle to the opposite and changes pages that the local storage remembers there choice, and override's matchMedia.

I'm having trouble with prefers-color-scheme with the logic that I'm trying to achieve. For example, with the prefers-color-scheme I have a toggle on my site that overrides this is a user prefers black while using light mode, and vice versa. The issue I'm running into is I can't toggle it…

Top comments (0)