Skip to content

Commit

Permalink
style: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyib authored and ianzone committed Aug 12, 2024
1 parent 8b97c54 commit 87f8076
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions config/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const menus = [
'useCounter',
'useTextSelection',
'useWebSocket',
'useTheme',
],
},
{
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import useVirtualList from './useVirtualList';
import useWebSocket from './useWebSocket';
import useWhyDidYouUpdate from './useWhyDidYouUpdate';
import useMutationObserver from './useMutationObserver';
import useTheme from './useTheme';

export {
useRequest,
Expand Down Expand Up @@ -156,4 +157,5 @@ export {
useRafTimeout,
useResetState,
useMutationObserver,
useTheme,
};
42 changes: 26 additions & 16 deletions packages/hooks/src/useTheme/index.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,64 @@
import { useEffect, useState } from 'react';
import useMemoizedFn from '../useMemoizedFn';

export enum ThemeMode {
LIGHT = 'light',
DARK = 'dark',
SYSTEM = 'system',
}

export type ThemeModeType = `${ThemeMode}`;

const matchMedia = window.matchMedia('(prefers-color-scheme: dark)');

function useCurrentTheme() {
const [theme, setTheme] = useState<'light' | 'dark'>(() => {
return matchMedia.matches ? 'dark' : 'light';
return matchMedia.matches ? ThemeMode.DARK : ThemeMode.LIGHT;
});

useEffect(() => {
// 监听系统颜色切换
const listener: MediaQueryList['onchange'] = (event) => {
const onThemeChange: MediaQueryList['onchange'] = (event) => {
if (event.matches) {
setTheme('dark');
setTheme(ThemeMode.DARK);
} else {
setTheme('light');
setTheme(ThemeMode.LIGHT);
}
};

matchMedia.addEventListener('change', listener);
matchMedia.addEventListener('change', onThemeChange);

return () => {
matchMedia.removeEventListener('change', listener);
matchMedia.removeEventListener('change', onThemeChange);
};
}, []);

return theme;
}

export type ThemeModeType = 'light' | 'dark' | 'system';

type PropsType = {
type Options = {
localStorageKey?: string;
};

export function useTheme(props: PropsType = {}) {
const { localStorageKey } = props;
export default function useTheme(options: Options = {}) {
const { localStorageKey } = options;

const [themeMode, setThemeMode] = useState<ThemeModeType>(() => {
const preferredThemeMode =
localStorageKey?.length && (localStorage.getItem(localStorageKey) as ThemeModeType | null);
return preferredThemeMode ? preferredThemeMode : 'system';

return preferredThemeMode ? preferredThemeMode : ThemeMode.SYSTEM;
});

const setThemeModeWithLocalStorage = (mode: ThemeModeType) => {
setThemeMode(mode);
localStorageKey?.length && localStorage.setItem(localStorageKey, mode);

if (localStorageKey?.length) {
localStorage.setItem(localStorageKey, mode);
}
};

const currentTheme = useCurrentTheme();

const theme = themeMode === 'system' ? currentTheme : themeMode;
const theme = themeMode === ThemeMode.SYSTEM ? currentTheme : themeMode;

return {
theme,
Expand Down

0 comments on commit 87f8076

Please sign in to comment.