{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "switch",
  "title": "Switch",
  "description": "A toggle whose thumb slides with a double bounce, ported from transitions.dev: past the end, back, settle. A wide pill thumb on a 44x22 track, hover and press states, geometry as CSS variables.",
  "dependencies": [
    "@base-ui/react"
  ],
  "registryDependencies": [
    "https://www.deltacomponents.dev/r/utils.json",
    "https://www.deltacomponents.dev/r/tokens.json"
  ],
  "files": [
    {
      "path": "registry/ui/switch.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Switch as SwitchPrimitive } from \"@base-ui/react/switch\";\n\nimport { cn } from \"@/lib/utils\";\n\ntype SwitchSize = \"sm\" | \"default\" | \"lg\";\n\ninterface SwitchProps extends SwitchPrimitive.Root.Props {\n  size?: SwitchSize;\n}\n\n/** The track and thumb geometry per size, as custom properties, so every\n *  derived value — the travel, the hover pill, the press squash — follows,\n *  and a consumer can still resize from `style`. The tracks are Ant\n *  Design's default (44×22) and small (28×16), and the default scaled by\n *  iOS's 51×31 proportion; the thumb is iOS 26's: a wide pill filling most\n *  of the track's height behind a thin inset, rather than a ball floating\n *  in it. */\nconst GEOMETRY: Record<SwitchSize, React.CSSProperties> = {\n  sm: {\n    \"--switch-w\": \"28px\",\n    \"--switch-h\": \"16px\",\n    \"--switch-pad\": \"1.5px\",\n    \"--switch-thumb-w\": \"17px\",\n    \"--switch-thumb-h\": \"13px\",\n    \"--switch-pill\": \"1px\",\n    \"--switch-press\": \"2px\",\n    \"--switch-overshoot\": \"1px\",\n  } as React.CSSProperties,\n  default: {\n    \"--switch-w\": \"44px\",\n    \"--switch-h\": \"22px\",\n    \"--switch-pad\": \"2px\",\n    \"--switch-thumb-w\": \"26px\",\n    \"--switch-thumb-h\": \"18px\",\n    \"--switch-pill\": \"2px\",\n    \"--switch-press\": \"4px\",\n    \"--switch-overshoot\": \"1px\",\n  } as React.CSSProperties,\n  lg: {\n    \"--switch-w\": \"56px\",\n    \"--switch-h\": \"28px\",\n    \"--switch-pad\": \"2.5px\",\n    \"--switch-thumb-w\": \"33px\",\n    \"--switch-thumb-h\": \"23px\",\n    \"--switch-pill\": \"3px\",\n    \"--switch-press\": \"5px\",\n    \"--switch-overshoot\": \"1.5px\",\n  } as React.CSSProperties,\n};\n\nfunction Switch({\n  size = \"default\",\n  className,\n  style,\n  checked,\n  defaultChecked,\n  onCheckedChange,\n  ...props\n}: SwitchProps) {\n  const [uncontrolledChecked, setUncontrolledChecked] = React.useState(defaultChecked ?? false);\n  const isChecked = checked ?? uncontrolledChecked;\n  // The double bounce is a keyframe sequence, and a keyframe plays whenever\n  // its selector matches — so with it attached from the start the \"off\"\n  // sequence would run on mount. It is keyed to the resolved checked value\n  // actually flipping, not to the change attempt, so a controlled switch\n  // whose parent declines the change stays still. It is dropped again when\n  // it ends, which also hands `translate` back to the transition below, so\n  // a hover that widens the thumb slides it rather than snapping it.\n  const [animate, setAnimate] = React.useState(false);\n  const [lastChecked, setLastChecked] = React.useState(isChecked);\n  if (lastChecked !== isChecked) {\n    setLastChecked(isChecked);\n    setAnimate(true);\n  }\n  // Hover is a promise of what a press will do, and the press just\n  // happened. So a change takes the hover state away until the pointer\n  // re-enters — after a click the pointer is already inside, so the next\n  // enter is the re-entry; a keyboard or label-text toggle made with the\n  // pointer elsewhere gets its hover back the moment the pointer arrives.\n  // A disabled switch promises nothing, so it never hovers either — it\n  // keeps its pointer events for the not-allowed cursor.\n  const [hoverSuspended, setHoverSuspended] = React.useState(false);\n  const thumbRef = React.useRef<HTMLSpanElement>(null);\n  const [from, setFrom] = React.useState(\"0px\");\n\n  return (\n    <SwitchPrimitive.Root\n      data-slot=\"switch\"\n      data-animate={animate || undefined}\n      data-no-hover={hoverSuspended || props.disabled || undefined}\n      {...props}\n      checked={isChecked}\n      onCheckedChange={(next, eventDetails) => {\n        // A toggle mid-flight continues from the thumb's current position\n        // rather than wherever the keyframes' first frame would otherwise\n        // snap it to.\n        const thumb = thumbRef.current;\n        if (thumb) {\n          const translate = getComputedStyle(thumb).translate;\n          setFrom(translate === \"none\" ? \"0px\" : translate.split(\" \")[0]);\n        }\n        setUncontrolledChecked(next);\n        setHoverSuspended(true);\n        onCheckedChange?.(next, eventDetails);\n      }}\n      onPointerEnter={(event) => {\n        setHoverSuspended(false);\n        props.onPointerEnter?.(event);\n      }}\n      data-size={size}\n      style={{ ...GEOMETRY[size], ...({ \"--switch-from\": from } as React.CSSProperties), ...style }}\n      className={cn(\n        \"group/switch relative inline-flex h-(--switch-h) w-(--switch-w) shrink-0 cursor-pointer items-center rounded-full outline-none select-none\",\n        // Derived geometry. The thumb's live width and height start at the\n        // resting ones; hover widens it into a pill, press widens it further\n        // and squashes it. The thumb is centred in the track's height, so a\n        // consumer's --switch-h override and the press squash both\n        // re-centre themselves.\n        \"[--switch-travel:calc(var(--switch-w)-var(--switch-thumb-w)-2*var(--switch-pad))]\",\n        \"[--switch-tw:var(--switch-thumb-w)] [--switch-th:var(--switch-thumb-h)] [--switch-top:calc((var(--switch-h)-var(--switch-th))/2)]\",\n        // The hover rule's `:not()` selectors outrank `:active`, so it has\n        // to exclude the press itself.\n        \"not-data-[no-hover]:not-active:hover:[--switch-tw:calc(var(--switch-thumb-w)+var(--switch-pill))]\",\n        \"active:[--switch-tw:calc(var(--switch-thumb-w)+var(--switch-press))] active:[--switch-th:calc(var(--switch-thumb-h)-var(--switch-press))]\",\n        // A widening thumb stays pinned to the track's far edge when on, so\n        // the travel gives back whatever width it gained.\n        \"[--switch-end:calc(var(--switch-travel)-(var(--switch-tw)-var(--switch-thumb-w)))]\",\n        // The target is at least 44px tall (48px on touch, Material's\n        // minimum), with the same slop either side — never under 8px — so\n        // the smaller the track, the more forgiving the margin around it.\n        \"[--switch-hit:44px] pointer-coarse:[--switch-hit:48px]\",\n        \"[--switch-slop:max(8px,calc((var(--switch-hit)-var(--switch-h))/2))]\",\n        \"before:absolute before:-inset-(--switch-slop) before:content-['']\",\n        // Light is input-grey off and primary on with a white thumb; dark\n        // follows iOS 26's dark switch — the off track is the input fill\n        // with a white thumb, on is primary with a primary-foreground\n        // thumb, so on/off is a jump in lightness in both themes; the\n        // hairline inset ring (12% overlay) defines the off pill against\n        // the page and disappears into the on track.\n        \"bg-input not-data-[no-hover]:hover:bg-[color-mix(in_oklab,var(--input),rgb(var(--overlay))_10%)]\",\n        \"data-[checked]:bg-primary not-data-[no-hover]:data-[checked]:hover:bg-primary/90\",\n        \"inset-ring inset-ring-[rgb(var(--overlay)/0.12)]\",\n        \"transition-colors [transition-duration:var(--motion-fast,80ms)]\",\n        \"focus-visible:ring-1 focus-visible:ring-[color:var(--focus-ring,#6B97FF)] focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n        \"data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50\",\n        className\n      )}\n    >\n      <SwitchPrimitive.Thumb\n        ref={thumbRef}\n        data-slot=\"switch-thumb\"\n        onAnimationEnd={() => setAnimate(false)}\n        className={cn(\n          \"pointer-events-none absolute top-(--switch-top) left-(--switch-pad) block h-(--switch-th) w-(--switch-tw) rounded-full shadow-sm\",\n          \"bg-white dark:group-data-[checked]/switch:bg-primary-foreground\",\n          \"translate-x-0 group-data-[checked]/switch:translate-x-(--switch-end)\",\n          \"transition-[translate,width,height,top,background-color] [transition-duration:var(--motion-moderate,160ms)] [transition-timing-function:var(--motion-ease,ease)]\",\n          // Thumb slides with a double bounce: past the end, back, settle.\n          // transitions.dev's toggle, on the slow tier; the curve is its\n          // own, the overshoot is in the keyframes.\n          \"group-data-[animate]/switch:group-data-[checked]/switch:animate-[switch-on_var(--motion-slow,240ms)_cubic-bezier(0.34,1.35,0.64,1)_both]\",\n          \"group-data-[animate]/switch:group-data-[unchecked]/switch:animate-[switch-off_var(--motion-slow,240ms)_cubic-bezier(0.34,1.35,0.64,1)_both]\"\n        )}\n      />\n    </SwitchPrimitive.Root>\n  );\n}\n\nexport { Switch, type SwitchProps, type SwitchSize };\n",
      "type": "registry:ui",
      "target": "components/ui/switch.tsx"
    }
  ],
  "css": {
    "@keyframes switch-on": {
      "0%": {
        "translate": "var(--switch-from, 0px) 0"
      },
      "55%": {
        "translate": "calc(var(--switch-end) + var(--switch-overshoot)) 0"
      },
      "80%, 100%": {
        "translate": "var(--switch-end) 0"
      }
    },
    "@keyframes switch-off": {
      "0%": {
        "translate": "var(--switch-from, var(--switch-end)) 0"
      },
      "55%": {
        "translate": "calc(0px - var(--switch-overshoot)) 0"
      },
      "80%, 100%": {
        "translate": "0 0"
      }
    },
    "@media (prefers-reduced-motion: reduce)": {
      "[data-slot=switch-thumb]": {
        "animation": "none"
      }
    }
  },
  "docs": "Docs: https://www.deltacomponents.dev/docs/switch.",
  "categories": [
    "components"
  ],
  "type": "registry:ui"
}
