'use client'; import * as React from 'react'; import { Switch as SwitchPrimitives } from '@base-ui-components/react/switch'; import { motion, type HTMLMotionProps } from 'motion/react'; import { cn } from '@workspace/ui/lib/utils'; type SwitchProps = Omit< React.ComponentProps, 'render' > & { motionProps?: HTMLMotionProps<'button'>; leftIcon?: React.ReactNode; rightIcon?: React.ReactNode; thumbIcon?: React.ReactNode; }; function Switch({ className, leftIcon, rightIcon, thumbIcon, onCheckedChange, motionProps, ...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, event: Event) => { setIsChecked(checked); onCheckedChange?.(checked, event); }, [onCheckedChange], ); return ( setIsTapped(true)} onTapCancel={() => setIsTapped(false)} onTap={() => setIsTapped(false)} {...motionProps} /> } > {leftIcon && ( {typeof leftIcon !== 'string' ? leftIcon : null} )} {rightIcon && ( {typeof rightIcon !== 'string' ? rightIcon : null} )} } > {thumbIcon && typeof thumbIcon !== 'string' ? thumbIcon : null} ); } Switch.displayName = SwitchPrimitives.Root.displayName; export { Switch, type SwitchProps };