/* ═══════════════════════════════════════════════════════════════ THEME TOGGLE Light or dark. The theme lives on , which index.html sets before the first paint (from this browser's choice, else the system setting) so a dark visitor never sees a white flash. Pressing this makes an explicit choice and remembers it; until then the site follows the system, including a change made while the page is open. Colors come from the tokens in index.css. ═══════════════════════════════════════════════════════════════ */ import { Sun, Moon } from '@phosphor-icons/react' import { useEffect, useState } from 'react' type Theme = 'light' | 'dark' export const THEME_STORAGE_KEY = 'site-theme' const current = (): Theme => document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light' function chosen(): Theme | null { try { const value = localStorage.getItem(THEME_STORAGE_KEY) return value === 'dark' || value === 'light' ? value : null } catch { return null } } export default function ThemeToggle({ className = '' }: { className?: string }) { const [theme, setTheme] = useState(current) // No choice made: follow the system as it changes. useEffect(() => { const media = window.matchMedia('(prefers-color-scheme: dark)') const follow = () => { if (chosen()) return const next: Theme = media.matches ? 'dark' : 'light' document.documentElement.dataset.theme = next setTheme(next) } media.addEventListener('change', follow) return () => media.removeEventListener('change', follow) }, []) const toggle = () => { const next: Theme = theme === 'dark' ? 'light' : 'dark' document.documentElement.dataset.theme = next setTheme(next) try { localStorage.setItem(THEME_STORAGE_KEY, next) } catch {} } const dark = theme === 'dark' return ( ) }