"use client"; import type {FC, ChangeEvent} from "react"; import type {SwitchProps} from "@heroui/react"; import {VisuallyHidden} from "@react-aria/visually-hidden"; import {useSwitch} from "@heroui/react"; import {useTheme} from "next-themes"; import {clsx} from "@heroui/shared-utils"; import {useIsSSR} from "@react-aria/ssr"; import {usePostHog} from "posthog-js/react"; import {SunLinearIcon, MoonIcon} from "@/components/icons"; export interface ThemeSwitchProps { className?: string; classNames?: SwitchProps["classNames"]; } export const ThemeSwitch: FC = ({className, classNames}) => { const {theme, setTheme} = useTheme(); const isSSR = useIsSSR(); const posthog = usePostHog(); const initialTheme = isSSR ? "light" : theme; const handleThemeChange = ( e?: ChangeEvent | React.MouseEvent | React.KeyboardEvent, ) => { e?.preventDefault(); e?.stopPropagation(); const newTheme = theme === "light" ? "dark" : "light"; setTheme(newTheme); posthog.capture("ThemeChange", { action: "click", category: "theme", data: newTheme, }); }; const {Component, slots, isSelected, getBaseProps, getInputProps, getWrapperProps} = useSwitch({ isSelected: initialTheme === "light", "aria-label": `Switch to ${initialTheme === "light" ? "dark" : "light"} mode`, onChange: handleThemeChange as (event: ChangeEvent) => void, }); return ( { if (e.key === "Enter" || e.key === " ") { handleThemeChange(e); } }, })} >
{!isSelected || isSSR ? : }
); };