import { useEffect, useState } from "react"; import { Moon, Sun } from "lucide-react"; import { cn } from "@/lib/utils"; type Theme = "dark" | "light"; const STORAGE_KEY = "adfd:theme"; /** Read & apply the persisted theme. Used at app boot AND by the toggle. */ export function useTheme() { const [theme, setTheme] = useState(() => { if (typeof document === "undefined") return "dark"; const stored = window.localStorage.getItem(STORAGE_KEY); if (stored === "light" || stored === "dark") return stored; // Fall back to system preference if no override. return window.matchMedia?.("(prefers-color-scheme: light)").matches ? "light" : "dark"; }); useEffect(() => { const root = document.documentElement; if (theme === "light") { root.classList.add("theme-light"); root.classList.remove("dark"); } else { root.classList.add("dark"); root.classList.remove("theme-light"); } window.localStorage.setItem(STORAGE_KEY, theme); }, [theme]); return { theme, setTheme }; } interface Props { className?: string; size?: "sm" | "md"; showLabel?: boolean; } /** Visual two-state switch: dark (default) ⇄ light. */ export default function ThemeToggle({ className, size = "md", showLabel = true, }: Props) { const { theme, setTheme } = useTheme(); const isLight = theme === "light"; const dim = size === "sm" ? "h-7" : "h-8"; const padX = size === "sm" ? "px-1" : "px-1.5"; const knob = size === "sm" ? "h-5 w-5" : "h-6 w-6"; return (
{showLabel && ( theme )}
); }