28 lines
740 B
TypeScript
28 lines
740 B
TypeScript
|
|
import { useState, useEffect, useCallback } from 'react';
|
||
|
|
|
||
|
|
type Theme = 'dark' | 'light';
|
||
|
|
|
||
|
|
const STORAGE_KEY = 'snp-batch-theme';
|
||
|
|
|
||
|
|
function getInitialTheme(): Theme {
|
||
|
|
if (typeof window === 'undefined') return 'dark';
|
||
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
||
|
|
if (stored === 'light' || stored === 'dark') return stored;
|
||
|
|
return 'dark';
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useTheme() {
|
||
|
|
const [theme, setTheme] = useState<Theme>(getInitialTheme);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
document.documentElement.setAttribute('data-theme', theme);
|
||
|
|
localStorage.setItem(STORAGE_KEY, theme);
|
||
|
|
}, [theme]);
|
||
|
|
|
||
|
|
const toggle = useCallback(() => {
|
||
|
|
setTheme((prev) => (prev === 'dark' ? 'light' : 'dark'));
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return { theme, toggle } as const;
|
||
|
|
}
|