{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-reveal-block",
  "title": "Text Reveal (Block)",
  "description": "Line-by-line text reveal with a colored block wipe, powered by GSAP SplitText.",
  "dependencies": ["gsap", "@gsap/react"],
  "registryDependencies": [
    "@soralabs/hooks-use-prefers-reduced-motion",
    "@soralabs/lib-scroll-trigger-utils"
  ],
  "files": [
    {
      "path": "registry/primitives/texts/text-reveal-block/index.tsx",
      "content": "/* eslint-disable import/no-named-as-default, unicorn/prefer-modern-dom-apis, eqeqeq, no-eq-null -- gsap scrollTrigger requires dynamic DOM manipulation */\n\"use client\";\n\nimport { useGSAP } from \"@gsap/react\";\nimport gsap from \"gsap\";\nimport { ScrollTrigger } from \"gsap/ScrollTrigger\";\nimport { SplitText } from \"gsap/SplitText\";\nimport {\n  Children,\n  cloneElement,\n  createElement,\n  isValidElement,\n  useRef,\n} from \"react\";\nimport type { ReactElement, ReactNode, Ref } from \"react\";\n\nimport { usePrefersReducedMotion } from \"@/hooks/use-prefers-reduced-motion\";\nimport {\n  observeWindowResize,\n  waitForScrollerReady,\n} from \"@/lib/scroll-trigger-utils\";\n\ngsap.registerPlugin(SplitText, ScrollTrigger);\n\nexport type TextRevealBlockDirection = \"down\" | \"left\" | \"right\" | \"up\";\n\ninterface DirectionConfig {\n  axis: \"scaleX\" | \"scaleY\";\n  enterOrigin: string;\n  exitOrigin: string;\n}\n\nconst DIRECTION_CONFIG = {\n  down: {\n    axis: \"scaleY\",\n    enterOrigin: \"bottom center\",\n    exitOrigin: \"top center\",\n  },\n  left: {\n    axis: \"scaleX\",\n    enterOrigin: \"left center\",\n    exitOrigin: \"right center\",\n  },\n  right: {\n    axis: \"scaleX\",\n    enterOrigin: \"right center\",\n    exitOrigin: \"left center\",\n  },\n  up: {\n    axis: \"scaleY\",\n    enterOrigin: \"top center\",\n    exitOrigin: \"bottom center\",\n  },\n} as const satisfies Record<TextRevealBlockDirection, DirectionConfig>;\n\nexport interface TextRevealBlockProps {\n  /** When true, each line plays once when the block enters the viewport. */\n  animateOnScroll?: boolean;\n  /** HTML tag when using the `text` prop. */\n  as?: keyof React.JSX.IntrinsicElements;\n  /** Color of the wipe block. */\n  blockColor?: string;\n  children?: ReactNode;\n  /** Class name for the element created from `text` + `as`. */\n  className?: string;\n  /** Delay before the first line starts, in seconds. */\n  delay?: number;\n  /** Wipe travel direction for each line. */\n  direction?: TextRevealBlockDirection;\n  /** Duration of each wipe phase, in seconds. */\n  duration?: number;\n  /** Scroll container for `animateOnScroll` triggers. @default window */\n  scroller?: Element | Window;\n  /**\n   * Custom event dispatched when a Lenis/proxied scroller is ready.\n   * Refreshes ScrollTrigger positions after the scroller mounts.\n   */\n  scrollReadyEvent?: string;\n  /** Delay between each line, in seconds. */\n  stagger?: number;\n  /** Plain-text alternative to `children`. */\n  text?: string;\n}\n\ninterface LineSetup {\n  blocks: HTMLDivElement[];\n  lines: Element[];\n  splits: SplitText[];\n}\n\ninterface AnimationOptions {\n  animateOnScroll: boolean;\n  blockColor: string;\n  container: HTMLElement;\n  delay: number;\n  direction: TextRevealBlockDirection;\n  duration: number;\n  prefersReducedMotion: boolean;\n  scroller: Element | Window;\n  stagger: number;\n}\n\nfunction assignRef<T>(ref: Ref<T> | undefined, value: T | null) {\n  if (typeof ref === \"function\") {\n    ref(value);\n    return;\n  }\n\n  if (ref && typeof ref === \"object\") {\n    (ref as { current: T | null }).current = value;\n  }\n}\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      assignRef(ref, node);\n    }\n  };\n}\n\n/** Single element suitable for cloneElement; skips whitespace text nodes. */\nfunction getCloneableChild(node: ReactNode): ReactElement | null {\n  if (isValidElement(node)) {\n    return node;\n  }\n\n  let found: ReactElement | null = null;\n  let count = 0;\n\n  Children.forEach(node, (child) => {\n    if (child == null || child === false) {\n      return;\n    }\n\n    if (typeof child === \"string\" && child.trim() === \"\") {\n      return;\n    }\n\n    if (isValidElement(child)) {\n      count += 1;\n      found = child;\n      return;\n    }\n\n    count += 1;\n  });\n\n  return count === 1 ? found : null;\n}\n\nfunction restoreWrappers(container: HTMLElement) {\n  const wrappers = container.querySelectorAll(\".text-reveal-block-line\");\n\n  for (const wrapper of wrappers) {\n    const firstChild = wrapper.firstChild;\n\n    if (wrapper.parentNode && firstChild) {\n      wrapper.parentNode.insertBefore(firstChild, wrapper);\n      wrapper.remove();\n    }\n  }\n}\n\nfunction createCleanup(container: HTMLElement, setup: LineSetup) {\n  return () => {\n    for (const split of setup.splits) {\n      split.revert();\n    }\n    restoreWrappers(container);\n  };\n}\n\nfunction setupLineReveal(\n  container: HTMLElement,\n  blockColor: string\n): LineSetup {\n  const splits: SplitText[] = [];\n  const lines: Element[] = [];\n  const blocks: HTMLDivElement[] = [];\n\n  const elements = Object.hasOwn(container.dataset, \"textRevealBlockWrapper\")\n    ? [...container.children]\n    : [container];\n\n  for (const element of elements) {\n    const split = SplitText.create(element, {\n      type: \"lines\",\n      linesClass: \"block-line++\",\n      lineThreshold: 0.1,\n    });\n\n    splits.push(split);\n\n    for (const line of split.lines) {\n      const parent = line.parentNode;\n      if (!parent) {\n        continue;\n      }\n\n      const wrapper = document.createElement(\"div\");\n      wrapper.className = \"text-reveal-block-line\";\n      wrapper.style.position = \"relative\";\n      wrapper.style.display = \"block\";\n      wrapper.style.width = \"max-content\";\n      wrapper.style.maxWidth = \"100%\";\n      wrapper.style.overflow = \"hidden\";\n\n      parent.insertBefore(wrapper, line);\n      wrapper.append(line);\n\n      const lineElement = line as HTMLElement;\n      lineElement.style.position = \"relative\";\n      lineElement.style.display = \"block\";\n\n      const block = document.createElement(\"div\");\n      block.className = \"text-reveal-block-wipe\";\n      block.style.position = \"absolute\";\n      block.style.top = \"0\";\n      block.style.left = \"0\";\n      block.style.width = \"101%\";\n      block.style.height = \"101%\";\n      block.style.pointerEvents = \"none\";\n      block.style.willChange = \"transform\";\n      block.style.zIndex = \"1\";\n      block.style.backgroundColor = blockColor;\n\n      wrapper.append(block);\n\n      lines.push(line);\n      blocks.push(block);\n    }\n  }\n\n  return { splits, lines, blocks };\n}\n\nfunction createBlockRevealTimeline(\n  block: HTMLDivElement,\n  line: Element,\n  index: number,\n  delay: number,\n  stagger: number,\n  duration: number,\n  direction: TextRevealBlockDirection\n) {\n  const { axis, exitOrigin } = DIRECTION_CONFIG[direction];\n\n  const tl = gsap.timeline({\n    delay: delay + index * stagger,\n  });\n\n  tl.to(block, {\n    [axis]: 1,\n    duration,\n    ease: \"power4.inOut\",\n  });\n  tl.set(line, { opacity: 1 });\n  tl.set(block, {\n    transformOrigin: exitOrigin,\n  });\n  tl.to(block, {\n    [axis]: 0,\n    duration,\n    ease: \"power4.inOut\",\n  });\n\n  return tl;\n}\n\nfunction playTimelineIfInView(\n  timeline: gsap.core.Timeline,\n  scrollTrigger: ScrollTrigger\n) {\n  if (scrollTrigger.isActive && timeline.paused()) {\n    timeline.play();\n  }\n}\n\nfunction refreshScrollRevealTriggers(scrollTriggers: ScrollTrigger[]) {\n  ScrollTrigger.refresh();\n\n  for (const scrollTrigger of scrollTriggers) {\n    const timeline = scrollTrigger.animation as gsap.core.Timeline | undefined;\n    if (timeline) {\n      playTimelineIfInView(timeline, scrollTrigger);\n    }\n  }\n}\n\nfunction runLineRevealAnimations(\n  options: AnimationOptions,\n  setup: LineSetup\n): ScrollTrigger[] {\n  const { animateOnScroll, container, delay, direction, duration, stagger } =\n    options;\n  const { axis, enterOrigin } = DIRECTION_CONFIG[direction];\n  const scrollTriggers: ScrollTrigger[] = [];\n\n  gsap.set(setup.lines, { opacity: 0 });\n  gsap.set(setup.blocks, {\n    [axis]: 0,\n    transformOrigin: enterOrigin,\n  });\n\n  for (const [index, block] of setup.blocks.entries()) {\n    const line = setup.lines[index];\n    if (!line) {\n      continue;\n    }\n\n    const timeline = createBlockRevealTimeline(\n      block,\n      line,\n      index,\n      delay,\n      stagger,\n      duration,\n      direction\n    );\n\n    if (animateOnScroll) {\n      scrollTriggers.push(\n        ScrollTrigger.create({\n          trigger: container,\n          scroller: options.scroller,\n          start: \"top 90%\",\n          once: true,\n          animation: timeline,\n          invalidateOnRefresh: true,\n          onRefresh(self) {\n            const activeTimeline = self.animation as\n              | gsap.core.Timeline\n              | undefined;\n            if (activeTimeline) {\n              playTimelineIfInView(activeTimeline, self);\n            }\n          },\n        })\n      );\n    }\n  }\n\n  if (scrollTriggers.length > 0) {\n    requestAnimationFrame(() => {\n      refreshScrollRevealTriggers(scrollTriggers);\n    });\n  }\n\n  return scrollTriggers;\n}\n\nfunction initTextRevealBlock(options: AnimationOptions) {\n  const setup = setupLineReveal(options.container, options.blockColor);\n\n  if (options.prefersReducedMotion) {\n    gsap.set(setup.lines, { opacity: 1 });\n    gsap.set(setup.blocks, { display: \"none\" });\n    return {\n      cleanup: createCleanup(options.container, setup),\n      scrollTriggers: [] as ScrollTrigger[],\n    };\n  }\n\n  const scrollTriggers = runLineRevealAnimations(options, setup);\n\n  return {\n    cleanup: createCleanup(options.container, setup),\n    scrollTriggers,\n  };\n}\n\nexport function TextRevealBlock({\n  children,\n  text,\n  as: Component = \"p\",\n  className,\n  animateOnScroll = true,\n  delay = 0,\n  blockColor = \"var(--foreground)\",\n  direction = \"left\",\n  stagger = 0.15,\n  duration = 0.75,\n  scroller: scrollerProp,\n  scrollReadyEvent,\n}: TextRevealBlockProps) {\n  const containerRef = useRef<HTMLElement | null>(null);\n  const prefersReducedMotion = usePrefersReducedMotion();\n\n  const resolvedChild =\n    children ?? (text ? createElement(Component, { className }, text) : null);\n\n  useGSAP(\n    () => {\n      const container = containerRef.current;\n      if (!container) {\n        return;\n      }\n\n      let disposed = false;\n      let teardown: (() => void) | undefined;\n      let scrollTriggers: ScrollTrigger[] = [];\n      let resizeObserver: ResizeObserver | undefined;\n      let unbindWindowResize: (() => void) | undefined;\n\n      const refreshTriggers = () => {\n        if (scrollTriggers.length > 0) {\n          refreshScrollRevealTriggers(scrollTriggers);\n        }\n      };\n\n      const onScrollReady = () => {\n        refreshTriggers();\n      };\n\n      const mountAnimation = async () => {\n        if (disposed || !containerRef.current) {\n          return;\n        }\n\n        const scroller = scrollerProp ?? window;\n        await waitForScrollerReady(scroller, scrollReadyEvent);\n\n        if (disposed || !containerRef.current) {\n          return;\n        }\n\n        const result = initTextRevealBlock({\n          animateOnScroll,\n          blockColor,\n          container: containerRef.current,\n          delay,\n          direction,\n          duration,\n          prefersReducedMotion,\n          scroller,\n          stagger,\n        });\n\n        scrollTriggers = result.scrollTriggers;\n        teardown = () => {\n          for (const trigger of scrollTriggers) {\n            trigger.kill();\n          }\n          result.cleanup();\n          scrollTriggers = [];\n        };\n\n        if (scrollTriggers.length > 0) {\n          if (scroller instanceof HTMLElement) {\n            resizeObserver = new ResizeObserver(() => {\n              refreshTriggers();\n            });\n            resizeObserver.observe(scroller);\n          } else {\n            unbindWindowResize = observeWindowResize(refreshTriggers);\n          }\n        }\n      };\n\n      if (scrollReadyEvent) {\n        window.addEventListener(scrollReadyEvent, onScrollReady);\n      }\n\n      mountAnimation().catch(() => {});\n\n      return () => {\n        disposed = true;\n        if (scrollReadyEvent) {\n          window.removeEventListener(scrollReadyEvent, onScrollReady);\n        }\n        resizeObserver?.disconnect();\n        unbindWindowResize?.();\n        teardown?.();\n      };\n    },\n    {\n      scope: containerRef,\n      dependencies: [\n        animateOnScroll,\n        delay,\n        blockColor,\n        direction,\n        stagger,\n        duration,\n        prefersReducedMotion,\n        scrollerProp,\n        scrollReadyEvent,\n      ],\n    }\n  );\n\n  if (!resolvedChild) {\n    return null;\n  }\n\n  const child = getCloneableChild(resolvedChild);\n\n  if (child) {\n    return cloneElement(child as ReactElement<{ ref?: Ref<HTMLElement> }>, {\n      ref: mergeRefs(\n        containerRef,\n        (child as ReactElement<{ ref?: Ref<HTMLElement> }>).props.ref\n      ),\n    });\n  }\n\n  return (\n    <div\n      data-text-reveal-block-wrapper=\"true\"\n      ref={containerRef as Ref<HTMLDivElement>}\n    >\n      {resolvedChild}\n    </div>\n  );\n}\n\nexport default TextRevealBlock;\n",
      "type": "registry:ui",
      "target": "components/sora-ui/texts/text-reveal-block.tsx"
    }
  ],
  "meta": {
    "demoProps": {
      "TextRevealBlock": {
        "text": {
          "value": "Framed in shadow and light, every line arrives with deliberate tension."
        },
        "animateOnScroll": {
          "value": false
        },
        "blockColor": {
          "value": "#fe0100"
        },
        "stagger": {
          "value": 0.15,
          "min": 0,
          "max": 0.5,
          "step": 0.05
        },
        "duration": {
          "value": 0.75,
          "min": 0.2,
          "max": 2,
          "step": 0.05
        },
        "delay": {
          "value": 0,
          "min": 0,
          "max": 1,
          "step": 0.05
        },
        "direction": {
          "value": "left",
          "options": {
            "left": "left",
            "right": "right",
            "up": "up",
            "down": "down"
          }
        }
      }
    }
  },
  "type": "registry:ui"
}
