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

Wrong theme if try to set "system" theme twice #34

Closed
thien-do opened this issue Mar 15, 2021 · 0 comments · Fixed by #30
Closed

Wrong theme if try to set "system" theme twice #34

thien-do opened this issue Mar 15, 2021 · 0 comments · Fixed by #30
Labels
bug Something isn't working

Comments

@thien-do
Copy link

What

  1. Setup ThemeProvider and useTheme with all default props
  2. Have a button that calls setTheme("system")
  3. Click on the button once -> it works correctly, HTML's data-theme is the correct theme (either "light" or "dark")
  4. Click again on that button -> it works incorrectly, HTML's data-theme is "system"

Why

Current source: https://github.com/pacocoursey/next-themes/blob/7c8a3269ca46f2accad778f04820075134bc6c50/index.tsx

In the 1st click of the button:

  1. When "setTheme" is called, "changeTheme" is always called
  2. In "changeTheme", the data-theme attribute is set to the chosen theme, which is "system"
  3. The "handleMediaQuery" effect is triggered due to "theme" changed, which set data-theme to the correct value (either "light" or "dark")

In the 2nd click of the button: step 1 and 2 are the same with the 1st click, but since the theme value (in state) is not changed (still "system"), the "handleMediaQuery" is not triggered, and thus the data-theme is not changed and is still "system" as in the 2nd step.

I reviewed the 0.13 PR and it seems this still happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants