diff --git a/components/pages/dark-mode-btn.tsx b/components/pages/dark-mode-btn.tsx index 1dd3f03..1dbb17e 100644 --- a/components/pages/dark-mode-btn.tsx +++ b/components/pages/dark-mode-btn.tsx @@ -1,23 +1,18 @@ import clsx from 'clsx'; +import { MEDIA, THEME_MAP } from 'lib/consts'; import useMounted from 'lib/hooks/use-mounted'; import { useTheme } from 'next-themes'; import { memo, useCallback, useEffect } from 'react'; import { FiMoon, FiSun } from 'react-icons/fi'; -const MEDIA = '(prefers-color-scheme: dark)'; - const DarkModeBtn = () => { const { mounted } = useMounted(); const { theme, setTheme } = useTheme(); const handleTheme = (type: 'latte' | 'mocha') => () => { const media = window.matchMedia(MEDIA); - const map = { - light: 'latte', - dark: 'mocha', - }; const system = media.matches ? 'dark' : 'light'; setTheme(type); - if (map[system] === type) { + if (THEME_MAP[system] === type) { try { window.localStorage.removeItem('rua-theme'); } catch (err) {} diff --git a/components/rua/rua-code-sandbox.tsx b/components/rua/rua-code-sandbox.tsx index 60f90f0..aa9402b 100644 --- a/components/rua/rua-code-sandbox.tsx +++ b/components/rua/rua-code-sandbox.tsx @@ -5,6 +5,7 @@ import useInView from 'lib/hooks/use-in-view'; import { useTheme } from 'next-themes'; import { memo, useEffect, useState } from 'react'; import RUALoading from './loading/rua-loading'; +import { THEME_CATPUCCIN_MAP, THEME_MAP } from 'lib/consts'; const pattern = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/; @@ -19,8 +20,7 @@ type Props = { const RUACodeSandbox = ({ url }: Props) => { const isUrl = pattern.test(url); - const { systemTheme, theme } = useTheme(); - const currentTheme = theme === 'system' ? systemTheme : theme ?? 'light'; + const { theme } = useTheme(); const { ref, inView } = useInView(); const embed = new URL(url).pathname.split('/')[2]; @@ -28,9 +28,13 @@ const RUACodeSandbox = ({ url }: Props) => { useEffect(() => { inView && setSrc( - `https://codesandbox.io/embed/${embed}?fontsize=14&hidenavigation=1&theme=${currentTheme}&view=preview`, + `https://codesandbox.io/embed/${embed}?fontsize=14&hidenavigation=1&theme=${ + THEME_CATPUCCIN_MAP[ + (theme ?? 'latte') as keyof typeof THEME_CATPUCCIN_MAP + ] + }&view=preview`, ); - }, [currentTheme, embed, inView]); + }, [embed, inView, theme]); const [load, setLoad] = useState(false); const handleLoad = () => { diff --git a/components/rua/rua-sandpack.tsx b/components/rua/rua-sandpack.tsx index 7d10fa2..bdacf08 100644 --- a/components/rua/rua-sandpack.tsx +++ b/components/rua/rua-sandpack.tsx @@ -1,21 +1,35 @@ 'use client'; import { Sandpack, SandpackProps } from '@codesandbox/sandpack-react'; +import { THEME_CATPUCCIN_MAP } from 'lib/consts'; +import useMounted from 'lib/hooks/use-mounted'; import { useTheme } from 'next-themes'; import { memo } from 'react'; interface Props extends SandpackProps {} const RUASandpack = ({ ...rest }: Props) => { - const { systemTheme, theme } = useTheme(); - const currentTheme = theme === 'system' ? systemTheme : theme; + const { mounted } = useMounted(); + const { theme } = useTheme(); + + if (!mounted) { + return ( +
+ +
+ ); + } return ( <>
diff --git a/lib/consts.ts b/lib/consts.ts new file mode 100644 index 0000000..5db3319 --- /dev/null +++ b/lib/consts.ts @@ -0,0 +1,9 @@ +export const MEDIA = '(prefers-color-scheme: dark)'; +export const THEME_CATPUCCIN_MAP = { + latte: 'light' as const, + mocha: 'dark' as const, +}; +export const THEME_MAP = { + light: 'latte' as const, + dark: 'mocha' as const, +};