Skip to content

fix(theme): persist dark/light mode across view transitions#11338

Open
jd wants to merge 1 commit intomainfrom
devs/jd/worktree-bug-theme/persist-dark-light-mode-across-view-transitions--0b1115aa
Open

fix(theme): persist dark/light mode across view transitions#11338
jd wants to merge 1 commit intomainfrom
devs/jd/worktree-bug-theme/persist-dark-light-mode-across-view-transitions--0b1115aa

Conversation

@jd
Copy link
Copy Markdown
Member

@jd jd commented Apr 30, 2026

Theme classes (theme-dark, dark) were lost on every navigation because
ClientRouter swaps the element and the server renders it with
class="initial". The inline FOUC-prevention script only runs on initial
page load (Astro deduplicates identical inline scripts across
navigations), so nothing reapplied the theme after a swap.

Re-run the theme detection on astro:before-swap, applying the classes
to event.newDocument.documentElement so the swap completes with the
correct theme and no flash. Also handle both theme-dark and dark
classes for parity with ThemeToggleButton, since theme.css uses both
selectors.

Copilot AI review requested due to automatic review settings April 30, 2026 13:02
@mergify mergify Bot had a problem deploying to Mergify Merge Protections April 30, 2026 13:02 Failure
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 30, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 🤖 Continuous Integration

Wonderful, this rule succeeded.
  • all of:
    • check-success = build
    • check-success = lint
    • check-success = test
    • any of:
      • check-success = test-broken-links
      • label = ignore-broken-links
    • any of:
      • check-success=Cloudflare Pages
      • -head-repo-full-name~=^Mergifyio/

🟢 👀 Review Requirements

Wonderful, this rule succeeded.
  • any of:
    • #approved-reviews-by >= 2
    • author = dependabot[bot]
    • author = mergify-ci-bot

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|ui)(?:\(.+\))?:

🟢 🔎 Reviews

Wonderful, this rule succeeded.
  • #changes-requested-reviews-by = 0
  • #review-requested = 0
  • #review-threads-unresolved = 0

🟢 📕 PR description

Wonderful, this rule succeeded.
  • body ~= (?ms:.{48,})

@mergify mergify Bot requested a review from a team April 30, 2026 13:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes loss of dark/light theme classes during Astro ClientRouter view transitions by reapplying the theme to the incoming <html> element before the swap, preventing post-navigation flashes and keeping theme selectors consistent.

Changes:

  • Refactors the inline FOUC-prevention theme initializer to an applyTheme() helper and re-runs it on astro:before-swap against event.newDocument.documentElement.
  • Applies/removes both theme-dark and dark classes for parity with existing CSS/selectors.
  • Adds Vitest coverage by extracting and executing the exact inline script from HeadCommon.astro, including regression tests for view transitions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/components/HeadCommon.astro Reapplies theme classes on astro:before-swap so <html> swaps retain correct theme without flashing.
src/components/HeadCommon.test.ts Adds tests that execute the shipped inline theme script and cover swap-time reapplication behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/components/HeadCommon.test.ts Outdated
@jd jd marked this pull request as ready for review April 30, 2026 13:12
Theme classes (theme-dark, dark) were lost on every navigation because
ClientRouter swaps the <html> element and the server renders it with
class="initial". The inline FOUC-prevention script only runs on initial
page load (Astro deduplicates identical inline scripts across
navigations), so nothing reapplied the theme after a swap.

Re-run the theme detection on astro:before-swap, applying the classes
to event.newDocument.documentElement so the swap completes with the
correct theme and no flash. Also handle both `theme-dark` and `dark`
classes for parity with ThemeToggleButton, since theme.css uses both
selectors.

Change-Id: I0b1115aac90e5944bac2d5ca6ea746b2273f1c05
@jd jd force-pushed the devs/jd/worktree-bug-theme/persist-dark-light-mode-across-view-transitions--0b1115aa branch from a5c7f76 to 6464a56 Compare April 30, 2026 13:14
@jd
Copy link
Copy Markdown
Member Author

jd commented Apr 30, 2026

Revision history

# Type Changes Reason Date
1 initial a5c7f76 2026-04-30 13:14 UTC
2 content a5c7f76 → 6464a56 address review: use namespace import for node:vm (ESM-correct) 2026-04-30 13:14 UTC

@mergify mergify Bot deployed to Mergify Merge Protections April 30, 2026 13:14 Active
@mergify mergify Bot requested a review from a team April 30, 2026 13:21
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 30, 2026

Merge Queue Status

This pull request spent 2 minutes 42 seconds in the queue, including 2 minutes 27 seconds running CI.

Waiting for:
  • schedule=Mon-Fri 09:00-17:30[Europe/Paris]
  • any of:
    • check-success = test-broken-links
    • label = ignore-broken-links
All conditions

Reason

The merge conditions cannot be satisfied due to failing checks

Failing checks:

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

mergify Bot added a commit that referenced this pull request Apr 30, 2026
@mergify mergify Bot added the queued label Apr 30, 2026
@mergify mergify Bot added dequeued and removed queued labels Apr 30, 2026
@jd
Copy link
Copy Markdown
Member Author

jd commented Apr 30, 2026

@Mergifyio queue

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 30, 2026

Merge Queue Status

  • Entered queue2026-04-30 17:15 UTC · Rule: default
  • 🚫 Left the queue2026-04-30 17:15 UTC · at 6464a56bfd544fb87708c6f41abca0b262d0be91

This pull request spent 23 seconds in the queue, with no time running CI.

Reason

The draft pull request cannot be created

Hint

If you want to requeue this pull request, you can post a @mergifyio queue comment.

@mergify mergify Bot added the queued label Apr 30, 2026
mergify Bot added a commit that referenced this pull request Apr 30, 2026
@jd
Copy link
Copy Markdown
Member Author

jd commented May 2, 2026

@Mergifyio queue

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 2, 2026

Merge Queue Status

@mergify mergify Bot added the queued label May 2, 2026
mergify Bot added a commit that referenced this pull request May 2, 2026
@mergify mergify Bot removed the dequeued label May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

4 participants