'use client'; import * as React from 'react'; import { Switch as SwitchPrimitives } from 'radix-ui'; import { motion, type HTMLMotionProps } from 'motion/react'; import { cn } from '@workspace/ui/lib/utils'; type SwitchProps = React.ComponentProps & HTMLMotionProps<'button'> & { leftIcon?: React.ReactNode; rightIcon?: React.ReactNode; thumbIcon?: React.ReactNode; }; function Switch({ className, leftIcon, rightIcon, thumbIcon, onCheckedChange, ...props }: SwitchProps) { const [isChecked, setIsChecked] = React.useState( props?.checked ?? props?.defaultChecked ?? false, ); const [isTapped, setIsTapped] = React.useState(false); React.useEffect(() => { if (props?.checked !== undefined) setIsChecked(props.checked); }, [props?.checked]); const handleCheckedChange = React.useCallback( (checked: boolean) => { setIsChecked(checked); onCheckedChange?.(checked); }, [onCheckedChange], ); 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 };