From f19c215386c9875b6158dfca425545733c60f0fe Mon Sep 17 00:00:00 2001 From: DefectingCat Date: Fri, 10 Nov 2023 14:46:36 +0800 Subject: [PATCH] add new use rua theme hook use new hook to handle theme switch --- components/models/cloud-model.tsx | 10 +++++----- components/pages/dark-mode-btn.tsx | 20 +++++--------------- lib/hooks/use-rua-theme.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 lib/hooks/use-rua-theme.ts diff --git a/components/models/cloud-model.tsx b/components/models/cloud-model.tsx index 15fba9a..d6974f5 100644 --- a/components/models/cloud-model.tsx +++ b/components/models/cloud-model.tsx @@ -1,13 +1,13 @@ import { easings, useSpring } from '@react-spring/three'; import { useFrame, useLoader, useThree } from '@react-three/fiber'; +import useRuaTheme from 'lib/hooks/use-rua-theme'; import { getMousePosition } from 'lib/utils'; -import { useTheme } from 'next-themes'; import { useEffect, useRef } from 'react'; +import useStore from 'store'; import * as THREE from 'three'; import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; import { rotationX, rotationY } from './home-model'; -import useStore from 'store'; const CloudModel = () => { const mixer = useRef(null); @@ -16,14 +16,14 @@ const CloudModel = () => { // After model loading, set theme to dark mode. const restore = useRef(false); - const { theme, setTheme } = useTheme(); + const { theme, handleTheme } = useRuaTheme(); // setDarkMode is async called by setTimeout, when component is unmounted // it should not be called. const setDarkMode = () => { if (theme === 'mocha') return; restore.current = true; document.body.style.transition = 'all 1.2s ease-out'; - setTheme('mocha'); + handleTheme('mocha'); }; const [_, api] = useSpring( { @@ -87,7 +87,7 @@ const CloudModel = () => { window.removeEventListener('touchmove', moveHandler); if (!restore.current) return; - setTheme('latte'); + handleTheme('latte'); document.body.style.transition = 'all 0.3s ease-out'; }; // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/components/pages/dark-mode-btn.tsx b/components/pages/dark-mode-btn.tsx index 1dbb17e..f9d23ad 100644 --- a/components/pages/dark-mode-btn.tsx +++ b/components/pages/dark-mode-btn.tsx @@ -1,23 +1,13 @@ import clsx from 'clsx'; -import { MEDIA, THEME_MAP } from 'lib/consts'; +import { MEDIA } from 'lib/consts'; import useMounted from 'lib/hooks/use-mounted'; -import { useTheme } from 'next-themes'; +import useRuaTheme from 'lib/hooks/use-rua-theme'; import { memo, useCallback, useEffect } from 'react'; import { FiMoon, FiSun } from 'react-icons/fi'; const DarkModeBtn = () => { const { mounted } = useMounted(); - const { theme, setTheme } = useTheme(); - const handleTheme = (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) {} - } - }; + const { theme, setTheme, handleTheme } = useRuaTheme(); const handleMediaQuery = useCallback( (e: MediaQueryListEvent | MediaQueryList) => { @@ -64,11 +54,11 @@ const DarkModeBtn = () => { <> {theme === 'mocha' ? ( ) : ( )} diff --git a/lib/hooks/use-rua-theme.ts b/lib/hooks/use-rua-theme.ts new file mode 100644 index 0000000..d5373c0 --- /dev/null +++ b/lib/hooks/use-rua-theme.ts @@ -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;