fix(theme): follow prefers-color-scheme until the reader chooses

The theme was resolved once at mount, so the site did not follow a later
change of the system preference while no explicit choice was stored. The
design contract asks for the site to keep following the system until the
reader makes a choice.

The theme module now subscribes to the prefers-color-scheme change event
and applies the new system preference while nothing is stored. A stored
choice still wins and stops the site from following the system.

The toggle subscribes to theme changes, so its icon and its accessible
label stay in step with the active theme.
This commit is contained in:
Locust agent
2026-11-03 01:58:22 +00:00
parent de96a4655b
commit de7d7d8039
2 changed files with 62 additions and 4 deletions
+10 -2
View File
@@ -1,6 +1,6 @@
<script setup> <script setup>
import { ref } from 'vue' import { onUnmounted, ref } from 'vue'
import { isDarkTheme, toggleTheme } from './theme' import { isDarkTheme, onThemeChange, toggleTheme } from './theme'
// The theme is already on the root element when the app mounts, so the // The theme is already on the root element when the app mounts, so the
// initial state is read straight from it. // initial state is read straight from it.
@@ -9,6 +9,14 @@ const isDark = ref(isDarkTheme())
const onToggle = () => { const onToggle = () => {
isDark.value = toggleTheme() === 'dark' isDark.value = toggleTheme() === 'dark'
} }
// The theme can also change without a click, when the site follows the
// system preference, so the button keeps its icon and label in step.
const unsubscribe = onThemeChange((theme) => {
isDark.value = theme === 'dark'
})
onUnmounted(unsubscribe)
</script> </script>
<template> <template>
+52 -2
View File
@@ -2,7 +2,8 @@
* Theme handling for the light and dark themes. * Theme handling for the light and dark themes.
* *
* The stored choice wins: localStorage holds it under THEME_STORAGE_KEY. * The stored choice wins: localStorage holds it under THEME_STORAGE_KEY.
* With nothing stored the site follows the prefers-color-scheme media query. * With nothing stored the site follows the prefers-color-scheme media query,
* and keeps following it for as long as no explicit choice is made.
* The dark theme is active while the root element carries the class "dark". * The dark theme is active while the root element carries the class "dark".
* *
* The module is free of Vue imports so the pre-mount bootstrap in main.js * The module is free of Vue imports so the pre-mount bootstrap in main.js
@@ -16,6 +17,14 @@ const DARK_CLASS = 'dark'
const DARK_QUERY = '(prefers-color-scheme: dark)' const DARK_QUERY = '(prefers-color-scheme: dark)'
const THEMES = ['light', 'dark'] const THEMES = ['light', 'dark']
/** The subscribers that want to know when the active theme changes. */
const subscribers = new Set()
/** Tell the subscribers that the theme is now this one. */
function notify (theme) {
for (const subscriber of [...subscribers]) subscriber(theme)
}
/** /**
* The stored theme, or null when nothing valid is stored. * The stored theme, or null when nothing valid is stored.
* Storage can be unavailable (private mode, blocked cookies); that is not an * Storage can be unavailable (private mode, blocked cookies); that is not an
@@ -50,6 +59,7 @@ export function applyTheme (theme) {
if (root) { if (root) {
root.classList.toggle(DARK_CLASS, theme === 'dark') root.classList.toggle(DARK_CLASS, theme === 'dark')
} }
notify(theme)
return theme return theme
} }
@@ -76,7 +86,47 @@ export function storeTheme (theme) {
* Returns the applied theme. * Returns the applied theme.
*/ */
export function initializeTheme () { export function initializeTheme () {
return applyTheme(resolveTheme()) const theme = resolveTheme()
applyTheme(theme)
followSystemTheme()
return theme
}
/**
* Follow the system preference while no explicit choice is stored.
*
* The listener applies the new system preference, unless the reader has made
* an explicit choice in the meantime: the stored choice always wins.
*/
function followSystemTheme () {
if (typeof window.matchMedia !== 'function') return
let query
try {
query = window.matchMedia(DARK_QUERY)
} catch {
return
}
if (!query || typeof query.addEventListener !== 'function') return
const onChange = (event) => {
if (readStoredTheme() !== null) return
applyTheme(event.matches ? 'dark' : 'light')
}
try {
query.addEventListener('change', onChange)
} catch {
return
}
}
/**
* Subscribe to theme changes. Returns a function that removes the subscriber.
* The subscriber is called with the theme that is now active.
*/
export function onThemeChange (subscriber) {
subscribers.add(subscriber)
return () => subscribers.delete(subscriber)
} }
/** /**