I added page transitions to this site with React's <ViewTransition>, and they seemed to work. Then I noticed they only played from the home page. Going from the projects list to a project, from one project to the next, or from the notes list to a post: nothing. This note is about why, and the small fix.
The setup that half works
Next.js runs page navigations as React transitions, so a <ViewTransition> with enter and exit animations plays whenever its content mounts or unmounts during a navigation. Layouts stay mounted between pages, so the usual advice is to put the wrapper in a template.tsx, which renders anew on navigation:
// app/(frontend)/template.tsx
import { ViewTransition } from 'react'
export default function Template({ children }: { children: React.ReactNode }) {
return (
<ViewTransition enter="page-enter" exit="page-exit" default="none">
{children}
</ViewTransition>
)
}With CSS for the two animation classes (further down), the old page fades out and the new one fades in. From / to /contact-me it worked every time.
Why it only works on some pages
A template doesn't re-render on every navigation. It re-renders when the route segment directly below it changes.
This template sits at the top of the site, so the segment below it is the first part of the URL:
| From | To | Segment below the template | Transition |
|---|---|---|---|
/ | /contact-me | changes | yes |
/about-me | /projects | changes | yes |
/projects | /projects/tuesday | projects both times | no |
/projects/tuesday | /projects/dogs-in-balance | projects both times | no |
/notes | /notes/some-post | notes both times | no |
When the segment stays the same, the template and its <ViewTransition> stay mounted. Nothing enters or exits, so no animation runs. For a site with a translated version under /nl, every Dutch page change falls in the "no" column too.
The fix: key the transition by the path
A <ViewTransition> also enters and exits when its key changes. So the fix is to key it by the current path, in a small client component:
// components/PageTransition.tsx
'use client'
import { ViewTransition } from 'react'
import { usePathname } from 'next/navigation'
export default function PageTransition({ children }: { children: React.ReactNode }) {
const pathname = usePathname()
return (
<ViewTransition key={pathname} enter="page-enter" exit="page-exit" default="none">
{children}
</ViewTransition>
)
}// app/(frontend)/template.tsx
import PageTransition from '@/components/PageTransition'
export default function Template({ children }: { children: React.ReactNode }) {
return <PageTransition>{children}</PageTransition>
}Every new path is a new key, so the old content exits and the new content enters, whatever part of the URL changed. usePathname leaves out the query string, so changing a filter like ?type=design doesn't trigger a page transition, which is what you want.
The animations
The enter and exit values become classes on the view-transition pseudo-elements. A fade with a small lift reads as "new page" without getting in the way:
/* Clicks during the animation reach the live page */
::view-transition {
pointer-events: none;
}
/* The header and footer live outside the wrapper: swap them instantly */
::view-transition-old(root) {
display: none;
}
::view-transition-new(root) {
animation: none;
}
::view-transition-old(.page-exit) {
animation: 180ms cubic-bezier(0.4, 0, 1, 1) both page-out;
}
::view-transition-new(.page-enter) {
/* Starts after the old page is gone, so the two never overlap */
animation: 520ms cubic-bezier(0.22, 1, 0.36, 1) 160ms both page-in;
}
@keyframes page-out {
to {
opacity: 0;
translate: 0 -16px;
filter: blur(2px);
}
}
@keyframes page-in {
from {
opacity: 0;
translate: 0 40px;
filter: blur(6px);
}
}
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}Two details worth copying. The enter animation waits until the exit has finished; without that delay, both pages are briefly visible on top of each other. And hiding the old root snapshot keeps the navbar from flickering, since it doesn't change between pages anyway.
TypeScript
ViewTransition ships in the React build that Next.js bundles for the App Router, but at the time of writing it isn't in the stable @types/react yet. One line in a .d.ts file pulls in the types:
// types/react-canary.d.ts
/// <reference types="react/canary" />How to check it really runs
"It seems to work" is how I missed this the first time. To be sure, wrap document.startViewTransition and list the animations each navigation starts:
const start = document.startViewTransition.bind(document)
document.startViewTransition = (...args) => {
const transition = start(...args)
transition.ready.then(() => {
const names = document
.getAnimations()
.filter((animation) => (animation.effect as KeyframeEffect)?.pseudoElement)
.map((animation) => (animation as CSSAnimation).animationName)
console.log(location.pathname, names)
})
return transition
}Paste it into the console, click through the site, and every navigation should log page-out and page-in. If a navigation logs nothing, that route isn't swapping its content. That's exactly what the projects and notes pages did here before the fix.
Browsers without the View Transitions API just change pages as before, so there's nothing to fall back to.