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.

dark mode: detect user's color theme preference

See original GitHub issue

Since Dark mode has landed, it would be nice if we could use system’s color theme as default prefers-color-scheme

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:26 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
dwellecommented, Mar 15, 2021

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 from appState 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 the onChange callback.

1reaction
xixixaocommented, Mar 15, 2021

@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,

  1. First time users should always see light mode
  2. If you previously had light OS preference and light mode on, but now you have dark OS preference, the UI will switch to dark mode. (same the other way around)

If we get a PR we can test out how it feels.

Read more comments on GitHub >

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

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