HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1431 min read

View Transitions API

You're animating screenshots, not your DOM

Web developmentweb development / animation & UX

One analogy, hold it tight

The browser photographs the old page, runs your DOM swap, photographs the new page, then crossfades the two photos. Your real DOM is already fully in its new state the entire time the animation plays.

That is literally the shape of the API:

document.startViewTransition(() => {
  renderNewState();   // the moment BETWEEN the two photos
});

Four phases: capture old → run callback → capture new → animate pseudo-elements.

Named elements get paired photos

Give the same view-transition-name to the old and the new element and the browser tweens position, size and opacity between their snapshots:

.hero { view-transition-name: hero; }

You then style a generated pseudo-element tree:

::view-transition-group(hero)        /* position + size */
::view-transition-old(hero)          /* the old photo */
::view-transition-new(hero)          /* the new photo */

::view-transition-group(hero) { animation-duration: 300ms; }

Where the analogy breaks, and bites

  • A name must be unique among visible elements at capture time. Two cards both carrying view-transition-name: card and the whole transition is skipped — silently, no error. This is the #1 reason "it just doesn't animate." Use per-item names: view-transition-name: card-42.
  • Snapshots are flat images. Text doesn't reflow mid-animation, it squashes. A scrolled overflow container or a sticky header photographs as it looks, not as a live element.
  • Rendering is blocked between the two photos. So await fetch() BEFORE you call startViewTransition, never inside the callback — an await in there freezes the UI for the full round trip:
const data = await fetch(url).then(r => r.json());   // ✅ first
document.startViewTransition(() => paint(data));     // then swap
  • The callback must finish fast and may not depend on a layout read of the new state; you get one synchronous swap, not a loop.

Debugging a transition that does nothing

startViewTransition returns promises. ready rejects when the transition is skipped — that is your error channel:

const t = document.startViewTransition(swap);
t.ready.catch(err => console.warn('skipped:', err));
await t.finished;   // resolves even if skipped

Chrome DevTools → Animations panel lets you pause and scrub it frame by frame.

Cross-document, zero JavaScript

Multi-page navigations opt in with CSS only. Both pages, same origin:

@view-transition { navigation: auto; }

And always:

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*) { animation: none; }
}

Your exercise

Build a list → detail view where the thumbnail grows into the hero image. Assign view-transition-name: item-${id} to the clicked card only, and clear it after t.finished resolves. Then deliberately break it: give two visible cards the same name. Catch t.ready and read the rejection — that message is the one you'll need at 2am someday.