Follow system theme on change when respect-user-color-scheme: true#14552
Open
rmvegasm wants to merge 1 commit into
Open
Follow system theme on change when respect-user-color-scheme: true#14552rmvegasm wants to merge 1 commit into
respect-user-color-scheme: true#14552rmvegasm wants to merge 1 commit into
Conversation
Collaborator
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
respect-user-color-scheme: trueis set in a Quarto HTML document, thepage respects the user's OS-level light/dark preference at load time, but does
not follow it when the OS theme changes while the page is open. This
behaviour is described in #12426 but I believe is no longer needed, and the
expected behaviour for most users setting
respect-user-color-scheme: truewould be for the document to follow the system theme anytime it changes.
Root Cause
The
matchMedia('(prefers-color-scheme: dark)') changelistener inquarto-html-before-body.ejsbails out early when any localStorage entryexists:
Since every manual toggle writes to
localStorage(viasetStyleSentinel()),one click permanently kills system-following.
Solution
Instead of returning early when a localStorage override is present, clear
the override and follow the system. The manual choice is still respected
across page loads, but it expires gracefully on the next OS theme change.
This is the only change. The existing sentinel machinery
(
hasAlternateSentinel,getColorSchemeSentinel,setStyleSentinel) worksunchanged: when localStorage is absent, it falls back to
localAlternateSentinel(which always reflects the current effective mode).Behavior
Files Changed
src/resources/formats/html/templates/quarto-html-before-body.ejs(6 lines changed in the
respectUserColorSchemeevent listener)tests/integration/playwright/tests/html-dark-mode-defaultdark.spec.ts(updated the
'...do not respect-user-color-scheme after toggling'test toreflect the new behaviour: renamed to
'...manual override expires on system theme change', assertions now expect the page to follow the system after amanual override expires, rather than sticking forever)
All 14 existing tests in the dark-mode and light-mode suites continue to
pass with these changes.
Fixes #14551