{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tile-reveal",
  "title": "Tile Reveal",
  "description": "A pinned image grid whose tiles fly in column by column on scroll, then zoom past the edges to reveal a headline and call to action. No dependencies.",
  "files": [
    {
      "path": "registry/mewo/ui/tile-reveal.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cn } from \"@/lib/utils\"\n\nexport interface TileRevealProps extends Omit<React.ComponentProps<\"section\">, \"onProgress\"> {\n  /** Image URLs laid out row by row into the grid. */\n  images: string[]\n  /** Content that stays visible through the whole sequence, centred over the tiles. */\n  headline?: React.ReactNode\n  /** Number of grid columns. */\n  columns?: number\n  /** Gap between tiles in pixels. */\n  gap?: number\n  /** Maximum grid width in pixels; shrinks with the stage. */\n  gridWidth?: number\n  /** Width to height ratio of each tile. */\n  tileAspect?: number\n  /** Corner radius of each tile in pixels. */\n  tileRadius?: number\n  /** Render the images in grayscale. */\n  grayscale?: boolean\n  /** Which edge columns enter through. */\n  direction?: \"alternate\" | \"top\" | \"bottom\"\n  /** Delay between tiles in a column, relative to one tile's flight time. */\n  stagger?: number\n  /** How far the zoom phase overlaps the fly-in, as a fraction of one flight. */\n  overlap?: number\n  /** Scale the grid reaches before the tiles leave the stage. */\n  zoom?: number\n  /**\n   * Minimum push, as a fraction of a tile, the columns and centre rows spread apart.\n   * Tiles always travel at least far enough to clear the stage.\n   */\n  spread?: number\n  /** Extra viewport heights of scroll that drive the sequence. */\n  scrollLength?: number\n  /** Seconds the motion lags behind the scroll position. Zero locks to it. */\n  scrub?: number\n  /** Gap between the headline and the revealed content in pixels. */\n  contentGap?: number\n  /** Stage background. */\n  backgroundColor?: string\n  /** Called with the sequence progress, 0 to 1. */\n  onProgress?: (progress: number) => void\n}\n\ninterface TilePlan {\n  /** Resting offset of the tile centre, measured against the stage centre. */\n  x: number\n  y: number\n  /** Push applied while the grid zooms, in unscaled pixels. */\n  pushX: number\n  pushY: number\n  /** -1 enters through the top edge, 1 through the bottom. */\n  side: number\n  /** Position in its column's launch order. */\n  order: number\n}\n\n// Smallest margin kept either side of the grid when the stage is narrow.\nconst MIN_GUTTER = 16\n// Centre-column rows part over the back half of the zoom.\nconst ROW_SPREAD_START = 0.5\n// The revealed content fades in over the tail of the zoom, rising this far.\nconst REVEAL_START = 0.68\nconst REVEAL_RISE = 12\n\nconst clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value))\nconst easeInOutCubic = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(2 - 2 * t, 3) / 2)\nconst easeInOutSine = (t: number) => (1 - Math.cos(Math.PI * t)) / 2\n\n/** The sequence measured in flights: one flight is a single tile's trip into the grid. */\nexport function tileRevealTimeline(rows: number, stagger: number, overlap: number) {\n  const flyEnd = 1 + Math.max(0, stagger) * Math.max(0, rows - 1)\n  const zoomStart = Math.max(0, flyEnd - Math.max(0, overlap))\n  return { flyEnd, zoomStart, duration: Math.max(flyEnd, zoomStart + 1) }\n}\n\n/** The nearest ancestor that scrolls vertically, or null when the page itself does. */\nfunction scrollParent(node: HTMLElement): HTMLElement | null {\n  for (let el = node.parentElement; el && el !== document.body && el !== document.documentElement; el = el.parentElement) {\n    const { overflowY } = getComputedStyle(el)\n    if (overflowY === \"auto\" || overflowY === \"scroll\") return el\n  }\n  return null\n}\n\nexport function TileReveal({\n  images,\n  headline,\n  children,\n  columns = 3,\n  gap = 28,\n  gridWidth = 720,\n  tileAspect = 1,\n  tileRadius = 0,\n  grayscale = true,\n  direction = \"alternate\",\n  stagger = 0.06,\n  overlap = 0.6,\n  zoom = 2.05,\n  spread = 0.4,\n  scrollLength = 3,\n  scrub = 0.08,\n  contentGap = 28,\n  backgroundColor = \"transparent\",\n  onProgress,\n  className,\n  style,\n  ...props\n}: TileRevealProps) {\n  const stageRef = React.useRef<HTMLDivElement>(null)\n  const tileRefs = React.useRef<(HTMLDivElement | null)[]>([])\n  const headlineRef = React.useRef<HTMLDivElement>(null)\n  const contentRef = React.useRef<HTMLDivElement>(null)\n  const progressRef = React.useRef<number | null>(null)\n  const reportedRef = React.useRef(-1)\n  const onProgressRef = React.useRef(onProgress)\n\n  React.useEffect(() => {\n    onProgressRef.current = onProgress\n  }, [onProgress])\n\n  const count = images.length\n  const hasContent = children != null && children !== false\n\n  React.useEffect(() => {\n    const stage = stageRef.current\n    // The root's ref stays the consumer's, so the section is reached through the stage.\n    const section = stage?.parentElement\n    if (!stage || !section) return\n\n    const scroller = scrollParent(section)\n    // Inside a scroll container the stage pins to that container, not the window.\n    const fit = () => {\n      if (scroller) section.style.setProperty(\"--mewo-viewport\", `${scroller.clientHeight}px`)\n    }\n    fit()\n\n    const report = (progress: number) => {\n      if (progress === reportedRef.current) return\n      reportedRef.current = progress\n      onProgressRef.current?.(progress)\n    }\n\n    // Reduced motion skips the sequence: CSS drops the tiles and the scroll runway,\n    // and the headline and content rest where the animation would have left them.\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      const content = contentRef.current\n      if (content) {\n        content.style.opacity = \"1\"\n        content.style.visibility = \"visible\"\n        content.style.pointerEvents = \"auto\"\n      }\n      report(1)\n      return\n    }\n\n    const lag = Math.max(0, scrub)\n    const cols = Math.max(1, Math.floor(columns))\n    const rows = Math.ceil(count / cols)\n    const peak = Math.max(0.01, zoom)\n    const delay = Math.max(0, stagger)\n    const { zoomStart, duration } = tileRevealTimeline(rows, stagger, overlap)\n\n    let plans: TilePlan[] = []\n    let travel = 0\n    let lift = 0\n    let target = 0\n    let raf = 0\n    let last = 0\n\n    const layout = () => {\n      const stageWidth = stage.clientWidth\n      const stageHeight = stage.clientHeight\n      const width = Math.max(0, Math.min(gridWidth, stageWidth - 2 * Math.max(gap, MIN_GUTTER)))\n      const tileWidth = Math.max(0, (width - gap * (cols - 1)) / cols)\n      const tileHeight = tileAspect > 0 ? tileWidth / tileAspect : tileWidth\n      const pitchX = tileWidth + gap\n      const pitchY = tileHeight + gap\n      const gridHeight = rows * tileHeight + gap * Math.max(0, rows - 1)\n\n      // Columns start parked just beyond the stage edge they enter through.\n      travel = (stageHeight + gridHeight) / 2\n\n      // How far out a tile centre must sit, before scaling, to be clear of the stage\n      // (by one gap) once the grid is fully zoomed.\n      const reachX = stageWidth / (2 * peak) + tileWidth / 2 + gap\n      const reachY = stageHeight / (2 * peak) + tileHeight / 2 + gap\n      const oddCols = cols % 2 === 1\n      // Side columns move as a block, so the innermost one sets their push.\n      const pushX = Math.max(spread * tileWidth, reachX - (oddCols ? pitchX : pitchX / 2))\n\n      plans = []\n      for (let i = 0; i < count; i++) {\n        const col = i % cols\n        const row = Math.floor(i / cols)\n        const centreCol = oddCols && col === (cols - 1) / 2\n        const fromTop = direction === \"top\" || (direction === \"alternate\" && col % 2 === 0)\n        const y = (row - (rows - 1) / 2) * pitchY\n        // Centre-column tiles part vertically, each going only as far as it needs.\n        const pushY = Math.max(spread * tileHeight, reachY - Math.abs(y))\n\n        plans.push({\n          x: (col - (cols - 1) / 2) * pitchX,\n          y,\n          pushX: centreCol ? 0 : col < (cols - 1) / 2 ? -pushX : pushX,\n          pushY: !centreCol ? 0 : row * 2 <= rows - 1 ? -pushY : pushY,\n          side: fromTop ? -1 : 1,\n          // The tile nearest the stage leads its column in.\n          order: fromTop ? rows - 1 - row : row,\n        })\n\n        const tile = tileRefs.current[i]\n        if (tile) {\n          tile.style.width = `${tileWidth}px`\n          tile.style.height = `${tileHeight}px`\n          tile.style.margin = `${-tileHeight / 2}px 0 0 ${-tileWidth / 2}px`\n        }\n      }\n\n      // The headline starts centred on its own, then makes room for the content.\n      const content = contentRef.current\n      lift = content ? (content.offsetHeight + contentGap) / 2 : 0\n    }\n\n    const measure = () => {\n      const origin = scroller ? scroller.getBoundingClientRect().top + scroller.clientTop : 0\n      const scrolled = origin - section.getBoundingClientRect().top\n      const runway = section.offsetHeight - stage.offsetHeight\n      target = runway > 0 ? clamp(scrolled / runway, 0, 1) : 0\n    }\n\n    const render = (progress: number) => {\n      const time = progress * duration\n      const zoomTime = clamp(time - zoomStart, 0, 1)\n      const zoomEase = easeInOutCubic(zoomTime)\n      const scale = 1 + (peak - 1) * zoomEase\n      const rowEase = clamp((zoomTime - ROW_SPREAD_START) / (1 - ROW_SPREAD_START), 0, 1)\n\n      for (let i = 0; i < plans.length; i++) {\n        const tile = tileRefs.current[i]\n        if (!tile) continue\n        const plan = plans[i]\n        const flight = clamp(time - plan.order * delay, 0, 1)\n        const x = scale * (plan.x + plan.pushX * zoomEase)\n        const y = scale * (plan.y + plan.side * travel * (1 - flight) + plan.pushY * rowEase)\n        tile.style.transform = `translate3d(${x}px, ${y}px, 0) scale(${scale})`\n      }\n\n      const reveal = easeInOutSine(clamp((zoomTime - REVEAL_START) / (1 - REVEAL_START), 0, 1))\n      const heading = headlineRef.current\n      if (heading) heading.style.transform = `translate3d(0, ${(1 - reveal) * lift}px, 0)`\n\n      const content = contentRef.current\n      if (content) {\n        content.style.opacity = `${reveal}`\n        content.style.transform = `translate3d(0, ${(1 - reveal) * REVEAL_RISE}px, 0)`\n        content.style.visibility = reveal > 0 ? \"visible\" : \"hidden\"\n        content.style.pointerEvents = reveal > 0.5 ? \"auto\" : \"none\"\n      }\n\n      report(progress)\n    }\n\n    const tick = (now: number) => {\n      const dt = clamp((now - last) / 1000, 0, 0.1)\n      last = now\n\n      let progress = progressRef.current ?? target\n      // A first-order lag trails a steady scroll by exactly `scrub` seconds.\n      progress = lag > 0 ? progress + (target - progress) * (1 - Math.exp(-dt / lag)) : target\n      if (Math.abs(target - progress) < 0.0002) progress = target\n\n      progressRef.current = progress\n      render(progress)\n      raf = progress === target ? 0 : requestAnimationFrame(tick)\n    }\n\n    const update = () => {\n      measure()\n      if (!raf) {\n        last = performance.now()\n        raf = requestAnimationFrame(tick)\n      }\n    }\n\n    const relayout = () => {\n      fit()\n      layout()\n      measure()\n      render(progressRef.current ?? target)\n      update()\n    }\n\n    layout()\n    measure()\n    // Snap on mount; afterwards keep the eased position across prop changes.\n    progressRef.current = progressRef.current ?? target\n    render(progressRef.current)\n    if (progressRef.current !== target) update()\n\n    const observer = new ResizeObserver(relayout)\n    observer.observe(stage)\n    if (scroller) observer.observe(scroller)\n    if (contentRef.current) observer.observe(contentRef.current)\n    // Scroll events do not bubble; capturing on the window hears a nested scroller too.\n    window.addEventListener(\"scroll\", update, { passive: true, capture: true })\n    window.addEventListener(\"resize\", update)\n\n    return () => {\n      cancelAnimationFrame(raf)\n      observer.disconnect()\n      window.removeEventListener(\"scroll\", update, { capture: true })\n      window.removeEventListener(\"resize\", update)\n    }\n  }, [count, hasContent, columns, gap, gridWidth, tileAspect, direction, stagger, overlap, zoom, spread, scrollLength, scrub, contentGap])\n\n  return (\n    <section\n      data-slot=\"tile-reveal\"\n      className={cn(\n        \"relative h-[calc(var(--mewo-viewport,100vh)*var(--mewo-length))] motion-reduce:h-[var(--mewo-viewport,100vh)]\",\n        className\n      )}\n      style={{ \"--mewo-length\": 1 + Math.max(0, scrollLength), ...style } as React.CSSProperties}\n      {...props}\n    >\n      <div\n        ref={stageRef}\n        className=\"sticky top-0 h-[var(--mewo-viewport,100vh)] w-full overflow-hidden\"\n        style={{ background: backgroundColor }}\n      >\n        {images.map((src, i) => (\n          <div\n            key={i}\n            ref={(el) => {\n              tileRefs.current[i] = el\n            }}\n            data-slot=\"tile-reveal-tile\"\n            aria-hidden\n            // Sized and placed by the effect once the stage has been measured.\n            className={cn(\n              \"absolute top-1/2 left-1/2 size-0 overflow-hidden bg-cover bg-center will-change-transform motion-reduce:hidden\",\n              grayscale && \"grayscale\"\n            )}\n            style={{ backgroundImage: `url(${JSON.stringify(src)})`, borderRadius: tileRadius }}\n          />\n        ))}\n\n        <div className=\"pointer-events-none absolute inset-0 z-10 flex flex-col items-center justify-center text-center\">\n          <div ref={headlineRef} data-slot=\"tile-reveal-headline\" className=\"will-change-transform\">\n            {headline}\n          </div>\n          {hasContent && (\n            <div\n              ref={contentRef}\n              data-slot=\"tile-reveal-content\"\n              className=\"invisible opacity-0\"\n              style={{ marginTop: contentGap }}\n            >\n              {children}\n            </div>\n          )}\n        </div>\n      </div>\n    </section>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "css": {
    "@media (scripting: none)": {
      "[data-slot=\"tile-reveal\"]": {
        "height": "100vh !important"
      },
      "[data-slot=\"tile-reveal-content\"]": {
        "opacity": "1 !important",
        "visibility": "visible !important"
      }
    }
  },
  "meta": {
    "added": "2026-10-01"
  },
  "categories": [
    "scroll"
  ],
  "type": "registry:ui"
}