Skip to content
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 media query value when useColorSchemeMediaQuery is set to true #981

Merged

Conversation

julianbenegas
Copy link
Contributor

@julianbenegas julianbenegas commented Jun 5, 2020

Currently, even if useColorSchemeMediaQuery: true, the color mode stored in localStorage wins. Also, the color mode from the media query gets stored to localStorage.

Update (4814af8): I made the media query color mode get stored in localStorage again, to make the initial flash prevention script still work.

Fixes #787

Absolutely loving theme-ui btw.

@julianbenegas
Copy link
Contributor Author

julianbenegas commented Jun 5, 2020

I also added media query listeners, which is a minor detail, but nice.

I used the docs to test it:

BeforeAfter

And save to localStorage even if useColorScheme...: true,

to make the flash prevention script work.
@julianbenegas
Copy link
Contributor Author

Thinking about an issue with this. The pr as is gives sense to the useColorSchemeMediaQuery key, but also makes manual toggles not work alongside it. Even if localStorage says one thing, the media query will always persist.

What if the user has useColorSchemeMediaQuery: true, but only wants it as a default, and also add a color mode toggle.

Example use case: The end user having three options: light, dark and auto. Very popular pattern.

  • If there's a value in localStorage, always use that one.
  • If there's no value in localStorage and useColorSchemeMediaQuery: true, use color scheme media query—without saving it to localStorage!
  • The initializeColorMode script could be tweaked to also check for color scheme media queries so that flash is prevented either way.

To enable "auto", the programmer just has to clear localStorage.

Let me know what you guys think and I'll update.

@lachlanjc lachlanjc added the enhancement New feature or request label Dec 3, 2020
@hasparus hasparus closed this Dec 14, 2020
@hasparus hasparus deleted the branch system-ui:develop December 14, 2020 08:53
@hasparus hasparus reopened this Dec 14, 2020
@vercel
Copy link

vercel bot commented Dec 14, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/systemui/theme-ui/gqe7nm3c3
✅ Preview: https://theme-ui-git-fix-color-scheme-media-query.systemui.vercel.app

@lachlanjc
Copy link
Member

Personally, I'd like to make this a separate option from the existing option. I think there are different usecases for these functionalities, & while personally I'd use it on most sites, I can definitely think of applications where you'd want other color modes but not use the media query live. I'm not quite sure how to name these differently:

  • We could rename the existing option useInitialColorSchemeMediaQuery, with this staying as useColorSchemeMediaQuery, but that'd be a breaking change for existing users unless they change their theme config upon upgrade
  • We could name this useLiveColorSchemeMediaQuery, keeping the existing name for the existing behavior but encouraging using useInitialColorSchemeMediaQuery in the future & deprecate it
  • Definitely plenty of other names we could use instead, but I think one of these two structural paths make sense

@lachlanjc lachlanjc mentioned this pull request Dec 16, 2020
@hasparus
Copy link
Member

We could rename the existing option useInitialColorSchemeMediaQuery, with this staying as useColorSchemeMediaQuery, but that'd be a breaking change for existing users unless they change their theme config upon upgrade
We could name this useLiveColorSchemeMediaQuery, keeping the existing name for the existing behavior but encouraging using useInitialColorSchemeMediaQuery in the future & deprecate it
Definitely plenty of other names we could use instead, but I think one of these two structural paths make sense

@lachlanjc (insert state machines meme) What do you think about this?

useColorSchemeMediaQuery: 'live' | 'initial' | true (same as initial for backwards compatibility) | false

I think I'd prefer to have a special color mode "system" which would pick "light" and "dark" from theme.colors, but I'm not strongly opinionated here.

@lachlanjc
Copy link
Member

Brilliant. Yeah, let’s do the string solution, that’s way better than conflicting Booleans!

Copy link
Member

@lachlanjc lachlanjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome work. Before we merge, let's implement this as a separate option from the existing functionality, using @hasparus's suggestion for syntax:

useColorSchemeMediaQuery: 'live' | 'initial' | true (same as initial for backwards compatibility) | false

@lachlanjc lachlanjc linked an issue Jan 3, 2021 that may be closed by this pull request
@albertusdev
Copy link

hello there!
great and exciting work on this PR since I'm looking to implement a manual system/light/dark theme toggler in my Gatsby site.
Is there any news/update on this PR progress that you can share with us?
Thanks!

@hasparus @lachlanjc

@lachlanjc
Copy link
Member

@albertusdev We’d still love for this feature to be implemented, but as this PR is stale, if you’d like to update it with the API I mentioned, that would be much-appreciated!

@hasparus hasparus merged commit 98f3f5e into system-ui:develop Oct 26, 2021
@hasparus hasparus added the prerelease This change is available in a prerelease. label Oct 28, 2021
@hasparus
Copy link
Member

🚀 PR was released in v0.12.0 🚀

@hasparus hasparus added released This issue/pull request has been released. and removed prerelease This change is available in a prerelease. labels Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

system color mode Use prefers-color-scheme preference over localStorage color mode
4 participants