{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-magnetic",
  "type": "registry:component",
  "title": "Text Magnetic",
  "description": "Each letter hangs on its own spring and leans toward the pointer, tilting with its speed and wobbling back into place when released. Autoplay drives a small magnet across the line when nobody is pointing.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "magnetic",
    "spring",
    "physics",
    "pointer",
    "letters",
    "interactive"
  ],
  "docs": "https://hyperblur.dev/effects/text-magnetic",
  "files": [
    {
      "path": "registry/hyperblur/text-magnetic.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/text-magnetic.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TextMagneticProps extends React.HTMLAttributes<HTMLSpanElement> {\n  text: string;\n  radius?: number;\n  strength?: number;\n  stiffness?: number;\n  damping?: number;\n  rotate?: number;\n  highlight?: string;\n  autoPlay?: boolean;\n}\n\nconst css = `\n.hb-vi9 {\n  position: relative;\n  display: inline-block;\n  font-family: inherit;\n  white-space: pre-wrap;\n  box-sizing: border-box;\n  touch-action: pan-y;\n}\n.hb-vi9 *, .hb-vi9 *::before, .hb-vi9 *::after { box-sizing: border-box; }\n.hb-vi9-sr {\n  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;\n  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;\n}\n.hb-vi9-w { display: inline-block; white-space: nowrap; }\n.hb-vi9-c {\n  display: inline-block;\n  transform: translate3d(var(--hb-vi9-x, 0px), var(--hb-vi9-y, 0px), 0) rotate(var(--hb-vi9-r, 0deg)) scale(var(--hb-vi9-s, 1));\n  color: color-mix(in oklab, var(--hb-vi9-hi) calc(var(--hb-vi9-p, 0) * 100%), currentColor);\n  will-change: transform;\n}\n.hb-vi9-dot {\n  position: absolute;\n  left: 0; top: 0;\n  width: 0.22em; height: 0.22em;\n  margin: -0.11em 0 0 -0.11em;\n  border-radius: 50%;\n  background: var(--hb-vi9-hi);\n  box-shadow: 0 0 0 0.12em color-mix(in srgb, var(--hb-vi9-hi) 22%, transparent), 0 0 1.2em color-mix(in srgb, var(--hb-vi9-hi) 60%, transparent);\n  opacity: 0;\n  transform: translate3d(var(--hb-vi9-mx, 0px), var(--hb-vi9-my, 0px), 0);\n  transition: opacity 0.4s ease;\n  pointer-events: none;\n}\n.hb-vi9[data-ghost] .hb-vi9-dot { opacity: 0.85; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-vi9 .hb-vi9-c { transform: none; will-change: auto; }\n  .hb-vi9 .hb-vi9-dot { display: none; }\n}\n`;\n\nexport function TextMagnetic({\n  text,\n  radius = 120,\n  strength = 18,\n  stiffness = 0.12,\n  damping = 0.78,\n  rotate = 10,\n  highlight = \"#7fb3ff\",\n  autoPlay = false,\n  className = \"\",\n  style,\n  onPointerMove,\n  onPointerLeave,\n  ...rest\n}: TextMagneticProps) {\n  const ref = React.useRef<HTMLSpanElement>(null);\n  const cells = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const words = React.useMemo(() => text.split(/(\\s+)/), [text]);\n  const total = React.useMemo(() => Array.from(text).filter((c) => c.trim() !== \"\").length, [text]);\n  const pointer = React.useRef<{ x: number; y: number } | null>(null);\n  const userAt = React.useRef(0);\n  const wakeRef = React.useRef<() => void>(() => undefined);\n  const [ghost, setGhost] = React.useState(false);\n\n  React.useEffect(() => {\n    const root = ref.current;\n    if (!root) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const list = cells.current.slice(0, total);\n    const n = list.length;\n    const px = new Float32Array(n);\n    const py = new Float32Array(n);\n    const vx = new Float32Array(n);\n    const vy = new Float32Array(n);\n    let homes: { x: number; y: number }[] = [];\n    let raf = 0;\n    let visible = true;\n    let t = 0;\n    let last = 0;\n    let ghostOn = false;\n\n    const measure = () => {\n      const r = root.getBoundingClientRect();\n      homes = list.map((el, i) => {\n        if (!el) return { x: -9999, y: -9999 };\n        const b = el.getBoundingClientRect();\n        return { x: b.left - r.left + b.width / 2 - px[i], y: b.top - r.top + b.height / 2 - py[i] };\n      });\n    };\n\n    const frame = (now: number) => {\n      const dt = last ? Math.min(40, now - last) : 16;\n      last = now;\n      const step = dt / 16.67;\n      let target = pointer.current;\n      const idle = now - userAt.current > 1400;\n      const useGhost = autoPlay && visible && idle && !target;\n      if (useGhost) {\n        t += dt / 1000;\n        const w = root.clientWidth;\n        const h = root.clientHeight;\n        target = {\n          x: w * (0.5 + Math.sin(t * 0.9) * 0.46),\n          y: h * (0.5 + Math.sin(t * 1.7 + 0.6) * 0.75),\n        };\n        root.style.setProperty(\"--hb-vi9-mx\", `${target.x.toFixed(1)}px`);\n        root.style.setProperty(\"--hb-vi9-my\", `${target.y.toFixed(1)}px`);\n      }\n      if (useGhost !== ghostOn) {\n        ghostOn = useGhost;\n        setGhost(useGhost);\n      }\n      let moving = false;\n      for (let i = 0; i < n; i++) {\n        let tx = 0;\n        let ty = 0;\n        let pull = 0;\n        if (target) {\n          const dx = target.x - homes[i].x;\n          const dy = target.y - homes[i].y;\n          const d = Math.hypot(dx, dy);\n          if (d < radius && d > 0.001) {\n            const f = 1 - d / radius;\n            pull = f * f;\n            const m = Math.min(d * 0.6, strength * pull * 1.6);\n            tx = (dx / d) * m;\n            ty = (dy / d) * m;\n          }\n        }\n        vx[i] = (vx[i] + (tx - px[i]) * stiffness * step) * Math.pow(damping, step);\n        vy[i] = (vy[i] + (ty - py[i]) * stiffness * step) * Math.pow(damping, step);\n        px[i] += vx[i] * step;\n        py[i] += vy[i] * step;\n        const el = list[i];\n        if (el) {\n          el.style.setProperty(\"--hb-vi9-x\", `${px[i].toFixed(2)}px`);\n          el.style.setProperty(\"--hb-vi9-y\", `${py[i].toFixed(2)}px`);\n          el.style.setProperty(\"--hb-vi9-r\", `${Math.max(-rotate, Math.min(rotate, vx[i] * rotate * 0.35 + px[i] * 0.4)).toFixed(2)}deg`);\n          const p = Math.min(1, Math.hypot(px[i], py[i]) / Math.max(1, strength));\n          el.style.setProperty(\"--hb-vi9-p\", p.toFixed(3));\n          el.style.setProperty(\"--hb-vi9-s\", (1 + p * 0.08).toFixed(3));\n        }\n        if (Math.abs(vx[i]) + Math.abs(vy[i]) > 0.01 || Math.abs(px[i] - tx) + Math.abs(py[i] - ty) > 0.05) moving = true;\n      }\n      raf = visible && (moving || autoPlay || pointer.current) ? requestAnimationFrame(frame) : 0;\n      if (!raf) last = 0;\n    };\n\n    const wake = () => {\n      if (!raf && visible) raf = requestAnimationFrame(frame);\n    };\n    wakeRef.current = wake;\n\n    measure();\n    const ro = typeof ResizeObserver !== \"undefined\" ? new ResizeObserver(measure) : null;\n    ro?.observe(root);\n    if (document.fonts?.ready) document.fonts.ready.then(measure).catch(() => undefined);\n    const io =\n      typeof IntersectionObserver !== \"undefined\"\n        ? new IntersectionObserver((entries) => {\n            visible = entries.some((e) => e.isIntersecting);\n            if (visible) wake();\n          })\n        : null;\n    io?.observe(root);\n    wake();\n    return () => {\n      cancelAnimationFrame(raf);\n      ro?.disconnect();\n      io?.disconnect();\n      wakeRef.current = () => undefined;\n    };\n  }, [radius, strength, stiffness, damping, rotate, autoPlay, total, words]);\n\n  const vars = { \"--hb-vi9-hi\": highlight, ...style } as React.CSSProperties;\n  let n = 0;\n\n  return (\n    <span\n      ref={ref}\n      className={`hb-vi9 ${className}`}\n      style={vars}\n      data-ghost={ghost ? \"\" : undefined}\n      onPointerMove={(e) => {\n        const root = ref.current;\n        if (root) {\n          const r = root.getBoundingClientRect();\n          pointer.current = { x: e.clientX - r.left, y: e.clientY - r.top };\n          userAt.current = performance.now();\n          wakeRef.current();\n        }\n        onPointerMove?.(e);\n      }}\n      onPointerLeave={(e) => {\n        pointer.current = null;\n        userAt.current = performance.now();\n        wakeRef.current();\n        onPointerLeave?.(e);\n      }}\n      {...rest}\n    >\n      <style href=\"hb-text-magnetic\" precedence=\"default\">{css}</style>\n      <span className=\"hb-vi9-sr\">{text}</span>\n      <span aria-hidden=\"true\">\n        {words.map((word, wi) => {\n          if (word.trim() === \"\") return <React.Fragment key={wi}>{word}</React.Fragment>;\n          return (\n            <span className=\"hb-vi9-w\" key={wi}>\n              {Array.from(word).map((ch, ci) => {\n                const i = n++;\n                return (\n                  <span\n                    key={ci}\n                    className=\"hb-vi9-c\"\n                    ref={(el) => {\n                      cells.current[i] = el;\n                    }}\n                  >\n                    {ch}\n                  </span>\n                );\n              })}\n            </span>\n          );\n        })}\n      </span>\n      {autoPlay ? <span className=\"hb-vi9-dot\" aria-hidden=\"true\" /> : null}\n    </span>\n  );\n}\n"
    }
  ]
}