{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-shatter",
  "type": "registry:component",
  "title": "Text Shatter",
  "description": "Every letter splits into four shards that burst apart from the point you touch and spring back into a clean glyph. Works on hover and focus, as a click toggle, or on its own loop.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "shatter",
    "break",
    "shards",
    "hover",
    "explode",
    "interactive"
  ],
  "docs": "https://hyperblur.dev/effects/text-shatter",
  "files": [
    {
      "path": "registry/hyperblur/text-shatter.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/text-shatter.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TextShatterProps extends React.HTMLAttributes<HTMLSpanElement> {\n  text: string;\n  trigger?: \"hover\" | \"click\";\n  force?: number;\n  accent?: string;\n  autoPlay?: boolean;\n  interval?: number;\n  onShatterChange?: (shattered: boolean) => void;\n}\n\nconst css = `\n.hb-vi14 {\n  position: relative;\n  display: inline-block;\n  font-family: inherit;\n  white-space: pre-wrap;\n  box-sizing: border-box;\n  outline: none;\n  cursor: default;\n}\n.hb-vi14 *, .hb-vi14 *::before, .hb-vi14 *::after { box-sizing: border-box; }\n.hb-vi14[role=\"button\"] { cursor: pointer; border-radius: 0.12em; }\n.hb-vi14:focus-visible { box-shadow: 0 0 0 2px #0a0a0a, 0 0 0 4px var(--hb-vi14-accent); }\n.hb-vi14-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-vi14-w { display: inline-block; white-space: nowrap; }\n.hb-vi14-c { position: relative; display: inline-grid; }\n.hb-vi14-g {\n  grid-area: 1 / 1;\n  opacity: 1;\n  transition: opacity 0s linear calc(var(--hb-vi14-cb) + 1s);\n}\n.hb-vi14[data-on] .hb-vi14-g { opacity: 0; transition-delay: var(--hb-vi14-co); }\n.hb-vi14-s {\n  grid-area: 1 / 1;\n  clip-path: var(--hb-vi14-clip);\n  opacity: 0;\n  transform: translate3d(0, 0, 0) rotate(0deg);\n  transition:\n    transform 0.9s cubic-bezier(.34, 1.36, .5, 1) var(--hb-vi14-back),\n    color 0.6s ease var(--hb-vi14-back),\n    opacity 0s linear calc(var(--hb-vi14-cb) + 1s);\n}\n.hb-vi14[data-on] .hb-vi14-s {\n  transform: translate3d(var(--hb-vi14-x), var(--hb-vi14-y), 0) rotate(var(--hb-vi14-r));\n  color: color-mix(in oklab, var(--hb-vi14-accent) var(--hb-vi14-tint), currentColor);\n  opacity: 0.94;\n  transition:\n    transform 0.55s cubic-bezier(.12, .8, .2, 1) var(--hb-vi14-co),\n    color 0.3s ease var(--hb-vi14-co),\n    opacity 0s linear var(--hb-vi14-co);\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-vi14 .hb-vi14-s, .hb-vi14[data-on] .hb-vi14-s { display: none; }\n  .hb-vi14 .hb-vi14-g, .hb-vi14[data-on] .hb-vi14-g { opacity: 1; transition: none; }\n}\n`;\n\nfunction hash(n: number) {\n  let x = (n | 0) ^ 0x9e3779b9;\n  x = Math.imul(x ^ (x >>> 16), 0x85ebca6b);\n  x = Math.imul(x ^ (x >>> 13), 0xc2b2ae35);\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nfunction unit(n: number) {\n  return (hash(n) % 10000) / 10000;\n}\n\nconst sides: [number, number, number, number][] = [\n  [0, 0, 100, 0],\n  [100, 0, 100, 100],\n  [100, 100, 0, 100],\n  [0, 100, 0, 0],\n];\n\nexport function TextShatter({\n  text,\n  trigger = \"hover\",\n  force = 1,\n  accent = \"#7fb3ff\",\n  autoPlay = false,\n  interval = 2.6,\n  onShatterChange,\n  className = \"\",\n  style,\n  onMouseEnter,\n  onMouseLeave,\n  onFocus,\n  onBlur,\n  onClick,\n  onKeyDown,\n  ...rest\n}: TextShatterProps) {\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 [on, setOn] = React.useState(false);\n  const [origin, setOrigin] = React.useState(-1);\n  const [hover, setHover] = React.useState(false);\n\n  const set = React.useCallback(\n    (v: boolean) => {\n      setOn((cur) => {\n        if (cur !== v) onShatterChange?.(v);\n        return v;\n      });\n    },\n    [onShatterChange],\n  );\n\n  React.useEffect(() => {\n    if (!autoPlay || hover) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const id = setTimeout(() => {\n      setOrigin(-1);\n      set(!on);\n    }, (on ? interval * 0.45 : interval) * 1000);\n    return () => clearTimeout(id);\n  }, [autoPlay, hover, on, interval, set]);\n\n  const vars = { \"--hb-vi14-accent\": accent, ...style } as React.CSSProperties;\n  const center = origin >= 0 ? origin : (total - 1) / 2;\n  let n = 0;\n  const interactive = trigger === \"click\";\n\n  return (\n    <span\n      className={`hb-vi14 ${className}`}\n      style={vars}\n      data-on={on ? \"\" : undefined}\n      role={interactive ? \"button\" : undefined}\n      tabIndex={interactive ? 0 : undefined}\n      aria-pressed={interactive ? on : undefined}\n      onMouseEnter={(e) => {\n        setHover(true);\n        if (trigger === \"hover\") set(true);\n        onMouseEnter?.(e);\n      }}\n      onMouseLeave={(e) => {\n        setHover(false);\n        if (trigger === \"hover\") set(false);\n        onMouseLeave?.(e);\n      }}\n      onFocus={(e) => {\n        if (trigger === \"hover\") set(true);\n        onFocus?.(e);\n      }}\n      onBlur={(e) => {\n        if (trigger === \"hover\") set(false);\n        onBlur?.(e);\n      }}\n      onClick={(e) => {\n        if (interactive) set(!on);\n        onClick?.(e);\n      }}\n      onKeyDown={(e) => {\n        if (interactive && (e.key === \"Enter\" || e.key === \" \")) {\n          e.preventDefault();\n          set(!on);\n        }\n        onKeyDown?.(e);\n      }}\n      {...rest}\n    >\n      <style href=\"hb-text-shatter\" precedence=\"default\">{css}</style>\n      {interactive ? null : <span className=\"hb-vi14-sr\">{text}</span>}\n      <span aria-hidden={interactive ? undefined : true} aria-label={interactive ? text : undefined} role={interactive ? \"img\" : undefined}>\n        {words.map((word, wi) => {\n          if (word.trim() === \"\") return <React.Fragment key={wi}>{word}</React.Fragment>;\n          return (\n            <span className=\"hb-vi14-w\" key={wi}>\n              {Array.from(word).map((ch, ci) => {\n                const i = n++;\n                const cx = 32 + unit(i * 11 + 1) * 36;\n                const cy = 30 + unit(i * 11 + 2) * 40;\n                const dist = Math.abs(i - center);\n                return (\n                  <span\n                    key={ci}\n                    className=\"hb-vi14-c\"\n                    style={\n                      {\n                        \"--hb-vi14-co\": `${(dist * 0.018).toFixed(3)}s`,\n                        \"--hb-vi14-cb\": `${(dist * 0.012).toFixed(3)}s`,\n                      } as React.CSSProperties\n                    }\n                    onMouseEnter={trigger === \"hover\" && !on ? () => setOrigin(i) : undefined}\n                  >\n                    <span className=\"hb-vi14-g\">{ch}</span>\n                    {sides.map(([x1, y1, x2, y2], s) => {\n                      const mx = (x1 + x2) / 2 - cx;\n                      const my = (y1 + y2) / 2 - cy;\n                      const len = Math.hypot(mx, my) || 1;\n                      const push = (0.16 + unit(i * 29 + s * 7) * 0.22) * force;\n                      const lift = (unit(i * 31 + s) - 0.5) * 0.12 * force;\n                      const rot = (unit(i * 37 + s * 3) - 0.5) * 70 * force;\n                      const shard = {\n                        \"--hb-vi14-clip\": `polygon(${x1}% ${y1}%, ${x2}% ${y2}%, ${cx.toFixed(1)}% ${cy.toFixed(1)}%)`,\n                        \"--hb-vi14-x\": `${((mx / len) * push).toFixed(3)}em`,\n                        \"--hb-vi14-y\": `${((my / len) * push + lift).toFixed(3)}em`,\n                        \"--hb-vi14-r\": `${rot.toFixed(1)}deg`,\n                        \"--hb-vi14-tint\": `${(10 + unit(i * 41 + s) * 50).toFixed(0)}%`,\n                        \"--hb-vi14-back\": `${(dist * 0.012 + s * 0.02).toFixed(3)}s`,\n                      } as React.CSSProperties;\n                      return (\n                        <span key={s} className=\"hb-vi14-s\" style={shard}>\n                          {ch}\n                        </span>\n                      );\n                    })}\n                  </span>\n                );\n              })}\n            </span>\n          );\n        })}\n      </span>\n    </span>\n  );\n}\n"
    }
  ]
}