| import { useState, useEffect } from "react"; |
|
|
| type Theme = "light" | "dark"; |
|
|
| export const useTheme = () => { |
| const [theme, setTheme] = useState<Theme>(() => { |
| if (typeof window !== "undefined") { |
| const stored = localStorage.getItem("theme") as Theme; |
| if (stored) return stored; |
| return window.matchMedia("(prefers-color-scheme: dark)").matches |
| ? "dark" |
| : "light"; |
| } |
| return "light"; |
| }); |
|
|
| useEffect(() => { |
| const root = document.documentElement; |
| root.classList.remove("light", "dark"); |
| root.classList.add(theme); |
| localStorage.setItem("theme", theme); |
| }, [theme]); |
|
|
| const toggleTheme = () => { |
| setTheme((prev) => (prev === "light" ? "dark" : "light")); |
| }; |
|
|
| return { theme, toggleTheme }; |
| }; |
|
|