Skip to content

Fix color mode: only persist explicit choice, follow system theme - #380

Open
Edward-Deakin wants to merge 1 commit into
mainfrom
fix/color-mode-url-theme-override
Open

Edward-Deakin wants to merge 1 commit into
mainfrom
fix/color-mode-url-theme-override

Conversation

@Edward-Deakin

Copy link
Copy Markdown
Collaborator

Problem

Auto theme switching defaulted to a stale value in `localStorage`. The provider wrote the resolved mode to `localStorage.theme` on every load, so the system `prefers-color-scheme` default (or a `?docusaurus-theme=` URL param) became a stored "choice" that overrode later system changes.

Fix (`packages/docs-ui/src/providers/ColorMode`)

  • Persist to `localStorage` only on explicit user set/toggle
  • Priority on load: URL param (this view only, not persisted) → stored explicit choice → system preference
  • Follow live `prefers-color-scheme` changes while no explicit choice is stored
  • Guard storage access with try/catch; validate stored values

Notes

  • Users who already have a `theme` value saved from the old behaviour keep it until they toggle or clear it.
  • Verified: `tsc --noEmit` clean, Prettier clean. ESLint still reports only quote/semicolon rule conflicts that also fire on the untouched file (ESLint config expects double quotes; repo Prettier uses single).
  • Not tested in a browser.

🤖 Generated with Claude Code

The resolved mode was written to localStorage on every load, freezing the
system default (or a URL param theme) into a stored choice that overrode
later system preference changes. Persist only on explicit user set/toggle,
don't persist the URL param, and follow live prefers-color-scheme changes
when no choice is stored.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Edward-Deakin
Edward-Deakin force-pushed the fix/color-mode-url-theme-override branch from 5bd1352 to 0a5b1be Compare September 21, 2026 16:55
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

Successfully merging this pull request may close these issues.

1 participant