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

Put the toggle theme button in the header and serialize the prefered theme value to localStorage with hook #1641

Closed
tonyvugithub opened this issue Feb 3, 2021 · 7 comments · Fixed by #1674
Assignees
Labels
area: css styling Anything related to CSS styling type: enhancement New feature or request

Comments

@tonyvugithub
Copy link
Contributor

tonyvugithub commented Feb 3, 2021

What would you like to be added:
We need to think of a better way to integrate the theme switching option. Maybe a switch button in the header? Also we need to think of a way to retain the chosen theme so when users revisit the site, the same colors appear to them.
Why would you like this to be added:
This will provide users option to choose their preferred theme and increase their engagement to the app.
Reference for dark theme can be found #1637

@tonyvugithub tonyvugithub added type: enhancement New feature or request area: css styling Anything related to CSS styling labels Feb 3, 2021
@chrispinkney
Copy link
Contributor

Would love to hear about more about this, I don't know anything about localStorage and never even considered how something like this works behind the scenes.

@humphd
Copy link
Contributor

humphd commented Feb 3, 2021

See https://github.com/streamich/react-use/blob/master/docs/useLocalStorage.md

Browsers have a number of built-in "storage" areas:

  • cookies: good for sharing small bits of data between the browser and server
  • session storage: good for storing things you don't want to go away if you refresh or open another tab, but don't need long term
  • local storage: good for storing small bits of text (or JSON serialized to text). You're limited to ~2-5M
  • indexeddb: good for working with arbitrary data, including binary data, and lots of it. You can store 10s to 100s of M
  • cache storage: good for storing responses from servers

For something like a preference for dark vs. light mode, local storage is perfect. You would use it vs. state via a hook, and set it (i.e., set it in the db) when the user changes it, and read it from the db on startup.

@tonyvugithub tonyvugithub changed the title Integrate better theme switching option and serialize the value to localStorage with hook Put the toggle theme button in the header and serialize the prefered theme value to localStorage with hook Feb 5, 2021
@tonyvugithub
Copy link
Contributor Author

@chrispinkney : Want to take this on Chris now that we have the header in?

@chrispinkney
Copy link
Contributor

@tonyvugithub I'd like to but I think I have too much on my plate right now, I'll make a reminder to re-evaluate this later in the week but if anyone reading this wants this feel free to jump on it.

@tonyvugithub
Copy link
Contributor Author

@chrispinkney i can take it

@tonyvugithub tonyvugithub self-assigned this Feb 6, 2021
@huyxgit
Copy link

huyxgit commented Feb 7, 2021

I think it's better to detect the system setting of the device first and set the dark/light theme automatically, and still have a switcher for user to change to their desired theme mode

@humphd
Copy link
Contributor

humphd commented Feb 7, 2021

I agree using prefer-color-scheme would be smart here for the initial value, and combine that with a localStorage hook to remember the preference.

https://web.dev/prefers-color-scheme/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: css styling Anything related to CSS styling type: enhancement New feature or request
Projects
None yet
4 participants