- The timeline's ngu-tl classes and keyframes are site-tl; nguLogo is siteLogo; the event card's ngu grid area is org; window.__NGU_SITE__ is window.__SITE__; QR downloads are qr-<page>.png - Browser keys (theme, closed banner and privacy notice, chunk reload) start site-; the page's first script moves any old ngu- values over once, so nobody loses their theme or sees a closed notice again - Session and Google sign-in cookies are site_session and site_oauth (__Host- over https); everyone signs in again once Record ids, server paths, the service and the database file keep their names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
THEME TOGGLE
|
|
|
|
Light or dark. The theme lives on <html data-theme>, 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. Colours 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<Theme>(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 (
|
|
<button
|
|
type="button"
|
|
onClick={toggle}
|
|
aria-label={dark ? 'Switch to light mode' : 'Switch to dark mode'}
|
|
title={dark ? 'Light mode' : 'Dark mode'}
|
|
className={`flex h-9 w-9 items-center justify-center rounded-full transition-colors ${className}`}
|
|
>
|
|
{dark ? <Sun size={20} weight="bold" aria-hidden="true" /> : <Moon size={20} weight="bold" aria-hidden="true" />}
|
|
</button>
|
|
)
|
|
}
|