{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"weight-wave-text","type":"registry:component","title":"Weight Wave Text","description":"Per-character variable font weight that swells around the pointer and drifts when idle.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/weight-wave-text.tsx","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport function WeightWaveText({\n  text,\n  minWeight = 200,\n  maxWeight = 900,\n  reach = 110,\n}: {\n  text: string;\n  minWeight?: number;\n  maxWeight?: number;\n  reach?: number;\n}) {\n  const hostRef = useRef<HTMLSpanElement>(null);\n  const charRefs = useRef<(HTMLSpanElement | null)[]>([]);\n  const pointer = useRef<number | null>(null);\n  // Restarts the loop after it has gone to sleep (see below).\n  const wake = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    if (!hostRef.current) return;\n    const host: HTMLSpanElement = hostRef.current;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let frame: number | null = null;\n    let onScreen = false;\n    let t = 0;\n    let last = 0;\n\n    function loop() {\n      frame = null;\n      const now = performance.now();\n      // Time-based, not per-frame: a fixed step per frame would run twice as\n      // fast on a 120Hz screen. Clamped so a long pause can't jump the wave.\n      const dt = last ? Math.min(100, now - last) : 16.7;\n      last = now;\n      t += (dt / 1000) * 1.2;\n\n      const chars = charRefs.current;\n      const at = pointer.current;\n      let intensities: number[];\n      if (at !== null) {\n        // Every read first, then every write. Measuring a letter right after\n        // restyling its neighbour forces a fresh layout per letter, per frame.\n        const left = host.getBoundingClientRect().left;\n        intensities = chars.map((el) => {\n          if (!el) return 0;\n          const rect = el.getBoundingClientRect();\n          const d = Math.abs(rect.left + rect.width / 2 - left - at);\n          // Squared falloff so the swell has a soft shoulder.\n          return Math.pow(Math.max(0, 1 - d / reach), 2);\n        });\n      } else if (!reduce.matches) {\n        // Idle: a slow sine wave travelling the line, so the headline is\n        // alive before anyone interacts with it.\n        intensities = chars.map((_, i) => ((Math.sin(t - i * 0.45) + 1) / 2) * 0.55);\n      } else {\n        // Reduced motion: no drift. The letters rest at the minimum weight\n        // and only respond to the pointer, which the reader controls.\n        intensities = chars.map(() => 0);\n      }\n\n      chars.forEach((el, i) => {\n        if (!el) return;\n        const weight = Math.round(minWeight + (maxWeight - minWeight) * intensities[i]);\n        // Written straight to the element. A setState per frame would\n        // re-render the whole headline ~60 times a second.\n        el.style.fontVariationSettings = '\"wght\" ' + weight;\n      });\n\n      // Keep running only while there is something to animate: the pointer\n      // is over the line, or the idle wave is on and the line is on screen.\n      // Otherwise sleep until wake() is called.\n      if (at !== null || (onScreen && !reduce.matches)) frame = requestAnimationFrame(loop);\n      else last = 0;\n    }\n\n    function start() {\n      if (frame === null) frame = requestAnimationFrame(loop);\n    }\n    wake.current = start;\n\n    const io = new IntersectionObserver((entries) => {\n      onScreen = entries[entries.length - 1].isIntersecting;\n      if (onScreen) start();\n    });\n    io.observe(host);\n    reduce.addEventListener(\"change\", start);\n    start();\n\n    return () => {\n      io.disconnect();\n      reduce.removeEventListener(\"change\", start);\n      if (frame !== null) cancelAnimationFrame(frame);\n      wake.current = () => {};\n    };\n  }, [minWeight, maxWeight, reach]);\n\n  return (\n    <span\n      ref={hostRef}\n      onMouseMove={(e) => {\n        const rect = hostRef.current?.getBoundingClientRect();\n        if (!rect) return;\n        pointer.current = e.clientX - rect.left;\n        wake.current();\n      }}\n      onMouseLeave={() => {\n        pointer.current = null;\n        wake.current();\n      }}\n      className=\"cursor-default select-none text-5xl leading-none tracking-tight\"\n    >\n      {/* One accessible copy; the per-character spans are decorative. */}\n      <span className=\"sr-only\">{text}</span>\n      <span aria-hidden>\n        {text.split(\"\").map((ch, i) => (\n          <span\n            key={i}\n            ref={(el) => {\n              charRefs.current[i] = el;\n            }}\n            className=\"inline-block\"\n            style={{ fontVariationSettings: '\"wght\" ' + minWeight }}\n          >\n            {ch === \" \" ? \"\\u00A0\" : ch}\n          </span>\n        ))}\n      </span>\n    </span>\n  );\n}\n","type":"registry:component","target":"components/viberdy/weight-wave-text.tsx"}],"meta":{"tier":"free","tags":["text","variable-font","hover","kinetic"],"href":"https://viberdy.dev/library/weight-wave-text","aiPrompt":"Create a React + Tailwind component named WeightWaveText — a headline whose per-character font weight swells around the pointer.\n\nBehavior:\n- Split the text into one span per character.\n- When the pointer is over the headline, each character's weight is a function of its horizontal distance from the pointer: intensity = max(0, 1 - distance / reach) ** 2, then weight = minWeight + (maxWeight - minWeight) * intensity. The squared falloff gives the swell a soft shoulder.\n- When the pointer leaves, fall back to an idle travelling sine wave: intensity = ((sin(t - i * 0.45) + 1) / 2) * 0.55, advancing t each frame — so the headline is alive before anyone touches it.\n- Apply weight via style.fontVariationSettings = '\"wght\" N'.\n\nCritical requirements:\n- This REQUIRES a variable font with a weight axis (Geist, Inter var, Roboto Flex). With a static two-weight family it will step between weights and look broken.\n- Write the weight DIRECTLY to each element from a requestAnimationFrame loop via refs. Do NOT use React state — a setState per frame across a dozen characters re-renders the whole headline ~60 times a second.\n- Keep the pointer position in a ref, not state.\n- In pointer mode, read every character's getBoundingClientRect first, then write every weight. Interleaving a read after each write forces a layout per character, per frame.\n- Only run the loop while there is something to animate: pause it with an IntersectionObserver while the headline is off-screen, and let it sleep when the pointer is away and the idle wave is off. The pointer handlers wake it again.\n- Cancel the animation frame and disconnect the observer on unmount.\n\nAccessibility:\n- Per-character splitting destroys text selection and can make screen readers announce letter by letter. Render ONE sr-only copy of the string and mark the split version aria-hidden.\n- Respect prefers-reduced-motion (and react to it changing): no idle wave, letters rest at minWeight, and only the pointer swell remains, since the reader controls it.\n\nProps: text (string), minWeight (number), maxWeight (number), reach (px).\n\nOutput a single self-contained \"use client\" component.","license":"https://viberdy.dev/license"}}