mirror of
https://github.com/lukaszraczylo/beat-delivery-methodology.git
synced 2026-11-26 11:08:13 +00:00
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:
@@ -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>
|
||||
@@ -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
|
||||
}
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user