| "use client"; |
|
|
| import { createContext, useContext, useEffect, useState } from "react"; |
|
|
| type Theme = "light" | "dark"; |
|
|
| interface ThemeContextType { |
| theme: Theme; |
| toggleTheme: () => void; |
| } |
|
|
| const ThemeContext = createContext<ThemeContextType | undefined>(undefined); |
|
|
| export function ThemeProvider({ children }: { children: React.ReactNode }) { |
| const [theme, setTheme] = useState<Theme>("dark"); |
|
|
| useEffect(() => { |
| const saved = localStorage.getItem("theme") as Theme | null; |
| if (saved) { |
| setTheme(saved); |
| document.documentElement.classList.toggle("dark", saved === "dark"); |
| } |
| }, []); |
|
|
| const toggleTheme = () => { |
| const newTheme = theme === "dark" ? "light" : "dark"; |
| setTheme(newTheme); |
| localStorage.setItem("theme", newTheme); |
| document.documentElement.classList.toggle("dark", newTheme === "dark"); |
| }; |
|
|
| return ( |
| <ThemeContext.Provider value={{ theme, toggleTheme }}> |
| {children} |
| </ThemeContext.Provider> |
| ); |
| } |
|
|
| export function useTheme() { |
| const context = useContext(ThemeContext); |
| if (!context) { |
| throw new Error("useTheme must be used within ThemeProvider"); |
| } |
| return context; |
| } |
|
|