{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-reveal",
  "title": "Text Reveal",
  "description": "Reveals text word by word, when it comes into view or tied to the scroll position.",
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/use-in-view"
  ],
  "files": [
    {
      "path": "registry/default/ui/text-reveal.tsx",
      "content": "\"use client\";\n\nimport {\n  Fragment,\n  useId,\n  useMemo,\n  useRef,\n  useState,\n  type ComponentProps,\n  type CSSProperties,\n  type Ref,\n} from \"react\";\n\nimport { useInView } from \"@/registry/default/hooks/use-in-view\";\nimport { cn } from \"@/registry/default/lib/utils\";\n\nconst EASE_OUT = \"cubic-bezier(0.22, 1, 0.36, 1)\";\n\nfunction prefersReducedMotion() {\n  return (\n    typeof window !== \"undefined\" &&\n    window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches === true\n  );\n}\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === \"function\") ref(node);\n      else if (ref) ref.current = node;\n    }\n  };\n}\n\ntype TextRevealTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\" | \"p\" | \"span\" | \"div\";\n\ninterface TextRevealProps extends Omit<ComponentProps<\"p\">, \"children\"> {\n  /** Plain text; it is split into words. */\n  children: string;\n  as?: TextRevealTag;\n  /**\n   * \"in-view\" brings the words in one after another once the text scrolls\n   * into view. \"scroll\" ties each word to the scroll position instead, with\n   * CSS scroll-driven animations and no JavaScript; browsers without them\n   * show the text as is. Scroll follows the nearest scroll container: clip\n   * ancestors with `overflow-clip`, as `overflow-hidden` makes one that never\n   * scrolls.\n   */\n  trigger?: \"in-view\" | \"scroll\";\n  /** In-view: delay between words, in ms. */\n  stagger?: number;\n  /** In-view: how long each word takes, in ms. */\n  duration?: number;\n  /** Scroll: opacity of the words not reached yet, from 0 to 1. */\n  dim?: number;\n  /**\n   * Scroll: where the reveal starts and ends, as percentages of the text\n   * crossing the viewport (0 is entering at the bottom, 100 is leaving at the top).\n   */\n  range?: [start: number, end: number];\n}\n\n/**\n * Reveals a line of text word by word. Screen readers get the sentence in\n * one piece: the animated words are hidden from them. With\n * `prefers-reduced-motion` the text is shown as is.\n */\nfunction TextReveal({\n  children,\n  as: Tag = \"p\",\n  trigger = \"in-view\",\n  stagger = 40,\n  duration = 500,\n  dim = 0.15,\n  range = [10, 55],\n  className,\n  style,\n  ref,\n  ...props\n}: TextRevealProps) {\n  const words = children.trim().split(/\\s+/).filter(Boolean);\n  const timeline = `--text-reveal-${useId().replace(/[^\\w-]/g, \"\")}`;\n  const [phase, setPhase] = useState<\"idle\" | \"hidden\" | \"shown\">(\"idle\");\n  const answered = useRef(false);\n  const [observe] = useInView<HTMLElement>({\n    once: true,\n    amount: 0.4,\n    onChange: (inView) => {\n      const first = !answered.current;\n      answered.current = true;\n      if (prefersReducedMotion()) return;\n      if (first && !inView) setPhase(\"hidden\");\n      else if (!first && inView) setPhase(\"shown\");\n    },\n  });\n  const mergedRef = useMemo(\n    () => mergeRefs(ref, trigger === \"in-view\" ? observe : undefined),\n    [ref, observe, trigger]\n  );\n\n  const [start, end] = range;\n  const wordStyle = (index: number): CSSProperties => {\n    if (trigger === \"scroll\") {\n      const from = start + ((end - start) * index) / words.length;\n      const to = start + ((end - start) * (index + 1)) / words.length;\n      return { animationTimeline: timeline, animationRange: `cover ${from}% cover ${to}%` };\n    }\n    if (phase === \"hidden\") return { opacity: 0, transform: \"translate3d(0, 0.35em, 0)\" };\n    if (phase === \"shown\") {\n      const wait = index * stagger;\n      return {\n        transition: `opacity ${duration}ms ${EASE_OUT} ${wait}ms, transform ${duration}ms ${EASE_OUT} ${wait}ms`,\n      };\n    }\n    return {};\n  };\n\n  return (\n    <Tag\n      ref={mergedRef as Ref<never>}\n      data-slot=\"text-reveal\"\n      data-trigger={trigger}\n      data-state={phase === \"hidden\" ? \"hidden\" : \"visible\"}\n      className={className}\n      style={\n        {\n          ...(trigger === \"scroll\"\n            ? { viewTimelineName: timeline, \"--text-reveal-dim\": String(dim) }\n            : null),\n          ...style,\n        } as CSSProperties\n      }\n      {...props}\n    >\n      <span className=\"sr-only\">{children}</span>\n      <span aria-hidden=\"true\">\n        {words.map((word, index) => (\n          <Fragment key={index}>\n            {index > 0 ? \" \" : null}\n            <span\n              data-slot=\"text-reveal-word\"\n              className={cn(\n                \"inline-block\",\n                trigger === \"scroll\" &&\n                  \"supports-[animation-timeline:view()]:animate-text-reveal motion-reduce:!animate-none\"\n              )}\n              style={wordStyle(index)}\n            >\n              {word}\n            </span>\n          </Fragment>\n        ))}\n      </span>\n    </Tag>\n  );\n}\n\nexport { TextReveal, type TextRevealProps };\n",
      "type": "registry:ui"
    }
  ],
  "tailwind": {
    "config": {
      "theme": {
        "extend": {
          "keyframes": {
            "text-reveal": {
              "from": {
                "opacity": "var(--text-reveal-dim, 0.15)"
              },
              "to": {
                "opacity": "1"
              }
            }
          },
          "animation": {
            "text-reveal": "text-reveal linear both"
          }
        }
      }
    }
  },
  "cssVars": {
    "theme": {
      "animate-text-reveal": "text-reveal linear both"
    }
  },
  "css": {
    "@keyframes text-reveal": {
      "from": {
        "opacity": "var(--text-reveal-dim, 0.15)"
      },
      "to": {
        "opacity": "1"
      }
    }
  },
  "categories": [
    "motion",
    "text"
  ],
  "type": "registry:ui"
}