{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "switch-spring",
  "title": "Spring switch",
  "description": "Switch with a springy thumb that squashes while pressed, icons that spin inside the thumb, three sizes, label and description.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/inputs/switch-spring.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { GooduiTokens, spring as springs } from \"@/lib/goodui-motion\"\n\nconst SIZES = {\n  sm: { track: \"h-5 w-9\", thumb: 16, travel: 16, icon: \"[&_svg]:size-2.5\" },\n  md: { track: \"h-6 w-11\", thumb: 20, travel: 20, icon: \"[&_svg]:size-3\" },\n  lg: { track: \"h-8 w-14\", thumb: 28, travel: 24, icon: \"[&_svg]:size-4\" },\n} as const\n\nexport type SwitchSpringProps = Omit<React.ComponentProps<\"button\">, \"onChange\" | \"value\" | \"defaultChecked\"> & {\n  /** Controlled on/off state; pair with onCheckedChange. */\n  checked?: boolean\n  /** Initial state when uncontrolled. */\n  defaultChecked?: boolean\n  /** Called with the new state on every toggle (click, Space or Enter). */\n  onCheckedChange?: (checked: boolean) => void\n  /** \"sm\" 20×36px, \"md\" 24×44px, \"lg\" 32×56px track. */\n  size?: keyof typeof SIZES\n  /** Icons drawn inside the thumb, e.g. { on: <Moon />, off: <Sun /> }. */\n  icons?: { on: React.ReactNode; off: React.ReactNode }\n  /** Clickable label next to the switch; also its accessible name. */\n  label?: React.ReactNode\n  /** Muted helper text under the label, linked with aria-describedby. */\n  description?: React.ReactNode\n  /** Put the label before the switch instead of after it. */\n  labelPosition?: \"start\" | \"end\"\n  /** Form field name; a hidden input submits \"on\" when checked and \"off\" when not. */\n  name?: string\n}\n\n/** Switch with a springy thumb that squashes while pressed, optional icons in the thumb, label and description. */\nexport function SwitchSpring({\n  checked: checkedProp,\n  defaultChecked = false,\n  onCheckedChange,\n  size = \"md\",\n  icons,\n  label,\n  description,\n  labelPosition = \"end\",\n  name,\n  disabled,\n  className,\n  id: idProp,\n  onClick,\n  onPointerDown,\n  onKeyDown,\n  onKeyUp,\n  ref,\n  ...props\n}: SwitchSpringProps) {\n  const reduce = useReducedMotion() ?? false\n  const [inner, setInner] = React.useState(defaultChecked)\n  const checked = checkedProp ?? inner\n  const [pressed, setPressed] = React.useState(false)\n  const uid = React.useId()\n  const id = idProp ?? uid\n  const s = SIZES[size]\n  const btnRef = React.useRef<HTMLButtonElement | null>(null)\n  // In a right-to-left context the thumb starts on the right, so it travels the other way.\n  const [rtl, setRtl] = React.useState(false)\n  React.useLayoutEffect(() => {\n    if (btnRef.current) setRtl(getComputedStyle(btnRef.current).direction === \"rtl\")\n  }, [])\n  const setRefs = (el: HTMLButtonElement | null) => {\n    btnRef.current = el\n    if (typeof ref === \"function\") ref(el)\n    else if (ref) (ref as React.RefObject<HTMLButtonElement | null>).current = el\n  }\n\n  React.useEffect(() => {\n    if (!pressed) return\n    const up = () => setPressed(false)\n    window.addEventListener(\"pointerup\", up)\n    window.addEventListener(\"pointercancel\", up)\n    return () => {\n      window.removeEventListener(\"pointerup\", up)\n      window.removeEventListener(\"pointercancel\", up)\n    }\n  }, [pressed])\n\n  const toggle = () => {\n    const next = !checked\n    if (checkedProp === undefined) setInner(next)\n    onCheckedChange?.(next)\n  }\n\n  const spring = reduce ? { duration: 0 } : springs.snap\n\n  const control = (\n    <button\n      ref={setRefs}\n      type=\"button\"\n      role=\"switch\"\n      id={id}\n      aria-checked={checked}\n      aria-describedby={description ? `${id}-desc` : undefined}\n      data-state={checked ? \"checked\" : \"unchecked\"}\n      disabled={disabled}\n      onClick={(e) => {\n        toggle()\n        onClick?.(e)\n      }}\n      onPointerDown={(e) => {\n        if (e.button === 0) setPressed(true)\n        onPointerDown?.(e)\n      }}\n      onKeyDown={(e) => {\n        if (e.key === \" \" || e.key === \"Enter\") setPressed(true)\n        onKeyDown?.(e)\n      }}\n      onKeyUp={(e) => {\n        setPressed(false)\n        onKeyUp?.(e)\n      }}\n      onBlur={() => setPressed(false)}\n      className={cn(\n        \"pc-press relative inline-flex shrink-0 cursor-pointer items-center rounded-full p-0.5 shadow-[inset_0_1px_2px_hsl(var(--pc-sh)/.18),inset_0_0_0_.5px_hsl(var(--pc-sh)/.06)] transition-colors duration-200 outline-none focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50\",\n        \"bg-input data-[state=checked]:bg-primary data-[state=checked]:bg-[image:var(--pc-sheen)] dark:data-[state=unchecked]:bg-input/80\",\n        s.track,\n        className,\n      )}\n      {...props}\n    >\n      <motion.span\n        aria-hidden=\"true\"\n        className={cn(\n          \"pointer-events-none relative grid place-items-center rounded-full bg-white shadow-[0_0_0_.5px_rgb(0_0_0/.06),0_1px_2px_hsl(var(--pc-sh)/.2),0_3px_6px_-1px_hsl(var(--pc-sh)/.22)]\",\n          s.icon,\n        )}\n        style={{ width: s.thumb, height: s.thumb, originX: checked !== rtl ? 1 : 0 }}\n        initial={false}\n        animate={{\n          x: checked ? (rtl ? -s.travel : s.travel) : 0,\n          scaleX: pressed && !reduce ? 1.18 : 1,\n          scaleY: pressed && !reduce ? 0.93 : 1,\n        }}\n        transition={spring}\n      >\n        {icons && (\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            <motion.span\n              key={checked ? \"on\" : \"off\"}\n              className={cn(\"grid place-items-center\", checked ? \"text-primary dark:text-neutral-900\" : \"text-neutral-500\")}\n              initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.3, rotate: checked ? -120 : 120 }}\n              animate={{ opacity: 1, scale: 1, rotate: 0 }}\n              exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.3, rotate: checked ? 120 : -120 }}\n              transition={reduce ? { duration: 0.1 } : springs.ui}\n            >\n              {checked ? icons.on : icons.off}\n            </motion.span>\n          </AnimatePresence>\n        )}\n      </motion.span>\n    </button>\n  )\n\n  const hidden = name ? <input type=\"hidden\" name={name} value={checked ? \"on\" : \"off\"} /> : null\n\n  if (!label && !description) {\n    return (\n      <>\n        <GooduiTokens />\n        {control}\n        {hidden}\n      </>\n    )\n  }\n\n  return (\n    <div className={cn(\"flex items-start gap-3\", labelPosition === \"start\" && \"flex-row-reverse justify-between\")}>\n      <GooduiTokens />\n      {control}\n      {/* Disabled text is muted rather than faded with opacity, so it keeps 4.5:1 contrast. */}\n      <div className=\"grid gap-0.5\">\n        {label && (\n          <label htmlFor={id} className={cn(\"text-sm leading-6 font-medium select-none\", disabled ? \"cursor-not-allowed text-muted-foreground\" : \"cursor-pointer\")}>\n            {label}\n          </label>\n        )}\n        {description && (\n          <p id={`${id}-desc`} className=\"text-xs text-muted-foreground\">\n            {description}\n          </p>\n        )}\n      </div>\n      {hidden}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/switch-spring.tsx"
    }
  ],
  "categories": [
    "components",
    "inputs"
  ],
  "type": "registry:component"
}