"use client"; import { useEffect, useState } from "react"; type Theme = "light" | "dark"; function readTheme(): Theme { if (typeof window === "undefined") return "light"; const stored = localStorage.getItem("theme"); if (stored === "light" || stored === "dark") return stored; return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; } function applyTheme(theme: Theme) { const root = document.documentElement; if (theme === "dark") root.classList.add("dark"); else root.classList.remove("dark"); } export function useTheme() { const [theme, setThemeState] = useState("light"); const [mounted, setMounted] = useState(false); useEffect(() => { setThemeState(readTheme()); setMounted(true); }, []); useEffect(() => { if (typeof window === "undefined") return; const onStorage = (e: StorageEvent) => { if (e.key === "theme" && (e.newValue === "light" || e.newValue === "dark")) { setThemeState(e.newValue); applyTheme(e.newValue); } }; window.addEventListener("storage", onStorage); return () => window.removeEventListener("storage", onStorage); }, []); function setTheme(next: Theme) { setThemeState(next); try { localStorage.setItem("theme", next); } catch {} applyTheme(next); } function toggle() { setTheme(theme === "dark" ? "light" : "dark"); } return { theme, setTheme, toggle, mounted }; }