'use client'; import * as React from 'react'; import { Switch as SwitchPrimitive, type SwitchProps as SwitchPrimitiveProps, } from '@headlessui/react'; import { motion, type HTMLMotionProps } from 'motion/react'; import { cn } from '@workspace/ui/lib/utils'; type SwitchProps = SwitchPrimitiveProps & Omit, 'children'> & { leftIcon?: React.ReactNode; rightIcon?: React.ReactNode; thumbIcon?: React.ReactNode; onCheckedChange?: (checked: boolean) => void; as?: TTag; }; function Switch({ className, leftIcon, rightIcon, thumbIcon, onChange, as = motion.button, ...props }: SwitchProps) { const [isChecked, setIsChecked] = React.useState( props.checked ?? props.defaultChecked ?? false, ); const [isTapped, setIsTapped] = React.useState(false); React.useEffect(() => { setIsChecked(props.checked ?? props.defaultChecked ?? false); }, [props.checked, props.defaultChecked]); const handleChange = React.useCallback( (checked: boolean) => { setIsChecked(checked); onChange?.(checked); }, [onChange], ); return ( setIsTapped(true)} onTapCancel={() => setIsTapped(false)} onTap={() => setIsTapped(false)} {...props} > {leftIcon && ( {typeof leftIcon !== 'string' ? leftIcon : null} )} {rightIcon && ( {typeof rightIcon !== 'string' ? rightIcon : null} )} {thumbIcon && typeof thumbIcon !== 'string' ? thumbIcon : null} ); } export { Switch, type SwitchProps };