mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 08:41:37 +00:00
29 lines
705 B
TypeScript
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;
|