diff --git a/src/components/random.tsx b/src/components/random.tsx index 4403cbd..457071c 100644 --- a/src/components/random.tsx +++ b/src/components/random.tsx @@ -2,30 +2,25 @@ import { useEffect, useState } from "react"; import { Button } from "./ui/button"; import { createRandomTheme } from "../lib/create-theme-config"; import { useTheme } from "next-themes"; -import { - getComputedHSLColor, - saveTheme, -} from "../lib/utils"; +import { getComputedHSLColor, saveTheme } from "../lib/utils"; import { Dices, Lock, UnLock } from "./icons"; import { SystemThemes, themeModes, ThemeWithHSLColor } from "../lib/theme"; import { themeEmittor } from "../lib/emittors"; +import { useIsMount } from "../hooks/useIsMount"; function RandomBtn() { - const { - resolvedTheme: NresolvedTheme = "" + undefined, - systemTheme: NsystemTheme = "dark", - } = useTheme(); - const [resolvedTheme, setResolvedTheme] = useState(); - const [systemTheme, setSystemTheme] = useState(); - useEffect(() => { - setResolvedTheme(NresolvedTheme); - setSystemTheme(NsystemTheme); - }, [NresolvedTheme, NsystemTheme]); + const { resolvedTheme = "" + undefined, systemTheme = "dark" } = useTheme(); + + const isMount = useIsMount(); const [lockPrimary, setLockPrimary] = useState(true); + + if (!isMount) return null; + const onClickHandler = () => { const themes = createRandomTheme( lockPrimary ? getComputedHSLColor("--primary") : undefined ); + let theme; if (SystemThemes.includes(resolvedTheme as any)) { @@ -37,7 +32,9 @@ function RandomBtn() { } themeEmittor.applyTheme(theme); }; + const LockIcon = lockPrimary ? Lock : UnLock; + return ( - - + {( + [ + ["light", SunIcon], + ["dark", MoonIcon], + ["system", MonitorIcon], + ] as const + ).map(([themeN, Icon]) => ( + + ))} ); } diff --git a/src/hooks/useIsMount.ts b/src/hooks/useIsMount.ts new file mode 100644 index 0000000..58a4c74 --- /dev/null +++ b/src/hooks/useIsMount.ts @@ -0,0 +1,12 @@ +import { useEffect, useState } from "react"; + +export function useIsMount(){ + const [isMount, setIsMount] = useState(false); + useEffect(() => { + setIsMount(true) + }, []); + + return isMount +} + +export const useIsClient = useIsMount \ No newline at end of file