{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"slingshot","type":"registry:ui","title":"Slingshot","description":"Pull the button back on rubber bands and let go: past the threshold it fires and whips home with overshoot. A normal click works too.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/slingshot.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useRef, useState, type ButtonHTMLAttributes, type MouseEvent, type PointerEvent } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\";\n\ntype NativeProps = Omit<\n  ButtonHTMLAttributes<HTMLButtonElement>,\n  \"onDrag\" | \"onDragStart\" | \"onDragEnd\" | \"onAnimationStart\"\n>;\n\nexport interface SlingshotProps extends NativeProps {\n  /** Px the button must be pulled back to fire on release. */\n  threshold?: number;\n  /** How far the button follows the pointer while pulled, 0-1. */\n  elasticity?: number;\n}\n\nconst css = `@keyframes sling-button-ping{from{transform:scale(1);opacity:.8}to{transform:scale(1.6);opacity:0}}`;\nconst origin = { top: 0, left: 0, right: 0, bottom: 0 };\n\nexport function Slingshot({\n  threshold = 50,\n  elasticity = 0.5,\n  className,\n  style,\n  children,\n  disabled,\n  onClick,\n  onPointerDown,\n  ...props\n}: SlingshotProps) {\n  const ref = useRef<HTMLButtonElement>(null);\n  const down = useRef<{ x: number; y: number } | null>(null);\n  const reduce = useReducedMotion();\n  const [shots, setShots] = useState(0);\n\n  const x = useMotionValue(0);\n  const y = useMotionValue(0);\n  const dist = useTransform(() => Math.hypot(x.get(), y.get()));\n  const bandOpacity = useTransform(dist, [0, 10], [0, 1]);\n  const bandWidth = useTransform(dist, [0, threshold * 2], [3, 1.25]);\n  const bandColor = useTransform(dist, (d) =>\n    d >= threshold ? \"var(--primary)\" : \"var(--muted-foreground)\",\n  );\n  const ring = useTransform(dist, (d) =>\n    d >= threshold\n      ? \"0 0 0 3px color-mix(in oklab, var(--primary) 45%, transparent), 0 14px 30px -12px var(--primary)\"\n      : \"0 0 0 0px transparent, 0 10px 24px -14px rgb(0 0 0 / .6)\",\n  );\n\n  const release = () => {\n    const fire = dist.get() >= threshold;\n    if (reduce) {\n      x.set(0);\n      y.set(0);\n    } else {\n      // Low damping when fired so it whips past the anchor and wobbles home.\n      const spring = { type: \"spring\", stiffness: 520, damping: fire ? 9 : 24 } as const;\n      animate(x, 0, spring);\n      animate(y, 0, spring);\n    }\n    if (fire) {\n      setShots((s) => s + 1);\n      ref.current?.click(); // detail === 0, so handleClick lets it through\n    }\n  };\n\n  const handleClick = (e: MouseEvent<HTMLButtonElement>) => {\n    const d = down.current;\n    // A real click landing after a drag is just the tail of the pull: ignore it.\n    if (e.detail > 0 && d && Math.hypot(e.clientX - d.x, e.clientY - d.y) > 5) return;\n    onClick?.(e);\n  };\n\n  return (\n    <span className=\"relative inline-grid place-items-center\">\n      <style href=\"slingshot\" precedence=\"default\">\n        {css}\n      </style>\n      {/* Rubber bands: anchored at the resting left/right edges, stretched to where the button is now. */}\n      {[\"left-0\", \"right-0\"].map((side) => (\n        <svg key={side} aria-hidden className={`pointer-events-none absolute top-1/2 ${side} size-px overflow-visible`}>\n          <motion.line\n            x1={0}\n            y1={0}\n            x2={x}\n            y2={y}\n            strokeLinecap=\"round\"\n            style={{ opacity: bandOpacity, strokeWidth: bandWidth, stroke: bandColor }}\n          />\n          <motion.circle r={3.5} style={{ opacity: bandOpacity, fill: bandColor }} />\n        </svg>\n      ))}\n      <motion.button\n        ref={ref}\n        type=\"button\"\n        disabled={disabled}\n        {...props}\n        drag={!disabled}\n        dragConstraints={origin}\n        dragElastic={elasticity}\n        dragMomentum={false}\n        onDragEnd={release}\n        onPointerDown={(e: PointerEvent<HTMLButtonElement>) => {\n          down.current = { x: e.clientX, y: e.clientY };\n          onPointerDown?.(e);\n        }}\n        onClick={handleClick}\n        style={{ ...style, x, y, boxShadow: ring }}\n        className={`relative inline-flex h-12 cursor-grab touch-none select-none items-center justify-center gap-2 rounded-full bg-primary px-8 text-sm font-semibold text-primary-foreground transition-[scale] duration-150 focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-ring enabled:active:scale-[0.96] enabled:active:cursor-grabbing disabled:cursor-not-allowed disabled:opacity-50 ${className ?? \"\"}`}\n      >\n        <span aria-hidden className=\"absolute inset-0 rounded-full shadow-[inset_0_1px_0_rgb(255_255_255/.45)]\" />\n        {shots > 0 && (\n          <span\n            key={shots}\n            aria-hidden\n            className=\"absolute inset-0 rounded-full border-2 border-primary [animation:sling-button-ping_.6s_ease-out_forwards] motion-reduce:hidden\"\n          />\n        )}\n        <span className=\"relative\">{children}</span>\n      </motion.button>\n    </span>\n  );\n}\n"}]}