Files
beat-delivery-methodology/public/src/components/theme/ThemeToggle.vue
T
Locust agent de7d7d8039 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.
2026-11-03 01:58:22 +00:00

33 lines
1.2 KiB
Vue

<script setup>
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.
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>
<button
type="button"
class="inline-flex items-center justify-center min-w-[44px] min-h-[44px] rounded-lg text-muted-foreground hover:text-foreground hover:bg-muted focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent transition-colors duration-200"
:aria-label="isDark ? 'Switch to light theme' : 'Switch to dark theme'"
@click="onToggle"
>
<i v-if="isDark" class="fas fa-sun text-lg" aria-hidden="true"></i>
<i v-else class="fas fa-moon text-lg" aria-hidden="true"></i>
</button>
</template>