Message 1 of 10Monday, 9:02 AM
From: A recruiter
Hi, calling about your resume. Love the experience. Less sure about the floppy disks. My assistant says you put one in the drive and the job comes up on screen? Netflix is the one in front.
React 19 can animate the change between any two renders. Wrap something in <ViewTransition>, change it inside a Transition, and React asks the browser to snapshot the old and new screens and animate from one to the other: a cross-fade by default, or anything CSS or the Web Animations API can do. Below are eight demos, each teaching a different part of the API: try it, see which animations ran, then read what's going on, step by step, next to the code that does it. Turn on slow motion to watch each one closely.
startTransition, a value from useDeferredValue, or a <Suspense> boundary revealing its content. Ordinary updates donât animate.<ViewTransition> boundaries it touches and gives each oneâs first DOM element a view-transition-name, so the browser knows what to track.React picks one for each boundary, from what happened to it. Each one is a prop that takes a class name (enter="slide-in"), and the âLast transitionâ line under every demo shows which ran.
| Prop | When | By default |
|---|---|---|
| enter | The boundary mounts, or its <Activity> is shown. | Fades in. |
| exit | The boundary unmounts, or its <Activity> is hidden. | Fades out. |
| update | Something inside it changed, or it moved or resized. | Its box morphs from the old size and place to the new, cross-fading. |
| share | A named boundary unmounts while another with the same name mounts. | One morphs into the other. |
::view-transition a layer over the whole page
ââ ::view-transition-group(.cls) moves and resizes (update, share)
ââ ::view-transition-image-pair(.cls)
ââ ::view-transition-old(.cls) the âbeforeâ picture (exit)
ââ ::view-transition-new(.cls) the âafterâ, live (enter).cls is the class you passed, so enter="slide-in" is styled with ::view-transition-new(.slide-in) { animation: ⌠}. Reactâs full reference is in its docs.
Try it Tap a card to open it, then go back with the button or your browserâs Back.
Last transitionnothing yet, give it a try
The thumbnail and the big picture are two different elements in two different parts of the tree: one unmounts as the other mounts. On their own theyâd just fade. Giving both the same name tells React theyâre the same thing, so it pairs them up (a share) and the browser animates one box into the other: position, size and a cross-fade between the two pictures.
Everything else in the grid has no partner, so it leaves with a plain exit, and the pageâs text arrives with an enter.
?open=gatsby). useDeferredValue hands back a copy that updates a moment later, as a Transition, and only Transitions animate. Thatâs why the browserâs Back button animates too.<ViewTransition> named after the card.<ViewTransition> with the same name. When one unmounts and the other mounts in the same Transition, React pairs them.share="vt-morph" adds a View Transition Class. CSS uses it to time the group, the box that moves and resizes from the old spot to the new one.1const open = useDeferredValue(search.open)
// In the grid
{GALLERY.map((g) => (
<button key={g.id} onClick={() => onOpen(g.id)}>
2 <ViewTransition name={`vt-photo-${g.id}`} share="vt-morph">
<img src={g.image} alt="" />
</ViewTransition>
</button>
))}
// On the page it opens
3<ViewTransition name={`vt-photo-${item.id}`} share="vt-morph">
<img className="hero" src={item.image} alt={item.title} />
</ViewTransition>
/* vt.css */
4::view-transition-group(.vt-morph) {
animation-duration: 450ms;
animation-timing-function: cubic-bezier(0.2, 0.9, 0.1, 1);
}
Try it Press â on a card to move it along, or let Fry move everything at once.
Last transitionnothing yet, give it a try
A card that changes column is removed from one <ul> and added to another. To React thatâs a brand new element. A name of its own turns that unmount and mount into a share, so the browser flies the card across instead of fading it.
The cards it leaves behind didnât change, they only moved up. React notices that their boxes moved and gives them an update, which slides them into place.
startTransition.update="vt-shift" sets their timing.{items.map((c) => (
1 <ViewTransition key={c.id} name={`vt-chore-${c.id}`}
3 share="vt-fly" update="vt-shift">
<li>{c.text}</li>
</ViewTransition>
))}
const move = (id, by) =>
2 startTransition(() => setChores((all) => moveCard(all, id, by)))
/* vt.css */
::view-transition-group(.vt-fly) { z-index: 2; animation-duration: 520ms; }
4::view-transition-image-pair(.vt-fly) { animation: vt-lift 520ms both; }
@keyframes vt-lift {
50% { transform: rotate(-3deg) scale(1.06); }
}
Try it Vote for a spot near the bottom until it climbs, add a spot, or take one off.
Last transitionnothing yet, give it a try
No names this time. Each row is a keyed <ViewTransition> sitting straight inside the list. When the order changes, React measures every row before and after; a row whose box moved gets an update and slides from the old place to the new one.
A new row is an enter and a removed one an exit, and their neighbours slide to make room.
<ViewTransition> per row, straight inside the <ol>, with a stable key.startTransition.enter and exit classes run keyframes on just the new or just the old snapshot.<ol>
{ranked(spots).map((spot) => (
1 <ViewTransition key={spot.id}
enter="vt-slide-in" exit="vt-slide-out" update="vt-shift">
<li>{spot.name} ¡ {spot.votes} votes</li>
</ViewTransition>
))}
</ol>
const vote = (id) =>
2 startTransition(() => setSpots((all) => addVote(all, id)))
/* vt.css */
3::view-transition-group(.vt-shift) { animation-duration: 380ms; }
4::view-transition-new(.vt-slide-in) { animation: vt-slide 320ms both; }
::view-transition-old(.vt-slide-out) { animation: vt-slide-away 240ms both; }
Try it Switch tabs left and right. Pet Fry a few times, visit another tab, and come back.
0 pets
Pet him a few times, switch tabs, then come back.
Last transitionnothing yet, give it a try
The same change (setTab) should slide left or right depending on which way you went. addTransitionType labels the Transition with that reason, and a boundaryâs class props can take an object that picks a class per label.
The panels live in <Activity>, which hides a panel instead of unmounting it. Hiding counts as an exit and showing as an enter, but the state inside survives. The underline is one element that only the selected tab renders, always with the same name, so itâs a share and slides across.
addTransitionType, called inside startTransition, tags this Transition tab-forward or tab-back.enter and exit take an object: a class per type, and default for anything else.<Activity mode="hidden"> keeps the panel and its state; hiding it runs exit, showing it runs enter.const select = (next) =>
startTransition(() => {
1 addTransitionType(next > tab ? 'tab-forward' : 'tab-back')
setTab(next)
})
3<Activity mode={i === tab ? 'visible' : 'hidden'}>
<ViewTransition default="none"
2 enter={{ 'tab-forward': 'vt-tab-in-right', 'tab-back': 'vt-tab-in-left', default: 'none' }}
exit={{ 'tab-forward': 'vt-tab-out-left', 'tab-back': 'vt-tab-out-right', default: 'none' }}>
<div role="tabpanel">...</div>
</ViewTransition>
</Activity>
{i === tab && (
4 <ViewTransition name="vt-tab-indicator" share="vt-indicator">
<span className="indicator" />
</ViewTransition>
)}
Try it Play the next message in each mode. Drag âTakesâ up to slow the tape down.
Last transitionnothing yet, give it a try
When a <Suspense> boundary swaps its fallback for the real content, React treats the reveal like a Transition, so it can animate even though nothing called startTransition at that moment.
Where the <ViewTransition> sits decides how. Around the boundary, the fallback and the message are the same box at two moments: an update that morphs its size and cross-fades. Inside it, each side has its own boundary: the fallback makes an exit and the message an enter.
startTransition. A new key makes a fresh boundary, which shows its fallback.Message calls use(promise), which suspends until the promise resolves.const next = () => {
const promise = fetchMessage(n + 1, delay)
1 startTransition(() => setRequest({ n: n + 1, promise }))
}
function Message({ promise }) {
2 const message = use(promise)
return <MessageCard message={message} />
}
// Around: one boundary, so fallback â message is an "update"
3<ViewTransition update="vt-reveal" default="none">
<div className="tape">
<Suspense key={n} fallback={<Rewinding />}>
<Message promise={promise} />
</Suspense>
</div>
</ViewTransition>
// Inside: a boundary on each side, so it's "exit" + "enter"
<Suspense key={n} fallback={
4 <ViewTransition enter="vt-rise-in" exit="vt-rise-out"><Rewinding /></ViewTransition>
}>
<ViewTransition enter="vt-rise-in" exit="vt-rise-out">
<Message promise={promise} />
</ViewTransition>
</Suspense>
Try it Type âtoyâ, then clear it. Or tap the chips.
24 of 24
Last transitionnothing yet, give it a try
The text box has to show every keystroke straight away; an animation per key would make typing lag. useDeferredValue gives you a second copy of the query that trails behind, and React renders that copy as a Transition. So the box stays instant while the grid animates.
Inside the grid itâs the same keyed list as before: stickers that drop out exit, ones that come back enter, and the rest slide over with an update.
query, ordinary state updated on every keystroke.useDeferredValue(query) lags behind, and its re-render counts as a Transition.const [query, setQuery] = useState('')
1<input value={query} onChange={(e) => setQuery(e.target.value)} />
2const deferred = useDeferredValue(query)
3const shown = STICKERS.filter(matches(deferred))
<ul className="grid">
{shown.map((st) => (
4 <ViewTransition key={st.name}
enter="vt-pop-in" exit="vt-pop-out" update="vt-shift">
<li>{st.emoji} {st.name}</li>
</ViewTransition>
))}
</ul>
Try it Tap squares to send Fry running. Turn the spring off to compare, or drag the sliders.
linear() with 75 stopsLast transitionnothing yet, give it a try
CSS classes can only choose between keyframes you wrote ahead of time. The callbacks, onEnter, onExit, onUpdate and onShare, run as the animation starts and hand you the pseudo-elements, so JavaScript can take over.
Here the browser has already built Fryâs move (it knows his old and new boxes), and onUpdate just changes its timing to a spring simulated in JS.
onUpdate runs once the animation exists. instance.group is this boundaryâs ::view-transition-group.getAnimations() returns the browserâs move; updateTiming swaps its duration and easing and keeps its keyframes.linear() curve, which can go past 1 to overshoot.1const go = (spot) => startTransition(() => setSpot(spot))
2<ViewTransition update="vt-spring" onUpdate={(instance) => {
for (const a of instance.group.getAnimations()) {
3 a.effect.updateTiming({ duration: curve.duration, easing: curve.easing })
}
}}>
<div style={{ gridArea: area(spot) }}><PixelCat /></div>
</ViewTransition>
// spring.ts: simulate it, sample it once a frame, hand CSS the samples
while (!settled) {
v += (stiffness * (1 - x) - damping * v) * dt
x += v * dt
points.push(x)
}
4const easing = `linear(${points.join(', ')})`
Try it Switch themes, here or at the top of the page.
Last transitionnothing yet, give it a try
Most of this page keeps the root out of the snapshot, so only the demo that changed animates and everything else stays live. For the theme switch the whole window is captured instead, and the new look grows out of the button in a circle.
React also passes each Transition Type on to the browser, so CSS can pick out this one transition with :active-view-transition-type(theme).
<html> (the pseudo-elements inherit from it), and the root gets a name so the whole window is snapshotted.addTransitionType('theme') labels the Transition.theme. Some boundary has to animate, or React wonât start a view transition at all.:active-view-transition-type(theme) keeps the old window still and reveals the new one through a growing clip-path circle.const pickTheme = (next, e) => {
1 root.style.setProperty('--vt-reveal-x', `${x}px`)
root.style.setProperty('view-transition-name', 'root')
startTransition(() => {
2 addTransitionType('theme')
setTheme(next)
})
}
3<ViewTransition default="none" update={{ theme: 'vt-picker', default: 'none' }}>
<div role="group">...</div>
</ViewTransition>
/* vt.css */
4:root:active-view-transition-type(theme)::view-transition-old(root) { animation: none; }
:root:active-view-transition-type(theme)::view-transition-new(root) {
animation: vt-reveal 650ms ease-in-out both;
}
@keyframes vt-reveal {
from { clip-path: circle(0 at var(--vt-reveal-x) var(--vt-reveal-y)); }
to { clip-path: circle(var(--vt-reveal-r) at var(--vt-reveal-x) var(--vt-reveal-y)); }
}
useEffect(() => {
5 root.style.removeProperty('view-transition-name')
}, [theme])