From 2f4fb657e6167931651013f373fc3f485c160f7c Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Mon, 2 Aug 2021 21:21:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DmatchMedia=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/hooks.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/utils/hooks.ts b/src/utils/hooks.ts index 3310831a..14ec3c60 100644 --- a/src/utils/hooks.ts +++ b/src/utils/hooks.ts @@ -47,7 +47,8 @@ export const useTheme = () => { const isDark = (media.matches && storageTheme !== 'light') || storageTheme === 'dark'; setTheme(isDark ? 'vs-dark' : 'vs'); - media.addEventListener('change', (e) => { + + const cb = (e: any) => { if (storageTheme === 'auto' || !storageTheme) { if (e.matches) { setTheme('vs-dark'); @@ -55,7 +56,12 @@ export const useTheme = () => { setTheme('vs'); } } - }); + }; + if (typeof media.addEventListener === 'function') { + media.addEventListener('change', cb); + } else if (typeof media.addListener === 'function') { + media.addListener(cb); + } }, []); return { theme };