{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-reveal",
  "title": "Text reveal",
  "description": "Reveals a heading word by word or letter by letter with a staggered blur, fade and rise when it scrolls into view or on demand.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/text/text-reveal.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useInView, useReducedMotion, type Variants } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { GooduiTokens, ease } from \"@/lib/goodui-motion\"\n\n/*\n * mask and sweep are plain CSS transitions keyed off data-shown, so they interrupt cleanly and need no JS per frame.\n * After layout each word gets its line number (--l) and its place on the line (--n): whole lines rise together.\n */\nconst CSS = `\n.pc-reveal-clip{display:inline-block;overflow:clip;padding-block:.16em;margin-block:-.16em;vertical-align:baseline}\n.pc-reveal-u{display:inline-block}\n.pc-reveal[data-effect=mask] .pc-reveal-u{transform:translateY(118%);transition:transform 180ms ease}\n.pc-reveal[data-effect=mask][data-shown] .pc-reveal-u{transform:none;transition:transform var(--pc-r-dur) var(--pc-ease-out-expo) calc(var(--pc-r-delay) + var(--l,0)*.1s + (var(--n,0) + var(--c,0))*var(--pc-r-step))}\n.pc-reveal[data-effect=sweep] .pc-reveal-u{--pc-r-dir:90deg;color:transparent;padding-block:.14em;margin-block:-.14em;background:linear-gradient(var(--pc-r-dir),currentcolor 0,var(--pc-r-ink) 0 44%,var(--chart-1) 48%,var(--chart-4) 52%,var(--chart-5) 56%,var(--pc-r-dim) 62% 100%) 100% 0/320% 100% no-repeat;-webkit-background-clip:text;background-clip:text;transition:background-position 180ms ease}\n.pc-reveal[data-effect=sweep]{--pc-r-ink:var(--foreground);--pc-r-dim:color-mix(in oklab,var(--foreground) 16%,transparent)}\n.pc-reveal[data-effect=sweep][data-shown] .pc-reveal-u{background-position:0 0;transition:background-position var(--pc-r-dur) linear calc(var(--pc-r-delay) + (var(--i,0) + var(--c,0))*var(--pc-r-step))}\n.pc-reveal[data-effect=sweep] .pc-reveal-u:dir(rtl){--pc-r-dir:270deg;background-position:0 0}\n.pc-reveal[data-effect=sweep][data-shown] .pc-reveal-u:dir(rtl){background-position:100% 0}\n@media (prefers-reduced-motion:reduce){\n  .pc-reveal .pc-reveal-u,.pc-reveal[data-effect][data-shown] .pc-reveal-u{transform:none!important;background:none!important;color:inherit;opacity:0;transition:opacity 200ms ease}\n  .pc-reveal[data-effect][data-shown] .pc-reveal-u{opacity:1}\n}\n`\n\ntype RevealTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"p\" | \"span\" | \"div\"\n\nexport type TextRevealProps = Omit<React.HTMLAttributes<HTMLElement>, \"children\"> & {\n  /** The text to reveal; a plain string so it can be split (screen readers get it whole). */\n  text: string\n  /** mask: lines rise out of a clip. sweep: a color band passes across dim text. blur: the older blur, fade and rise. */\n  effect?: \"mask\" | \"sweep\" | \"blur\"\n  /** Animate word by word or character by character. */\n  by?: \"word\" | \"char\"\n  /** Element to render, e.g. \"h1\" for a page title. */\n  as?: RevealTag\n  /** \"view\" plays when the text scrolls into view, \"mount\" plays right away. Ignored when `play` is set. */\n  trigger?: \"view\" | \"mount\"\n  /** Controlled mode: true reveals, false hides. Leave undefined to use `trigger`. */\n  play?: boolean\n  /** With trigger=\"view\": play only the first time it enters the viewport. */\n  once?: boolean\n  /** Seconds before the first word or character. */\n  delay?: number\n  /** Seconds between two words or characters. Defaults depend on `effect`. */\n  stagger?: number\n  /** Seconds per unit. */\n  duration?: number\n  /** Starting blur in px for effect=\"blur\". Passing it without `effect` selects blur. */\n  blur?: number\n  /** Called when the last word or character has finished revealing. */\n  onComplete?: () => void\n}\n\n// Arabic-script letters join, so splitting them into separate boxes breaks their shapes: char mode falls back to words.\nconst JOINING = /[؀-ۿݐ-ݿࢠ-ࣿﭐ-﷿ﹰ-﻿܀-ݏ߀-߿]/\n\n/** Splits into user-perceived characters (keeps emoji and combining marks together). */\nfunction graphemes(s: string) {\n  // Typed by hand so it compiles under lib targets older than ES2022 (Vite's default is ES2020).\n  const Segmenter = (Intl as unknown as { Segmenter?: new (l: undefined, o: { granularity: \"grapheme\" }) => { segment(s: string): Iterable<{ segment: string }> } }).Segmenter\n  if (Segmenter) return Array.from(new Segmenter(undefined, { granularity: \"grapheme\" }).segment(s), (g) => g.segment)\n  return Array.from(s)\n}\n\n/** Reveals text with a line-masked rise, a color sweep or a blur. Screen readers get the plain text. */\nexport function TextReveal({\n  text,\n  effect: effectProp,\n  by = \"word\",\n  as = \"p\",\n  trigger = \"view\",\n  play,\n  once = true,\n  delay = 0,\n  stagger,\n  duration: durationProp,\n  blur: blurProp,\n  onComplete,\n  className,\n  ...props\n}: TextRevealProps) {\n  const reduce = useReducedMotion() ?? false\n  const ref = React.useRef<HTMLElement>(null)\n  const inView = useInView(ref, { once, amount: 0.35 })\n  const shown = play ?? (trigger === \"mount\" ? true : inView)\n  // CSS transitions need a hidden frame first, or a mount-triggered reveal would simply appear.\n  const [armed, setArmed] = React.useState(false)\n  React.useEffect(() => {\n    let inner = 0\n    const outer = requestAnimationFrame(() => {\n      inner = requestAnimationFrame(() => setArmed(true))\n    })\n    return () => {\n      cancelAnimationFrame(outer)\n      cancelAnimationFrame(inner)\n    }\n  }, [])\n  const unitBy = by === \"char\" && JOINING.test(text) ? \"word\" : by\n  const effect = effectProp ?? (blurProp === undefined ? \"mask\" : \"blur\")\n  const blur = blurProp ?? 8\n  const duration = durationProp ?? (effect === \"mask\" ? 1 : effect === \"sweep\" ? 0.7 : 0.5)\n  const css = effect !== \"blur\"\n  const step = stagger ?? (effect === \"mask\" ? (unitBy === \"word\" ? 0.02 : 0.012) : effect === \"sweep\" ? (unitBy === \"word\" ? 0.075 : 0.016) : unitBy === \"word\" ? 0.06 : 0.018)\n\n  // Group words into visual lines. Runs before paint and again when the width changes the wrapping.\n  React.useLayoutEffect(() => {\n    const root = ref.current\n    if (!css || !root) return\n    const measure = () => {\n      let top: number | null = null\n      let line = -1\n      let n = 0\n      root.querySelectorAll<HTMLElement>(\"[data-word]\").forEach((w) => {\n        if (top === null || Math.abs(w.offsetTop - top) > 4) {\n          top = w.offsetTop\n          line++\n          n = 0\n        }\n        w.style.setProperty(\"--l\", String(line))\n        w.style.setProperty(\"--n\", String(n++))\n      })\n    }\n    measure()\n    const ro = new ResizeObserver(measure)\n    ro.observe(root)\n    return () => ro.disconnect()\n  }, [css, text, unitBy])\n\n  const variants: Variants = {\n    hidden: reduce\n      ? { opacity: 0, transition: { duration: 0.15 } }\n      : { opacity: 0, y: \"0.35em\", filter: `blur(${blur}px)`, transition: { duration: 0.2 } },\n    visible: (i: number) => ({\n      opacity: 1,\n      y: 0,\n      filter: \"blur(0px)\",\n      transition: reduce ? { duration: 0.2, delay } : { delay: delay + i * step, duration, ease: ease.out },\n    }),\n  }\n\n  // Split into words (kept whole so lines never break inside a word), then optionally into characters.\n  const tokens = text.split(/(\\s+)/).filter(Boolean)\n  const total = unitBy === \"word\" ? tokens.filter((t) => !/^\\s+$/.test(t)).length : tokens.reduce((n, t) => (/^\\s+$/.test(t) ? n : n + graphemes(t).length), 0)\n  let index = 0\n\n  const unit = (content: string, i: number, charIndex?: number) =>\n    css ? (\n      <span key={i} className=\"pc-reveal-u\" style={charIndex === undefined ? undefined : ({ \"--c\": charIndex } as React.CSSProperties)}>\n        {content}\n      </span>\n    ) : (\n    <motion.span\n      key={i}\n      custom={i}\n      variants={variants}\n      initial=\"hidden\"\n      animate={shown ? \"visible\" : \"hidden\"}\n      onAnimationComplete={i === total - 1 ? (def) => def === \"visible\" && onComplete?.() : undefined}\n      className=\"inline-block\"\n    >\n      {content}\n    </motion.span>\n    )\n\n  let wordIndex = 0\n  const children = tokens.map((token, ti) => {\n    if (/^\\s+$/.test(token)) return \" \"\n    return (\n      <span\n        key={ti}\n        data-word=\"\"\n        className={cn(\"inline-block whitespace-nowrap\", effect === \"mask\" && \"pc-reveal-clip\")}\n        style={css ? ({ \"--i\": wordIndex++ } as React.CSSProperties) : undefined}\n      >\n        {unitBy === \"word\" ? unit(token, index++) : graphemes(token).map((ch, c) => unit(ch, index++, c))}\n      </span>\n    )\n  })\n\n  return React.createElement(\n    as,\n    {\n      ref,\n      className: cn(css && \"pc-reveal\", className),\n      \"data-effect\": effect,\n      \"data-shown\": css && shown && armed ? \"\" : undefined,\n      ...props,\n      style: { ...props.style, \"--pc-r-dur\": `${duration}s`, \"--pc-r-delay\": `${delay}s`, \"--pc-r-step\": `${step}s` } as React.CSSProperties,\n      onTransitionEnd: (e: React.TransitionEvent<HTMLElement>) => {\n        props.onTransitionEnd?.(e)\n        const units = ref.current?.querySelectorAll(\".pc-reveal-u\")\n        if (css && shown && units && e.target === units[units.length - 1]) onComplete?.()\n      },\n    },\n    <GooduiTokens key=\"tokens\" />,\n    <style key=\"css\" href=\"pc-text-reveal\" precedence=\"default\">\n      {CSS}\n    </style>,\n    <span key=\"sr\" className=\"sr-only\">{text}</span>,\n    <span key=\"t\" aria-hidden=\"true\">{children}</span>,\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/text-reveal.tsx"
    }
  ],
  "categories": [
    "components",
    "text"
  ],
  "type": "registry:component"
}