/** * TwoUX composition: light and dark themes. Light is the default; dark and "follow the system" are a choice the person makes. * The theme lives on , so every role token (light-dark() pairs in twoux.css) switches at once, with no provider. * The choice is remembered in localStorage. Add THEME_SCRIPT to
so the page doesn't flash light before React loads. */ import { Monitor, Moon, Sun } from "lucide-react" import * as React from "react" import { Button } from "./button" import { DropdownMenu, DropdownMenuContent, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger } from "./dropdown-menu" import { Tooltip, TooltipContent, TooltipTrigger } from "./tooltip" export type ThemeChoice = "light" | "dark" | "system" const KEY = "twoux-theme" const listeners = new Set<() => void>() /** Inline in , before the stylesheet: applies the saved theme before the first paint. */ export const THEME_SCRIPT = `try{var t=localStorage.getItem("${KEY}");if(t==="dark"||t==="system")document.documentElement.dataset.theme=t}catch(e){}` const read = (): ThemeChoice => { if (typeof document === "undefined") return "light" const t = document.documentElement.dataset.theme return t === "dark" || t === "system" ? t : "light" } const prefersDark = () => typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches export function setTheme(choice: ThemeChoice) { const root = document.documentElement if (choice === "light") delete root.dataset.theme else root.dataset.theme = choice try { if (choice === "light") localStorage.removeItem(KEY); else localStorage.setItem(KEY, choice) } catch { /* private mode: the choice lasts this visit */ } listeners.forEach((l) => l()) } /** The person's choice, and what it resolves to now ("system" follows the OS and updates when it changes). */ export function useTheme() { const [choice, setChoice] = React.useState