29 lines
744 B
TypeScript
29 lines
744 B
TypeScript
import { useState, useEffect } from 'react';
|
|
|
|
type Theme = 'light' | 'dark';
|
|
|
|
export function useTheme() {
|
|
const [theme, setTheme] = useState<Theme>(() => {
|
|
const savedTheme = localStorage.getItem('theme') as Theme;
|
|
if (savedTheme) {
|
|
return savedTheme;
|
|
}
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
});
|
|
|
|
useEffect(() => {
|
|
document.documentElement.classList.remove('light', 'dark');
|
|
document.documentElement.classList.add(theme);
|
|
localStorage.setItem('theme', theme);
|
|
}, [theme]);
|
|
|
|
const toggleTheme = () => {
|
|
setTheme(prevTheme => prevTheme === 'light' ? 'dark' : 'light');
|
|
};
|
|
|
|
return {
|
|
theme,
|
|
toggleTheme,
|
|
isDark: theme === 'dark'
|
|
};
|
|
}
|