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>
import { ref } from 'vue'
import { isDarkTheme, toggleTheme } from './theme'
import { onUnmounted, ref } from 'vue'
import { isDarkTheme, onThemeChange, toggleTheme } from './theme'
// The theme is already on the root element when the app mounts, so the
// initial state is read straight from it.
@@ -9,6 +9,14 @@ const isDark = ref(isDarkTheme())
const onToggle = () => {
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>
<template>