{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "page-transition",
  "title": "Page Transition",
  "description": "App Router page transitions: a provider, TransitionLink and usePageTransition hook with curtain, stairs, iris, slide and blinds variants.",
  "dependencies": [
    "gsap",
    "@gsap/react"
  ],
  "registryDependencies": [
    "utils",
    "https://trytweenly.vercel.app/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/new-york/page-transition/page-transition.tsx",
      "content": "\"use client\"\n\nimport { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from \"react\"\nimport { usePathname, useRouter } from \"next/navigation\"\nimport gsap from \"gsap\"\nimport { useGSAP } from \"@gsap/react\"\nimport { cn } from \"@/lib/utils\"\nimport { useReducedMotion } from \"@/registry/new-york/hooks/use-reduced-motion\"\n\ngsap.registerPlugin(useGSAP)\n\nexport type PageTransitionVariant = \"curtain\" | \"stairs\" | \"iris\" | \"slide\" | \"blinds\"\nexport type PageTransitionEase = \"power4\" | \"expo\" | \"circ\" | \"sine\"\n\nexport interface PageTransitionNavigateOptions {\n  /** Click position (client coordinates). The iris variant opens from here. */\n  x?: number\n  y?: number\n  /** Text shown on the cover for this navigation. Overrides the provider `label`. */\n  label?: string\n  /** Replace the current history entry instead of pushing one (default router only). */\n  replace?: boolean\n}\n\nexport interface PageTransitionContextValue {\n  /** Cover the page, navigate to `href`, then reveal the new route. */\n  navigate: (href: string, options?: PageTransitionNavigateOptions) => void\n  /** Prefetch a route (default router only). */\n  prefetch: (href: string) => void\n  /** True from the moment the cover starts until the reveal ends. */\n  isTransitioning: boolean\n  /** Whether a custom `navigate` is in use (then every same-origin link is intercepted). */\n  custom: boolean\n}\n\nexport interface PageTransitionProviderProps {\n  /** Transition style. Default: \"curtain\" */\n  variant?: PageTransitionVariant\n  /** Length of each phase (cover and reveal), in seconds. Default: 0.8 */\n  duration?: number\n  /** Cover color (any CSS color). Inverts the theme by default. Default: \"var(--foreground)\" */\n  color?: string\n  /** Text color on the cover. Default: \"var(--background)\" */\n  foreground?: string\n  /** Accent used for the dot beside the label. Default: \"#ff4d12\" */\n  accent?: string\n  /** Text shown on the cover, or a function of the destination href. Default: undefined (no label) */\n  label?: string | ((href: string) => string | undefined)\n  /** Number of columns (\"stairs\") or bars (\"blinds\"). Default: 5 */\n  columns?: number\n  /** GSAP ease family used for cover and reveal. Default: \"power4\" */\n  ease?: PageTransitionEase\n  /** \"fixed\" covers the viewport; \"absolute\" covers the nearest positioned parent. Default: \"fixed\" */\n  position?: \"fixed\" | \"absolute\"\n  /**\n   * Performs the actual navigation. May return a promise that resolves once the new page is rendered.\n   * Default: `useRouter().push` (the reveal waits for `usePathname()` to change).\n   */\n  navigate?: (href: string) => void | Promise<void>\n  /** Your pages. */\n  children?: React.ReactNode\n  /** Classes for the element wrapping your pages (the \"slide\" variant transforms it). */\n  className?: string\n  /** Classes for the cover overlay. */\n  overlayClassName?: string\n}\n\nconst EASES: Record<PageTransitionEase, string> = {\n  power4: \"power4.inOut\",\n  expo: \"expo.inOut\",\n  circ: \"circ.inOut\",\n  sine: \"sine.inOut\",\n}\n\nconst PageTransitionContext = createContext<PageTransitionContextValue | null>(null)\n\ntype Phase = \"idle\" | \"cover\" | \"wait\" | \"reveal\"\ntype Pending = { href: string; options: PageTransitionNavigateOptions }\n\nconst frame = () => new Promise<void>((r) => requestAnimationFrame(() => r()))\nconst wait = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))\nconst finished = (tl: gsap.core.Timeline) => new Promise<void>((r) => tl.eventCallback(\"onComplete\", () => r()))\n\n/** Rectangle with a circular hole, as an even-odd polygon (clip-path can't subtract a circle). */\nfunction holePolygon(w: number, h: number, cx: number, cy: number, r: number, steps = 72) {\n  const pts: string[] = []\n  for (let i = 0; i <= steps; i++) {\n    const a = (i / steps) * Math.PI * 2\n    pts.push(`${(cx + Math.cos(a) * r).toFixed(1)}px ${(cy + Math.sin(a) * r).toFixed(1)}px`)\n  }\n  return `polygon(evenodd, 0px 0px, ${w}px 0px, ${w}px ${h}px, 0px ${h}px, 0px 0px, ${pts.join(\", \")}, 0px 0px)`\n}\n\n/** Geometry that keeps the scaled page centred on, and clipped to, the visible area. */\nfunction pageFrame(page: HTMLElement, overlay: HTMLElement) {\n  const wr = page.getBoundingClientRect()\n  const ov = overlay.getBoundingClientRect()\n  const t = Math.max(0, ov.top - wr.top)\n  const l = Math.max(0, ov.left - wr.left)\n  const r = Math.max(0, wr.right - ov.right)\n  const b = Math.max(0, wr.bottom - ov.bottom)\n  return {\n    origin: `${ov.left + ov.width / 2 - wr.left}px ${ov.top + ov.height / 2 - wr.top}px`,\n    clip: (radius: number) => `inset(${t}px ${r}px ${b}px ${l}px round ${radius}px)`,\n    height: ov.height,\n  }\n}\n\nexport function PageTransitionProvider({\n  variant = \"curtain\",\n  duration = 0.8,\n  color = \"var(--foreground)\",\n  foreground = \"var(--background)\",\n  accent = \"#ff4d12\",\n  label,\n  columns = 5,\n  ease = \"power4\",\n  position = \"fixed\",\n  navigate: customNavigate,\n  children,\n  className,\n  overlayClassName,\n}: PageTransitionProviderProps) {\n  const router = useRouter()\n  const pathname = usePathname()\n  const reduced = useReducedMotion()\n  const overlayRef = useRef<HTMLDivElement>(null)\n  const pageRef = useRef<HTMLDivElement>(null)\n  const [isTransitioning, setIsTransitioning] = useState(false)\n  const [coverLabel, setCoverLabel] = useState<string | undefined>(undefined)\n  const { contextSafe } = useGSAP({ scope: overlayRef })\n\n  const count = Math.max(1, Math.round(columns))\n  const settings = { variant, duration, ease, reduced, label, customNavigate, router }\n  const settingsRef = useRef(settings)\n  const pathnameRef = useRef(pathname)\n  const waitersRef = useRef<(() => void)[]>([])\n  const runRef = useRef<{ id: number; phase: Phase; target: string; label?: string; pending: Pending | null }>({\n    id: 0,\n    phase: \"idle\",\n    target: \"\",\n    pending: null,\n  })\n\n  useLayoutEffect(() => {\n    settingsRef.current = settings\n  })\n\n  // A pathname change means the new route has committed; release anyone waiting on it\n  useEffect(() => {\n    pathnameRef.current = pathname\n    const waiters = waitersRef.current\n    waitersRef.current = []\n    waiters.forEach((resolve) => resolve())\n  }, [pathname])\n\n  // Abort in-flight transitions on unmount (and StrictMode's simulated unmount)\n  useEffect(() => {\n    const run = runRef.current\n    return () => {\n      run.id++\n      run.phase = \"idle\"\n      run.pending = null\n    }\n  }, [])\n\n  const waitForRoute = useCallback(async (href: string, result: void | Promise<void>, custom: boolean) => {\n    if (custom) {\n      await result\n      await frame()\n      await frame()\n      await wait(40)\n      return\n    }\n    const target = new URL(href, window.location.href).pathname\n    if (target !== pathnameRef.current) {\n      await Promise.race([new Promise<void>((r) => waitersRef.current.push(r)), wait(8000)])\n    }\n    await frame()\n    await frame()\n  }, [])\n\n  const start = useCallback(\n    async (href: string, options: PageTransitionNavigateOptions): Promise<Pending | null> => {\n      const overlay = overlayRef.current\n      const page = pageRef.current\n      if (!overlay || !page) return null\n      const s = settingsRef.current\n      const run = runRef.current\n      const id = ++run.id\n      const resolveLabel = (h: string) => options.label ?? (typeof s.label === \"function\" ? s.label(h) : s.label)\n      const text = resolveLabel(href)\n      run.phase = \"cover\"\n      run.target = href\n      setIsTransitioning(true)\n      setCoverLabel(text)\n\n      const q = gsap.utils.selector(overlay)\n      const d = Math.max(0.15, s.duration)\n      const E = EASES[s.ease] ?? EASES.power4\n\n      // Click point relative to the overlay, for the iris\n      const box = overlay.getBoundingClientRect()\n      const cx = options.x === undefined ? box.width / 2 : options.x - box.left\n      const cy = options.y === undefined ? box.height / 2 : options.y - box.top\n      const R =\n        Math.max(\n          Math.hypot(cx, cy),\n          Math.hypot(box.width - cx, cy),\n          Math.hypot(cx, box.height - cy),\n          Math.hypot(box.width - cx, box.height - cy),\n        ) + 2\n\n      // ---------- cover ----------\n      const cover = contextSafe(() => {\n        const tl = gsap.timeline()\n        tl.set(overlay, { visibility: \"visible\" }).set(q(\"[data-pt-label]\"), { yPercent: 110 })\n        if (s.reduced) {\n          tl.fromTo(q(\"[data-pt-fade]\"), { opacity: 0 }, { opacity: 1, duration: 0.25, ease: \"power1.out\" })\n          return tl\n        }\n        if (s.variant === \"curtain\") {\n          tl.set(q(\"[data-pt-panel]\"), { willChange: \"transform\" })\n            .fromTo(q(\"[data-pt-panel]\"), { yPercent: 118 }, { yPercent: 0, duration: d, ease: E })\n            .fromTo(q(\"[data-pt-curve-top]\"), { scaleY: 1 }, { scaleY: 0, duration: d, ease: E }, \"<\")\n            .set(q(\"[data-pt-curve-bottom]\"), { scaleY: 1 })\n        } else if (s.variant === \"stairs\") {\n          tl.fromTo(q(\"[data-pt-col]\"), { yPercent: 100 }, { yPercent: 0, duration: d, ease: E, stagger: d * 0.08 })\n        } else if (s.variant === \"blinds\") {\n          tl.fromTo(\n            q(\"[data-pt-bar]\"),\n            { scaleY: 0, transformOrigin: \"50% 0%\" },\n            { scaleY: 1, duration: d * 0.8, ease: E, stagger: d * 0.06 },\n          )\n        } else if (s.variant === \"iris\") {\n          tl.fromTo(\n            q(\"[data-pt-panel]\"),\n            { clipPath: `circle(0px at ${cx}px ${cy}px)` },\n            { clipPath: `circle(${R}px at ${cx}px ${cy}px)`, duration: d, ease: E },\n          )\n        } else {\n          const f = pageFrame(page, overlay)\n          const panel = q(\"[data-pt-panel]\")\n          tl.set(page, { transformOrigin: f.origin, clipPath: f.clip(0), willChange: \"transform\" })\n            .to(page, { scale: 0.9, clipPath: f.clip(28), duration: d * 0.45, ease: E })\n            .fromTo(\n              panel,\n              { yPercent: 100, scale: 0.9, clipPath: \"inset(0px round 28px)\" },\n              { yPercent: 0, duration: d * 0.8, ease: E },\n              d * 0.25,\n            )\n            .to(page, { y: -f.height * 0.25, opacity: 0.4, duration: d * 0.8, ease: E }, \"<\")\n            .to(panel, { scale: 1, clipPath: \"inset(0px round 0px)\", duration: d * 0.4, ease: E })\n        }\n        if (text) tl.to(q(\"[data-pt-label]\"), { yPercent: 0, duration: 0.5, ease: \"expo.out\" }, \"-=0.1\")\n        return tl\n      })()\n\n      await finished(cover)\n      if (id !== run.id) return null\n\n      // ---------- navigate ----------\n      run.phase = \"wait\"\n      const target = run.target\n      const nextText = target === href ? text : resolveLabel(target)\n      if (target !== href) setCoverLabel(nextText)\n      const custom = !!s.customNavigate\n      const result = s.customNavigate\n        ? s.customNavigate(target)\n        : options.replace\n          ? s.router.replace(target)\n          : s.router.push(target)\n      await waitForRoute(target, result, custom)\n      if (id !== run.id) return null\n\n      // ---------- reveal ----------\n      run.phase = \"reveal\"\n      const reveal = contextSafe(() => {\n        const tl = gsap.timeline()\n        if (text && !s.reduced) tl.to(q(\"[data-pt-label]\"), { yPercent: -110, duration: 0.35, ease: \"power3.in\" })\n        if (s.reduced) {\n          tl.to(q(\"[data-pt-fade]\"), { opacity: 0, duration: 0.25, ease: \"power1.out\" })\n        } else if (s.variant === \"curtain\") {\n          tl.to(q(\"[data-pt-panel]\"), { yPercent: -100, duration: d, ease: E }, \"-=0.05\").to(\n            q(\"[data-pt-curve-bottom]\"),\n            { scaleY: 0, duration: d, ease: E },\n            \"<\",\n          )\n        } else if (s.variant === \"stairs\") {\n          tl.to(q(\"[data-pt-col]\"), { yPercent: -100, duration: d, ease: E, stagger: d * 0.08 }, \"-=0.05\")\n        } else if (s.variant === \"blinds\") {\n          tl.set(q(\"[data-pt-bar]\"), { transformOrigin: \"50% 100%\" }).to(\n            q(\"[data-pt-bar]\"),\n            { scaleY: 0, duration: d * 0.8, ease: E, stagger: d * 0.06 },\n            \"-=0.05\",\n          )\n        } else if (s.variant === \"iris\") {\n          const panel = q(\"[data-pt-panel]\")[0]\n          const w = panel.offsetWidth\n          const h = panel.offsetHeight\n          const hole = { r: 0 }\n          tl.set(panel, { clipPath: holePolygon(w, h, cx, cy, 0) }).to(hole, {\n            r: R,\n            duration: d,\n            ease: E,\n            onUpdate: () => {\n              panel.style.clipPath = holePolygon(w, h, cx, cy, hole.r)\n            },\n          })\n        } else {\n          const f = pageFrame(page, overlay)\n          const panel = q(\"[data-pt-panel]\")\n          tl.set(page, { transformOrigin: f.origin, clipPath: f.clip(28), scale: 0.9, y: 0, opacity: 1 })\n            .to(panel, { scale: 0.9, clipPath: \"inset(0px round 28px)\", duration: d * 0.4, ease: E })\n            .to(panel, { yPercent: -100, duration: d * 0.8, ease: E })\n            .to(page, { scale: 1, clipPath: f.clip(0), duration: d * 0.5, ease: E }, \"-=0.25\")\n        }\n        return tl\n      })()\n\n      await finished(reveal)\n      if (id !== run.id) return null\n\n      contextSafe(() => {\n        gsap.set(overlay, { visibility: \"hidden\" })\n        gsap.set(q(\"[data-pt-panel], [data-pt-col], [data-pt-bar]\"), { clearProps: \"clipPath,willChange\" })\n        gsap.set(page, { clearProps: \"transform,transformOrigin,clipPath,opacity,willChange\" })\n      })()\n      run.phase = \"idle\"\n      setIsTransitioning(false)\n\n      const next = run.pending\n      run.pending = null\n      return next && next.href !== run.target ? next : null\n    },\n    [contextSafe, waitForRoute],\n  )\n\n  const navigate = useCallback(\n    (href: string, options: PageTransitionNavigateOptions = {}) => {\n      const run = runRef.current\n      const s = settingsRef.current\n      if (run.phase === \"cover\") {\n        run.target = href\n        return\n      }\n      if (run.phase !== \"idle\") {\n        run.pending = { href, options }\n        return\n      }\n      // Same route (hash or query change): no cover needed\n      if (!s.customNavigate) {\n        const url = new URL(href, window.location.href)\n        if (url.pathname === pathnameRef.current) {\n          if (options.replace) s.router.replace(href)\n          else s.router.push(href)\n          return\n        }\n      }\n      void (async () => {\n        let next: Pending | null = { href, options }\n        while (next) next = await start(next.href, next.options)\n      })()\n    },\n    [start],\n  )\n\n  const prefetch = useCallback((href: string) => {\n    const s = settingsRef.current\n    if (s.customNavigate || new URL(href, window.location.href).origin !== window.location.origin) return\n    s.router.prefetch(href)\n  }, [])\n\n  const value = useMemo<PageTransitionContextValue>(\n    () => ({ navigate, prefetch, isTransitioning, custom: !!customNavigate }),\n    [navigate, prefetch, isTransitioning, customNavigate],\n  )\n\n  const curve = \"absolute left-[-25%] h-[16cqh] w-[150%]\"\n  let layers: React.ReactNode = null\n  if (variant === \"curtain\") {\n    layers = (\n      <div data-pt-panel className=\"absolute inset-0\" style={{ background: color }}>\n        <div\n          data-pt-curve-top\n          className={cn(curve, \"bottom-[calc(100%-1px)] origin-bottom rounded-t-[100%]\")}\n          style={{ background: color }}\n        />\n        <div\n          data-pt-curve-bottom\n          className={cn(curve, \"top-[calc(100%-1px)] origin-top rounded-b-[100%]\")}\n          style={{ background: color }}\n        />\n      </div>\n    )\n  } else if (variant === \"stairs\") {\n    layers = (\n      <div className=\"absolute inset-0 flex\">\n        {Array.from({ length: count }, (_, i) => (\n          <div key={i} data-pt-col className=\"-mr-px h-full flex-1\" style={{ background: color }} />\n        ))}\n      </div>\n    )\n  } else if (variant === \"blinds\") {\n    layers = (\n      <div className=\"absolute inset-0 flex flex-col\">\n        {Array.from({ length: count }, (_, i) => (\n          <div key={i} data-pt-bar className=\"-mb-px w-full flex-1\" style={{ background: color }} />\n        ))}\n      </div>\n    )\n  } else {\n    layers = <div data-pt-panel className=\"absolute inset-0\" style={{ background: color }} />\n  }\n\n  return (\n    <PageTransitionContext.Provider value={value}>\n      <div ref={pageRef} className={className}>\n        {children}\n      </div>\n      <div\n        ref={overlayRef}\n        aria-hidden\n        className={cn(\n          \"inset-0 z-[9999] overflow-hidden [container-type:size]\",\n          isTransitioning ? \"pointer-events-auto\" : \"pointer-events-none\",\n          position,\n          overlayClassName,\n        )}\n        style={{ visibility: \"hidden\", color: foreground }}\n      >\n        {layers}\n        <div data-pt-fade className=\"absolute inset-0 opacity-0\" style={{ background: color }} />\n        <div className=\"absolute inset-0 grid place-items-center\">\n          <span className=\"block overflow-hidden pb-[0.08em]\">\n            <span data-pt-label className=\"flex items-center gap-[0.3em] text-[clamp(2rem,7cqw,5rem)] font-medium tracking-tight\">\n              {coverLabel && <span className=\"size-[0.18em] shrink-0 rounded-full\" style={{ background: accent }} />}\n              {coverLabel}\n            </span>\n          </span>\n        </div>\n      </div>\n    </PageTransitionContext.Provider>\n  )\n}\n\n/** Returns `navigate(href, options?)` and `isTransitioning`. Falls back to plain navigation outside a provider. */\nexport function usePageTransition() {\n  const ctx = useContext(PageTransitionContext)\n  return useMemo(\n    () => ({\n      navigate: ctx?.navigate ?? ((href: string) => window.location.assign(href)),\n      isTransitioning: ctx?.isTransitioning ?? false,\n    }),\n    [ctx],\n  )\n}\n\nexport interface TransitionLinkProps extends Omit<React.ComponentProps<\"a\">, \"href\"> {\n  /** Destination. External links and modifier-clicks behave like a normal anchor. */\n  href: string\n  /** Text shown on the cover for this link. Overrides the provider `label`. */\n  label?: string\n  /** Replace the current history entry instead of pushing one. Default: false */\n  replace?: boolean\n  /** Prefetch the route on hover and focus (default router only). Default: true */\n  prefetch?: boolean\n}\n\n/** Drop-in anchor that plays the page transition on plain left-clicks. */\nexport function TransitionLink({\n  href,\n  label,\n  replace = false,\n  prefetch = true,\n  onClick,\n  onMouseEnter,\n  onFocus,\n  target,\n  download,\n  ...props\n}: TransitionLinkProps) {\n  const ctx = useContext(PageTransitionContext)\n\n  const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {\n    onClick?.(e)\n    if (e.defaultPrevented || !ctx) return\n    if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return\n    if ((target && target !== \"_self\") || download !== undefined) return\n    const url = new URL(href, window.location.href)\n    if (url.origin !== window.location.origin) return\n    // In-page anchors scroll natively when the real router is in use\n    if (!ctx.custom && url.pathname === window.location.pathname && url.search === window.location.search && url.hash) return\n    e.preventDefault()\n    // detail === 0 means keyboard activation: no meaningful pointer position\n    const point = e.detail === 0 ? {} : { x: e.clientX, y: e.clientY }\n    ctx.navigate(href, { ...point, label, replace })\n  }\n\n  const warm = () => {\n    if (prefetch && ctx) ctx.prefetch(href)\n  }\n\n  return (\n    <a\n      href={href}\n      target={target}\n      download={download}\n      onClick={handleClick}\n      onMouseEnter={(e) => {\n        onMouseEnter?.(e)\n        warm()\n      }}\n      onFocus={(e) => {\n        onFocus?.(e)\n        warm()\n      }}\n      {...props}\n    />\n  )\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs, live playground and every prop: https://trytweenly.vercel.app/docs/components/page-transition",
  "type": "registry:component"
}