fix: useTheme now subscribes to live OS theme changes in 'system' mode#34
Merged
abayomicornelius merged 4 commits intoJul 17, 2026
Conversation
useTheme read matchMedia('(prefers-color-scheme: dark)').matches once,
inside an effect keyed only on `theme`, so the <html> dark class never
updated again unless `theme` itself changed. A user on 'system' whose
OS switched between light/dark (time-of-day auto-switching, or a
manual OS toggle) while the app stayed open would get stuck on
whichever mode was active at mount/last theme change.
useIsDarkMode() (src/hooks/useMediaQuery.ts) already solves this
correctly via matchMedia's 'change' event — useTheme just wasn't
calling it. Swap the inline one-shot check for useIsDarkMode() and add
its value to the effect's dependency array, so OS-level changes now
also trigger a re-toggle when (and only when) theme === 'system'.
Covers the exact reproduction from the issue: with theme === 'system', firing a mocked matchMedia 'change' event now flips the dark class without theme itself changing (both directions, light->dark and dark->light). Regression-tests that explicit 'light'/'dark' selections are unaffected by OS-level changes — only 'system' should react.
useTheme's correctness now depends on this hook actually re-firing on 'change' events, and it had no direct test coverage at all. Covers the initial synchronous value, reacting to a change event, and that the change listener is removed on unmount (no dangling setState after unmount).
Contributor
Author
|
All checks has passed. kindly review |
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.
Summary
Fixes #31.
useThemereadmatchMedia('(prefers-color-scheme: dark)').matchesonce, inside an effect keyed only ontheme, so the<html>element'sdarkclass never updated again unlessthemeitself changed. A user on'system'whose OS switched between light/dark (time-of-day auto-switching, or a manual OS toggle) while the app stayed open would get visually stuck on whichever mode was active at mount or at the last explicit theme change — invisible in normal manual QA, very visible to real users.The fix already existed one file away:
useIsDarkMode()(src/hooks/useMediaQuery.ts) correctly subscribes tomatchMedia'schangeevent viauseMediaQuery.useThemejust wasn't using it — it re-implemented a one-shot, non-reactive version of the same check inline. Swapped the inline check foruseIsDarkMode()and added its value to the effect's dependency array, so an OS-level change now re-triggers the effect (and only affects thedarkclass) whentheme === 'system'.While in there, I noticed
useMediaQuery/useIsDarkMode— whichuseTheme's correctness now directly depends on — had zero test coverage of its own, so I added that too.Test plan
src/hooks/useTheme.test.ts(new): reproduces the issue's exact repro steps with a mockedmatchMediawhosechangehandler is fired mid-test — asserts thedarkclass updates in both directions (light→dark, dark→light) with no change tothemeitself, and regression-tests that explicit'light'/'dark'selections are unaffected by OS-level changessrc/hooks/useMediaQuery.test.ts(new): initial synchronous value, reacts to achangeevent, and removes its listener on unmountnpx vitest run— 74/74 passingnpm run lint— cleannpx tsc --noEmit— cleannpm run build— succeeds