Page transitions in Next.js: Motion in the Pages Router, ViewTransition in the App Router

A page transition is a short animation, usually a fade or slide, that plays when the user navigates from one route to another. Next.js doesn’t animate route changes on its own, and how you add one depends on which router your app uses.
Pages Router: Motion and AnimatePresence
Section titled “Pages Router: Motion and AnimatePresence”In the Pages Router every page is rendered through pages/_app.js, so that is the one place to add the transition. Install Motion:
npm install motionThen wrap the page component in AnimatePresence and a motion.div keyed by the current path:
import { AnimatePresence, motion } from 'motion/react';import { useRouter } from 'next/router';import '../styles/globals.css';
export default function App({ Component, pageProps }) { const router = useRouter();
return ( <AnimatePresence mode="wait" initial={false}> <motion.div key={router.asPath} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.3, ease: 'easeInOut' }} > <Component {...pageProps} /> </motion.div> </AnimatePresence> );}How it works:
- When the route changes,
router.asPathchanges, so themotion.divgets a newkey. React treats that as removing the old page and adding a new one. AnimatePresencekeeps the old page mounted until itsexitanimation (fade toopacity: 0) finishes.mode="wait"makes the new page wait until the old one has gone, then it animates frominitialtoanimate.initial={false}skips the fade on the very first page load.
Pages don’t need any changes; they stay plain components.
Scroll position
Section titled “Scroll position”Next.js scrolls to the top when the route changes, which happens before the old page has finished fading out, so you see it jump. To scroll after the exit animation instead, turn off the automatic scroll on your links and scroll in onExitComplete:
<AnimatePresence mode="wait" initial={false} onExitComplete={() => window.scrollTo(0, 0)}>import Link from 'next/link';
<Link href="/about" scroll={false}>About</Link>App Router: React’s ViewTransition
Section titled “App Router: React’s ViewTransition”The AnimatePresence approach doesn’t carry over well to the App Router, because layouts stay mounted and pages are rendered by the router rather than through a single component you control.
The approach the Next.js docs describe for the App Router is React’s <ViewTransition> component, built on the browser’s View Transitions API. In Next.js, route navigations run as React transitions, so a <ViewTransition> boundary animates automatically when you navigate. With no props it uses the browser’s default cross-fade:
import { ViewTransition } from 'react';
export default function AboutPage() { return ( <ViewTransition> <main> <h1>About</h1> </main> </ViewTransition> );}You can customise the animation with the ::view-transition-old(...) and ::view-transition-new(...) CSS pseudo-elements and with the component’s enter, exit and default props. Browsers without View Transitions support simply navigate without animating.
The current Next.js guide (16.4) says this works in the App Router with no configuration. Earlier releases put it behind the experimental.viewTransition flag in next.config, so check the docs for the Next.js version you are on.
Gotchas
Section titled “Gotchas”- Keep it short. 200–400 ms is plenty. Long exit animations make navigation feel slow, and with
mode="wait"the user waits for the exit and the enter. - Reduced motion. Respect
prefers-reduced-motion. Motion’sMotionConfighas areducedMotion="user"setting, and for view transitions you can disable the animation in a@media (prefers-reduced-motion: reduce)block. - Don’t key on anything that changes within a page. Keying the
motion.divonrouter.asPathmeans a query-string or hash change also triggers a full fade. Key onrouter.pathnameif that’s not what you want.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)