View Transitions
React's ViewTransition component integrates with the browser's View Transitions API. Name elements that should persist across routes, and the browser animates between their old and new positions — no animation library needed.
Thumbnail → Hero animation
Wrap both the grid thumbnail and the detail view in {" with the same name. The browser finds matching elements and morphs between their size and position automatically.
Click a photo to see the shared element concept
// components/photo-grid.tsx
import { ViewTransition } from 'react'
import Link from 'next/link'
function PhotoGrid({ photos }) {
return (
<div className="grid grid-cols-3 gap-3">
{photos.map((photo) => (
<Link key={photo.id} href={`/photo/${photo.id}`}
transitionTypes={['nav-forward']}>
<ViewTransition name={`photo-${photo.id}`}>
<img src={photo.src} alt={photo.title} />
</ViewTransition>
</Link>
))}
</div>
)
}// app/photo/[id]/photo-content.tsx
import { ViewTransition } from 'react'
async function PhotoContent({ id }) {
const photo = await getPhoto(id)
return (
// Same name = browser animates between positions
<ViewTransition name={`photo-${photo.id}`}>
<div style={{ position: 'relative', aspectRatio: '3 / 2' }}>
<img src={photo.src} alt={photo.title} fill />
</div>
</ViewTransition>
)
}Forward/Back slide animations
Tag links with transitionTypes to communicate direction. Wrap page content in {' that maps types to animations. The header stays fixed using view-transition-name: site-header.
page.tsx, not layout — layouts persist and never trigger enter/exit.// For directional navigation
<Link
href="/photo/next"
transitionTypes={['nav-forward']}
>
Next →
</Link>
<Link
href="/"
transitionTypes={['nav-back']}
>
← Gallery
</Link>
// Wrap page content to respond to direction
<ViewTransition
enter={{ 'nav-forward': 'nav-forward', 'nav-back': 'nav-back', default: 'none' }}
exit={{ 'nav-forward': 'nav-forward', 'nav-back': 'nav-back', default: 'none' }}
default="none"
>
{/* page content */}
</ViewTransition>View Transition pseudo-elements
Target ::view-transition-old(.class) and ::view-transition-new(.class) to customize each transition class. Always add @media (prefers-reduced-motion).
/* globals.css */
::view-transition-old(.nav-forward) {
--slide-offset: -60px;
animation: 150ms ease-in both fade-out, 400ms ease-in-out both slide-x reverse;
}
::view-transition-new(.nav-forward) {
--slide-offset: 60px;
animation: 210ms ease-out 150ms both fade-in, 400ms ease-in-out both slide-x;
}
/* Anchor the header — don't let it slide */
::view-transition-group(site-header) {
animation: none; z-index: 100;
}
::view-transition-old(site-header) { display: none; }
::view-transition-new(site-header) { animation: none; }
/* Accessibility */
@media (prefers-reduced-motion: reduce) {
::view-transition-old(*), ::view-transition-new(*), ::view-transition-group(*) {
animation-duration: 0s !important;
}
}