{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"fling-confirm","type":"registry:ui","title":"Fling Confirm","description":"Slide-to-confirm track: the knob stretches as you fling it, commits past the threshold with a popping check, and springs home with a wobble if you let go early.","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/ui/fling-confirm.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform, useVelocity } from \"motion/react\";\nimport { Check, ChevronsRight } from \"lucide-react\";\n\nexport interface FlingConfirmProps {\n  label?: string;\n  confirmedLabel?: string;\n  /** Fill color once committed. */\n  color?: string;\n  /** Fraction of the track (0-1) the knob must pass to commit on release. */\n  threshold?: number;\n  /** Ms before it resets itself after committing. 0 stays committed. */\n  resetAfter?: number;\n  /** Called once the knob reaches the end. */\n  onCommit?: () => void;\n  className?: string;\n}\n\nconst css = `@keyframes slide-commit-shine{from{background-position:150% 0}to{background-position:-50% 0}}`;\nconst KNOB = 48;\n\nexport function FlingConfirm({\n  label = \"Slide to confirm\",\n  confirmedLabel = \"Confirmed\",\n  color = \"#22c55e\",\n  threshold = 0.9,\n  resetAfter = 2500,\n  onCommit,\n  className,\n}: FlingConfirmProps) {\n  const reduce = useReducedMotion();\n  const rail = useRef<HTMLDivElement>(null);\n  const [max, setMax] = useState(0);\n  const [done, setDone] = useState(false);\n  const [pct, setPct] = useState(0);\n  const timer = useRef<number | undefined>(undefined);\n\n  const x = useMotionValue(0);\n  const v = useVelocity(x);\n  const span = useMotionValue(0);\n  const progress = useTransform(() => (span.get() ? x.get() / span.get() : 0));\n  const labelOpacity = useTransform(progress, [0, 0.6], [1, 0]);\n  const fillWidth = useTransform(x, (n) => n + KNOB);\n  const fillOpacity = useTransform(progress, [0, 1], [0.25, 1]);\n  const scaleX = useTransform(v, (s) => 1 + Math.min(Math.abs(s) / 2600, 0.32));\n  const scaleY = useTransform(v, (s) => 1 - Math.min(Math.abs(s) / 5200, 0.16));\n\n  useLayoutEffect(() => {\n    const el = rail.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => {\n      setMax(el.clientWidth - KNOB);\n      span.set(el.clientWidth - KNOB);\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [span]);\n  useEffect(() => () => window.clearTimeout(timer.current), []);\n\n  const to = (target: number, bouncy = false) =>\n    reduce ? x.set(target) : animate(x, target, { type: \"spring\", stiffness: 520, damping: bouncy ? 13 : 30 });\n\n  const commit = () => {\n    if (done) return;\n    to(max);\n    setDone(true);\n    setPct(100);\n    onCommit?.();\n    if (resetAfter > 0)\n      timer.current = window.setTimeout(() => {\n        setDone(false);\n        setPct(0);\n        to(0, true);\n      }, resetAfter);\n  };\n\n  const release = () => {\n    if (x.get() >= max * threshold) commit();\n    else {\n      to(0, true); // springs home with a little overshoot\n      setPct(0);\n    }\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    if (done) return;\n    const now = max ? x.get() / max : 0;\n    let next: number | null = null;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") next = Math.min(1, now + 0.25);\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") next = Math.max(0, now - 0.25);\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\" || e.key === \"Enter\") next = 1;\n    if (next === null) return;\n    e.preventDefault();\n    if (next >= 1) return commit();\n    to(next * max);\n    setPct(Math.round(next * 100));\n  };\n\n  return (\n    <div\n      className={`relative h-14 w-72 select-none overflow-hidden rounded-full border border-border bg-muted p-1 shadow-[inset_0_2px_6px_rgb(0_0_0/.25)] ${className ?? \"\"}`}\n    >\n      <style href=\"fling-confirm\" precedence=\"default\">\n        {css}\n      </style>\n      <motion.span\n        aria-hidden\n        className=\"absolute inset-y-1 left-1 rounded-full\"\n        style={{ width: fillWidth, opacity: fillOpacity, background: color }}\n      />\n      <motion.span\n        aria-hidden\n        style={{ opacity: done ? 0 : labelOpacity }}\n        className=\"pointer-events-none absolute inset-0 grid place-items-center pl-10 text-sm font-medium\"\n      >\n        <span\n          className=\"bg-clip-text text-transparent [animation:slide-commit-shine_2.4s_linear_infinite] motion-reduce:[animation:none]\"\n          style={{\n            backgroundImage:\n              \"linear-gradient(100deg, var(--muted-foreground) 40%, var(--foreground) 50%, var(--muted-foreground) 60%)\",\n            backgroundSize: \"200% 100%\",\n          }}\n        >\n          {label}\n        </span>\n      </motion.span>\n      <motion.span\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 grid place-items-center pr-10 text-sm font-semibold text-white\"\n        initial={false}\n        animate={{ opacity: done ? 1 : 0, scale: done ? 1 : 0.8 }}\n        transition={{ type: \"spring\", stiffness: 500, damping: 20 }}\n      >\n        {confirmedLabel}\n      </motion.span>\n\n      <div ref={rail} className=\"relative h-full\">\n        <motion.div\n          role=\"slider\"\n          tabIndex={0}\n          aria-label={label}\n          aria-valuemin={0}\n          aria-valuemax={100}\n          aria-valuenow={pct}\n          aria-valuetext={done ? confirmedLabel : `${pct}%`}\n          drag={done ? false : \"x\"}\n          dragConstraints={{ left: 0, right: max }}\n          dragElastic={0.08}\n          dragMomentum={false}\n          onDragEnd={release}\n          onKeyDown={onKey}\n          onBlur={() => {\n            if (done || x.get() === 0) return;\n            to(0, true);\n            setPct(0);\n          }}\n          whileTap={done ? undefined : { scale: 0.92 }}\n          style={{ x, scaleX, scaleY }}\n          className=\"relative z-10 grid size-12 cursor-grab touch-none place-items-center rounded-full bg-foreground text-background shadow-[0_2px_4px_rgb(0_0_0/.25),0_8px_20px_-6px_rgb(0_0_0/.45)] outline-none ring-offset-2 ring-offset-muted focus-visible:ring-2 focus-visible:ring-ring active:cursor-grabbing\"\n        >\n          <motion.span\n            key={done ? \"done\" : \"idle\"}\n            initial={{ scale: 0.3, rotate: done ? -60 : 0 }}\n            animate={{ scale: 1, rotate: 0 }}\n            transition={{ type: \"spring\", stiffness: 600, damping: 16 }}\n            style={{ color: done ? color : undefined }}\n          >\n            {done ? <Check className=\"size-5\" strokeWidth={3} aria-hidden /> : <ChevronsRight className=\"size-5\" aria-hidden />}\n          </motion.span>\n        </motion.div>\n      </div>\n    </div>\n  );\n}\n"}]}