From 46512142fa0551915a6fd3d01bd7823d743b60f8 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 23 May 2022 12:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=BB=E9=A2=98=E5=88=87?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/index.tsx | 30 +++++++++-------------- src/pages/setting/index.tsx | 47 ++++++++++++++----------------------- src/utils/hooks.ts | 12 ++++++++-- 3 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/layouts/index.tsx b/src/layouts/index.tsx index 8479ac31..f9957c20 100644 --- a/src/layouts/index.tsx +++ b/src/layouts/index.tsx @@ -23,7 +23,7 @@ import { init } from '../utils/init'; export default function (props: any) { const ctx = useCtx(); - const theme = useTheme(); + const { theme, reloadTheme } = useTheme(); const [user, setUser] = useState({}); const [loading, setLoading] = useState(true); const [systemInfo, setSystemInfo] = useState<{ isInitialized: boolean }>(); @@ -90,18 +90,6 @@ export default function (props: any) { getUser(needLoading); }; - const setTheme = () => { - const media = window.matchMedia('(prefers-color-scheme: dark)'); - const storageTheme = localStorage.getItem('qinglong_dark_theme'); - const isDark = - (media.matches && storageTheme !== 'light') || storageTheme === 'dark'; - if (isDark) { - document.body.setAttribute('data-dark', 'true'); - } else { - document.body.setAttribute('data-dark', 'false'); - } - }; - useEffect(() => { if (systemInfo && systemInfo.isInitialized && !user) { getUser(); @@ -115,8 +103,12 @@ export default function (props: any) { }, [systemInfo]); useEffect(() => { - setTheme(); - }, [theme.theme]); + if (theme === 'vs-dark') { + document.body.setAttribute('data-dark', 'true'); + } else { + document.body.setAttribute('data-dark', 'false'); + } + }, [theme]); useEffect(() => { vhCheck(); @@ -208,10 +200,10 @@ export default function (props: any) { return React.Children.map(props.children, (child) => { return React.cloneElement(child, { ...ctx, - ...theme, + theme, user, reloadUser, - reloadTheme: setTheme, + reloadTheme, ws: ws.current, }); }); @@ -331,10 +323,10 @@ export default function (props: any) { {React.Children.map(props.children, (child) => { return React.cloneElement(child, { ...ctx, - ...theme, + theme, user, reloadUser, - reloadTheme: setTheme, + reloadTheme, socketMessage, }); })} diff --git a/src/pages/setting/index.tsx b/src/pages/setting/index.tsx index 528c6e08..98c50ccf 100644 --- a/src/pages/setting/index.tsx +++ b/src/pages/setting/index.tsx @@ -111,8 +111,7 @@ const Setting = ({ ]; const [loading, setLoading] = useState(true); - const defaultDarken = localStorage.getItem('qinglong_dark_theme') || 'auto'; - const [theme, setTheme] = useState(defaultDarken); + const defaultTheme = localStorage.getItem('qinglong_dark_theme') || 'auto'; const [dataSource, setDataSource] = useState([]); const [isModalVisible, setIsModalVisible] = useState(false); const [editedApp, setEditedApp] = useState(); @@ -130,8 +129,18 @@ const Setting = ({ } = DarkReader || {}; const themeChange = (e: any) => { - setTheme(e.target.value); + const _theme = e.target.value; localStorage.setItem('qinglong_dark_theme', e.target.value); + setFetchMethod(fetch); + + if (_theme === 'dark') { + enableDarkMode({}); + } else if (_theme === 'light') { + disableDarkMode(); + } else { + followSystemColorScheme({}); + } + reloadTheme(); }; const getApps = () => { @@ -302,30 +311,6 @@ const Setting = ({ }); }; - useEffect(() => { - const _theme = localStorage.getItem('qinglong_dark_theme') || 'auto'; - if (typeof window === 'undefined') return; - if (typeof window.matchMedia === 'undefined') return; - if (!DarkReader) { - return () => null; - } - setFetchMethod(fetch); - - if (_theme === 'dark') { - enableDarkMode({}); - } else if (_theme === 'light') { - disableDarkMode(); - } else { - followSystemColorScheme({}); - } - reloadTheme(theme); - - // unmount - return () => { - disableDarkMode(); - }; - }, [theme]); - return (
- + diff --git a/src/utils/hooks.ts b/src/utils/hooks.ts index a4dffff7..9dda08b2 100644 --- a/src/utils/hooks.ts +++ b/src/utils/hooks.ts @@ -38,7 +38,15 @@ export const useCtx = () => { }; export const useTheme = () => { - const [theme, setTheme] = useState(''); + const [theme, setTheme] = useState<'vs' | 'vs-dark'>(); + + const reloadTheme = () => { + const media = window.matchMedia('(prefers-color-scheme: dark)'); + const storageTheme = localStorage.getItem('qinglong_dark_theme'); + const isDark = + (media.matches && storageTheme !== 'light') || storageTheme === 'dark'; + setTheme(isDark ? 'vs-dark' : 'vs'); + }; useEffect(() => { const media = window.matchMedia('(prefers-color-scheme: dark)'); @@ -63,5 +71,5 @@ export const useTheme = () => { } }, []); - return { theme }; + return { theme, reloadTheme }; };