{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "micro-sling-button",
  "type": "registry:component",
  "title": "Sling Button",
  "description": "Drag the button back against a stretching band and let go. It slings forward, overshoots and settles on a spring.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "button",
    "drag",
    "spring",
    "elastic",
    "slingshot",
    "micro"
  ],
  "docs": "https://hyperblur.dev/effects/micro-sling-button",
  "files": [
    {
      "path": "registry/hyperblur/micro-sling-button.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/micro-sling-button.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MicroSlingButtonProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  children?: React.ReactNode;\n  maxPull?: number;\n  accent?: string;\n  disabled?: boolean;\n  idle?: boolean;\n  onSling?: (power: number) => void;\n}\n\nconst css = `\n.hb-ql1 {\n  position: relative;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 100%;\n  height: 72px;\n  box-sizing: border-box;\n  font-family: inherit;\n  --hb-ql1-c: #2f7bff;\n}\n.hb-ql1 *, .hb-ql1 *::before, .hb-ql1 *::after { box-sizing: border-box; }\n.hb-ql1-s {\n  position: absolute;\n  inset: 0;\n  width: 100%;\n  height: 100%;\n  overflow: visible;\n  pointer-events: none;\n}\n.hb-ql1-b {\n  fill: none;\n  stroke: var(--hb-ql1-c);\n  stroke-linecap: round;\n}\n.hb-ql1-p {\n  fill: #171717;\n  stroke: #3a3a3a;\n  stroke-width: 2;\n}\n.hb-ql1-k {\n  position: relative;\n  z-index: 1;\n  margin: 0;\n  padding: 0.7em 1.5em;\n  font: inherit;\n  font-weight: 600;\n  line-height: 1.2;\n  color: #fff;\n  background: linear-gradient(180deg, color-mix(in srgb, var(--hb-ql1-c) 86%, #fff) 0%, var(--hb-ql1-c) 100%);\n  border: 1px solid color-mix(in srgb, var(--hb-ql1-c) 60%, #fff);\n  border-radius: 999px;\n  box-shadow: 0 8px 22px -10px var(--hb-ql1-c), inset 0 1px 0 rgba(255, 255, 255, 0.35);\n  cursor: grab;\n  user-select: none;\n  -webkit-user-select: none;\n  -webkit-touch-callout: none;\n  -webkit-tap-highlight-color: transparent;\n  touch-action: none;\n  white-space: nowrap;\n  will-change: transform;\n}\n.hb-ql1-k:active { cursor: grabbing; }\n.hb-ql1-k:focus-visible { outline: 2px solid var(--hb-ql1-c); outline-offset: 4px; }\n.hb-ql1-k:disabled { opacity: 0.45; cursor: not-allowed; }\n`;\n\nfunction ease(t: number) {\n  return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;\n}\n\nexport function MicroSlingButton({\n  children = \"Sling\",\n  maxPull = 72,\n  accent = \"#2f7bff\",\n  disabled = false,\n  idle = true,\n  onSling,\n  className = \"\",\n  style,\n  onPointerEnter,\n  onPointerLeave,\n  ...rest\n}: MicroSlingButtonProps) {\n  const trackRef = React.useRef<HTMLDivElement>(null);\n  const btnRef = React.useRef<HTMLButtonElement>(null);\n  const bandRef = React.useRef<SVGPathElement>(null);\n  const postRef = React.useRef<SVGCircleElement>(null);\n  const state = React.useRef({\n    x: 0,\n    v: 0,\n    drag: false,\n    dragged: false,\n    startX: 0,\n    baseRaw: 0,\n    hover: false,\n    resumeAt: 0,\n    idleStart: -1,\n  });\n  const api = React.useRef({ wake: () => {}, draw: () => {}, reduced: false });\n  const latest = React.useRef({ maxPull, idle, onSling });\n\n  React.useEffect(() => {\n    latest.current = { maxPull, idle, onSling };\n  }, [maxPull, idle, onSling]);\n\n  React.useEffect(() => {\n    const track = trackRef.current;\n    const btn = btnRef.current;\n    const band = bandRef.current;\n    const post = postRef.current;\n    if (!track || !btn || !band || !post) 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 draw = () => {\n      const pull = latest.current.maxPull;\n      const w = track.clientWidth;\n      const cy = track.clientHeight / 2;\n      const bx = btn.offsetLeft + btn.offsetWidth + s.x;\n      const px = w - 22;\n      const stretch = Math.min(1.2, Math.max(0, -s.x) / pull);\n      const slack = Math.max(0, s.x);\n      const sag = 7 * (1 - Math.min(1, stretch * 2)) + slack * 1.3;\n      const mid = (bx + px) / 2;\n      band.setAttribute(\n        \"d\",\n        `M ${bx.toFixed(1)} ${cy.toFixed(1)} Q ${mid.toFixed(1)} ${(cy + sag).toFixed(1)} ${px.toFixed(1)} ${cy.toFixed(1)}`,\n      );\n      band.setAttribute(\"stroke-width\", Math.max(2, 7 - 4.6 * stretch).toFixed(2));\n      band.setAttribute(\"stroke-opacity\", (0.45 + 0.5 * Math.min(1, stretch)).toFixed(2));\n      post.setAttribute(\"cx\", px.toFixed(1));\n      post.setAttribute(\"cy\", cy.toFixed(1));\n      post.setAttribute(\"r\", \"7\");\n      btn.style.transform = `translate3d(${s.x.toFixed(2)}px,0,0) scale(${(1 + stretch * 0.07).toFixed(3)},${(1 - stretch * 0.05).toFixed(3)})`;\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 cfg = latest.current;\n      const idleOn = cfg.idle && !s.hover && !s.drag && now >= s.resumeAt && !api.current.reduced;\n      let scripted = false;\n      if (idleOn) {\n        if (s.idleStart < 0) s.idleStart = now;\n        const ph = (now - s.idleStart) % 3400;\n        if (ph < 800) {\n          scripted = true;\n          s.x = -52 * ease(ph / 800);\n          s.v = 0;\n        }\n      } else {\n        s.idleStart = -1;\n      }\n      if (!s.drag && !scripted) {\n        let rem = dt;\n        while (rem > 0) {\n          const h = Math.min(rem, 1 / 120);\n          s.v += (-260 * s.x - 12 * s.v) * h;\n          s.x += s.v * h;\n          rem -= h;\n        }\n        if (Math.abs(s.x) < 0.01 && Math.abs(s.v) < 0.01) {\n          s.x = 0;\n          s.v = 0;\n        }\n      }\n      draw();\n      raf = requestAnimationFrame(tick);\n    };\n\n    const wake = () => {\n      if (api.current.reduced) {\n        draw();\n        return;\n      }\n      if (!raf && visible) {\n        last = performance.now();\n        raf = requestAnimationFrame(tick);\n      }\n    };\n\n    api.current.wake = wake;\n    api.current.draw = draw;\n    draw();\n    if (!api.current.reduced) 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(track);\n    const ro = new ResizeObserver(() => draw());\n    ro.observe(track);\n    const onMq = () => {\n      api.current.reduced = mq.matches;\n      if (mq.matches) {\n        s.x = 0;\n        s.v = 0;\n        cancelAnimationFrame(raf);\n        raf = 0;\n        draw();\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  function release(power: number) {\n    const s = state.current;\n    s.resumeAt = performance.now() + 2600;\n    if (api.current.reduced) {\n      s.x = 0;\n      s.v = 0;\n      api.current.draw();\n    } else api.current.wake();\n    latest.current.onSling?.(power);\n  }\n\n  function fire(power: number) {\n    const s = state.current;\n    s.drag = false;\n    if (!api.current.reduced) {\n      s.x = -latest.current.maxPull * power;\n      s.v = 0;\n    }\n    release(power);\n  }\n\n  function move(clientX: number) {\n    const s = state.current;\n    const pull = latest.current.maxPull;\n    const raw = s.baseRaw + (clientX - s.startX);\n    if (Math.abs(clientX - s.startX) > 4) s.dragged = true;\n    s.x = raw < 0 ? -pull * (1 - Math.exp(raw / pull)) : raw * 0.15;\n    api.current.draw();\n  }\n\n  const vars = { \"--hb-ql1-c\": accent, ...style } as React.CSSProperties;\n\n  return (\n    <>\n      <style href=\"hb-micro-sling-button\" precedence=\"default\">{css}</style>\n      <div\n        {...rest}\n        ref={trackRef}\n        className={`hb-ql1 ${className}`}\n        style={vars}\n        onPointerEnter={(e) => {\n          onPointerEnter?.(e);\n          state.current.hover = true;\n        }}\n        onPointerLeave={(e) => {\n          onPointerLeave?.(e);\n          state.current.hover = false;\n          state.current.resumeAt = performance.now() + 1500;\n        }}\n      >\n        <svg className=\"hb-ql1-s\" aria-hidden=\"true\">\n          <path ref={bandRef} className=\"hb-ql1-b\" d=\"M0 0\" />\n          <circle ref={postRef} className=\"hb-ql1-p\" r=\"7\" />\n        </svg>\n        <button\n          ref={btnRef}\n          type=\"button\"\n          className=\"hb-ql1-k\"\n          disabled={disabled}\n          onPointerDown={(e) => {\n            if (e.button !== 0 || disabled) return;\n            const s = state.current;\n            const pull = latest.current.maxPull;\n            e.currentTarget.setPointerCapture(e.pointerId);\n            s.drag = true;\n            s.dragged = false;\n            s.startX = e.clientX;\n            s.baseRaw = s.x < 0 ? pull * Math.log(Math.max(0.01, 1 + s.x / pull)) : s.x / 0.15;\n            s.v = 0;\n            api.current.wake();\n          }}\n          onPointerMove={(e) => {\n            if (state.current.drag) move(e.clientX);\n          }}\n          onPointerUp={(e) => {\n            const s = state.current;\n            if (!s.drag) return;\n            if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);\n            s.drag = false;\n            if (s.dragged && s.x < -6) release(Math.min(1, -s.x / latest.current.maxPull));\n            else {\n              s.resumeAt = performance.now() + 2600;\n              api.current.wake();\n            }\n          }}\n          onPointerCancel={() => {\n            state.current.drag = false;\n            api.current.wake();\n          }}\n          onClick={() => {\n            if (state.current.dragged) {\n              state.current.dragged = false;\n              return;\n            }\n            fire(0.62);\n          }}\n        >\n          {children}\n        </button>\n      </div>\n    </>\n  );\n}\n"
    }
  ]
}