-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
feat: improve syncing of user preferred theme #8078
Conversation
Hi @juliusmarminge! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
⚡️ Lighthouse report for the deploy preview of this PR
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
I don't agree with that solution, as explained in #8074 (comment) As a user, I want to visit the Docusaurus website only in dark-mode, despite using dynamic OS light/dark theme switches. It is legit to expect a site to not follow your OS theme changes, particularly if you want to have dynamic OS theme and you have per-site theme preferences that should override what your OS says. The way it is designed is that a value in localstorage, if present, should always be applied. If you want to reset to the OS theme you have to delete the localstorage value instead of trying to sync it to the os theme Side note, your implementation does not take into account that the final choice of a theme must be decided in the critical path of the app, before the React hydration process even starts. We have inlined JS here: https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-theme-classic/src/index.ts#L29 Due to your implementation, the sync only happens later and it can produce a flash (ie is initially light and then switch to dark with your new sync system). If we were to implement your solution (which I'm against), it should be inlined and My suggestion:
Note we already have a case where a site initially has dark mode support, and later the site owner wants to remove it. You'll notice that we have code to handle that already, and it simply does |
@slorber If I never interacted with the color mode toggle, I won't consider the current mode "my choice"—it's merely the default, and I'm perfectly fine if it shifts. |
I'm not sure what's the point you are trying to make here with this extra comment 😅 but I agree with you. Considering site has dark/light mode and respectPrefersColorScheme:
Note: |
Do you suggest we make a 3-way-toggle with dark-light-system? Would that toggle be opt-in? |
Yes, I think we can give it a try where we have 3 distinct svgs to represent the 3 states. I'm pretty sure I've already seen some svg examples for "system". Otherwise we could use something similar to Mozilla:
We could make it opt-in for now and eventually turn it on by default if successful (with opt-out). Note: if the user uses Or maybe the 3rd option could be enabled automatically if https://docusaurus.io/docs/api/themes/configuration#respectPrefersColorScheme |
closing this, as it's not the right direction to solve this problem Let's continue the discussion in #8074 |
Pre-flight checklist
Motivation
As mentioned in #8074, I believe that if I change my preferred theme between sessions, I probably want it to update if my preferred theme has changed.
I implemented a feature that stores the user's preferred theme in localStorage (if
respectPrefersColorScheme
is used). That way, we can check when the user visits the site the next time, if their preferred theme has changed.This is needed because the
theme
key (set when toggling using the switch) takes precedence over the media-matchedprefers-color-scheme
entry. This means that if I use the switch, the site no longer respects my system preferences. This is good when navigating the page to keep it from switching themes when navigating. But if I come back later at night, and my system preference has changed, this change would previously not be recognized.Let me explain the new behavior with some examples (examples goes both ways for light/dark mode):
The user browses the site during the day and uses light mode. When they come back at night, their system preferences has changed to dark mode. This change will be registered so the site will use dark mode too.
The user browses the site using light mode, because they don't like the dark mode of that site. The user always uses dark mode for their system preferences, so when coming back it won't have changed so the site will keep running in light mode.
The user navigates the site and uses the toggle to select light mode. They switch page. The dark mode system preference does not take precedence so we keep using light mode.
Test Plan
I have not yet added any tests, if you feel like it's required let me know and we can plan a test plan together.
Test links
Deploy preview: https://deploy-preview-8078--docusaurus-2.netlify.app/
Related issues/PRs
Closes #8074