{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"slit-light-heading","type":"registry:component","title":"Slit Light Heading","description":"A heading lit by a narrow slit of light that crosses it as it scrolls into view: letters under the slit burn white, letters it has passed stay lit, letters ahead wait in the dark.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/slit-light-heading.tsx","content":"\"use client\";\n\nimport { createContext, useContext, useEffect, useRef, useSyncExternalStore } from \"react\";\nimport type { CSSProperties, ReactNode, RefObject } from \"react\";\n\n/**\n * SlitLightHeading — a heading lit by a slit of light that follows scroll.\n *\n * As the heading scrolls up the viewport a narrow slit of light crosses\n * it from left to right: letters under the slit burn white with a glow in\n * the accent, letters it has passed stay lit, letters ahead wait in the\n * dark. Pass scrollRoot when the heading sits in its own scroller.\n * Reduced motion shows it fully lit.\n *\n * Part of the Dark Precision kit: OLED black, 1px hairlines, one cool accent\n * used only as light (default #4fd1ff; also mint, amber, white or any\n * #rrggbb). Fonts come from CSS variables with Geist fallbacks. Respects\n * prefers-reduced-motion. No dependencies beyond React.\n */\n\nconst INK = \"rgba(255,255,255,0.92)\";\n\nconst HEX = /^#[0-9a-fA-F]{6}$/;\n\nexport const DP_ACCENTS = {\n  ice: \"#4fd1ff\",\n  mint: \"#5ef2c1\",\n  amber: \"#ffb45e\",\n  white: \"#f2f4f7\",\n} as const;\n\nfunction own<T extends object>(map: T, key: string): key is Extract<keyof T, string> {\n  return Object.prototype.hasOwnProperty.call(map, key);\n}\n\n/** A named accent or a #rrggbb hex; anything else falls back to ice. */\nfunction accentHex(value: string): string {\n  if (own(DP_ACCENTS, value)) return DP_ACCENTS[value];\n  return HEX.test(value) ? value : DP_ACCENTS.ice;\n}\n\nfunction rgbOf(hex: string): [number, number, number] {\n  const n = parseInt(hex.slice(1), 16);\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction rgba(hex: string, a: number): string {\n  const [r, g, b] = rgbOf(hex);\n  return `rgba(${r},${g},${b},${a})`;\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\n/** True when the visitor asked for reduced motion. False on the server. */\nfunction useReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/** The element that scrolls: null for the window, or the site itself in \"self\" mode. */\nconst ScrollRootContext = createContext<RefObject<HTMLElement | null> | null>(null);\n\nexport type SlitLightHeadingProps = {\n  children: ReactNode;\n  accent?: string;\n  as?: \"h1\" | \"h2\" | \"h3\";\n  id?: string;\n  className?: string;\n  style?: CSSProperties;\n  /** The element that scrolls, if not the window. */\n  scrollRoot?: RefObject<HTMLElement | null>;\n};\n\n/**\n * A heading lit by a narrow slit of light that follows scroll. Letters the\n * slit has passed stay lit; letters ahead of it wait in the dark.\n */\nexport function SlitLightHeading({\n  children,\n  accent = \"ice\",\n  as: Tag = \"h2\",\n  id,\n  className,\n  style,\n  scrollRoot,\n}: SlitLightHeadingProps) {\n  const acc = accentHex(accent);\n  const siteRoot = useContext(ScrollRootContext);\n  const rootRef = scrollRoot ?? siteRoot;\n  const ref = useRef<HTMLHeadingElement>(null);\n  const reduced = useReducedMotion();\n  useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const root = rootRef?.current ?? null;\n    const target: HTMLElement | Window = root ?? window;\n    let raf = 0;\n    const update = () => {\n      raf = 0;\n      let top: number;\n      let vh: number;\n      const r = el.getBoundingClientRect();\n      if (root) {\n        const rr = root.getBoundingClientRect();\n        top = r.top - rr.top;\n        vh = rr.height;\n      } else {\n        top = r.top;\n        vh = window.innerHeight;\n      }\n      const k = reduced ? 1 : Math.min(1, Math.max(0, (vh * 0.88 - top) / (vh * 0.5)));\n      const p = -12 + k * 124;\n      el.style.setProperty(\"--p\", p.toFixed(2) + \"%\");\n      el.style.setProperty(\"--o\", Math.min(1, Math.max(0, Math.min(p, 100 - p) / 10)).toFixed(3));\n    };\n    const onScroll = () => {\n      if (!raf) raf = requestAnimationFrame(update);\n    };\n    update();\n    target.addEventListener(\"scroll\", onScroll, { passive: true });\n    window.addEventListener(\"resize\", onScroll);\n    return () => {\n      cancelAnimationFrame(raf);\n      target.removeEventListener(\"scroll\", onScroll);\n      window.removeEventListener(\"resize\", onScroll);\n    };\n  }, [rootRef, reduced]);\n  const layer: CSSProperties = { position: \"absolute\", inset: 0, display: \"block\" };\n  return (\n    <Tag ref={ref} id={id} className={\"relative\" + (className ? \" \" + className : \"\")} style={{ ...style }}>\n      <span className=\"block\" style={{ color: \"rgba(255,255,255,0.26)\" }}>\n        {children}\n      </span>\n      <span\n        aria-hidden\n        style={{\n          ...layer,\n          color: INK,\n          WebkitMaskImage: \"linear-gradient(90deg, #000 calc(var(--p, 112%) - 7%), transparent calc(var(--p, 112%) + 1%))\",\n          maskImage: \"linear-gradient(90deg, #000 calc(var(--p, 112%) - 7%), transparent calc(var(--p, 112%) + 1%))\",\n        }}\n      >\n        {children}\n      </span>\n      <span\n        aria-hidden\n        style={{\n          ...layer,\n          color: \"#ffffff\",\n          textShadow: `0 0 18px ${rgba(acc, 0.7)}, 0 0 2px rgba(255,255,255,0.8)`,\n          WebkitMaskImage: \"linear-gradient(90deg, transparent calc(var(--p, 112%) - 3.5%), #000 var(--p, 112%), transparent calc(var(--p, 112%) + 3.5%))\",\n          maskImage: \"linear-gradient(90deg, transparent calc(var(--p, 112%) - 3.5%), #000 var(--p, 112%), transparent calc(var(--p, 112%) + 3.5%))\",\n        }}\n      >\n        {children}\n      </span>\n      <span\n        aria-hidden\n        className=\"pointer-events-none absolute\"\n        style={{\n          top: \"-14%\",\n          bottom: \"-14%\",\n          left: \"var(--p, 112%)\",\n          width: 1,\n          background: `linear-gradient(180deg, transparent, ${rgba(acc, 0.9)} 30%, #ffffff 50%, ${rgba(acc, 0.9)} 70%, transparent)`,\n          boxShadow: `0 0 14px 1px ${rgba(acc, 0.55)}`,\n          opacity: \"var(--o, 0)\",\n        }}\n      />\n    </Tag>\n  );\n}\n","type":"registry:component","target":"components/viberdy/slit-light-heading.tsx"}],"meta":{"tier":"free","tags":["text effect","heading","scroll","reveal","light","mask","dark","scroll-driven"],"href":"https://viberdy.dev/library/slit-light-heading","aiPrompt":"Build a React text effect, `SlitLightHeading({ children, as, accent, scrollRoot, className, style })`, that stacks three copies of a heading.\n\nThe base copy (in flow, readable) is white at 26%. An aria-hidden trail copy in white 92% is masked with linear-gradient(90deg, #000 calc(var(--p) - 7%), transparent calc(var(--p) + 1%)). An aria-hidden slit copy in pure white with an accent text-shadow (0 0 18px) is masked to a narrow band: transparent at var(--p) - 3.5%, black at var(--p), transparent at var(--p) + 3.5%. A 1px absolutely positioned line at left: var(--p), extending 14% above and below, carries an accent-to-white vertical gradient and a glow, with opacity var(--o) so it fades out near either end.\n\nOn scroll (rAF-throttled) and resize, compute k = (0.88 * viewportHeight - top) / (0.5 * viewportHeight) clamped to 0-1, using the scroll root's rect when given (or a site-wide context), else the window; set --p = -12% + k * 124% and --o from the distance to either end. Reduced motion sets k to 1.\n\nDark Precision grammar: ground #050505, surfaces #0b0b0d and #121215, hairlines white 6% at rest and 14% lit (drawn as inset box-shadows or SVG strokes, never border colours), ink white 92/60/38%, one cool accent used only as light (default #4fd1ff; mint #5ef2c1, amber #ffb45e, white #f2f4f7 or any #rrggbb, validated against /^#[0-9a-fA-F]{6}$/), Geist never heavier than 600 with tight tracking, Geist Mono only for captions and data, fonts through CSS variables with fallbacks (var(--font-sans, \"Geist\", ...)). Morphs use cubic-bezier(0.16, 1, 0.3, 1); ambient loops are 30s or slower. Respect prefers-reduced-motion. No dependencies beyond React.","license":"https://viberdy.dev/license"}}