{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "headless-switch", "type": "registry:ui", "title": "Headless Switch", "description": "Switches are a pleasant interface for toggling a value between two states, and offer the same semantics and keyboard navigation as native checkbox elements.", "dependencies": [ "@headlessui/react", "motion" ], "files": [ { "path": "registry/headless/switch/index.tsx", "content": "'use client';\n\nimport * as React from 'react';\nimport {\n Switch as SwitchPrimitive,\n type SwitchProps as SwitchPrimitiveProps,\n} from '@headlessui/react';\nimport { motion, type HTMLMotionProps } from 'motion/react';\n\nimport { cn } from '@/lib/utils';\n\ntype SwitchProps =\n SwitchPrimitiveProps &\n Omit, 'children'> & {\n leftIcon?: React.ReactNode;\n rightIcon?: React.ReactNode;\n thumbIcon?: React.ReactNode;\n onCheckedChange?: (checked: boolean) => void;\n as?: TTag;\n };\n\nfunction Switch({\n className,\n leftIcon,\n rightIcon,\n thumbIcon,\n onChange,\n as = motion.button,\n ...props\n}: SwitchProps) {\n const [isChecked, setIsChecked] = React.useState(\n props.checked ?? props.defaultChecked ?? false,\n );\n const [isTapped, setIsTapped] = React.useState(false);\n\n React.useEffect(() => {\n setIsChecked(props.checked ?? props.defaultChecked ?? false);\n }, [props.checked, props.defaultChecked]);\n\n const handleChange = React.useCallback(\n (checked: boolean) => {\n setIsChecked(checked);\n onChange?.(checked);\n },\n [onChange],\n );\n\n return (\n setIsTapped(true)}\n onTapCancel={() => setIsTapped(false)}\n onTap={() => setIsTapped(false)}\n {...props}\n >\n {leftIcon && (\n \n {typeof leftIcon !== 'string' ? leftIcon : null}\n \n )}\n\n {rightIcon && (\n \n {typeof rightIcon !== 'string' ? rightIcon : null}\n \n )}\n\n \n {thumbIcon && typeof thumbIcon !== 'string' ? thumbIcon : null}\n \n \n );\n}\n\nexport { Switch, type SwitchProps };\n", "type": "registry:ui", "target": "components/animate-ui/headless/switch.tsx" } ] }