{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rgb-glitch-text",
  "type": "registry:component",
  "title": "RGB Glitch Text",
  "description": "Clean text that occasionally breaks into short bursts: split RGB channels and shifted slices.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Text"
  ],
  "docs": "Installed in components/furin/rgb-glitch-text/. Docs, live controls and props table: https://furin.dev/components/rgb-glitch-text/",
  "files": [
    {
      "path": "registry/rgb-glitch-text/RGBGlitchText.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, RefObject } from \"react\";\nimport \"./RGBGlitchText.css\";\n\nexport interface RGBGlitchTextProps {\n  /** El texto (acepta saltos de línea). */\n  children?: string;\n  /** Tamaño de la letra, en px. */\n  size?: number;\n  /** Fuerza de la separación de canales y de los cortes (0.3 a 2). */\n  intensity?: number;\n  /** Segundos promedio entre ráfagas. */\n  interval?: number;\n  /** Dispara una ráfaga al pasar el cursor por encima. */\n  onHover?: boolean;\n  /** Color del texto. */\n  color?: string;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\n/** Lee #rgb, #rrggbb o rgb()/rgba() sin tocar el DOM (seguro para SSR); si no entiende el color, usa blanco hueso. */\nfunction rgbDe(css: string): [number, number, number] {\n  const c = css.trim().toLowerCase();\n  let m = /^#([0-9a-f]{3})$/.exec(c);\n  if (m) return [...m[1]].map((h) => parseInt(h + h, 16)) as [number, number, number];\n  m = /^#([0-9a-f]{6})/.exec(c);\n  if (m) return [parseInt(m[1].slice(0, 2), 16), parseInt(m[1].slice(2, 4), 16), parseInt(m[1].slice(4, 6), 16)];\n  const r = /^rgba?\\(\\s*(\\d+)[,\\s]+(\\d+)[,\\s]+(\\d+)/.exec(c);\n  if (r) return [Number(r[1]), Number(r[2]), Number(r[3])];\n  return [244, 245, 247];\n}\n\nconst rnd = (a: number, b: number) => a + Math.random() * (b - a);\nconst CORTES = 3;\n\n/** Si el texto no cabe en su contenedor, encoge la letra lo justo (se recalcula al cambiar el tamaño del contenedor). */\nfunction useAjuste(ref: RefObject<HTMLElement | null>, variable: string, dep: unknown) {\n  useEffect(() => {\n    const el = ref.current, padre = el?.parentElement;\n    if (!el || !padre) return;\n    let raf = 0;\n    const ajustar = () => {\n      el.style.setProperty(variable, \"1\");\n      const natural = el.scrollWidth;\n      const libre = padre.clientWidth - 28;\n      el.style.setProperty(variable, (natural > libre && libre > 40 ? Math.max(0.35, libre / natural) : 1).toFixed(3));\n    };\n    const pedir = () => { cancelAnimationFrame(raf); raf = requestAnimationFrame(ajustar); };\n    const ro = new ResizeObserver(pedir);\n    ro.observe(padre);\n    document.fonts?.ready.then(pedir);\n    pedir();\n    return () => { cancelAnimationFrame(raf); ro.disconnect(); };\n  }, [dep]);\n}\n\n/** Texto limpio que cada tanto se parte en ráfagas cortas: canales RGB separados y franjas desplazadas. */\nexport function RGBGlitchText({\n  children = \"SIGNAL\",\n  size = 96,\n  intensity = 1,\n  interval = 2.8,\n  onHover = true,\n  color = \"#F4F5F7\",\n  className = \"\",\n  style,\n}: RGBGlitchTextProps) {\n  const rootRef = useRef<HTMLSpanElement>(null);\n  const texto = String(children ?? \"\");\n  useAjuste(rootRef, \"--furin-rgb-fit\", texto + size);\n  const cfg = useRef({ size, intensity, interval, onHover });\n  cfg.current = { size, intensity, interval, onHover };\n  const disparar = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const base = root.querySelector<HTMLElement>(\".furin-rgb-glitch__base\")!;\n    const R = root.querySelector<HTMLElement>(\".furin-rgb-glitch__r\")!;\n    const G = root.querySelector<HTMLElement>(\".furin-rgb-glitch__g\")!;\n    const B = root.querySelector<HTMLElement>(\".furin-rgb-glitch__b\")!;\n    const cortes = Array.from(root.querySelectorAll<HTMLElement>(\".furin-rgb-glitch__corte\"));\n    const reducido = matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    const limpiar = () => {\n      root.dataset.glitch = \"0\";\n      for (const e of [base, R, G, B]) { e.style.clipPath = \"\"; e.style.transform = \"\"; }\n      base.style.transform = \"\";\n      for (const c of cortes) { c.style.clipPath = \"inset(100% 0 0 0)\"; c.style.transform = \"\"; }\n    };\n    limpiar();\n    if (reducido) {\n      // Estado quieto: una separación fija y discreta, sin ráfagas.\n      const dx = cfg.current.size * 0.012;\n      root.dataset.glitch = \"1\";\n      R.style.transform = `translate(${-dx}px, 0)`; B.style.transform = `translate(${dx}px, 0)`;\n      return;\n    }\n\n    let timer = 0, visible = true, muerto = false, enRafaga = false, pendiente = false, ultimaRafaga = 0;\n\n    // Un cuadro de la ráfaga: canales desplazados + hasta tres franjas horizontales movidas.\n    const cuadro = (fuerza: number) => {\n      const { size: S, intensity: I } = cfg.current;\n      const f = fuerza * I;\n      const dx = S * 0.042 * f * rnd(0.5, 1.15);\n      root.dataset.glitch = \"1\";\n      R.style.transform = `translate(${(-dx).toFixed(1)}px, ${rnd(-0.6, 0.6).toFixed(1)}px)`;\n      B.style.transform = `translate(${(dx * rnd(0.7, 1.1)).toFixed(1)}px, ${rnd(-0.6, 0.6).toFixed(1)}px)`;\n      G.style.transform = `translate(${rnd(-0.12, 0.12) * dx}px, 0)`;\n      const n = Math.random() < 0.25 ? 0 : 1 + Math.floor(Math.random() * Math.min(CORTES, 1 + f * 1.5));\n      const bandas: [number, number][] = [];\n      for (let i = 0; i < n; i++) {\n        const top = rnd(14, 76);\n        const h = rnd(6, 20);\n        bandas.push([top, Math.min(100, top + h)]);\n      }\n      bandas.sort((a, b) => a[0] - b[0]);\n      // Sin traslapes: cada franja arranca después de que termina la anterior.\n      const ok: [number, number][] = [];\n      for (const b of bandas) { const prev = ok[ok.length - 1]; if (!prev || b[0] > prev[1] + 1) ok.push(b); }\n      cortes.forEach((c, i) => {\n        const b = ok[i];\n        if (!b) { c.style.clipPath = \"inset(100% 0 0 0)\"; return; }\n        c.style.clipPath = `inset(${b[0].toFixed(1)}% 0 ${(100 - b[1]).toFixed(1)}% 0)`;\n        const sh = (Math.random() < 0.5 ? -1 : 1) * S * rnd(0.04, 0.3) * Math.min(1.4, f);\n        c.style.transform = `translateX(${sh.toFixed(1)}px)`;\n      });\n      // La capa base pierde esas franjas para que el corte se vea limpio: un polígono \"peine\" que las deja fuera.\n      if (ok.length) {\n        const pts = [\"0 0\", \"100% 0\"];\n        for (const b of ok) pts.push(`100% ${b[0].toFixed(1)}%`, `0 ${b[0].toFixed(1)}%`, `0 ${b[1].toFixed(1)}%`, `100% ${b[1].toFixed(1)}%`);\n        pts.push(\"100% 100%\", \"0 100%\");\n        const poligono = `polygon(${pts.join(\", \")})`;\n        for (const e of [base, R, G, B]) e.style.clipPath = poligono;\n      } else for (const e of [base, R, G, B]) e.style.clipPath = \"\";\n    };\n\n    const rafaga = () => {\n      if (enRafaga || muerto) return;\n      enRafaga = true; ultimaRafaga = performance.now();\n      const cuadros = 5 + Math.floor(Math.random() * 6);\n      let k = 0;\n      const siguiente = () => {\n        if (muerto) return;\n        if (k >= cuadros) { limpiar(); enRafaga = false; programar(); return; }\n        // La fuerza sube, se sostiene con altibajos y se apaga; algún cuadro queda limpio.\n        const t = k / (cuadros - 1);\n        const envolvente = Math.sin(Math.PI * Math.min(1, t * 0.9 + 0.1)) ;\n        if (Math.random() < 0.18) limpiar(); else cuadro(0.35 + envolvente * rnd(0.55, 1.05));\n        k++;\n        timer = window.setTimeout(siguiente, rnd(34, 92));\n      };\n      siguiente();\n    };\n    disparar.current = () => {\n      if (!visible || enRafaga || performance.now() - ultimaRafaga < 700) return;\n      clearTimeout(timer); rafaga();\n    };\n\n    function programar() {\n      clearTimeout(timer);\n      if (muerto) return;\n      if (!visible) { pendiente = true; return; }\n      timer = window.setTimeout(rafaga, cfg.current.interval * 1000 * rnd(0.6, 1.5));\n    }\n    const io = new IntersectionObserver(([e]) => {\n      visible = e.isIntersecting;\n      if (visible && pendiente) { pendiente = false; programar(); }\n    }, { rootMargin: \"40px\" });\n    io.observe(root);\n    timer = window.setTimeout(rafaga, 650); // una primera ráfaga pronto, para que se entienda\n    return () => { muerto = true; clearTimeout(timer); io.disconnect(); disparar.current = () => {}; };\n  }, [texto]);\n\n  const [r, g, b] = rgbDe(color);\n  const vars = {\n    \"--furin-rgb-size\": `${size}px`,\n    \"--furin-rgb-color\": color,\n    \"--furin-rgb-r\": `rgb(${r}, 0, 0)`,\n    \"--furin-rgb-g\": `rgb(0, ${g}, 0)`,\n    \"--furin-rgb-b\": `rgb(0, 0, ${b})`,\n  } as CSSProperties;\n\n  return (\n    <span\n      ref={rootRef}\n      className={`furin-rgb-glitch ${className}`}\n      data-glitch=\"0\"\n      style={{ ...vars, ...style }}\n      onPointerEnter={() => { if (cfg.current.onHover) disparar.current(); }}\n      onPointerDown={() => disparar.current()}\n    >\n      <span className=\"furin-rgb-glitch__sr\">{texto}</span>\n      <span className=\"furin-rgb-glitch__capa furin-rgb-glitch__base\" aria-hidden=\"true\">{texto}</span>\n      <span className=\"furin-rgb-glitch__capa furin-rgb-glitch__canal furin-rgb-glitch__r\" aria-hidden=\"true\">{texto}</span>\n      <span className=\"furin-rgb-glitch__capa furin-rgb-glitch__canal furin-rgb-glitch__g\" aria-hidden=\"true\">{texto}</span>\n      <span className=\"furin-rgb-glitch__capa furin-rgb-glitch__canal furin-rgb-glitch__b\" aria-hidden=\"true\">{texto}</span>\n      {Array.from({ length: CORTES }, (_, i) => (\n        <span key={i} className=\"furin-rgb-glitch__capa furin-rgb-glitch__corte\" aria-hidden=\"true\">{texto}</span>\n      ))}\n    </span>\n  );\n}\n\nexport default RGBGlitchText;\n",
      "type": "registry:component",
      "target": "components/furin/rgb-glitch-text/RGBGlitchText.tsx"
    },
    {
      "path": "registry/rgb-glitch-text/RGBGlitchText.css",
      "content": ".furin-rgb-glitch {\n  position: relative;\n  display: inline-block;\n  max-width: 100%;\n  font-family: \"Geist\", \"Inter\", \"SF Pro Display\", system-ui, -apple-system, \"Helvetica Neue\", Arial, sans-serif;\n  font-size: calc(var(--furin-rgb-size) * var(--furin-rgb-fit, 1));\n  font-weight: 800;\n  line-height: 1.04;\n  letter-spacing: -0.035em;\n  color: var(--furin-rgb-color);\n  text-align: center;\n  white-space: pre;\n  isolation: isolate;\n  cursor: default;\n  -webkit-font-smoothing: antialiased;\n}\n.furin-rgb-glitch__capa { display: block; }\n/* La capa base es la única en el flujo; el resto se apila encima. */\n.furin-rgb-glitch__capa:not(.furin-rgb-glitch__base) { position: absolute; inset: 0; pointer-events: none; user-select: none; }\n/* Los canales se suman con \"screen\": en reposo valen lo mismo que el texto base y se ocultan. */\n.furin-rgb-glitch__canal { mix-blend-mode: screen; opacity: 0; }\n.furin-rgb-glitch__r { color: var(--furin-rgb-r); }\n.furin-rgb-glitch__g { color: var(--furin-rgb-g); }\n.furin-rgb-glitch__b { color: var(--furin-rgb-b); }\n.furin-rgb-glitch[data-glitch=\"1\"] .furin-rgb-glitch__canal { opacity: 1; }\n/* Con los canales activos, la base solo aporta el cuerpo que quede entre ellos. */\n.furin-rgb-glitch[data-glitch=\"1\"] .furin-rgb-glitch__base { color: transparent; }\n.furin-rgb-glitch__corte { clip-path: inset(100% 0 0 0); text-shadow: -0.045em 0 rgba(255, 30, 70, 0.9), 0.045em 0 rgba(30, 220, 255, 0.9); }\n.furin-rgb-glitch__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }\n",
      "type": "registry:file",
      "target": "components/furin/rgb-glitch-text/RGBGlitchText.css"
    }
  ]
}
