| import { useEffect, useState } from 'react'; | |
| const STORAGE_KEY = 'uaide-theme'; | |
| function getInitialTheme() { | |
| if (typeof window === 'undefined') return 'dark'; | |
| const stored = window.localStorage.getItem(STORAGE_KEY); | |
| if (stored === 'light' || stored === 'dark') return stored; | |
| return 'dark'; | |
| } | |
| export function useTheme() { | |
| const [theme, setTheme] = useState(getInitialTheme); | |
| useEffect(() => { | |
| document.documentElement.setAttribute('data-theme', theme); | |
| window.localStorage.setItem(STORAGE_KEY, theme); | |
| }, [theme]); | |
| const toggleTheme = () => { | |
| setTheme((current) => (current === 'dark' ? 'light' : 'dark')); | |
| }; | |
| return { theme, toggleTheme, setTheme }; | |
| } | |