feat(theme): add theme toggle and pre-mount theme bootstrap

Add a self-contained theme toggle component under
public/src/components/theme/ that toggles the dark class on the root
element and stores the choice under the beat-theme key in
localStorage. Apply the stored theme, or the prefers-color-scheme
default when nothing is stored, before the app mounts so a cold load
never paints the wrong theme.

The pre-paint script in index.html and both 404 copies reads the same
beat-theme key, so the theme is already on the root element when the
first frame is painted.
This commit is contained in:
Locust agent
2026-11-02 18:03:56 +00:00
parent bbb9ce3ae9
commit 640a04d483
7 changed files with 147 additions and 48 deletions
+9 -8
View File
@@ -32,18 +32,19 @@
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Dark mode initialization (prevents flash) -->
<!-- Theme initialization (prevents flash) -->
<script>
(function() {
const theme = localStorage.theme;
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (theme === 'dark' || (!theme && systemDark)) {
document.documentElement.classList.add('dark');
}
var stored = null;
try { stored = window.localStorage.getItem('beat-theme'); } catch (e) { stored = null; }
var systemDark = false;
try { systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches; } catch (e) { systemDark = false; }
var dark = stored === 'dark' || (stored !== 'light' && systemDark);
document.documentElement.classList.toggle('dark', dark);
})();
</script>
<script type="module" crossorigin src="./assets/index-B5k1T3fV.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-iYQAJ_UH.css">
<script type="module" crossorigin src="./assets/index-BEWnN2hq.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-k4KrMhav.css">
</head>
<body class="bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 transition-colors duration-300 antialiased">
<!-- Skip to main content link for accessibility -->
+7 -6
View File
@@ -32,14 +32,15 @@
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Dark mode initialization (prevents flash) -->
<!-- Theme initialization (prevents flash) -->
<script>
(function() {
const theme = localStorage.theme;
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (theme === 'dark' || (!theme && systemDark)) {
document.documentElement.classList.add('dark');
}
var stored = null;
try { stored = window.localStorage.getItem('beat-theme'); } catch (e) { stored = null; }
var systemDark = false;
try { systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches; } catch (e) { systemDark = false; }
var dark = stored === 'dark' || (stored !== 'light' && systemDark);
document.documentElement.classList.toggle('dark', dark);
})();
</script>
</head>
+9 -8
View File
@@ -32,18 +32,19 @@
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Dark mode initialization (prevents flash) -->
<!-- Theme initialization (prevents flash) -->
<script>
(function() {
const theme = localStorage.theme;
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (theme === 'dark' || (!theme && systemDark)) {
document.documentElement.classList.add('dark');
}
var stored = null;
try { stored = window.localStorage.getItem('beat-theme'); } catch (e) { stored = null; }
var systemDark = false;
try { systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches; } catch (e) { systemDark = false; }
var dark = stored === 'dark' || (stored !== 'light' && systemDark);
document.documentElement.classList.toggle('dark', dark);
})();
</script>
<script type="module" crossorigin src="./assets/index-B5k1T3fV.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-iYQAJ_UH.css">
<script type="module" crossorigin src="./assets/index-BEWnN2hq.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-k4KrMhav.css">
</head>
<body class="bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 transition-colors duration-300 antialiased">
<!-- Skip to main content link for accessibility -->
+2 -26
View File
@@ -1,31 +1,7 @@
<script setup>
import { ref, onMounted } from 'vue'
const isDark = ref(false)
onMounted(() => {
isDark.value = document.documentElement.classList.contains('dark')
})
const toggleTheme = () => {
isDark.value = !isDark.value
if (isDark.value) {
document.documentElement.classList.add('dark')
localStorage.theme = 'dark'
} else {
document.documentElement.classList.remove('dark')
localStorage.theme = 'light'
}
}
import ThemeToggle from '@/components/theme/ThemeToggle.vue'
</script>
<template>
<button
@click="toggleTheme"
class="text-gray-600 dark:text-gray-300 hover:text-gray-900 dark:hover:text-gray-100 p-2 min-w-[44px] min-h-[44px] flex items-center justify-center transition-colors duration-200"
aria-label="Toggle theme"
>
<i v-if="isDark" class="fas fa-sun text-xl"></i>
<i v-else class="fas fa-moon text-xl"></i>
</button>
<ThemeToggle />
</template>
@@ -0,0 +1,24 @@
<script setup>
import { ref } from 'vue'
import { isDarkTheme, 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'
}
</script>
<template>
<button
type="button"
class="inline-flex items-center justify-center min-w-[44px] min-h-[44px] rounded-lg text-gray-600 dark:text-gray-300 hover:text-gray-900 dark:hover:text-gray-100 hover:bg-gray-100 dark:hover:bg-gray-800 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 dark:focus-visible:outline-blue-400 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>
+91
View File
@@ -0,0 +1,91 @@
/**
* Theme handling for the light and dark themes.
*
* The stored choice wins: localStorage holds it under THEME_STORAGE_KEY.
* With nothing stored the site follows the prefers-color-scheme media query.
* 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
* can use it before the app exists.
*/
/** localStorage key that holds the reader's choice. */
export const THEME_STORAGE_KEY = 'beat-theme'
const DARK_CLASS = 'dark'
const DARK_QUERY = '(prefers-color-scheme: dark)'
const THEMES = ['light', 'dark']
/**
* The stored theme, or null when nothing valid is stored.
* Storage can be unavailable (private mode, blocked cookies); that is not an
* error, it only means there is no stored choice.
*/
export function readStoredTheme () {
try {
const value = window.localStorage.getItem(THEME_STORAGE_KEY)
return THEMES.includes(value) ? value : null
} catch {
return null
}
}
/** True when the system asks for the dark theme. */
export function systemPrefersDark () {
try {
return typeof window.matchMedia === 'function' && window.matchMedia(DARK_QUERY).matches === true
} catch {
return false
}
}
/** The theme to show: the stored choice wins, the system preference is the default. */
export function resolveTheme () {
return readStoredTheme() ?? (systemPrefersDark() ? 'dark' : 'light')
}
/** Put the theme on the root element and return the theme that is now active. */
export function applyTheme (theme) {
const root = document.documentElement
if (root) {
root.classList.toggle(DARK_CLASS, theme === 'dark')
}
return theme
}
/** True when the root element currently carries the dark theme. */
export function isDarkTheme () {
const root = document.documentElement
return Boolean(root && root.classList.contains(DARK_CLASS))
}
/**
* Remember the theme so it survives a reload.
* A storage failure leaves the theme applied for this page view only.
*/
export function storeTheme (theme) {
try {
window.localStorage.setItem(THEME_STORAGE_KEY, theme)
} catch {
// Storage is unavailable; the theme still applies for this page view.
}
}
/**
* Apply the stored theme, or the system default, before the app mounts.
* Returns the applied theme.
*/
export function initializeTheme () {
return applyTheme(resolveTheme())
}
/**
* Switch between the light and the dark theme, remember the choice and
* return the theme that is now active.
*/
export function toggleTheme () {
const theme = isDarkTheme() ? 'light' : 'dark'
applyTheme(theme)
storeTheme(theme)
return theme
}
+5
View File
@@ -1,8 +1,13 @@
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { initializeTheme } from './components/theme/theme'
import './assets/styles/main.css'
// The theme is applied before the app mounts, so a cold load never paints the
// wrong theme first.
initializeTheme()
const app = createApp(App)
app.use(router)
app.mount('#app')