{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-reveal-mask",
  "title": "Text Reveal (Mask)",
  "description": "A scroll-triggered masked slide-up text reveal animation, powered by Motion.",
  "dependencies": ["motion"],
  "registryDependencies": ["utils"],
  "files": [
    {
      "path": "registry/primitives/texts/text-reveal-mask/index.tsx",
      "content": "/* oxlint-disable react/no-array-index-key, react-doctor/no-array-index-as-key -- Split indices are stable for static text. */\n\n\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport { motion, useInView, useReducedMotion } from \"motion/react\";\nimport type { UseInViewOptions } from \"motion/react\";\nimport {\n  isValidElement,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n  useState,\n} from \"react\";\nimport type { ElementType, ReactNode, RefObject } from \"react\";\n\ntype SplitBy = \"lines\" | \"words\" | \"chars\";\n\nconst SPLIT_DEFAULTS: Record<SplitBy, { duration: number; stagger: number }> = {\n  lines: { duration: 0.8, stagger: 0.08 },\n  words: { duration: 0.6, stagger: 0.06 },\n  chars: { duration: 0.4, stagger: 0.01 },\n};\n\nconst EXPO_OUT = [0.19, 1, 0.22, 1] as const;\n\nconst WHITESPACE_RE = /\\s+/;\n\ninterface WordToken {\n  emphasized: boolean;\n  text: string;\n}\n\ninterface LineCharUnit {\n  char: string;\n  emphasized: boolean;\n  staggerIndex: number;\n}\n\nexport interface MaskedTextRevealProps {\n  /**\n   * HTML tag for the container element.\n   * @default \"p\"\n   */\n  as?: keyof React.JSX.IntrinsicElements;\n  /** Rich text content. Supports inline emphasis elements such as `<strong>`. */\n  children?: ReactNode;\n  className?: string;\n  /**\n   * Delay before the first unit starts animating, in seconds.\n   * @default 0\n   */\n  delay?: number;\n  /**\n   * Duration of each unit's animation in seconds.\n   */\n  duration?: number;\n  /**\n   * Trigger the animation only once.\n   * @default true\n   */\n  once?: boolean;\n  /**\n   * Split mode: line-by-line, word-by-word, or character-by-character.\n   * @default \"lines\"\n   */\n  splitBy?: SplitBy;\n  /**\n   * Delay between each successive unit in seconds.\n   */\n  stagger?: number;\n  /** Plain-text alternative to `children`. */\n  text?: string;\n  /**\n   * Additional className applied to each animated unit.\n   */\n  unitClassName?: string;\n  /**\n   * Viewport margin that controls when the animation triggers.\n   * Triggers when the element reaches the top 75% of the viewport by default.\n   * @default \"0px 0px -25% 0px\"\n   */\n  viewportMargin?: UseInViewOptions[\"margin\"];\n  /**\n   * Initial vertical offset as a percentage of the unit height.\n   * @default 110\n   */\n  yPercent?: number;\n}\n\nfunction collectWords(node: ReactNode, emphasized = false): WordToken[] {\n  if (typeof node === \"string\") {\n    return node\n      .split(WHITESPACE_RE)\n      .filter(Boolean)\n      .map((text) => ({ text, emphasized }));\n  }\n\n  if (Array.isArray(node)) {\n    return node.flatMap((child) => collectWords(child, emphasized));\n  }\n\n  if (isValidElement(node)) {\n    const nextEmphasis =\n      emphasized || node.type === \"strong\" || node.type === \"b\";\n    return collectWords(\n      (node.props as { children?: ReactNode }).children,\n      nextEmphasis\n    );\n  }\n\n  return [];\n}\n\nfunction getContentNode(\n  text: string | undefined,\n  children: ReactNode\n): ReactNode {\n  if (text) {\n    return text;\n  }\n\n  return children;\n}\n\nfunction getAccessibleLabel(words: WordToken[]): string {\n  return words.map((word) => word.text).join(\" \");\n}\n\ntype LineGroup = number[];\n\nfunction groupWordsByLine(measureNode: HTMLDivElement): LineGroup[] {\n  const measureWords = measureNode.querySelectorAll(\"[data-measure-word]\");\n  if (measureWords.length === 0) {\n    return [[0]];\n  }\n\n  const groups: LineGroup[] = [];\n  let currentGroup: LineGroup = [];\n  let lastTop = -1;\n\n  for (const [index, node] of measureWords.entries()) {\n    const top = (node as HTMLElement).offsetTop;\n    if (lastTop !== -1 && top > lastTop + 1) {\n      groups.push(currentGroup);\n      currentGroup = [];\n    }\n    currentGroup.push(index);\n    lastTop = top;\n  }\n\n  if (currentGroup.length > 0) {\n    groups.push(currentGroup);\n  }\n\n  return groups.length > 0 ? groups : [[0]];\n}\n\nfunction resolveContainerWidth(container: HTMLElement): number {\n  if (container.clientWidth > 0) {\n    return container.clientWidth;\n  }\n\n  const parent = container.parentElement;\n  if (parent && parent.clientWidth > 0) {\n    return parent.clientWidth;\n  }\n\n  return 0;\n}\n\nfunction useLineGroups(\n  words: WordToken[],\n  className: string | undefined,\n  containerRef: RefObject<HTMLDivElement | null>\n) {\n  const measureRef = useRef<HTMLDivElement>(null);\n  const [lineGroups, setLineGroups] = useState<LineGroup[] | null>(null);\n\n  const measurementKey = `${className ?? \"\"}:${words\n    .map((word) => `${word.emphasized ? \"1\" : \"0\"}:${word.text}`)\n    .join(\"\\u0000\")}`;\n\n  // oxlint-disable-next-line react-hooks/exhaustive-deps -- Remeasure when text, width, or typography changes.\n  useLayoutEffect(() => {\n    const container = containerRef.current;\n    const measureNode = measureRef.current;\n    if (!(container && measureNode)) {\n      return;\n    }\n\n    let cancelled = false;\n\n    const measureLines = () => {\n      if (cancelled) {\n        return;\n      }\n\n      const width = resolveContainerWidth(container);\n      if (width > 0) {\n        measureNode.style.width = `${width}px`;\n      }\n\n      setLineGroups(groupWordsByLine(measureNode));\n    };\n\n    measureLines();\n    document.fonts.ready.then(measureLines);\n\n    const resizeObserver = new ResizeObserver(measureLines);\n    resizeObserver.observe(container);\n\n    return () => {\n      cancelled = true;\n      resizeObserver.disconnect();\n    };\n  }, [containerRef, measurementKey]);\n\n  const measureLayer = (\n    <div\n      aria-hidden\n      className={cn(\n        \"pointer-events-none invisible fixed top-0 left-[-9999px] block [overflow-wrap:break-word] [word-wrap:break-word]\",\n        className\n      )}\n      ref={measureRef}\n    >\n      {words.map((word, index) => (\n        <span\n          className={cn(\n            \"split-word inline-block [backface-visibility:hidden]\",\n            index < words.length - 1 && \"me-[0.25em]\",\n            word.emphasized && \"text-foreground font-medium\"\n          )}\n          data-measure-word\n          key={index}\n        >\n          {word.text}\n        </span>\n      ))}\n    </div>\n  );\n\n  return { lineGroups, measureLayer };\n}\n\nfunction renderWordContent(word: WordToken, unitClassName?: string) {\n  if (word.emphasized) {\n    return <strong className={unitClassName}>{word.text}</strong>;\n  }\n\n  return word.text;\n}\n\nfunction SplitLine({\n  children,\n  className,\n}: {\n  children: ReactNode;\n  className?: string;\n}) {\n  return (\n    <span\n      className={cn(\n        \"split-line block overflow-hidden [backface-visibility:hidden]\",\n        className\n      )}\n    >\n      {children}\n    </span>\n  );\n}\n\nfunction RevealTarget({\n  animate,\n  children,\n  className,\n  delay: unitDelay,\n  display = \"inline-block\",\n  duration: unitDuration,\n  maskClassName,\n  masked = false,\n  yPercent,\n}: {\n  animate: boolean;\n  children: ReactNode;\n  className?: string;\n  delay: number;\n  display?: \"block\" | \"inline-block\";\n  duration: number;\n  maskClassName?: string;\n  masked?: boolean;\n  yPercent: number;\n}) {\n  const hiddenY = `${yPercent}%`;\n\n  const target = (\n    <motion.span\n      animate={animate ? { y: \"0%\" } : { y: hiddenY }}\n      className={cn(\n        \"will-change-transform [backface-visibility:hidden]\",\n        display === \"block\" ? \"block\" : \"inline-block\",\n        className\n      )}\n      initial={{ y: hiddenY }}\n      transition={{\n        duration: unitDuration,\n        delay: unitDelay,\n        ease: EXPO_OUT,\n      }}\n    >\n      {children}\n    </motion.span>\n  );\n\n  if (!masked) {\n    return target;\n  }\n\n  return (\n    <span\n      className={cn(\"inline-block overflow-hidden align-bottom\", maskClassName)}\n    >\n      {target}\n    </span>\n  );\n}\n\nfunction WordSpan({\n  isLastWord,\n  word,\n  unitClassName,\n}: {\n  isLastWord: boolean;\n  word: WordToken;\n  unitClassName?: string;\n}) {\n  return (\n    <span\n      className={cn(\n        \"split-word inline-block [backface-visibility:hidden]\",\n        !isLastWord && \"me-[0.25em]\"\n      )}\n    >\n      {renderWordContent(word, unitClassName)}\n    </span>\n  );\n}\n\nexport function MaskedTextReveal({\n  text,\n  children,\n  as: Tag = \"p\",\n  splitBy = \"lines\",\n  duration,\n  stagger,\n  yPercent = 110,\n  delay = 0,\n  once = true,\n  viewportMargin = \"0px 0px -25% 0px\",\n  className,\n  unitClassName,\n}: MaskedTextRevealProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const prefersReducedMotion = useReducedMotion();\n  const isInView = useInView(rootRef, { once, margin: viewportMargin });\n\n  const content = getContentNode(text, children);\n  const words = useMemo(() => collectWords(content), [content]);\n  const accessibleLabel = useMemo(() => getAccessibleLabel(words), [words]);\n\n  const defaults = SPLIT_DEFAULTS[splitBy];\n  const resolvedDuration = duration ?? defaults.duration;\n  const resolvedStagger = stagger ?? defaults.stagger;\n\n  const { lineGroups, measureLayer } = useLineGroups(words, className, rootRef);\n\n  const wordStaggerMap = useMemo(() => {\n    const groups = lineGroups ?? [];\n    const map = new Map<number, number>();\n    let index = 0;\n\n    for (const group of groups) {\n      for (const wordIndex of group) {\n        map.set(wordIndex, index);\n        index++;\n      }\n    }\n\n    return map;\n  }, [lineGroups]);\n\n  const lineCharGroups = useMemo(() => {\n    if (!lineGroups?.length) {\n      return [];\n    }\n\n    const result: LineCharUnit[][] = [];\n    let globalStagger = 0;\n\n    for (const group of lineGroups) {\n      const lineChars: LineCharUnit[] = [];\n\n      for (const [index, wordIndex] of group.entries()) {\n        const word = words[wordIndex];\n        if (!word) {\n          continue;\n        }\n\n        for (const char of word.text) {\n          lineChars.push({\n            char,\n            emphasized: word.emphasized,\n            staggerIndex: globalStagger,\n          });\n          globalStagger += 1;\n        }\n\n        if (index < group.length - 1) {\n          lineChars.push({\n            char: \" \",\n            emphasized: false,\n            staggerIndex: globalStagger,\n          });\n          globalStagger += 1;\n        }\n      }\n\n      result.push(lineChars);\n    }\n\n    return result;\n  }, [lineGroups, words]);\n\n  const Component = Tag as ElementType;\n  const isReady = lineGroups !== null && lineGroups.length > 0;\n  const hasContent = words.length > 0;\n  const shouldAnimate = isInView && isReady && !prefersReducedMotion;\n\n  if (prefersReducedMotion) {\n    return (\n      <div className=\"w-full\" ref={rootRef}>\n        <Component className={className}>{content}</Component>\n      </div>\n    );\n  }\n\n  if (!hasContent) {\n    return (\n      <div className=\"w-full\" ref={rootRef}>\n        <Component className={className}>{content}</Component>\n      </div>\n    );\n  }\n\n  const rootClassName = cn(\n    \"overflow-wrap-anywhere block [transform:translateZ(0)] [overflow-wrap:break-word] [will-change:transform] [contain:layout_style_paint] [word-wrap:break-word]\",\n    !isReady && \"invisible\",\n    className\n  );\n\n  const lineContent =\n    isReady &&\n    lineGroups.map((group, lineIndex) => (\n      <SplitLine key={lineIndex}>\n        <RevealTarget\n          animate={shouldAnimate}\n          delay={delay + lineIndex * resolvedStagger}\n          display=\"block\"\n          duration={resolvedDuration}\n          yPercent={yPercent}\n        >\n          {group.map((wordIndex) => {\n            const word = words[wordIndex];\n            if (!word) {\n              return null;\n            }\n\n            return (\n              <WordSpan\n                isLastWord={wordIndex === words.length - 1}\n                key={wordIndex}\n                unitClassName={unitClassName}\n                word={word}\n              />\n            );\n          })}\n        </RevealTarget>\n      </SplitLine>\n    ));\n\n  const wordContent =\n    isReady &&\n    lineGroups.map((group, lineIndex) => (\n      <SplitLine key={lineIndex}>\n        {group.map((wordIndex) => {\n          const word = words[wordIndex];\n          if (!word) {\n            return null;\n          }\n\n          return (\n            <RevealTarget\n              animate={shouldAnimate}\n              className={cn(\"split-word\", unitClassName)}\n              delay={\n                delay + (wordStaggerMap.get(wordIndex) ?? 0) * resolvedStagger\n              }\n              duration={resolvedDuration}\n              key={wordIndex}\n              maskClassName={cn(\n                \"split-word\",\n                wordIndex < words.length - 1 && \"me-[0.25em]\"\n              )}\n              masked\n              yPercent={yPercent}\n            >\n              {renderWordContent(word)}\n            </RevealTarget>\n          );\n        })}\n      </SplitLine>\n    ));\n\n  const charContent =\n    isReady &&\n    lineCharGroups.map((group, lineIndex) => (\n      <SplitLine key={lineIndex}>\n        {group.map((token, charIndex) => (\n          <RevealTarget\n            animate={shouldAnimate}\n            className={cn(\n              \"split-char\",\n              token.emphasized && \"text-foreground font-medium\",\n              unitClassName\n            )}\n            delay={delay + token.staggerIndex * resolvedStagger}\n            duration={resolvedDuration}\n            key={charIndex}\n            masked\n            yPercent={yPercent}\n          >\n            {token.char === \" \" ? \"\\u00A0\" : token.char}\n          </RevealTarget>\n        ))}\n      </SplitLine>\n    ));\n\n  let splitContent = charContent;\n  if (splitBy === \"lines\") {\n    splitContent = lineContent;\n  } else if (splitBy === \"words\") {\n    splitContent = wordContent;\n  }\n\n  return (\n    <div className=\"relative w-full\" ref={rootRef}>\n      {measureLayer}\n      <Component\n        aria-label={accessibleLabel}\n        className={rootClassName}\n        key={splitBy}\n      >\n        {splitContent}\n      </Component>\n    </div>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/sora-ui/texts/text-reveal-mask.tsx"
    }
  ],
  "meta": {
    "inspiration": {
      "type": "reimplemented",
      "label": "Annnimate",
      "url": "https://www.annnimate.com",
      "stack": "Motion and React"
    },
    "demoProps": {
      "MaskedTextReveal": {
        "text": {
          "value": "Every stride is a statement. Outrun expectations."
        },
        "splitBy": {
          "value": "lines",
          "options": {
            "Lines": "lines",
            "Words": "words",
            "Characters": "chars"
          }
        },
        "stagger": {
          "value": 0.1,
          "min": 0.01,
          "max": 0.5,
          "step": 0.01
        },
        "duration": {
          "value": 0.8,
          "min": 0.1,
          "max": 2,
          "step": 0.1
        },
        "yPercent": {
          "value": 110,
          "min": 50,
          "max": 200,
          "step": 5
        },
        "delay": {
          "value": 0,
          "min": 0,
          "max": 2,
          "step": 0.1
        }
      }
    }
  },
  "type": "registry:ui"
}
