How it works
Snapshots, swapped and animated
The View Transitions API takes a picture of the page, lets you change the DOM in one go, takes another picture and animates between the two. What makes it useful is that both pictures are pseudo-elements you can style and animate like anything else.
The idea
document.startViewTransition(update) captures the current rendering, calls update, waits for the DOM change (a returned promise is awaited), captures the
new rendering and builds a tree of pseudo-elements on top of the page: ::view-transition-group(name) per captured element, holding an ::view-transition-old(name) image and a live ::view-transition-new(name) one. By default the group moves and resizes from the
old box to the new one while the old image fades out and the new one fades in. The real DOM already
has its new state underneath; the pseudo-elements just cover it until the animation ends.
Which elements get their own group is up to view-transition-name. Everything
without one is part of the group root, which is the whole document.
1. The circular reveal
The demo box gets a name, so it is captured separately from the page. Its old and new images
lose their default fade (animation: none), so the old state stays fully
visible. Once transition.ready resolves, the pseudo-elements exist and the Web
Animations API can target one of them through pseudoElement: the new image gets
a clip-path: circle() that grows from the click point. The end radius is the
distance from that point to the farthest corner of the box, so the circle covers it exactly
when the animation ends. A keyboard activation has no pointer position (the click event's detail is 0), so the circle starts at the center of the button.
The theme itself is scoped too: the box carries data-theme and the library's
tokens use light-dark(), which resolves against the nearest color-scheme. The accent buttons change a hue custom property on the box, and
the same reveal applies to them.
.app {
view-transition-class: reveal;
}
/* no cross-fade: the old state stays, the new one is uncovered */
::view-transition-old(*.reveal),
::view-transition-new(*.reveal) {
animation: none;
mix-blend-mode: normal;
} async function toggleTheme(event) {
const box = document.querySelector('.app');
const update = () => {
box.dataset.theme = box.dataset.theme === 'dark' ? 'light' : 'dark';
};
// no API, reduced motion: just switch
if (!document.startViewTransition ||
matchMedia('(prefers-reduced-motion: reduce)').matches) {
return update();
}
// origin: the click, relative to the box (keyboard: center of the button)
const r = box.getBoundingClientRect();
const t = event.currentTarget.getBoundingClientRect();
const keyboard = event.detail === 0;
const x = (keyboard ? t.left + t.width / 2 : event.clientX) - r.left;
const y = (keyboard ? t.top + t.height / 2 : event.clientY) - r.top;
// radius: distance to the farthest corner
const radius = Math.hypot(Math.max(x, r.width - x), Math.max(y, r.height - y));
// element-scoped (Chrome 147+): the box is the group "root"
const scoped = 'startViewTransition' in box;
const root = scoped ? box : document.documentElement;
const name = scoped ? 'root' : 'app';
if (!scoped) box.style.viewTransitionName = name;
const transition = scoped
? box.startViewTransition(update)
: document.startViewTransition(update);
transition.finished.then(() => (box.style.viewTransitionName = ''));
await transition.ready;
root.animate(
{ clipPath: [
`circle(0px at ${x}px ${y}px)`,
`circle(${radius}px at ${x}px ${y}px)`
] },
{
duration: 650,
easing: 'ease-in-out',
fill: 'both',
pseudoElement: `::view-transition-new(${name})`
}
);
} 2. Keeping it in the box: element-scoped transitions
A document transition covers the whole page with its pseudo-element tree: while it runs,
nothing can be clicked, and a named element is painted above everything else, including the
control panel floating over the stage. Chrome 147 added element.startViewTransition(): the tree hangs off that element, only its
subtree is captured, and the rest of the page stays live. The element becomes the group root of its own transition. Both demos use it where available and fall back to
a document transition otherwise (switch Element-scoped off to compare); for the
fallback the reveal box gets a temporary view-transition-name that is removed when
the transition finishes, so it doesn't permanently create a stacking context.
3. Shared-element morphs
In the color list every tile, its swatch and its title have a name of their own, unique per
item (a view-transition-name has to be unique among rendered elements, or the transition
is skipped). After a shuffle or a layout switch, the browser finds the same names in new places
and animates each group from its old box to its new one. Opening a color does the same across
views: the detail panel, its swatch and its title use the tile's names, so the tile grows into
the panel. Only one of the two views is rendered at a time; if both carried the names at once,
they would collide.
All of them share a view-transition-class, so one rule styles every group
without knowing the names. Duration and easing come from custom properties set on the
transition's root element while it runs; the pseudo-elements inherit from there.
.card {
/* one unique name per item, set inline: view-transition-name: card-3 */
view-transition-class: morph;
}
::view-transition-group(*.morph),
::view-transition-old(*.morph),
::view-transition-new(*.morph) {
animation-duration: 450ms;
animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
} 4. Svelte: change the DOM before the callback returns
Svelte batches DOM updates and applies them in a microtask, after the update callback has
returned. The browser would then capture the "new" state before anything changed. Wrapping
the state change in flushSync applies it synchronously, inside the callback.
<script>
import { flushSync } from 'svelte';
let order = $state(items);
function shuffle() {
// the update has to change the DOM before the callback returns
const update = () => flushSync(() => (order = shuffled(order)));
if (!document.startViewTransition) update();
else document.startViewTransition(update);
}
</script>
{#each order as item (item.id)}
<div class="card" style:view-transition-name="card-{item.id}">…</div>
{/each} 5. Fallback and motion
Without startViewTransition the update runs directly: same result, no
animation. The same happens under prefers-reduced-motion: reduce and inside a [data-motion="paused"] region; a sliding or growing snapshot is exactly the kind
of motion those settings ask to avoid, and the state change itself needs no animation to be understood.
Focus is handled in the update: opening a color focuses the back button, closing it returns focus
to the tile.
Browser support
Same-document view transitions are Baseline (newly available) since October 2025, as is view-transition-class (per web-features; see view transitions and view-transition-class on
webstatus.dev). Element-scoped transitions are Chrome 147+ only (webstatus.dev). Cross-document transitions between pages are a separate feature and not used here.
Tested in Chromium 156 only; Firefox and Safari, which take the document-wide path, are
untested. A browser that has the API but not view-transition-class would ignore the
class rules and fall back to the default cross-fade.
Performance notes
- Snapshots cost memory, not much time. Every named element is rendered into an image of its size; a few dozen tiles are fine, thousands of named list items are not.
- The animations are cheap. Groups animate
transformand size, the imagesopacity, the revealclip-pathon one layer. All of it runs on already rendered images. - Rendering is paused during the capture. Between the start and the first frame of the animation the page doesn't paint; a slow update callback shows up as a freeze, so keep the update itself short.