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

Light / dark mode does not persist between pages in static export #319

Open
Tracked by #1106 ...
choldgraf opened this issue Mar 2, 2024 · 5 comments
Open
Tracked by #1106 ...
Labels
bug Something isn't working

Comments

@choldgraf
Copy link
Member

Description

The light and dark mode changes back to the default when you navigate between pages. This is on an HTML build and can be demoed here:

https://2i2c.org/report-czi-2021/

For example:

CleanShot 2024-03-01 at 18 00 46

Proposed solution

Persist the status of light/dark by using an HTML browser window key variable.

Additional notes

@choldgraf choldgraf added the bug Something isn't working label Mar 2, 2024
@agoose77
Copy link
Collaborator

agoose77 commented Mar 4, 2024

@stevejpurves could you move this to myst-theme please?

Architecturally, it looks like myst-theme populates the "default" theme for a server route using a cookie, and local manipulation of the theme invokes an API request to update the cookie.

I think we can move this to localStorage for static builds -- Remix will have SSR'd the page, so we can safely read from localStorage. The question will be how deep this goes; can we abstract out the API request in favour of something that reads/writes localStorage. Perhaps a different provider should be used.

@choldgraf
Copy link
Member Author

If it's helpful, here's how the pydata theme (what jupyter book builds off of) handles this:

https://github.com/pydata/pydata-sphinx-theme/blob/b9719c2a789eca3a193d4fd64ab71d6283a923db/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js#L7-L33

@stevejpurves stevejpurves transferred this issue from executablebooks/mystmd Mar 5, 2024
Copy link

welcome bot commented Mar 5, 2024

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! 🎉

@stevejpurves
Copy link
Member

localstorage is not a good solution for SSR but for static builds we can probably opt out of the cookie and do local storage instead

@rowanc1 rowanc1 changed the title Light / dark mode does not persist between pages Light / dark mode does not persist between pages in static export Mar 11, 2024
@rowanc1
Copy link
Member

rowanc1 commented Mar 11, 2024

As @stevejpurves mentioned, we should continue using cookies for SSR, but switch to local storage for the static render. As local storage is not present on the server, there is a flicker which can be avoided when the theme preference is treated as shared state between server/client (best practice in that scenario).

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

No branches or pull requests

4 participants