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
2 min readReact

Slide forward and back with addTransitionType

Tag React navigation updates with addTransitionType so forward and back use different slide directions, even when they lead to the same screen.

The Delivery step in a checkout can open from Address or from Review. If both clicks play the same slide, Back looks like another step forward. Pass the direction with the update so React can use a different animation for each trip to Delivery.

Tag the update that changes the step

Call addTransitionType inside the same startTransition callback as the state update. Use forward for Continue and back for Back. The destination remains an index and the direction belongs to the Transition.

Use React and React DOM 19.3 or later. Paste the demo into a React DOM entry point with a root element in the HTML.

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

const steps = ['Address', 'Delivery', 'Review']

function Checkout() {
  const [step, setStep] = useState(0)
  const lastStep = steps.length - 1

  function move(direction, offset) {
    startTransition(() => {
      addTransitionType(direction)
      setStep(step + offset)
    })
  }

  return (
    <main>
      <button disabled={step === 0} onClick={() => move('back', -1)}>
        Back
      </button>
      <button disabled={step === lastStep} onClick={() => move('forward', 1)}>
        Continue
      </button>
      <button onClick={() => startTransition(() => setStep(lastStep))}>
        Jump to review
      </button>
      <ViewTransition
        key={step}
        enter={{ forward: 'enter-right', back: 'enter-left', default: 'none' }}
        exit={{ forward: 'exit-left', back: 'exit-right', default: 'none' }}
      >
        <h1>{steps[step]}</h1>
      </ViewTransition>
    </main>
  )
}

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

Continue from Address and Back from Review both open Delivery, with a different pair of animation classes for each arrival.

The key makes React remove one heading boundary and insert another, which is why these maps use exit and enter. On Jump to review the update still runs in a Transition, but each map falls back to default: 'none'. Use that for a jump where a slide would imply a step-by-step move.

Give each direction a pair of offsets

Write the following in checkout.css. Apply the enter class to the incoming snapshot and the exit class to the outgoing snapshot. Each offset is relative to the heading’s own width.

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

@media (prefers-reduced-motion: no-preference) {
  ::view-transition-new(*.enter-right),
  ::view-transition-old(*.exit-right) {
    --offset: 24%;
  }

  ::view-transition-new(*.enter-left),
  ::view-transition-old(*.exit-left) {
    --offset: -24%;
  }

  ::view-transition-new(*.enter-right),
  ::view-transition-new(*.enter-left) {
    animation: step-in 200ms ease-out both;
  }

  ::view-transition-old(*.exit-right),
  ::view-transition-old(*.exit-left) {
    animation: step-out 160ms ease-in both;
  }
}

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

@keyframes step-in {
  from { transform: translateX(var(--offset)); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes step-out {
  from { transform: translateX(0); opacity: 1; }
  to { transform: translateX(var(--offset)); opacity: 0; }
}

Delivery arrives from the right on Continue and from the left on Back, while Jump to review changes the heading without a slide.

If neither button slides, check that the type strings match the keys in both maps. If you reuse this with Suspense, check which commit contains the new screen. React clears the types after each commit, so a fallback can receive the direction and the content that replaces it can miss it.

For browser Back and Forward, get the direction from your router’s history position before committing the next route. Turn a decrease into back and an increase into forward. Keep the default for a navigation whose direction you don’t know. For help placing those boundaries, see the keyed route example.