mirror of https://github.com/wg-easy/wg-easy
Browse Source
`@eschricht/nuxt-color-mode` deliberately returns `undefined` from
`useClientPreferredColorScheme()` during hydration to avoid Vue hydration
warnings. The consequence is that `<html class>` is unresolved until
`onNuxtReady` fires (~100ms in dev), so dark-OS users on `theme=system`
see a light flash on first paint. The same hydration path can also leave
the class empty on subsequent cookie changes — clicking the theme toggle
to switch from `system` to the opposite of the OS preference (e.g.
`system → light` on a dark-OS machine) leaves the page rendering against
the OS preference instead of the user's pick.
Add a single inline blocking <script> in <head> that:
1. Reads the `theme` cookie + matchMedia synchronously and sets
`<html class="dark"|"light">` before first paint.
2. Installs a permanent MutationObserver on `<html class>` that
re-reads the cookie + matchMedia on every fire and re-applies the
resolved value whenever something else writes a different one
(including empty).
3. Subscribes to `prefers-color-scheme` change events so `theme=system`
tracks the OS without a reload.
Verified across the full 6-case matrix (system/light/dark × OS dark/light)
with Playwright: cookie, `<html class>`, and computed body bg all match
the resolved theme after every cycle step.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
pull/2634/head
1 changed files with 12 additions and 0 deletions
Loading…
Reference in new issue