Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
3 min readReact

Animate route changes with React ViewTransition

Use React ViewTransition to fade between client-side routes and carry a shared cover from a catalog thumbnail into its detail view.

You open a record from a catalog, and the cover jumps from a thumbnail to a full-size image. With React’s <ViewTransition>, you can fade the outgoing and incoming pages while the cover moves between them.

Give each route its own boundary

Put a keyed <ViewTransition> around the part of the page your router replaces. A new key tells React to remove the old boundary and mount a new one, so the old page qualifies for exit and the new one for enter. Keep the navigation outside that boundary so the button keeps focus during the swap.

Use the same name on a second boundary around the cover in each route. React pairs those boundaries when it removes one and inserts the other in the same Transition. The share animation takes precedence over enter and exit for that pair.

This demo uses state in place of a router. Use React and React DOM 19.3 or later and put the example in a React DOM entry point with a root element in the HTML.

import { ViewTransition, startTransition, useState } from 'react'
import { createRoot } from 'react-dom/client'
import './catalog.css'

function Catalog() {
  const [route, setRoute] = useState('catalog')
  const detail = route === 'record'

  function toggleRoute() {
    startTransition(() => {
      setRoute(detail ? 'catalog' : 'record')
    })
  }

  return (
    <main>
      <button onClick={toggleRoute}>
        {detail ? 'Back to catalog' : 'Open record'}
      </button>
      <ViewTransition key={route} enter="page-in" exit="page-out">
        <section>
          <h1>{detail ? 'Night Train' : 'Catalog'}</h1>
          <ViewTransition name="cover-night-train" share="auto">
            <div className={detail ? 'cover cover-large' : 'cover'}>
              Night Train
            </div>
          </ViewTransition>
          <p>{detail ? 'Side A · 18 min' : 'One record in the catalog'}</p>
        </section>
      </ViewTransition>
    </main>
  )
}

createRoot(document.getElementById('root')).render(<Catalog />)

Press Open record to see the small cover grow into its detail view, then press Back to catalog to see it return.

The route change runs inside startTransition so React can start a browser view transition. Call your router’s transition-aware navigation at the same point in a real app. React calls document.startViewTransition() for its own boundary, so a second call around navigation can interfere with that animation. See the ViewTransition reference.

Fade the page around the shared cover

In catalog.css, apply page-in to the new snapshot and page-out to the old one. The dot inside each selector matches the transition class supplied by React. The cover uses the browser’s shared-element animation through share="auto".

.cover {
  display: grid;
  place-items: center;
  width: 6rem;
  aspect-ratio: 1;
  background: midnightblue;
  color: white;
}

.cover-large {
  width: 12rem;
}

::view-transition-old(root),
::view-transition-new(root) {
  animation: none;
}

::view-transition-new(*.page-in) {
  animation: appear 220ms ease-out both;
}

::view-transition-old(*.page-out) {
  animation: disappear 140ms ease-in both;
}

@keyframes appear {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes disappear {
  from { opacity: 1; }
  to { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
}

The page text now fades around the moving cover, and the reduced-motion setting removes the fade and resize.

If the page fades but the cover jumps, check the two cover names first. Both must resolve to cover-night-train in the same route change. For a real catalog, derive the name from the record ID so each visible cover has a unique name.

If the whole route swaps without motion, check whether the router marks its update as a Transition. Then test without the browser’s View Transition API and with reduced motion enabled. You should still reach the detail page and return to the catalog in both cases. To vary the motion by navigation direction once the swap works, tag forward and back with addTransitionType.