Files
DefectingCat.github.io/lib/hooks/use-rua-theme.ts
DefectingCat f19c215386 add new use rua theme hook
use new hook to handle theme switch
2023-11-10 14:46:36 +08:00

29 lines
705 B
TypeScript

import { MEDIA, THEME_MAP } from 'lib/consts';
import { useTheme } from 'next-themes';
import { useCallback } from 'react';
const useRuaTheme = () => {
const { theme, setTheme } = useTheme();
const handleTheme = useCallback((type: 'latte' | 'mocha') => {
const media = window.matchMedia(MEDIA);
const system = media.matches ? 'dark' : 'light';
setTheme(type);
if (THEME_MAP[system] === type) {
try {
window.localStorage.removeItem('rua-theme');
} catch (err) {
console.error(err);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return {
theme,
setTheme,
handleTheme,
};
};
export default useRuaTheme;