dark mode: detect user's color theme preference
See original GitHub issueSince Dark mode has landed, it would be nice if we could use system’s color theme as default prefers-color-scheme
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:26 (14 by maintainers)
Top Results From Across the Web
How do I detect dark mode using JavaScript? - Stack Overflow
To update the color-scheme dynamically based on the user's preference, the following can be used: function setColorScheme(scheme) { switch(scheme){ case ...
Read more >prefers-color-scheme - CSS: Cascading Style Sheets | MDN
The prefers-color-scheme CSS media feature is used to detect if a user has requested light or dark color themes. A user indicates their ......
Read more >How to detect dark mode using JavaScript - Flavio Copes
Then, check if it's dark mode using window.matchMedia('(prefers-color-scheme: dark)').matches. This will return true if dark mode is enabled.
Read more >How to detect if the User's OS prefers dark mode and change ...
There's a few things here to consider when using prefers-color-scheme and detecting dark mode: Using the existing theme as much as possible.
Read more >prefers-color-scheme: Hello darkness, my old friend - web.dev
The prefers-color-scheme media feature is used to detect if the user has requested the page to use a light or dark color theme....
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
props on @xixixao’s proposal.
@giuscri yes, I’d just set some key in localStorage. If present, it means you’re a returning user (provided you set it after you initially read it for the purpose of setting the theme).
In fact, we may wanna move
appState.theme
fromappState
to localStorage altogether (which we’ve discussed in a recent PR), because we’ll need to do so if we want to apply the theme to the initial loading screen. But I’m not sure how it’d tie in to the fact that we want it to be controlled by the app (core). /cc @ad1992 thoughts?Ultimately it doesn’t matter and it can live in localStorage even if the app keeps it in
appState
after init. We’d just update another LS key on top of the elements/appState keys that we do in theonChange
callback.@giuscri makes sense. I’m not sure whether we can prevent a flash, given you’re just opening the laptop after the dark mode switched.
Nevertheless, it would make sense to me follow the preference IF it has changed. So,
If we get a PR we can test out how it feels.