{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "micro-rubber-segment",
  "type": "registry:component",
  "title": "Rubber Segment",
  "description": "A segmented control whose selection pill stretches like rubber toward the new option, leading with one edge and letting the other catch up. Dragging across segments works too.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "segmented",
    "tabs",
    "radio",
    "elastic",
    "pill",
    "form"
  ],
  "docs": "https://hyperblur.dev/effects/micro-rubber-segment",
  "files": [
    {
      "path": "registry/hyperblur/micro-rubber-segment.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/micro-rubber-segment.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MicroRubberSegmentOption {\n  value: string;\n  label: React.ReactNode;\n  disabled?: boolean;\n}\n\nexport interface MicroRubberSegmentProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  options?: MicroRubberSegmentOption[];\n  value?: string;\n  defaultValue?: string;\n  accent?: string;\n  idle?: boolean;\n  onChange?: (value: string) => void;\n}\n\nconst defaultOptions: MicroRubberSegmentOption[] = [\n  { value: \"day\", label: \"Day\" },\n  { value: \"week\", label: \"Week\" },\n  { value: \"month\", label: \"Month\" },\n];\n\nconst css = `\n.hb-ql7 {\n  position: relative;\n  display: flex;\n  width: 100%;\n  padding: 4px;\n  box-sizing: border-box;\n  font-family: inherit;\n  background: #111111;\n  border: 1px solid #262626;\n  border-radius: 16px;\n  touch-action: pan-y;\n  user-select: none;\n  -webkit-user-select: none;\n  --hb-ql7-c: #2f7bff;\n}\n.hb-ql7 *, .hb-ql7 *::before, .hb-ql7 *::after { box-sizing: border-box; }\n.hb-ql7-p {\n  position: absolute;\n  left: 0;\n  top: 4px;\n  bottom: 4px;\n  width: 0;\n  background: linear-gradient(180deg, color-mix(in srgb, var(--hb-ql7-c) 82%, #fff), var(--hb-ql7-c));\n  border-radius: 12px;\n  box-shadow: 0 8px 20px -8px var(--hb-ql7-c), inset 0 1px 0 rgba(255, 255, 255, 0.3);\n  will-change: transform, width;\n}\n.hb-ql7-b {\n  position: relative;\n  z-index: 1;\n  flex: 1 1 0;\n  min-width: 0;\n  margin: 0;\n  padding: 0.7em 0.5em;\n  overflow: hidden;\n  font: inherit;\n  font-size: 0.92em;\n  font-weight: 600;\n  color: #8f8f8f;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n  background: none;\n  border: 0;\n  border-radius: 12px;\n  cursor: pointer;\n  transition: color 0.25s ease;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-ql7-b:hover { color: #ededed; }\n.hb-ql7-b[data-on=\"true\"] { color: #fff; }\n.hb-ql7-b:disabled { opacity: 0.4; cursor: not-allowed; }\n.hb-ql7-b:focus-visible { outline: 2px solid #fff; outline-offset: -3px; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-ql7-b { transition: none; }\n}\n`;\n\nexport function MicroRubberSegment({\n  options = defaultOptions,\n  value,\n  defaultValue,\n  accent = \"#2f7bff\",\n  idle = true,\n  onChange,\n  className = \"\",\n  style,\n  onPointerEnter,\n  onPointerLeave,\n  onFocus,\n  onBlur,\n  ...rest\n}: MicroRubberSegmentProps) {\n  const firstEnabled = options.find((o) => !o.disabled)?.value ?? options[0]?.value ?? \"\";\n  const [inner, setInner] = React.useState(defaultValue ?? firstEnabled);\n  const current = value ?? inner;\n  const valueIdx = Math.max(0, options.findIndex((o) => o.value === current));\n  const [ghost, setGhost] = React.useState<number | null>(null);\n  const [engaged, setEngaged] = React.useState(false);\n  const shown = ghost !== null && ghost < options.length ? ghost : valueIdx;\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const pillRef = React.useRef<HTMLSpanElement>(null);\n  const btns = React.useRef<(HTMLButtonElement | null)[]>([]);\n  const state = React.useRef({\n    L: -1,\n    R: 0,\n    vL: 0,\n    vR: 0,\n    dir: 1,\n    drag: false,\n    prev: -1,\n  });\n  const api = React.useRef({ wake: () => {}, reduced: false });\n  const latest = React.useRef({ shown, value, onChange, options });\n\n  React.useEffect(() => {\n    const s = state.current;\n    latest.current = { shown, value, onChange, options };\n    if (s.prev !== shown) {\n      s.dir = s.prev < 0 ? 1 : shown >= s.prev ? 1 : -1;\n      s.prev = shown;\n    }\n    api.current.wake();\n  }, [shown, value, onChange, options]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const pill = pillRef.current;\n    if (!root || !pill) return;\n    const s = state.current;\n    const mq = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    api.current.reduced = mq.matches;\n    let raf = 0;\n    let last = 0;\n    let visible = true;\n\n    const target = () => {\n      const b = btns.current[latest.current.shown];\n      if (!b) return null;\n      return { l: b.offsetLeft, r: b.offsetLeft + b.offsetWidth };\n    };\n\n    const draw = (tw: number) => {\n      const width = Math.max(4, s.R - s.L);\n      const stretch = tw > 0 ? width / tw : 1;\n      const sy = 1 / Math.sqrt(Math.max(0.6, stretch));\n      pill.style.width = `${width.toFixed(2)}px`;\n      pill.style.transform = `translate3d(${s.L.toFixed(2)}px,0,0) scaleY(${Math.min(1.08, sy).toFixed(3)})`;\n    };\n\n    const placeNow = () => {\n      const t = target();\n      if (!t) return;\n      s.L = t.l;\n      s.R = t.r;\n      s.vL = 0;\n      s.vR = 0;\n      draw(t.r - t.l);\n    };\n\n    const tick = (now: number) => {\n      raf = 0;\n      if (!visible) return;\n      const dt = Math.min(0.05, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      const t = target();\n      if (!t) {\n        raf = requestAnimationFrame(tick);\n        return;\n      }\n      if (s.L < 0) {\n        s.L = t.l;\n        s.R = t.r;\n      }\n      const lead = { k: 520, c: 30 };\n      const trail = { k: 170, c: 17 };\n      const gl = s.dir > 0 ? trail : lead;\n      const gr = s.dir > 0 ? lead : trail;\n      let rem = dt;\n      while (rem > 0) {\n        const h = Math.min(rem, 1 / 120);\n        s.vL += (gl.k * (t.l - s.L) - gl.c * s.vL) * h;\n        s.L += s.vL * h;\n        s.vR += (gr.k * (t.r - s.R) - gr.c * s.vR) * h;\n        s.R += s.vR * h;\n        rem -= h;\n      }\n      draw(t.r - t.l);\n      const settled =\n        Math.abs(t.l - s.L) < 0.05 && Math.abs(t.r - s.R) < 0.05 && Math.abs(s.vL) < 0.05 && Math.abs(s.vR) < 0.05;\n      if (settled) {\n        s.L = t.l;\n        s.R = t.r;\n        s.vL = 0;\n        s.vR = 0;\n        draw(t.r - t.l);\n        return;\n      }\n      raf = requestAnimationFrame(tick);\n    };\n\n    const wake = () => {\n      if (api.current.reduced) {\n        placeNow();\n        return;\n      }\n      if (!raf && visible) {\n        last = performance.now();\n        raf = requestAnimationFrame(tick);\n      }\n    };\n\n    api.current.wake = wake;\n    placeNow();\n    wake();\n\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1]?.isIntersecting ?? true;\n      if (visible) wake();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(root);\n    const ro = new ResizeObserver(() => {\n      s.L = -1;\n      placeNow();\n      wake();\n    });\n    ro.observe(root);\n    const onMq = () => {\n      api.current.reduced = mq.matches;\n      if (mq.matches) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n        placeNow();\n      } else wake();\n    };\n    mq.addEventListener(\"change\", onMq);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      ro.disconnect();\n      mq.removeEventListener(\"change\", onMq);\n    };\n  }, []);\n\n  React.useEffect(() => {\n    if (!idle || engaged || options.length < 2) {\n      const reset = window.setTimeout(() => setGhost(null), 0);\n      return () => window.clearTimeout(reset);\n    }\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const n = options.length;\n    const stride = Math.max(1, n - 1);\n    let g = valueIdx;\n    const id = window.setInterval(() => {\n      let next = (g + stride) % n;\n      let guard = 0;\n      while (options[next]?.disabled && guard < n) {\n        next = (next + 1) % n;\n        guard++;\n      }\n      g = next;\n      setGhost(g);\n    }, 1800);\n    return () => window.clearInterval(id);\n  }, [idle, engaged, options, valueIdx]);\n\n  function choose(i: number) {\n    const o = options[i];\n    if (!o || o.disabled) return;\n    if (value === undefined) setInner(o.value);\n    if (o.value !== current) onChange?.(o.value);\n  }\n\n  function indexAt(clientX: number) {\n    let best = -1;\n    let bestDist = Infinity;\n    btns.current.forEach((b, i) => {\n      if (!b || options[i]?.disabled) return;\n      const r = b.getBoundingClientRect();\n      const d = clientX < r.left ? r.left - clientX : clientX > r.right ? clientX - r.right : 0;\n      if (d < bestDist) {\n        bestDist = d;\n        best = i;\n      }\n    });\n    return best;\n  }\n\n  function step(from: number, delta: number) {\n    const n = options.length;\n    for (let k = 1; k <= n; k++) {\n      const i = (from + delta * k + n * k) % n;\n      if (!options[i]?.disabled) return i;\n    }\n    return from;\n  }\n\n  const vars = { \"--hb-ql7-c\": accent, ...style } as React.CSSProperties;\n\n  return (\n    <>\n      <style href=\"hb-micro-rubber-segment\" precedence=\"default\">{css}</style>\n      <div\n        role=\"radiogroup\"\n        {...rest}\n        ref={rootRef}\n        className={`hb-ql7 ${className}`}\n        style={vars}\n        onPointerEnter={(e) => {\n          onPointerEnter?.(e);\n          setEngaged(true);\n        }}\n        onPointerLeave={(e) => {\n          onPointerLeave?.(e);\n          if (!state.current.drag) setEngaged(false);\n        }}\n        onFocus={(e) => {\n          onFocus?.(e);\n          setEngaged(true);\n        }}\n        onBlur={(e) => {\n          onBlur?.(e);\n          setEngaged(false);\n        }}\n        onPointerDown={(e) => {\n          if (e.button !== 0) return;\n          const i = indexAt(e.clientX);\n          if (i < 0) return;\n          e.currentTarget.setPointerCapture(e.pointerId);\n          state.current.drag = true;\n          choose(i);\n        }}\n        onPointerMove={(e) => {\n          if (!state.current.drag) return;\n          const i = indexAt(e.clientX);\n          if (i >= 0) choose(i);\n        }}\n        onPointerUp={(e) => {\n          if (!state.current.drag) return;\n          if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);\n          state.current.drag = false;\n        }}\n        onPointerCancel={() => {\n          state.current.drag = false;\n        }}\n        onKeyDown={(e) => {\n          let next = -1;\n          if (e.key === \"ArrowRight\" || e.key === \"ArrowDown\") next = step(valueIdx, 1);\n          else if (e.key === \"ArrowLeft\" || e.key === \"ArrowUp\") next = step(valueIdx, -1);\n          else if (e.key === \"Home\") next = step(-1, 1);\n          else if (e.key === \"End\") next = step(options.length, -1);\n          else return;\n          e.preventDefault();\n          choose(next);\n          btns.current[next]?.focus();\n        }}\n      >\n        <span ref={pillRef} className=\"hb-ql7-p\" aria-hidden=\"true\" />\n        {options.map((o, i) => (\n          <button\n            key={o.value}\n            ref={(el) => {\n              btns.current[i] = el;\n            }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={i === valueIdx}\n            tabIndex={i === valueIdx ? 0 : -1}\n            disabled={o.disabled}\n            className=\"hb-ql7-b\"\n            data-on={i === shown}\n            onClick={(e) => {\n              if (e.detail === 0) choose(i);\n            }}\n          >\n            {o.label}\n          </button>\n        ))}\n      </div>\n    </>\n  );\n}\n"
    }
  ]
}