perf: toggle theme too much render in catalog

This commit is contained in:
jialin
2025-08-11 09:35:01 +08:00
parent bb67a8cbbb
commit e1ab735823
2 changed files with 20 additions and 9 deletions
+3 -3
View File
@@ -15,7 +15,7 @@ const fadeOut = keyframes`
} }
`; `;
const Mask = styled.div<{ isHiding: boolean }>` const Mask = styled.div<{ $isHiding: boolean }>`
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
@@ -26,7 +26,7 @@ const Mask = styled.div<{ isHiding: boolean }>`
height: 100vh; height: 100vh;
background-color: rgba(59, 59, 59, 1); background-color: rgba(59, 59, 59, 1);
z-index: 9999; z-index: 9999;
animation: ${({ isHiding }) => (isHiding ? fadeOut : 'none')} ${duration}s animation: ${({ $isHiding }) => ($isHiding ? fadeOut : 'none')} ${duration}s
ease forwards; ease forwards;
`; `;
@@ -79,7 +79,7 @@ const DarkModeMask = () => {
return () => el.removeEventListener('animationend', handleAnimationEnd); return () => el.removeEventListener('animationend', handleAnimationEnd);
}, [visible]); }, [visible]);
return visible ? <Mask ref={maskRef} isHiding={isHiding}></Mask> : null; return visible ? <Mask ref={maskRef} $isHiding={isHiding}></Mask> : null;
}; };
export default DarkModeMask; export default DarkModeMask;
+17 -6
View File
@@ -9,6 +9,8 @@ export default function useUserSettings() {
const { light, dark, colorPrimary } = themeConfig; const { light, dark, colorPrimary } = themeConfig;
const [userSettings, setUserSettings] = useAtom(userSettingsHelperAtom); const [userSettings, setUserSettings] = useAtom(userSettingsHelperAtom);
console.log('userSettings===', userSettings);
const getCurrentTheme = (mode: Theme): 'light' | 'realDark' => { const getCurrentTheme = (mode: Theme): 'light' | 'realDark' => {
if (mode === 'auto') { if (mode === 'auto') {
return window.matchMedia('(prefers-color-scheme: dark)').matches return window.matchMedia('(prefers-color-scheme: dark)').matches
@@ -46,23 +48,32 @@ export default function useUserSettings() {
}); });
}; };
useEffect(() => { const initTheme = () => {
setHtmlThemeAttr(userSettings.theme); const systemTheme = window.matchMedia('(prefers-color-scheme: dark)')
}, [userSettings.theme]); .matches
? 'realDark'
: 'light';
if (userSettings.mode === 'auto' && systemTheme !== userSettings.theme) {
setTheme('auto');
}
};
useEffect(() => { useEffect(() => {
if (userSettings.mode !== 'auto') return; initTheme();
}, [userSettings.theme, userSettings.mode]);
setTheme('auto'); useEffect(() => {
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const handleChange = () => { const handleChange = () => {
// The theme follows the system preference, when the mode is set to 'auto'
if (userSettings.mode !== 'auto') return;
setTheme('auto'); setTheme('auto');
}; };
mediaQuery.addEventListener('change', handleChange); mediaQuery.addEventListener('change', handleChange);
return () => { return () => {
mediaQuery.removeEventListener('change', handleChange); mediaQuery.removeEventListener('change', handleChange);
}; };
}, [userSettings.mode]); }, [userSettings.mode, userSettings.theme]);
return { return {
userSettings, userSettings,