{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"page-transition","type":"registry:ui","title":"Page Transition","description":"A staggered, layered column curtain that covers the old view, swaps content, then sweeps away to reveal the new one.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/page-transition.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface PageTransitionProps {\n  /** Change this (e.g. the active tab or route) to play the transition. */\n  transitionKey: string | number;\n  /** The current view. Swapped in while the curtain is closed. */\n  children: ReactNode;\n  /** Number of curtain columns. 1 = a plain wipe. */\n  columns?: number;\n  /** Curtain layers, back to front. */\n  colors?: string[];\n  /** Ms for each half (cover, then reveal). */\n  duration?: number;\n  /** Ms between neighboring columns. */\n  stagger?: number;\n  /** Direction the curtain travels. */\n  direction?: \"up\" | \"down\";\n  className?: string;\n}\n\nconst EASE = \"cubic-bezier(.76,0,.24,1)\";\nconst COLORS = [\"#c6ff3d\", \"#18181b\"];\n\nexport function PageTransition({\n  transitionKey,\n  children,\n  columns = 5,\n  colors = COLORS,\n  duration = 550,\n  stagger = 55,\n  direction = \"up\",\n  className,\n}: PageTransitionProps) {\n  const [shownKey, setShownKey] = useState(transitionKey);\n  const [stale, setStale] = useState<ReactNode>(children);\n  // Keep a copy of the visible view so it can stay on screen while the curtain closes.\n  if (shownKey === transitionKey && stale !== children) setStale(children);\n  const content = shownKey === transitionKey ? children : stale;\n\n  const cols = useRef<(HTMLDivElement | null)[]>([]);\n  const body = useRef<HTMLDivElement>(null);\n  const covered = useRef(false);\n\n  useEffect(() => {\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dur = reduced ? 1 : duration;\n    const gap = reduced ? 0 : stagger;\n    const nl = colors.length;\n    const lag = dur * 0.18;\n    const from = direction === \"up\" ? \"bottom\" : \"top\";\n    const to = direction === \"up\" ? \"top\" : \"bottom\";\n    const els = cols.current.slice(0, columns * nl).filter((c): c is HTMLDivElement => !!c);\n\n    const reveal = () => {\n      covered.current = false;\n      els.forEach((el, i) => {\n        const layer = Math.floor(i / columns);\n        const current = getComputedStyle(el).transform;\n        el.getAnimations().forEach((a) => a.cancel());\n        el.animate(\n          [\n            { transform: current === \"none\" ? \"scaleY(0)\" : current, transformOrigin: to },\n            { transform: \"scaleY(0)\", transformOrigin: to },\n          ],\n          { duration: dur, delay: (i % columns) * gap + (nl - 1 - layer) * lag, easing: EASE, fill: \"forwards\" },\n        );\n      });\n      body.current?.animate(\n        [\n          { transform: `translateY(${direction === \"up\" ? 28 : -28}px)`, opacity: 0.4 },\n          { transform: \"none\", opacity: 1 },\n        ],\n        { duration: dur * 1.4, delay: dur * 0.3, easing: \"cubic-bezier(.2,.8,.2,1)\", fill: \"backwards\" },\n      );\n    };\n\n    if (shownKey === transitionKey) {\n      // Switched back to the visible view mid-cover: just open the curtain again.\n      if (covered.current) reveal();\n      return;\n    }\n    let alive = true;\n    covered.current = true;\n    const cover = els.map((el, i) => {\n      const layer = Math.floor(i / columns);\n      const current = getComputedStyle(el).transform;\n      el.getAnimations().forEach((a) => a.cancel());\n      return el.animate(\n        [\n          { transform: current === \"none\" ? \"scaleY(0)\" : current, transformOrigin: from },\n          { transform: \"scaleY(1)\", transformOrigin: from },\n        ],\n        { duration: dur, delay: (i % columns) * gap + layer * lag, easing: EASE, fill: \"forwards\" },\n      );\n    });\n\n    Promise.all(cover.map((a) => a.finished))\n      .then(() => {\n        if (!alive) return;\n        setShownKey(transitionKey);\n        reveal();\n      })\n      .catch(() => {}); // cancelled by a newer transition\n    return () => {\n      alive = false;\n    };\n  }, [transitionKey, shownKey, columns, colors, duration, stagger, direction]);\n\n  return (\n    <div className={cn(\"relative overflow-hidden\", className)}>\n      <div ref={body} className=\"size-full\">\n        {content}\n      </div>\n      <div aria-hidden className=\"pointer-events-none absolute inset-0 z-20\">\n        {colors.map((color, layer) => (\n          <div key={layer} className=\"absolute inset-0 flex\">\n            {Array.from({ length: columns }, (_, col) => (\n              <div\n                key={col}\n                ref={(el) => {\n                  cols.current[layer * columns + col] = el;\n                }}\n                className=\"h-full flex-1\"\n                style={{ background: color, transform: \"scaleY(0)\", marginLeft: col ? -1 : 0 }}\n              />\n            ))}\n          </div>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}]}