import { useCallback, useEffect, useRef, useState } from "react"; import { Check, Palette } from "lucide-react"; import { THEMES, applyTheme, loadStoredTheme, storeTheme, type ThemeKey, } from "../lib/theme"; import { useDismissOn } from "../lib/useDismissOn"; /** * TopBar theme switcher. * * - Applies the stored theme on mount (before first paint the attribute is * already set by the pre-hydration inline script in index.html, so this * effect is only a no-op reconcile in the happy path). * - Dropdown lists all registered themes with a preview swatch and * tagline; selection immediately flips the `` attribute * and persists to localStorage. */ export function ThemeSwitcher() { const [theme, setTheme] = useState(() => loadStoredTheme()); const [open, setOpen] = useState(false); const rootRef = useRef(null); // Keep DOM in sync with React state — covers both the initial mount // (in case the pre-hydration script is missing) and any later change. useEffect(() => { applyTheme(theme); storeTheme(theme); }, [theme]); useDismissOn(open, rootRef, useCallback(() => setOpen(false), [])); return (
{open && ( // Mobile ( {/* w- is viewport-aware: 17rem (272px) on wide screens, else capped at viewport minus 24px so it stays flush with the right edge and never overruns the left edge of the screen. */} {THEMES.map((t) => { const active = t.key === theme; return ( ); })}
)} ); }