Skip to content

fix(theme-common): do not persist color mode for OS-triggered changes - #7200

Merged
Josh-Cena merged 3 commits into
mainfrom
jc/color-mode
Apr 21, 2022
Merged

Josh-Cena merged 3 commits into
mainfrom
jc/color-mode

Conversation

@Josh-Cena

Copy link
Copy Markdown
Collaborator

Motivation

Fix #7199.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

@Josh-Cena Josh-Cena added the pr: bug fix This PR fixes a bug in a past release. label Apr 18, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 18, 2022
@netlify

netlify Bot commented Apr 18, 2022 •

Copy link
Copy Markdown

✅ [V2]

Name Link
🔨 Latest commit 4bc9c27
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/62616374770d8c000837e20d
😎 Deploy Preview https://deploy-preview-7200--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions

github-actions Bot commented Apr 18, 2022 •

Copy link
Copy Markdown

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 67
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

Lighthouse ran on https://deploy-preview-7200--docusaurus-2.netlify.app/

@github-actions

github-actions Bot commented Apr 18, 2022 •

Copy link
Copy Markdown

Size Change: +288 B (0%)

Total Size: 800 kB

Filename Size Change
website/build/assets/css/styles.********.css 107 kB +172 B (0%)
website/build/assets/js/main.********.js 604 kB +116 B (0%)
ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 50 kB
website/build/index.html 38.8 kB

compressed-size-action

@Josh-Cena Josh-Cena changed the title fix(theme-common): do not persist color mode if switch is disabled fix(theme-common): do not persist color mode for system-theme-triggered changes Apr 21, 2022
@Josh-Cena
Josh-Cena requested a review from slorber April 21, 2022 00:47
@Josh-Cena Josh-Cena changed the title fix(theme-common): do not persist color mode for system-theme-triggered changes fix(theme-common): do not persist color mode for os-triggered changes Apr 21, 2022
@Josh-Cena Josh-Cena changed the title fix(theme-common): do not persist color mode for os-triggered changes fix(theme-common): do not persist color mode for OS-triggered changes Apr 21, 2022
setColorMode(matches ? ColorModes.dark : ColorModes.light);
// Do not persist this change because it's system-theme-triggered and not
// a user action.
setColorMode(matches ? ColorModes.dark : ColorModes.light, {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not good enough.

Even if persisted value is not overridden by theme changes (and thus appropriately restored on OS theme change), switching theme in the OS is still applied immediately.

  • Select dark mode with toggle
  • Move OS to light mode => UI changes to light mode (it shouldn't)
  • Refresh => UI is restored to dark

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should change to light mode. I don't really know, but what's the best UX? I feel like if OS is light mode, the site should really reflect that.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 possible choices here. We may or not agree on the best choice but at least it should be consistent: if you refresh a page you should end up with the same color as before, in all cases

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've set it to deleting the storage (using setColorMode(null)). This should force it to reset to system settings.

@Josh-Cena
Josh-Cena requested a review from slorber April 21, 2022 15:17
}, [disableSwitch]);

const setColorMode = useCallback(
(newColorMode: ColorMode | null, options: {persist?: boolean} = {}) => {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the persist option is actually unused internally. Keeping it might still be useful? I don't have a great use-case for it though, given that it would be reset on refresh.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment here #7200 (comment)

It's up to you, I'm fine with both solutions as long as it's consistent

return;
}
setColorMode(matches ? ColorModes.dark : ColorModes.light);
setColorMode(null);

@slorber slorber Apr 21, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️ this wouldn't be my choice but I'm ok with that

I would have used something like:

const storedColorMode = ColorModeStorage.get();
if (storedColorMode === null) {
      setColorMode(matches ? ColorModes.dark : ColorModes.light,{persist: false});
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know. I'm slightly leaning towards "OS setting should override user choice" until we have a dedicated "OS default" theme option. The localstorage is really just a convenient way to restore what was previously there the next time the user opens the site; its semantic should not be "freeze the color mode until the user explicitly clicks the button"

@slorber slorber left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Now depending on the solution, we only need null or {persist: false} internally.

Maybe it's worth keeping this and exposing to users 🤷‍♂️

(ie they could build their own toggle UI to reset to system theme with swizzle)

@Josh-Cena

Copy link
Copy Markdown
Collaborator Author

Yeah, I feel like this is more extensible. persist: false can be a footgun, though, given that it resets on refresh and causes so-called "inconsistency". But this certainly gives theme authors more freedom.

@Josh-Cena
Josh-Cena merged commit 1412441 into main Apr 21, 2022
@Josh-Cena
Josh-Cena deleted the jc/color-mode branch April 21, 2022 15:29
@slorber

slorber commented Apr 21, 2022 •

Copy link
Copy Markdown
Collaborator

Note the public api (+ doc) is not updated:

  readonly setColorMode: (colorMode: ColorMode) => void;

Should we make this officially public or kept as a secret API? 🤷‍♂️ API seems good enough to me to become public

persist: false can be a footgun

I guess it's fine, users are unlikely to use this option without knowing what they are doing

@Josh-Cena

Copy link
Copy Markdown
Collaborator Author

Let's keep it secret for now. I don't see a great use-case for it, and without actual warning of its weird UX + a concrete example use-case I'd rather not let them do it.

mrizwanashiq pushed a commit to mrizwanashiq/docusaurus that referenced this pull request Jun 25, 2026
…meta#7200)

* fix(theme-common): do not persist color mode if switch is disabled

* New setColorMode(null) API

* reset to default without RPCS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make it so automatic theme switcher prefers system theme preference over 'last theme chosen' as option

3 participants