Loading...
Loading...
This skill should be used when the user asks to "animate page or route transitions", "add a page transition or crossfade between views", "animate route changes in Next.js App Router", "use the View Transitions API", "AnimatePresence exit doesn't fire on navigation", or "Framer Motion exit animation not working / exit animation before unmount in Next.js". Covers enter/exit page-transition animation with the View Transitions API and Framer Motion, for Next.js App Router and general SPA routing.
npx skill4agent add iart-ai/web-animation-skills page-transition-animationAnimatePresenceAnimatePresenceAnimatePresenceAnimatePresencenext-view-transitionsexitAnimatePresenceAnimatePresenceAnimatePresencekeytemplate.tsxlayout.tsxAnimatePresence// app/template.tsx
'use client';
import { AnimatePresence } from 'framer-motion';
import { usePathname } from 'next/navigation';
import { FrozenRouter } from './frozen-router';
import { PageTransition } from './page-transition';
export default function Template({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
return (
<AnimatePresence mode="wait" initial={false}>
{/* key on pathname so AnimatePresence sees old removed / new added */}
<PageTransition key={pathname}>
{/* FrozenRouter keeps the OUTGOING tree rendering its old content
while it animates out */}
<FrozenRouter>{children}</FrozenRouter>
</PageTransition>
</AnimatePresence>
);
}// app/page-transition.tsx
'use client';
import { motion } from 'framer-motion';
export function PageTransition({ children }: { children: React.ReactNode }) {
return (
<motion.div
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -12 }}
transition={{ duration: 0.3, ease: [0.22, 1, 0.36, 1] }}
>
{children}
</motion.div>
);
}// app/frozen-router.tsx
'use client';
import { useContext, useRef } from 'react';
import {
LayoutRouterContext,
} from 'next/dist/shared/lib/app-router-context.shared-runtime';
// Freezes the router context so the exiting page keeps rendering its
// OWN content during the exit animation instead of the next route's.
export function FrozenRouter({ children }: { children: React.ReactNode }) {
const context = useContext(LayoutRouterContext ?? {});
const frozen = useRef(context).current;
return (
<LayoutRouterContext.Provider value={frozen}>
{children}
</LayoutRouterContext.Provider>
);
}mode="wait"initial={false}key={pathname}FrozenRouterLayoutRouterContextLayoutRouterContext/* globals.css */
@view-transition { navigation: auto; } /* opt MPA-style in (where supported) */
::view-transition-old(root) { animation: fade 0.25s both reverse; }
::view-transition-new(root) { animation: fade 0.25s both; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }if (document.startViewTransition) {
document.startViewTransition(() => router.push(href));
} else {
router.push(href);
}next-view-transitions// app/layout.tsx
import { ViewTransitions } from 'next-view-transitions';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<ViewTransitions>
<html lang="en"><body>{children}</body></html>
</ViewTransitions>
);
}// use the library's Link instead of next/link
import { Link } from 'next-view-transitions';
<Link href="/about">About</Link>;if (document.startViewTransition)AnimatePresencetemplate.tsxkey={pathname}exitmotion.*AnimatePresencemode="wait"'use client'mode="wait"Packaged helper ():scripts/freezes thescripts/seek-shot.sh anim.html 0 1.5 3harness and screenshots each moment;?t=Ntiles them for one-glance review. Seescripts/contact-sheet.sh sheet.png frame-*.png.scripts/README.md
motion.html<script type="module">motiondocument.startViewTransition::view-transition-*?view=b?phase=exit<script>
const p = new URLSearchParams(location.search);
document.documentElement.dataset.view = p.get("view") || "a"; // CSS/React keys off this
// Framer Motion: set the controlled key/route from ?view; for mode="wait" the resolved end is the new page
// View Transitions: screenshot the END state (snapshots are transient); or pause via emulate slow animations
window.__ready = true;
</script>?view=a?phase=exit?view=bmode="wait"npx playwright screenshot --wait-for-timeout=500 "file://$PWD/transition.html?view=b" dest.pngmotionstartViewTransition?view=?phase=prefers-reduced-motionmode="wait"| Need | Approach |
|---|---|
| Per-route enter animation | |
| Exit animation on nav | keyed |
| Outgoing page shows old content | FrozenRouter (snapshot |
| Detect route change | |
| Simple cross-page fade/morph | View Transitions API / next-view-transitions |
| Shared element transition | |
| Skip first-load animation | |
layout.tsxtemplate.tsxLayoutRouterContextkey={pathname}mode="wait"document.startViewTransition'use client'references/full-examples.mdview-transition-namemode="wait"loading.tsx