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

ThemeSwitcherIcon mismatch on first render - Dark Mode #2957

Open
kiurious opened this issue Jun 23, 2023 · 0 comments
Open

ThemeSwitcherIcon mismatch on first render - Dark Mode #2957

kiurious opened this issue Jun 23, 2023 · 0 comments

Comments

@kiurious
Copy link

kiurious commented Jun 23, 2023

Tested on Windows.
When your preference OS theme is set to 'dark', the page originally loads the dark theme, but fails to update the icon to the dark version on first render.
I think this is a mismatch between SSR and CSR. You can only see this on the build version and after clicking on the button once, it starts syncs correctly.
When building, document is not available so it defaults to 'light'. And then because the state is not set using the useEffect on ThemeSwitcher, the button is not being reloaded with the current state. The same happens with the aria-pressed state of the button, it also displays wrong info on first load theme==="dark".

I tried logging the state before and after, and it all points to dark, so it seems to be an issue with how it is being done with SSR.
By updating the state it causes the button to be correctly shown on first render, as it force triggers CSR:

...
const [reload, setReload] = useState(false);

 useEffect(() => {
    document.documentElement.dataset.theme = theme;
    localStorage.setItem('selected_theme', theme);
    setReload(true)
  }, [theme]);
  ...
  
  {reload && <ThemeSwitcherIcon mode={theme} />}
  

But I think the above is not an elegant solution, so I did not submit a PR. I couldn't find a way to avoid the mismatch, either through the onRenderBody function or on the ThemeSwitcher function.
Screenshot 2023-06-23 143925

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant