import { DesktopIcon, MoonIcon, SunIcon } from "./icons"; import { useEffect, useState } from "react"; import { Button } from "./ui/button"; import { DropdownMenu } from "./ui/overlay"; export type ThemeMode = "system" | "light" | "dark"; const KEY = "refs-theme"; const CHANGED = "refs-theme"; const RAIL_KEY = "refs-rail"; const RAIL_CHANGED = "refs-rail"; /** * Runs before paint, so the page never flashes the wrong theme or the wrong layout. The theme follows the * system until the reader picks one, and the rail shows until the reader hides it. Both choices live in this browser. */ export const BOOT_SCRIPT = `(() => { const query = matchMedia("(prefers-color-scheme: dark)"); const apply = () => { let saved = null; try { saved = localStorage.getItem("${KEY}"); } catch {} document.documentElement.dataset.mode = saved === "light" || saved === "dark" ? saved : query.matches ? "dark" : "light"; }; apply(); query.addEventListener("change", apply); addEventListener("storage", apply); addEventListener("${CHANGED}", apply); const rail = () => { let hidden = false; try { hidden = localStorage.getItem("${RAIL_KEY}") === "hidden"; } catch {} if (hidden) document.documentElement.dataset.rail = "hidden"; else delete document.documentElement.dataset.rail; }; rail(); addEventListener("storage", rail); addEventListener("${RAIL_CHANGED}", rail); })();`; /** Whether the reader has hidden the rail, and a switch for it. The change is instant: people use it often. */ export function useRailHidden(): [boolean, () => void] { const [hidden, setHidden] = useState(false); useEffect(() => { const sync = () => setHidden(document.documentElement.dataset.rail === "hidden"); sync(); addEventListener(RAIL_CHANGED, sync); addEventListener("storage", sync); return () => { removeEventListener(RAIL_CHANGED, sync); removeEventListener("storage", sync); }; }, []); const toggle = () => { const next = !hidden; try { if (next) localStorage.setItem(RAIL_KEY, "hidden"); else localStorage.removeItem(RAIL_KEY); } catch { // Storage is blocked: the choice lasts for this page only. } if (next) document.documentElement.dataset.rail = "hidden"; else delete document.documentElement.dataset.rail; dispatchEvent(new Event(RAIL_CHANGED)); }; return [hidden, toggle]; } function saved(): ThemeMode { try { const value = localStorage.getItem(KEY); return value === "light" || value === "dark" ? value : "system"; } catch { return "system"; } } function store(mode: ThemeMode) { try { if (mode === "system") localStorage.removeItem(KEY); else localStorage.setItem(KEY, mode); } catch { // Storage is blocked: the choice lasts for this page only. document.documentElement.dataset.mode = mode === "system" ? (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light") : mode; } dispatchEvent(new Event(CHANGED)); } /** The reader's theme choice and a setter. The swap cross-fades, so brightness never jumps. */ export function useThemeMode(): [ThemeMode, (mode: ThemeMode) => void] { const [mode, setMode] = useState("system"); useEffect(() => { const sync = () => setMode(saved()); sync(); addEventListener(CHANGED, sync); addEventListener("storage", sync); return () => { removeEventListener(CHANGED, sync); removeEventListener("storage", sync); }; }, []); const choose = (next: ThemeMode) => { const calm = matchMedia("(prefers-reduced-motion: reduce)").matches; if (!calm && "startViewTransition" in document) document.startViewTransition(() => store(next)); else store(next); }; return [mode, choose]; } const MODES: { value: ThemeMode; label: string; icon: typeof SunIcon }[] = [ { value: "system", label: "System", icon: DesktopIcon }, { value: "light", label: "Light", icon: SunIcon }, { value: "dark", label: "Dark", icon: MoonIcon }, ]; /** The three theme choices as menu rows, for use inside another menu. */ export function ThemeChoices() { const [mode, choose] = useThemeMode(); return ( <> Theme choose(value as ThemeMode)}> {MODES.map(({ value, label, icon: Glyph }) => ( {label} ))} ); } /** A small control that opens the theme choices, for pages without an account menu. */ export function ThemeMenu({ className, side = "bottom" }: { className?: string; side?: "top" | "bottom" }) { const [mode] = useThemeMode(); const Glyph = MODES.find((item) => item.value === mode)?.icon ?? DesktopIcon; return ( }> ); }