View transitions

Topic 2 of 2 in Styling & motion8 min readIntermediateFree course

Overview

Animation helps users follow what changed: a new page slides in, a removed item fades out, a thumbnail grows into a full-size image. Browsers now have a built-in View Transition API that snapshots the page before and after a DOM update and animates between them. React 19.3 integrates it with the <ViewTransition> component — and it’s now stable.

Four steps: React snapshots the old UI; a startTransition state update happens; React commits the new DOM; the browser animates from old to new. Code: a ViewTransition wrapping either CourseDetails or CourseSummary depending on showDetails, and startTransition(() => setShowDetails(true)) — only Transitions animate.

The basics

JSX
import { ViewTransition, startTransition, useState } from 'react';

function CourseSection({ course }) {
  const [showDetails, setShowDetails] = useState(false);

  return (
    <>
      <ViewTransition>
        {showDetails ? <CourseDetails course={course} /> : <CourseSummary course={course} />}
      </ViewTransition>
      <button onClick={() => startTransition(() => setShowDetails((s) => !s))}>
        {showDetails ? 'Show less' : 'Show more'}
      </button>
    </>
  );
}

That’s all it takes for a smooth cross-fade between the summary and the details.

Only Transitions animate

The crucial rule: <ViewTransition> only activates for updates that are Transitions:

  • state updates inside startTransition (or useTransition),
  • Suspense boundaries revealing content,
  • updates from useDeferredValue,
  • navigations in routers and frameworks that use transitions.

A plain setShowDetails(true) is an urgent update and is applied instantly, without animation. This is deliberate: typing, clicking checkboxes and other immediate feedback should never wait for an animation.

Four kinds of animation

React decides what kind of animation to run from how the tree changed:

Trigger When
enter a <ViewTransition> is added to the tree
exit a <ViewTransition> is removed
update content or layout inside a <ViewTransition> changes
share a named <ViewTransition> unmounts in one place and mounts in another

Each kind can be customised with a prop of the same name, plus default for anything not specified. Values can be:

  • "auto" — the browser’s default cross-fade,
  • "none" — no animation for that trigger,
  • a class name — React applies it during the animation so you can style it in CSS,
  • an object keyed by transition type (below).
JSX
<ViewTransition enter="slide-up" exit="fade-out" update="none">
  {isOpen && <Panel />}
</ViewTransition>

Styling with CSS

During the animation, the browser creates pseudo-elements for the old and new snapshots. Target the class you passed:

CSS
@keyframes slide-up { from { transform: translateY(16px); opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }

::view-transition-new(.slide-up) {
  animation: 250ms ease-out slide-up;
}

::view-transition-old(.fade-out) {
  animation: 150ms ease-in fade-out;
}
  • ::view-transition-old(.x) — the snapshot of what’s leaving,
  • ::view-transition-new(.x) — the live view of what’s arriving,
  • ::view-transition-group(.x) — the container that animates size and position between them.

Enter and exit animations

Conditionally rendering a <ViewTransition> gives it enter and exit animations:

JSX
{showToast && (
  <ViewTransition enter="slide-up" exit="fade-out">
    <Toast message="Saved!" />
  </ViewTransition>
)}

The <ViewTransition> must be the first thing in the part of the tree that appears — if a <div> wraps it, the div is what enters and exits, and the boundary only sees updates.

Changing a <ViewTransition>’s key also makes the old one exit and a new one enter — a simple way to animate between items in a slideshow or tabs.

Transition types: animating by direction

The same state change can mean different things: “next” should slide left, “previous” should slide right. Mark the reason with addTransitionType inside the transition, and map types to classes with objects:

JSX
import { addTransitionType, startTransition } from 'react';

function goNext() {
  startTransition(() => {
    addTransitionType('next');
    setIndex((i) => i + 1);
  });
}

<ViewTransition
  key={slide.id}
  enter={{ next: 'slide-from-right', previous: 'slide-from-left', default: 'auto' }}
  exit={{ next: 'slide-to-left', previous: 'slide-to-right', default: 'auto' }}
>
  <Slide slide={slide} />
</ViewTransition>

Shared element transitions

When the same thing appears in two places — a course thumbnail in a list and as a hero image on the course page — give both <ViewTransition>s the same name. When one unmounts and the other mounts in the same transition, the browser morphs one into the other:

JSX
function CourseThumbnail({ course, onOpen }) {
  return (
    <ViewTransition name={`course-image-${course.id}`}>
      <img src={course.image} alt="" onClick={() => startTransition(() => onOpen(course.id))} />
    </ViewTransition>
  );
}

function CourseHero({ course }) {
  return (
    <ViewTransition name={`course-image-${course.id}`}>
      <img src={course.image} alt={course.title} className="hero" />
    </ViewTransition>
  );
}

Names must be unique across everything currently mounted — include an id for list items. React reports an error if two mounted boundaries share a name.

Suspense and loading

Wrapping a Suspense boundary animates the swap from fallback to content:

JSX
<ViewTransition>
  <Suspense fallback={<CourseSkeleton />}>
    <CourseDetails id={id} />
  </Suspense>
</ViewTransition>

When the data arrives, the skeleton cross-fades into the real content instead of snapping. Combined with a router that navigates in transitions, you get animated page changes almost for free.

Accessibility and restraint

  • Respect reduced motion. React doesn’t turn animations off for you. Users who set “reduce motion” in their OS should get none (or a simple fade):
CSS
@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
}
  • Keep it short — 150–300 ms. Animations should explain changes, not slow people down.
  • Don’t animate everything. Use default="none" on boundaries where only one kind of animation makes sense.

Browser support

The View Transition API is supported in Chrome and Edge (since version 111), Safari (since 18) and Firefox (since 144). Transition types need slightly newer versions (Chrome 125, Safari 18.2, Firefox 147). Where the API isn’t available, React simply applies the update without animation — your app still works. <ViewTransition> works with React DOM only; React Native support is in progress.

Don’t call document.startViewTransition yourself in a React app — React manages view transitions and a manual call would interrupt them.

Exercise

An animated course switcher

Build CourseCarousel({ courses }) showing one course at a time with “Previous” and “Next” buttons. Wrap the visible course in a <ViewTransition> keyed by the course id, and update the index inside startTransition with addTransitionType('next') or addTransitionType('previous'). Use enter/exit objects so “next” slides in from the right and “previous” from the left, and write the CSS for the slide-* classes, including a prefers-reduced-motion override.

JavaScript
import { ViewTransition, addTransitionType, startTransition, useState } from 'react';

export function CourseCarousel({ courses }) {
  // your code here
}

Summary

  • <ViewTransition> animates its children using the browser’s View Transition API; it’s stable in React 19.3.
  • Only Transitions animate: updates in startTransition, Suspense reveals and useDeferredValue — not urgent setState calls.
  • React chooses enter, exit, update or share animations; the default is a cross-fade.
  • Pass class names to enter, exit, update, share or default, and style them with ::view-transition-old/new(.class).
  • Give two <ViewTransition>s the same unique name for shared-element animations, and always respect prefers-reduced-motion.