{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rubber-range","type":"registry:ui","title":"Rubber Range","description":"Range slider whose track stretches like rubber when dragged past either end, then springs back. Keyboard accessible.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/rubber-range.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { animate, motion, useMotionValue, useTransform } from \"motion/react\";\nimport { Volume, Volume2 } from \"lucide-react\";\nimport { useRef, useState, type KeyboardEvent, type PointerEvent, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface RubberRangeProps {\n  defaultValue?: number;\n  min?: number;\n  max?: number;\n  step?: number;\n  /** Max px the track stretches when dragged past an end. */\n  stretch?: number;\n  /** Show the current value under the track. */\n  showValue?: boolean;\n  /** Icon at the low end. */\n  leftIcon?: ReactNode;\n  /** Icon at the high end. */\n  rightIcon?: ReactNode;\n  /** Accessible name. */\n  label?: string;\n  onValueChange?: (value: number) => void;\n  className?: string;\n}\n\nconst release = { type: \"spring\", stiffness: 520, damping: 14, mass: 0.8 } as const;\n\nexport function RubberRange({\n  defaultValue = 50,\n  min = 0,\n  max = 100,\n  step = 1,\n  stretch = 28,\n  showValue = true,\n  leftIcon = <Volume />,\n  rightIcon = <Volume2 />,\n  label = \"Volume\",\n  onValueChange,\n  className,\n}: RubberRangeProps) {\n  const [value, setValue] = useState(defaultValue);\n  const [active, setActive] = useState(false);\n  const [hover, setHover] = useState(false);\n  const trackRef = useRef<HTMLDivElement>(null);\n  const dragging = useRef(false);\n\n  // Signed overflow in px: < 0 stretches the left end, > 0 the right end.\n  const over = useMotionValue(0);\n  const left = useTransform(over, (o) => Math.min(o, 0));\n  const right = useTransform(over, (o) => -Math.max(o, 0));\n  const thin = useTransform(over, (o) => 1 - Math.min(Math.abs(o) / (stretch * 5 || 1), 0.35));\n  const rightX = useTransform(over, (o) => Math.max(o, 0));\n  const leftScale = useTransform(over, (o) => 1 + Math.max(-o, 0) / (stretch * 3 || 1));\n  const rightScale = useTransform(over, (o) => 1 + Math.max(o, 0) / (stretch * 3 || 1));\n\n  const clamp = (v: number) => Math.min(max, Math.max(min, v));\n  const commit = (v: number) => {\n    const next = clamp(min + Math.round((v - min) / step) * step);\n    const fixed = Number(next.toFixed(6));\n    if (fixed !== value) {\n      setValue(fixed);\n      onValueChange?.(fixed);\n    }\n  };\n\n  const track = (clientX: number) => {\n    const r = trackRef.current!.getBoundingClientRect();\n    const x = clientX - r.left;\n    const o = x < 0 ? x : x > r.width ? x - r.width : 0;\n    // Exponential falloff: easy to start stretching, impossible to exceed `stretch`.\n    over.set(Math.sign(o) * stretch * (1 - Math.exp(-Math.abs(o) / (stretch * 2.5 || 1))));\n    commit(min + (Math.min(Math.max(x, 0), r.width) / r.width) * (max - min));\n  };\n\n  const onDown = (e: PointerEvent<HTMLDivElement>) => {\n    e.currentTarget.setPointerCapture(e.pointerId);\n    e.currentTarget.focus();\n    dragging.current = true;\n    setActive(true);\n    over.stop();\n    track(e.clientX);\n  };\n  const onMove = (e: PointerEvent<HTMLDivElement>) => dragging.current && track(e.clientX);\n  const onUp = () => {\n    if (!dragging.current) return;\n    dragging.current = false;\n    setActive(false);\n    animate(over, 0, release);\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLDivElement>) => {\n    const big = Math.max(step, (max - min) / 10);\n    const delta: Record<string, number> = {\n      ArrowRight: step, ArrowUp: step, ArrowLeft: -step, ArrowDown: -step,\n      PageUp: big, PageDown: -big, Home: min - max, End: max - min,\n    };\n    const d = delta[e.key];\n    if (d === undefined) return;\n    e.preventDefault();\n    // Pushing past an end gives a little elastic bump instead of nothing.\n    if ((d > 0 && value >= max) || (d < 0 && value <= min)) {\n      animate(over, Math.sign(d) * stretch * 0.6, { duration: 0.09, ease: \"easeOut\" }).then(() => animate(over, 0, release));\n    }\n    commit(value + d);\n  };\n\n  const pct = max > min ? ((value - min) / (max - min)) * 100 : 0;\n  const lifted = active || hover;\n\n  return (\n    <div className={cn(\"flex w-full max-w-sm flex-col items-center gap-3 select-none\", className)}>\n      <div className=\"flex w-full items-center gap-4\">\n        <motion.span\n          aria-hidden\n          className={cn(\"grid size-5 shrink-0 place-items-center transition-colors [&>svg]:size-full\", lifted ? \"text-foreground\" : \"text-muted-foreground\")}\n          style={{ x: left, scale: leftScale }}\n        >\n          {leftIcon}\n        </motion.span>\n        <div\n          ref={trackRef}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label={label}\n          aria-valuemin={min}\n          aria-valuemax={max}\n          aria-valuenow={value}\n          onPointerDown={onDown}\n          onPointerMove={onMove}\n          onPointerUp={onUp}\n          onPointerCancel={onUp}\n          onPointerEnter={() => setHover(true)}\n          onPointerLeave={() => setHover(false)}\n          onKeyDown={onKey}\n          className=\"group relative h-8 flex-1 cursor-grab touch-none rounded-full outline-none active:cursor-grabbing\"\n        >\n          <motion.div\n            className=\"absolute top-1/2 -translate-y-1/2 overflow-hidden rounded-full bg-muted ring-offset-2 ring-offset-background group-focus-visible:ring-2 group-focus-visible:ring-ring\"\n            style={{ left, right, scaleY: thin }}\n            animate={{ height: lifted ? 12 : 6 }}\n            transition={{ type: \"spring\", stiffness: 400, damping: 28 }}\n          >\n            <div className=\"h-full bg-foreground\" style={{ width: `${pct}%` }} />\n          </motion.div>\n        </div>\n        <motion.span\n          aria-hidden\n          className={cn(\"grid size-5 shrink-0 place-items-center transition-colors [&>svg]:size-full\", lifted ? \"text-foreground\" : \"text-muted-foreground\")}\n          style={{ x: rightX, scale: rightScale }}\n        >\n          {rightIcon}\n        </motion.span>\n      </div>\n      {showValue && (\n        <span aria-hidden className=\"font-mono text-sm tabular-nums text-muted-foreground\">\n          {Math.round(value * 100) / 100}\n        </span>\n      )}\n    </div>\n  );\n}\n"}]}