{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"weight-slam-text","type":"registry:component","title":"Weight Slam Text","description":"Display type whose weight slams instead of easing: letters jump 400 → 650 → 900 in two hard steps, left to right like a row of stamps, and let go in three.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/weight-slam-text.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport type { CSSProperties, PointerEvent as ReactPointerEvent } from \"react\";\n\n/**\n * WeightSlamText — neo-brutalist display type whose weight slams instead of\n * easing. Each letter jumps from its rest weight to heavy in two hard steps\n * (400, 650, 900) inside 80ms, like a press coming down, and lets go in three\n * slower steps. There is no interpolation to watch, only the hits.\n *\n * Three modes:\n * - \"sweep\" (the default): pointing at the text, or focusing a link or\n *   button inside it, slams the letters left to right 45ms apart, a row of\n *   stamps coming down; leaving releases them right to left.\n * - \"press\": the letter under the pointer is struck to the peak and its\n *   neighbours part-way, in the same hard steps. Which letter is \"under\" the\n *   pointer is judged against the letters' rest positions (an invisible copy\n *   set at the rest weight), so the line can reflow as letters thicken\n *   without the strike jittering back and forth. On a touch screen, where\n *   nothing hovers, it plays the sweep once instead, when it scrolls into view.\n * - \"view\": slams left to right once, when most of it is on screen, and stays.\n *\n * Screen readers get the words once, from a visually hidden copy; the\n * letters are aria-hidden. Under prefers-reduced-motion the steps are dropped\n * and the weight simply changes. A variable font gives the in-between step\n * (the stack starts with var(--font-display), then Archivo); a static font\n * still slams between its own cuts.\n *\n * Needs Tailwind v4 (or v3.4+). No dependencies beyond React.\n */\n\nconst DISPLAY = 'var(--font-display, \"Archivo\", \"Archivo Black\", \"Arial Black\", \"Helvetica Neue\", Arial, sans-serif)';\nconst HEX = /^#[0-9a-fA-F]{6}$/;\nconst TAGS = [\"h1\", \"h2\", \"h3\", \"p\"] as const;\nconst NBSP = String.fromCharCode(160);\n\nexport type WeightSlamTextProps = {\n  /** The text; \"\\n\" starts a new line. */\n  text?: string;\n  as?: \"h1\" | \"h2\" | \"h3\" | \"p\";\n  mode?: \"sweep\" | \"press\" | \"view\";\n  /** Rest weight, 100 to 700. */\n  rest?: number;\n  /** Peak weight, 500 to 1000. */\n  peak?: number;\n  /** How many neighbours on each side a press thickens, 0 to 3. */\n  reach?: number;\n  /** Letters also widen as they slam (needs a width axis, like Archivo's). */\n  stretch?: boolean;\n  caps?: boolean;\n  /** Text colour, #rrggbb. */\n  color?: string;\n  className?: string;\n};\n\nfunction clampNum(v: number, lo: number, hi: number, fallback: number) {\n  return Number.isFinite(v) ? Math.max(lo, Math.min(hi, v)) : fallback;\n}\n\nexport function WeightSlamText({\n  text = \"Build loud.\\nShip daily.\",\n  as = \"h2\",\n  mode = \"sweep\",\n  rest = 400,\n  peak = 900,\n  reach = 1,\n  stretch = true,\n  caps = true,\n  color = \"#000000\",\n  className = \"\",\n}: WeightSlamTextProps) {\n  const Tag = (TAGS as readonly string[]).includes(as) ? as : \"h2\";\n  const lo = clampNum(rest, 100, 700, 400);\n  const hi = Math.max(lo, clampNum(peak, 500, 1000, 900));\n  const span = Math.round(clampNum(reach, 0, 3, 1));\n  const ink = HEX.test(color) ? color : \"#000000\";\n  const lines = (text || \" \").split(\"\\n\").map((l) => [...l]);\n  const count = lines.reduce((n, l) => n + l.length, 0);\n  // Where each line's letters start in the flat index.\n  const starts = lines.map((_, li) => lines.slice(0, li).reduce((n, l) => n + l.length, 0));\n\n  const face = useRef<HTMLSpanElement>(null);\n  const ghost = useRef<HTMLSpanElement>(null);\n  const [struck, setStruck] = useState(-1);\n  const [on, setOn] = useState(false);\n  const [auto, setAuto] = useState(false);\n  // A new text clears a strike measured against the old one.\n  const [seenText, setSeenText] = useState(text);\n  if (seenText !== text) {\n    setSeenText(text);\n    setStruck(-1);\n  }\n\n  // \"view\" mode, and \"press\" on screens that cannot hover: slam once when most of it is on screen.\n  useEffect(() => {\n    const el = face.current;\n    if (!el || (mode !== \"view\" && mode !== \"press\")) return;\n    const io = new IntersectionObserver(\n      (entries) => {\n        const e = entries[entries.length - 1];\n        if (!e || e.intersectionRatio < 0.6) return;\n        if (mode === \"press\" && !window.matchMedia(\"(hover: none)\").matches) return;\n        setAuto(true);\n        io.disconnect();\n      },\n      { threshold: [0, 0.6, 1] },\n    );\n    io.observe(el);\n    return () => io.disconnect();\n  }, [mode]);\n\n  const onMove = (e: ReactPointerEvent<HTMLElement>) => {\n    if (mode !== \"press\" || e.pointerType === \"touch\") return;\n    const g = ghost.current;\n    if (!g) return;\n    const r = g.getBoundingClientRect();\n    // Previews can be transform-scaled: map screen pixels to layout pixels.\n    const k = r.width / (g.offsetWidth || r.width) || 1;\n    const x = (e.clientX - r.left) / k;\n    const y = (e.clientY - r.top) / k;\n    let best = -1;\n    let bestD = Infinity;\n    g.querySelectorAll<HTMLElement>(\"[data-i]\").forEach((s) => {\n      const cx = s.offsetLeft + s.offsetWidth / 2;\n      const cy = s.offsetTop + s.offsetHeight / 2;\n      // Rows count double, so the strike stays on the line the pointer is on.\n      const d = Math.abs(cx - x) + Math.abs(cy - y) * 2;\n      if (d < bestD) {\n        bestD = d;\n        best = Number(s.dataset.i);\n      }\n    });\n    if (best !== struck) setStruck(best);\n  };\n\n  const sweeping = (mode === \"sweep\" && on) || auto;\n  const weightOf = (i: number) => {\n    if (sweeping) return hi;\n    if (mode !== \"press\" || struck < 0) return lo;\n    const d = Math.abs(i - struck);\n    if (d === 0) return hi;\n    if (d <= span) return Math.round(lo + ((hi - lo) * (span + 1 - d)) / (span + 2));\n    return lo;\n  };\n\n  const letters = (ghostLayer: boolean) =>\n    lines.map((line, li) => (\n      <span key={li} className=\"block whitespace-nowrap\">\n        {line.map((ch, ci) => {\n          const i = starts[li] + ci;\n          const w = ghostLayer ? lo : weightOf(i);\n          const up = w > lo;\n          const delay = mode === \"press\" ? 0 : up ? i * 45 : (count - 1 - i) * 30;\n          const timing = up ? \"80ms steps(2, end) \" + delay + \"ms\" : \"180ms steps(3, end) \" + delay + \"ms\";\n          return (\n            <span\n              key={ci}\n              data-i={i}\n              className=\"inline-block motion-reduce:!transition-none\"\n              style={{\n                fontWeight: w,\n                fontStretch: stretch && up ? Math.round(100 + ((w - lo) / Math.max(1, hi - lo)) * 12) + \"%\" : \"100%\",\n                transition: ghostLayer ? \"none\" : \"font-weight \" + timing + \", font-stretch \" + timing,\n              }}\n            >\n              {ch === \" \" ? NBSP : ch}\n            </span>\n          );\n        })}\n      </span>\n    ));\n\n  return (\n    <Tag\n      className={\"relative select-none leading-[0.9] tracking-[-0.02em] \" + (caps ? \"uppercase \" : \"\") + className}\n      style={{ fontFamily: DISPLAY, color: ink } as CSSProperties}\n      onPointerMove={onMove}\n      onPointerLeave={() => {\n        setStruck(-1);\n        setOn(false);\n      }}\n      onPointerEnter={(e) => {\n        if (mode === \"sweep\" && e.pointerType !== \"touch\") setOn(true);\n      }}\n      onFocusCapture={() => {\n        if (mode === \"sweep\") setOn(true);\n      }}\n      onBlurCapture={() => {\n        if (mode === \"sweep\") setOn(false);\n      }}\n    >\n      <span className=\"sr-only\">{text.replace(/\\n/g, \" \")}</span>\n      <span ref={face} aria-hidden className=\"block\">\n        {letters(false)}\n      </span>\n      {/* The rest-weight copy the press measures against; never painted. */}\n      <span ref={ghost} aria-hidden className=\"pointer-events-none invisible absolute left-0 top-0 block w-full\">\n        {letters(true)}\n      </span>\n    </Tag>\n  );\n}\n","type":"registry:component","target":"components/viberdy/weight-slam-text.tsx"}],"meta":{"tier":"free","tags":["text effect","variable font","font weight","hover","neo-brutalism","brutalist","headline","kinetic type"],"href":"https://viberdy.dev/library/weight-slam-text","aiPrompt":"Create a React + Tailwind text effect, WeightSlamText: neo-brutalist display type whose weight slams instead of easing. Each letter jumps from its rest weight to heavy in two hard steps inside 80ms (transition font-weight 80ms steps(2, end)), like a press coming down, and lets go in three slower steps (180ms steps(3, end)). No smooth interpolation anywhere.\n\nProps (export WeightSlamTextProps): text (default \"Build loud.\\nShip daily.\"; \"\\n\" starts a new line), as (\"h1\" | \"h2\" | \"h3\" | \"p\", allowlisted at run time), mode (\"sweep\" default | \"press\" | \"view\"), rest (100-700, default 400), peak (500-1000, default 900), reach (0-3 neighbours a press thickens, default 1), stretch (slammed letters also widen up to font-stretch 112%, for fonts with a width axis like Archivo), caps, color (#rrggbb, regex-validated), className (sets the size, e.g. text-[clamp(52px,12.5cqw,168px)]). Clamp every number and fall back on NaN.\n\nModes:\n- \"sweep\": pointer enter (not touch), or focus entering the text (a link or button inside it), sets every letter to the peak with a transition delay of index x 45ms, so they come down left to right like a row of stamps; pointer leave or blur releases them with delays of (count - 1 - index) x 30ms, right to left.\n- \"press\": on pointer move (not touch), the letter under the pointer goes to the peak and each neighbour within reach part-way (rest + (peak - rest) x (reach + 1 - distance) / (reach + 2)). Judge which letter is under the pointer against an invisible copy of the text set at the rest weight (absolutely positioned over the visible one, aria-hidden, pointer-events none), using each ghost letter's offset centre, with vertical distance counted double so the strike stays on the pointer's line, and map the pointer into layout pixels (divide by rect width over offsetWidth, for transform-scaled previews). That way the visible line can reflow as letters thicken without the strike jittering. On screens with (hover: none) it plays the sweep once when 60% of it is on screen (IntersectionObserver, read the last entry's intersectionRatio).\n- \"view\": slams left to right once when 60% is on screen, and stays.\n\nRender each line as a nowrap block of inline-block letter spans (spaces as a non-breaking space, built with String.fromCharCode(160)), aria-hidden, with a visually hidden copy of the whole text for screen readers (no aria-label, which a paragraph cannot carry). Type: var(--font-display, \"Archivo\", \"Archivo Black\", \"Arial Black\", \"Helvetica Neue\", Arial, sans-serif), leading 0.9, tracking -0.02em, uppercase by default, select-none. Under prefers-reduced-motion drop the transitions so the weight simply changes.\n\nOutput one self-contained \"use client\" TSX file with no dependencies beyond React. Build strings with + rather than template literals.","license":"https://viberdy.dev/license"}}