import { FC, useState, useEffect } from "react"; import { VisuallyHidden } from "@react-aria/visually-hidden"; import { SwitchProps, useSwitch } from "@heroui/switch"; import { useTheme } from "next-themes"; import clsx from "clsx"; import { SunFilledIcon, MoonFilledIcon } from "@/components/icons"; export interface ThemeSwitchProps { className?: string; classNames?: SwitchProps["classNames"]; } export const ThemeSwitch: FC = ({ className, classNames, }) => { const [isMounted, setIsMounted] = useState(false); const { theme, setTheme } = useTheme(); const onChange = () => { theme === "light" ? setTheme("dark") : setTheme("light"); }; const { Component, slots, isSelected, getBaseProps, getInputProps, getWrapperProps, } = useSwitch({ isSelected: theme === "light", onChange, }); useEffect(() => { setIsMounted(true); }, [isMounted]); // Prevent Hydration Mismatch if (!isMounted) return
; return (
{isSelected ? ( ) : ( )}
); };