mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +00:00
add new use rua theme hook
use new hook to handle theme switch
This commit is contained in:
28
lib/hooks/use-rua-theme.ts
Normal file
28
lib/hooks/use-rua-theme.ts
Normal file
@ -0,0 +1,28 @@
|
||||
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;
|
Reference in New Issue
Block a user