{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"elastic-slider","type":"registry:component","title":"Elastic slider","description":"Stretches like rubber when you drag past the end, then springs back.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"components/elastic-slider.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n  animate,\n  motion,\n  useMotionTemplate,\n  useMotionValue,\n  useSpring,\n  useTransform,\n} from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\n\n// The furthest the track can stretch past either end, in px.\nconst MAX_STRETCH = 24;\n// A little bounce on release sells the rubber; this is the one place in the\n// lab where overshoot is the point.\nconst SNAP_BACK = { type: \"spring\", visualDuration: 0.5, bounce: 0.4 } as const;\n// Stiff enough that dragging still feels attached to the finger, soft enough\n// that a click glides across instead of jumping. No bounce: a fill that\n// overshoots would read as the wrong volume.\nconst FILL = { stiffness: 400, damping: 40 };\nconst ICON_SWAP = { type: \"spring\", duration: 0.3, bounce: 0 } as const;\nconst STEP = 0.05;\n\n// Gives freely at first, then stiffens so it never passes MAX_STRETCH.\nfunction resist(overflow: number) {\n  return MAX_STRETCH * Math.tanh(overflow / (MAX_STRETCH * 4));\n}\n\nexport function ElasticSlider({\n  value,\n  onChange,\n  muted,\n  onMutedChange,\n  label,\n}: {\n  value: number;\n  onChange: (value: number) => void;\n  muted: boolean;\n  onMutedChange: (muted: boolean) => void;\n  label: string;\n}) {\n  const reduceMotion = useReducedMotion();\n  const trackRef = useRef<HTMLDivElement>(null);\n  const dragging = useRef(false);\n  const overflow = useMotionValue(0);\n\n  const shown = muted ? 0 : value;\n  const fill = useSpring(shown, FILL);\n  const fillTransform = useMotionTemplate`scaleX(${fill})`;\n\n  useEffect(() => {\n    if (reduceMotion) fill.jump(shown);\n    else fill.set(shown);\n  }, [shown, reduceMotion, fill]);\n\n  const width = () => trackRef.current?.offsetWidth ?? 1;\n  const stretch = useTransform(overflow, (o) => 1 + Math.abs(o) / width());\n  // Stretching thins the track slightly, like pulling a rubber band.\n  const thin = useTransform(overflow, (o) => 1 - Math.abs(o) / (MAX_STRETCH * 6));\n  const transform = useMotionTemplate`scale(${stretch}, ${thin})`;\n  // Stretch away from the end being pulled, so the far end stays put.\n  const origin = useTransform(overflow, (o) => (o < 0 ? \"right\" : \"left\"));\n  const lowIconX = useTransform(overflow, (o) => Math.min(o, 0));\n  const highIconX = useTransform(overflow, (o) => Math.max(o, 0));\n\n  const release = () => animate(overflow, 0, SNAP_BACK);\n\n  // Reaching for the slider means you want sound, so any change unmutes.\n  // Unmute first, so the new value lands after anything unmuting restores.\n  const set = (next: number) => {\n    if (muted) onMutedChange(false);\n    onChange(next);\n  };\n\n  const track = (clientX: number) => {\n    const box = trackRef.current?.getBoundingClientRect();\n    if (!box) return;\n    const raw = (clientX - box.left) / box.width;\n    set(Math.min(Math.max(raw, 0), 1));\n    if (reduceMotion) return;\n    const past =\n      clientX < box.left\n        ? clientX - box.left\n        : clientX > box.right\n          ? clientX - box.right\n          : 0;\n    overflow.set(resist(past));\n  };\n\n  // Keys can't pull, so pressing past an end gives a small nudge instead.\n  const bump = (direction: 1 | -1) => {\n    if (reduceMotion) return;\n    overflow.set(direction * 6);\n    release();\n  };\n\n  const silent = shown === 0;\n\n  return (\n    <div className=\"flex w-80 items-center gap-2 text-muted-foreground\">\n      <motion.button\n        type=\"button\"\n        aria-label={muted ? \"Unmute\" : \"Mute\"}\n        aria-pressed={muted}\n        onClick={() => onMutedChange(!muted)}\n        style={{ x: lowIconX }}\n        className=\"relative flex size-8 shrink-0 items-center justify-center rounded-full outline-hidden transition-[scale,color] duration-150 ease-out hover:text-foreground focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96]\"\n      >\n        <SwapIcon visible={!silent}>\n          <path d=\"M3 9.5v5h3.5L11 18V6L6.5 9.5Z\" />\n        </SwapIcon>\n        <SwapIcon visible={silent}>\n          <path d=\"M3 9.5v5h3.5L11 18V6L6.5 9.5Z\" />\n          <path d=\"m15 10 4 4M19 10l-4 4\" />\n        </SwapIcon>\n      </motion.button>\n      <div\n        ref={trackRef}\n        role=\"slider\"\n        tabIndex={0}\n        aria-label={label}\n        aria-valuemin={0}\n        aria-valuemax={100}\n        aria-valuenow={Math.round(shown * 100)}\n        className=\"group flex h-8 flex-1 cursor-pointer touch-none items-center rounded-full outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-4 focus-visible:outline-foreground\"\n        onPointerDown={(e) => {\n          if (e.button !== 0) return;\n          dragging.current = true;\n          e.currentTarget.setPointerCapture(e.pointerId);\n          track(e.clientX);\n        }}\n        onPointerMove={(e) => {\n          if (dragging.current) track(e.clientX);\n        }}\n        onPointerUp={() => {\n          dragging.current = false;\n          release();\n        }}\n        onPointerCancel={() => {\n          dragging.current = false;\n          release();\n        }}\n        onKeyDown={(e) => {\n          const delta = {\n            ArrowRight: STEP,\n            ArrowUp: STEP,\n            ArrowLeft: -STEP,\n            ArrowDown: -STEP,\n            PageUp: STEP * 2,\n            PageDown: -STEP * 2,\n            Home: -1,\n            End: 1,\n          }[e.key];\n          if (delta === undefined) return;\n          e.preventDefault();\n          const next = Math.min(Math.max(shown + delta, 0), 1);\n          if (next === shown) bump(delta > 0 ? 1 : -1);\n          set(next);\n        }}\n      >\n        <motion.div\n          className=\"h-1.5 w-full overflow-hidden rounded-full bg-border transition-[height] duration-150 ease-out group-hover:h-2.5 group-active:h-2.5\"\n          style={{ transform, transformOrigin: origin }}\n        >\n          <motion.div\n            className=\"h-full origin-left bg-foreground\"\n            style={{ transform: fillTransform }}\n          />\n        </motion.div>\n      </div>\n      <motion.svg\n        style={{ x: highIconX }}\n        className=\"mx-1.5 size-5 shrink-0\"\n        {...STROKE}\n        aria-hidden\n      >\n        <path d=\"M3 9.5v5h3.5L11 18V6L6.5 9.5Z\" />\n        <path d=\"M15 9a4 4 0 0 1 0 6M17.5 6.5a7.5 7.5 0 0 1 0 11\" />\n      </motion.svg>\n    </div>\n  );\n}\n\nfunction SwapIcon({\n  visible,\n  children,\n}: {\n  visible: boolean;\n  children: React.ReactNode;\n}) {\n  return (\n    <motion.svg\n      className=\"absolute size-5\"\n      {...STROKE}\n      aria-hidden\n      initial={false}\n      animate={\n        visible\n          ? { scale: 1, opacity: 1, filter: \"blur(0px)\" }\n          : { scale: 0.25, opacity: 0, filter: \"blur(4px)\" }\n      }\n      transition={ICON_SWAP}\n    >\n      {children}\n    </motion.svg>\n  );\n}\n\nconst STROKE = {\n  viewBox: \"0 0 24 24\",\n  fill: \"none\",\n  stroke: \"currentColor\",\n  strokeWidth: 1.5,\n  strokeLinecap: \"round\",\n  strokeLinejoin: \"round\",\n} as const;\n\nexport default function ElasticSliderDemo() {\n  const [volume, setVolume] = useState(0.6);\n  const [muted, setMuted] = useState(false);\n\n  return (\n    <ElasticSlider\n      label=\"Volume\"\n      value={volume}\n      onChange={setVolume}\n      muted={muted}\n      onMutedChange={(next) => {\n        // Unmuting at zero would stay silent, so bring back a usable level.\n        if (!next && volume === 0) setVolume(0.5);\n        setMuted(next);\n      }}\n    />\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"categories":["inputs"],"docs":"From ui lab: https://lab.xevrion.dev/lab/elastic-slider"}