topic-analysis / frontend /src /hooks /useTheme.ts
alexchilton
Initial deployment: Sentiment & Topic Analysis Dashboard
6242ddb
raw
history blame contribute delete
664 Bytes
import { useState, useEffect, useCallback } from 'react';
import type { Theme } from '../types';
export function useTheme() {
const [theme, setTheme] = useState<Theme>(() => {
const stored = localStorage.getItem('theme') as Theme | null;
if (stored) return stored;
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
});
useEffect(() => {
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
}, [theme]);
const toggleTheme = useCallback(() => {
setTheme((prev) => (prev === 'dark' ? 'light' : 'dark'));
}, []);
return { theme, toggleTheme };
}