{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "elastic-slider",
  "type": "registry:component",
  "title": "Elastic Slider",
  "description": "A slider (role slider, full keyboard support) that stretches like rubber when dragged past its limits and snaps back with a bounce; the track squashes to preserve volume.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Navigation"
  ],
  "docs": "Installed in components/furin/elastic-slider/. Docs, live controls and props table: https://furin.dev/components/elastic-slider/",
  "files": [
    {
      "path": "registry/elastic-slider/ElasticSlider.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, KeyboardEvent as ReactKeyboardEvent } from \"react\";\nimport \"./ElasticSlider.css\";\n\nexport interface ElasticSliderProps {\n  /** Valor mínimo. */\n  min?: number;\n  /** Valor máximo. */\n  max?: number;\n  /** Paso entre valores. */\n  step?: number;\n  /** Valor actual (modo controlado). */\n  value?: number;\n  /** Valor inicial (modo libre). */\n  defaultValue?: number;\n  /** Se llama al cambiar el valor. */\n  onChange?: (value: number) => void;\n  /** Texto del control (también su etiqueta accesible). */\n  label?: string;\n  /** Muestra el valor numérico. */\n  showValue?: boolean;\n  /** Sufijo del valor, por ejemplo \"%\". */\n  unit?: string;\n  /** Cuánto se estira al pasar los límites, en px. */\n  stretch?: number;\n  /** Íconos de los extremos. */\n  icon?: \"brightness\" | \"volume\" | \"none\";\n  /** Color del relleno. */\n  color?: string;\n  /** Rebote al regresar: 0.2 rebota mucho, 0.6 casi no. */\n  damping?: number;\n  /** Se arrastra solo, pasándose de los límites, hasta que lo tocas. */\n  autoplay?: boolean;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nconst clamp = (v: number, a: number, b: number) => Math.min(Math.max(v, a), b);\n\n/** Íconos geométricos de los extremos: el de la izquierda es el mínimo y el de la derecha, el máximo. */\nfunction Icono({ tipo, alto }: { tipo: \"brightness\" | \"volume\"; alto: boolean }) {\n  const p = { fill: \"none\", stroke: \"currentColor\", strokeWidth: 1.6, strokeLinecap: \"round\", strokeLinejoin: \"round\" } as const;\n  if (tipo === \"brightness\") {\n    return (\n      <svg viewBox=\"0 0 20 20\" aria-hidden=\"true\">\n        <circle {...p} cx=\"10\" cy=\"10\" r={alto ? 3.4 : 2.6} />\n        <path {...p} d={alto ? \"M10 2.4v2M10 15.6v2M2.4 10h2M15.6 10h2M4.6 4.6l1.4 1.4M14 14l1.4 1.4M4.6 15.4 6 14M14 6l1.4-1.4\" : \"M10 4.2v1M10 14.8v1M4.2 10h1M14.8 10h1\"} />\n      </svg>\n    );\n  }\n  return (\n    <svg viewBox=\"0 0 20 20\" aria-hidden=\"true\">\n      <path {...p} d=\"M3.6 8v4h2.8L10 15V5L6.4 8z\" />\n      {alto ? <path {...p} d=\"M12.6 7.2a4 4 0 0 1 0 5.6M14.6 5.2a6.8 6.8 0 0 1 0 9.6\" /> : <path {...p} d=\"M12.8 8.2a2.6 2.6 0 0 1 0 3.6\" />}\n    </svg>\n  );\n}\n\n/** Deslizador (role slider) que se estira como goma al arrastrarlo más allá de sus límites y regresa con rebote. */\nexport function ElasticSlider({\n  min = 0,\n  max = 100,\n  step = 1,\n  value,\n  defaultValue = 62,\n  onChange,\n  label = \"Brightness\",\n  showValue = true,\n  unit = \"%\",\n  stretch = 56,\n  icon = \"brightness\",\n  color = \"#F3F4F7\",\n  damping = 0.28,\n  autoplay = false,\n  className = \"\",\n  style,\n}: ElasticSliderProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const zona = useRef<HTMLDivElement>(null);\n  const valRef = useRef(clamp(value ?? defaultValue, min, max));\n  const live = useRef({ min, max, step, stretch, damping, onChange, autoplay, controlado: value !== undefined, unit });\n  live.current = { min, max, step, stretch, damping, onChange, autoplay, controlado: value !== undefined, unit };\n  const api = useRef<{ set: (v: number, emit: boolean) => void; empuja: (dir: number) => void }>({ set: () => {}, empuja: () => {} });\n\n  // Modo controlado: la prop manda cuando no se está arrastrando.\n  useEffect(() => { if (value !== undefined) api.current.set(clamp(value, min, max), false); }, [value, min, max]);\n\n  useEffect(() => {\n    const el = root.current, z = zona.current;\n    if (!el || !z) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const txt = el.querySelector<HTMLElement>(\".furin-elastic-slider__valor\");\n    let W = 1, raf = 0, last = 0, visible = true;\n    // Estiramiento (px, con signo) como resorte; presión como resorte de escala.\n    let s = 0, sv = 0, sT = 0, pr = 0, prv = 0, prT = 0, arrastra = false, lastReal = -1e9, t = 0, ratioV = 0;\n    const redondea = (v: number) => {\n      const L = live.current;\n      const n = Math.round((v - L.min) / L.step) * L.step + L.min;\n      return clamp(Number(n.toFixed(6)), L.min, L.max);\n    };\n    const ratio = () => (valRef.current - live.current.min) / Math.max(live.current.max - live.current.min, 1e-9);\n\n    const pinta = () => {\n      const L = live.current;\n      const r = arrastra || autoOn ? ratio() : ratio();\n      void ratioV;\n      const sx = 1 + Math.abs(s) / W;\n      const sy = 1 - Math.min(0.32, (Math.abs(s) / Math.max(L.stretch, 1)) * 0.3);\n      // Si se estira por la derecha, la pista crece desde la izquierda; por la izquierda, desde la derecha.\n      el.style.setProperty(\"--furin-es-origen\", s >= 0 ? \"left center\" : \"right center\");\n      el.style.setProperty(\"--furin-es-sx\", sx.toFixed(4));\n      el.style.setProperty(\"--furin-es-sy\", sy.toFixed(4));\n      el.style.setProperty(\"--furin-es-fill\", `${(r * 100).toFixed(3)}%`);\n      // Posición del tirador: el punto del valor, dentro de la pista ya estirada.\n      const x = s >= 0 ? r * (W + s) : W - (1 - r) * (W + Math.abs(s));\n      el.style.setProperty(\"--furin-es-x\", `${x.toFixed(2)}px`);\n      el.style.setProperty(\"--furin-es-press\", pr.toFixed(3));\n      el.style.setProperty(\"--furin-es-izq\", `${Math.min(s, 0).toFixed(2)}px`);\n      el.style.setProperty(\"--furin-es-der\", `${Math.max(s, 0).toFixed(2)}px`);\n    };\n    const texto = () => {\n      if (txt) txt.textContent = `${valRef.current}${live.current.unit}`;\n      z.setAttribute(\"aria-valuenow\", String(valRef.current));\n      z.setAttribute(\"aria-valuetext\", `${valRef.current}${live.current.unit}`);\n      el.style.setProperty(\"--furin-es-alto\", String(ratio() > 0.5 ? 1 : 0));\n    };\n\n    const fisica = (dtIn: number) => {\n      const dt = Math.min(0.032, dtIn);\n      const L = live.current;\n      const k = 190, c = 2 * Math.sqrt(k) * L.damping;\n      const h = dt / 3;\n      for (let i = 0; i < 3; i++) {\n        sv += (k * (sT - s) - c * sv) * h;\n        s += sv * h;\n      }\n      prv += ((prT - pr) * 420 - prv * 24) * dt;\n      pr += prv * dt;\n    };\n\n    let autoOn = false;\n    const frame = (nowMs: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, Math.max(0.001, (nowMs - last) / 1000));\n      last = nowMs;\n      t += dt;\n      const L = live.current;\n      const auto = L.autoplay && !arrastra && nowMs - lastReal > 2600 && !reduce;\n      if (auto) {\n        // Arrastre virtual: una oscilación lenta que se pasa de los dos límites.\n        if (!autoOn) { autoOn = true; prT = 1; }\n        const ph = Math.sin(t * 0.85);\n        const px = 0.5 + ph * 0.68;   // fracción de la pista, más allá de [0, 1]\n        valRef.current = redondea(L.min + clamp(px, 0, 1) * (L.max - L.min));\n        const over = (px - clamp(px, 0, 1)) * W;\n        sT = Math.sign(over) * L.stretch * (1 - Math.exp(-Math.abs(over) / (L.stretch * 1.4)));\n        texto();\n      } else if (autoOn) { autoOn = false; prT = 0; sT = 0; }\n      if (!reduce) fisica(dt);\n      else { s = sT; pr = prT; sv = prv = 0; }\n      pinta();\n      const quieto = !arrastra && !auto && Math.abs(s - sT) < 0.05 && Math.abs(sv) < 0.1 && Math.abs(pr - prT) < 0.002 && Math.abs(prv) < 0.02;\n      if ((!quieto || (L.autoplay && !reduce)) && visible) raf = requestAnimationFrame(frame);\n      else if (quieto) { s = sT; pr = prT; pinta(); }\n    };\n    const wake = () => { if (raf || !visible) return; last = performance.now(); raf = requestAnimationFrame(frame); };\n\n    const aValor = (clientX: number) => {\n      const L = live.current;\n      const r = z.getBoundingClientRect();\n      W = r.width;\n      const f = (clientX - r.left) / Math.max(r.width, 1);\n      const v = redondea(L.min + clamp(f, 0, 1) * (L.max - L.min));\n      // Goma: la parte del arrastre que se pasa de la pista estira con resistencia creciente.\n      const over = (f - clamp(f, 0, 1)) * r.width;\n      sT = Math.sign(over) * L.stretch * (1 - Math.exp(-Math.abs(over) / (L.stretch * 1.4)));\n      if (v !== valRef.current) { valRef.current = v; texto(); L.onChange?.(v); }\n    };\n\n    const onDown = (e: PointerEvent) => {\n      if (e.button !== 0 && e.pointerType === \"mouse\") return;\n      arrastra = true; lastReal = performance.now(); prT = 1;\n      z.setPointerCapture(e.pointerId);\n      z.classList.add(\"is-arrastra\");\n      aValor(e.clientX);\n      wake();\n    };\n    const onMove = (e: PointerEvent) => { if (!arrastra) return; lastReal = performance.now(); aValor(e.clientX); wake(); };\n    const onUp = (e: PointerEvent) => {\n      if (!arrastra) return;\n      arrastra = false; lastReal = performance.now(); sT = 0; prT = 0;\n      z.classList.remove(\"is-arrastra\");\n      try { z.releasePointerCapture(e.pointerId); } catch { /* ya soltado */ }\n      wake();\n    };\n    z.addEventListener(\"pointerdown\", onDown);\n    z.addEventListener(\"pointermove\", onMove);\n    z.addEventListener(\"pointerup\", onUp);\n    z.addEventListener(\"pointercancel\", onUp);\n\n    api.current.set = (v, emit) => {\n      const nv = redondea(v);\n      if (nv === valRef.current) return;\n      valRef.current = nv; texto(); pinta();\n      if (emit) live.current.onChange?.(nv);\n    };\n    // Al intentar pasar el límite con el teclado, la goma da un tirón y regresa.\n    api.current.empuja = (dir) => { sv += dir * 240; lastReal = performance.now(); wake(); };\n\n    const io = new IntersectionObserver(([en]) => { visible = en.isIntersecting; if (visible) wake(); else if (raf) { cancelAnimationFrame(raf); raf = 0; } });\n    io.observe(el);\n    const ro = new ResizeObserver(() => { W = z.getBoundingClientRect().width; pinta(); });\n    ro.observe(z);\n    W = z.getBoundingClientRect().width;\n    texto();\n    pinta();\n    wake();\n    return () => {\n      cancelAnimationFrame(raf);\n      z.removeEventListener(\"pointerdown\", onDown);\n      z.removeEventListener(\"pointermove\", onMove);\n      z.removeEventListener(\"pointerup\", onUp);\n      z.removeEventListener(\"pointercancel\", onUp);\n      io.disconnect();\n      ro.disconnect();\n    };\n  }, []);\n\n  const teclas = (e: ReactKeyboardEvent) => {\n    const L = live.current;\n    const big = Math.max(L.step, (L.max - L.min) / 10);\n    const v = valRef.current;\n    let n = v;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") n = v + L.step;\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") n = v - L.step;\n    else if (e.key === \"PageUp\") n = v + big;\n    else if (e.key === \"PageDown\") n = v - big;\n    else if (e.key === \"Home\") n = L.min;\n    else if (e.key === \"End\") n = L.max;\n    else return;\n    e.preventDefault();\n    if (n > L.max) { api.current.empuja(1); return; }\n    if (n < L.min) { api.current.empuja(-1); return; }\n    api.current.set(n, true);\n  };\n\n  const ini = clamp(value ?? defaultValue, min, max);\n  const vars = { \"--furin-es-color\": color, \"--furin-es-fill\": `${((ini - min) / Math.max(max - min, 1e-9)) * 100}%` } as CSSProperties;\n  return (\n    <div ref={root} className={`furin-elastic-slider ${className}`} style={{ ...vars, ...style }}>\n      <div className=\"furin-elastic-slider__cab\">\n        <span className=\"furin-elastic-slider__etq\">{label}</span>\n        {showValue ? <span className=\"furin-elastic-slider__valor\" aria-hidden=\"true\">{ini}{unit}</span> : null}\n      </div>\n      <div className=\"furin-elastic-slider__fila\">\n        {icon !== \"none\" ? <span className=\"furin-elastic-slider__ico furin-elastic-slider__ico--izq\"><Icono tipo={icon} alto={false} /></span> : null}\n        <div\n          ref={zona}\n          className=\"furin-elastic-slider__zona\"\n          role=\"slider\"\n          tabIndex={0}\n          aria-label={label}\n          aria-orientation=\"horizontal\"\n          aria-valuemin={min}\n          aria-valuemax={max}\n          aria-valuenow={ini}\n          aria-valuetext={`${ini}${unit}`}\n          onKeyDown={teclas}\n        >\n          <div className=\"furin-elastic-slider__pista\">\n            <div className=\"furin-elastic-slider__relleno\" />\n          </div>\n          <span className=\"furin-elastic-slider__tirador\" />\n        </div>\n        {icon !== \"none\" ? <span className=\"furin-elastic-slider__ico furin-elastic-slider__ico--der\"><Icono tipo={icon} alto /></span> : null}\n      </div>\n    </div>\n  );\n}\n\nexport default ElasticSlider;\n",
      "type": "registry:component",
      "target": "components/furin/elastic-slider/ElasticSlider.tsx"
    },
    {
      "path": "registry/elastic-slider/ElasticSlider.css",
      "content": ".furin-elastic-slider {\n  --furin-es-color: #f3f4f7;\n  --furin-es-origen: left center;\n  --furin-es-sx: 1;\n  --furin-es-sy: 1;\n  --furin-es-fill: 60%;\n  --furin-es-x: 60%;\n  --furin-es-press: 0;\n  --furin-es-izq: 0px;\n  --furin-es-der: 0px;\n  display: grid;\n  gap: 14px;\n  width: min(400px, 90%);\n  font-family: Geist, system-ui, -apple-system, sans-serif;\n  color: #f2f3f5;\n  user-select: none;\n}\n.furin-elastic-slider__cab { display: flex; align-items: baseline; justify-content: space-between; }\n.furin-elastic-slider__etq { font-size: 14px; font-weight: 500; letter-spacing: -0.01em; color: rgba(255, 255, 255, 0.75); }\n.furin-elastic-slider__valor { font: 500 14px/1 \"JetBrains Mono\", ui-monospace, \"SF Mono\", Menlo, monospace; font-variant-numeric: tabular-nums; color: #fff; }\n\n.furin-elastic-slider__fila { display: flex; align-items: center; gap: 14px; }\n.furin-elastic-slider__ico { display: grid; place-items: center; width: 22px; height: 22px; flex: none; color: rgba(255, 255, 255, 0.55); will-change: transform; }\n.furin-elastic-slider__ico svg { width: 100%; height: 100%; display: block; }\n/* Los íconos de los extremos acompañan el estiramiento y se ven más vivos al tirar. */\n.furin-elastic-slider__ico--izq { transform: translateX(var(--furin-es-izq)); }\n.furin-elastic-slider__ico--der { transform: translateX(var(--furin-es-der)); }\n\n.furin-elastic-slider__zona { position: relative; flex: 1; height: 44px; display: grid; align-items: center; cursor: grab; touch-action: pan-y; outline: none; border-radius: 22px; }\n.furin-elastic-slider__zona.is-arrastra { cursor: grabbing; }\n.furin-elastic-slider__zona:focus-visible { box-shadow: 0 0 0 2px rgba(142, 162, 255, 0.7); }\n\n/* Pista: se estira en X desde el lado contrario al tirón y se aplasta un poco en Y para conservar el volumen. */\n.furin-elastic-slider__pista {\n  position: relative;\n  height: calc(12px + var(--furin-es-press) * 6px);\n  border-radius: 999px;\n  overflow: hidden;\n  background: rgba(255, 255, 255, 0.11);\n  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.05);\n  transform: scale(var(--furin-es-sx), var(--furin-es-sy));\n  transform-origin: var(--furin-es-origen);\n  will-change: transform;\n}\n.furin-elastic-slider__relleno {\n  position: absolute;\n  inset: 0 auto 0 0;\n  width: var(--furin-es-fill);\n  border-radius: inherit;\n  background: linear-gradient(180deg, color-mix(in srgb, var(--furin-es-color) 100%, #fff 20%), var(--furin-es-color) 60%, color-mix(in srgb, var(--furin-es-color) 82%, #000));\n  box-shadow: 0 0 20px -2px color-mix(in srgb, var(--furin-es-color) 45%, transparent);\n}\n/* Tirador: una cápsula en el extremo del relleno; fuera de la pista para no deformarse al estirar. */\n.furin-elastic-slider__tirador {\n  position: absolute;\n  left: 0; top: 50%;\n  width: 4px;\n  height: calc(22px + var(--furin-es-press) * 4px);\n  margin: calc(-11px - var(--furin-es-press) * 2px) 0 0 -2px;\n  border-radius: 4px;\n  background: #fff;\n  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(0, 0, 0, 0.12);\n  transform: translateX(var(--furin-es-x)) translateX(-5px);\n  pointer-events: none;\n}\n.furin-elastic-slider__zona:hover .furin-elastic-slider__ico { color: #fff; }\n@media (prefers-reduced-motion: reduce) { .furin-elastic-slider__pista, .furin-elastic-slider__tirador { transition: none; } }\n",
      "type": "registry:file",
      "target": "components/furin/elastic-slider/ElasticSlider.css"
    }
  ]
}
