-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use prefers-color-scheme
preference over localStorage color mode
#787
Comments
Hey Joe! This is actually the expected behavior for Theme UI right now, though some folks were having a conversation in #624 about changing it the way you requested (which personally I'd love to see). |
@lachlanjc Ah I did take a glance at that! I thought it was focussed more on setting |
I agree, this would be awesome. As a supplement, I went ahead and created a workaround to automatically update (and override) the color mode when the Feel free to use this for the time being if it's helpful.
That said, something I have noticed (and I'm not sure if this is the case for those using Gatsby), but when a default theme is set and This issue happens when visiting the page for the first time, I'm assuming it may be due to the color mode method initially looking for a stored value in local storage? Could there be a better way to prevent this from happening? |
Extending on @dannykeane's snippet I think it should cleanup on unmount import { jsx, useColorMode } from "theme-ui";
// ..
const [, setColorMode] = useColorMode();
useEffect(() => {
const switchMode = (e) => {
const isDarkMode = e.matches;
isDarkMode ? setMode("dark") : setMode("light");
};
const darkModeMediaQuery = window.matchMedia(
"(prefers-color-scheme: dark)"
);
darkModeMediaQuery.addListener(switchMode);
// cleanup on unmount
return () => darkModeMediaQuery.removeEventListener(switchMode);
}, [setMode]); Please correct me if I'm wrong |
If there is interest and if it will be some time until this behavior is fleshed-out in Them-UI, I will publish a gatsby plugin that will provide intuitive default behavior for color modes with configuration options to alter the behavior. My initial thoughts are that it, most importantly, will detect the user's preferred color mode and adapt UI accordingly (as described by @joe-bell above), have configuration option to override localstorage mode value, provide options for initial color mode behaviors, etc. As a website user, I 100% expect websites to honor my devices' mode settings and adjust their modes automatically. |
I'm a few months late in this discussion, but, after reading the entire thing, I still can't seem to figure out if it's implemented yet or no. According to version1 roadmap, it's not, so, if that's the case, how exactly can I get this to work in Gatsby? |
prefers-color-scheme
behaviourprefers-color-scheme
preference over localStorage color mode
🚀 Issue was released in |
Describe the bug
When toggling between light/dark mode on a supoprted device (in this case iOS, or MacOS via Firefox), Theme-UI's color scheme does not update. Updating the app to reflect the user's color mode requires the localstorage to be deleted.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
As I've enabled
useColorSchemeMediaQuery
, I would expect the color modes to behave as a media query (in other words, on user-agent change - much like a breakpoint).Screenshots
An example in Theme-UI: (using
useColorSchemeMediaQuery
)Examples of 'expected behaviour' in non-Theme-UI applications
Additional context
Related issues: #367
The text was updated successfully, but these errors were encountered: