View Transitions API
Also called CSS view transitions or startViewTransition
A browser feature that animates between two states of a page by snapshotting the old view, applying your update, and animating to the new one.
import { useState } from 'react';
import { flushSync } from 'react-dom';
const products = [
{ id: 'chair', name: 'Lounge chair', price: '$420' },
{ id: 'lamp', name: 'Arc floor lamp', price: '$180' },
{ id: 'table', name: 'Oak side table', price: '$240' }
];
type Product = (typeof products)[number];
function viewTransition(update: () => void) {
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!document.startViewTransition || reduceMotion) return update();
// flushSync commits the new view inside the callback, before the browser snapshots it.
document.startViewTransition(() => flushSync(update));
}
// Matching names pair a thumbnail with the hero image. Each name is unique on the page.
const image = (product: Product) => ({
src: `/products/${product.id}.jpg`,
style: { viewTransitionName: `product-${product.id}` },
className: 'aspect-square w-full rounded-xl object-cover'
});
export function ProductBrowser() {
const [selected, setSelected] = useState<Product | null>(null);
const show = (next: Product | null) =>
viewTransition(() => setSelected(next));
if (selected) {
return (
<article className='w-80'>
<img {...image(selected)} alt={selected.name} />
<h2 className='mt-4 text-xl font-semibold'>{selected.name}</h2>
<p className='text-muted-foreground'>{selected.price}</p>
<button
type='button'
onClick={() => show(null)}
className='mt-4 rounded-full border px-4 py-2 text-sm font-medium'
>
All products
</button>
</article>
);
}
return (
<ul className='grid w-80 grid-cols-3 gap-3 text-sm'>
{products.map((product) => (
<li key={product.id}>
<button type='button' onClick={() => show(product)}>
<img {...image(product)} alt='' />
{product.name}
</button>
</li>
))}
</ul>
);
}Use the View Transitions API to animate switching between the product list and the product detail view: wrap the state update in document.startViewTransition(), give the clicked product image view-transition-name: product-image on both views so it grows from the thumbnail into the hero, and crossfade the rest over 300ms. Fall back to an instant update when document.startViewTransition is missing, and disable the animations under prefers-reduced-motion.
Paste into Layerwise or any AI coding tool.
View Transitions API explained
The View Transitions API lets the browser do the hard part of a transition. You call document.startViewTransition() with a function that updates the page. The browser captures a snapshot of the current page, runs your update, then animates from the old snapshot to the new, live view. By default, the whole page crossfades.
Give an element a view-transition-name and the browser animates it separately: it moves and resizes that element from its old position to its new one while the rest of the page crossfades. That is how a thumbnail can grow into a hero image without an animation library. Each name must be unique on the page at the moment of the snapshot.
Customize the motion with CSS pseudo-elements such as ::view-transition-old(root) and ::view-transition-new(root), using ordinary animation properties. For multi-page sites, adding @view-transition { navigation: auto; } to both pages animates same-origin navigations in browsers that support it. The old view is a still image during the animation, so keep it short.
Same-document transitions work in current versions of Chrome, Edge, Safari, and Firefox; cross-document support is less complete. Check that document.startViewTransition exists and call your update directly when it does not, and turn the animations off for people who prefer reduced motion.
Often confused with
- Page transition
- A page transition is any animation between pages. The View Transitions API is a built-in browser way to create one, with or without a JavaScript animation library.
Related terms
All termsShared element transition
A transition where an element from one view, like a thumbnail, travels and grows into its place in the next view, linking the two screens.
Crossfade
A transition where the old content fades out while the new content fades in at the same spot, so the two briefly overlap.
Reduced motion
An operating system setting that asks apps to minimize movement, which websites detect with the prefers-reduced-motion media query.
Page transition
An animation that plays when you move from one page or screen to another, such as the old page fading out as the new one fades in.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.


