"use client"; import { Monitor, Moon, Sun } from "lucide-react"; import { useEffect, useState } from "react"; type Theme = "light" | "dark" | "system"; const ORDER: Theme[] = ["system", "light", "dark"]; const ICON = { system: Monitor, light: Sun, dark: Moon }; const LABEL = { system: "System", light: "Light", dark: "Dark" }; function apply(theme: Theme) { const dark = theme === "dark" || (theme === "system" && window.matchMedia("(prefers-color-scheme: dark)").matches); document.documentElement.classList.toggle("dark", dark); } /** Cycles theme System → Light → Dark, persisting the choice in localStorage. */ export function ThemeToggle() { const [theme, setTheme] = useState("system"); useEffect(() => { const saved = (localStorage.getItem("theme") as Theme) || "system"; setTheme(saved); }, []); // Keep "system" in sync if the OS preference changes while selected. useEffect(() => { if (theme !== "system") return; const mq = window.matchMedia("(prefers-color-scheme: dark)"); const onChange = () => apply("system"); mq.addEventListener("change", onChange); return () => mq.removeEventListener("change", onChange); }, [theme]); function cycle() { const next = ORDER[(ORDER.indexOf(theme) + 1) % ORDER.length]; localStorage.setItem("theme", next); setTheme(next); apply(next); } const Icon = ICON[theme]; return ( ); }