import { useEffect, useState } from "react";
type Theme = "light" | "dark";
export function useTheme() {
const [theme, setTheme] = useState<Theme>(() => {
if (typeof window !== "undefined") {
return (localStorage.getItem("theme") as Theme) || "dark";
}
return "dark";
});
useEffect(() => {
const root = window.document.documentElement;
if (theme === "dark") {
root.classList.add("dark");
} else {
root.classList.remove("dark");
}
localStorage.setItem("theme", theme);
}, [theme]);
return { theme, setTheme };
}