Sub topic 2
What the browser does: snapshots, update, animate
This chapter explains what happens between the call to document.startViewTransition() and the moment the animation ends. It assumes you can read promises and async/await, know CSS pseudo-elements and @keyframes, and have wired up a DOM update in a single-page app. The chapter covers the order of events and what the user sees at each step. The pseudo-element tree is covered in the next chapter.
The core idea: images, not live elements
A view transition does not animate your DOM elements from state A to state B. The browser takes a picture of the old state, runs your update, and takes a picture of the new state. It then animates between the pictures.
This explains the rest of the feature:
- Your update code can change the DOM in any way, because the browser never needs to interpolate between the two element trees.
- Animations run on rendered output, so they can be smooth even when the update changes layout.
- The page behaves differently while a transition runs, because you are looking at snapshots.
The four phases
| Phase | What happens | What the user sees |
|---|---|---|
| 1. Capture old | The browser records the current rendering of the page and of each element that has a `view-transition-name`. | The page as it was. |
| 2. Update | Rendering is paused, then your callback runs. If it returns a promise, the browser waits for it. | The old frame, frozen. |
| 3. Capture new | The browser builds a tree of pseudo-elements and sets up the new state. | Still the old frame. |
| 4. Animate | The default or custom animations run, then the pseudo-elements are removed. | The animated change, then the live page. |
Phase 1 holds a detail that surprises people. startViewTransition() does not capture anything at the moment you call it. Capture happens at the next rendering opportunity, which is the next frame. Your callback runs after the capture, not at the call.
Watching the order yourself
The following page logs each stage. Save it as an HTML file and open it in a browser that supports view transitions, such as current Chrome or Edge.
<!doctype html>
<html lang="en">
<body>
<button id="toggle">Toggle</button>
<div id="box" style="width: 100px; height: 100px; background: teal"></div>
<script>
const box = document.querySelector('#box');
function toggle() {
const apply = () => {
console.log('2. update callback runs (old state already captured)');
box.style.width = box.style.width === '300px' ? '100px' : '300px';
};
if (!document.startViewTransition) {
apply();
return;
}
const transition = document.startViewTransition(apply);
console.log('1. right after the call, width is still', box.style.width || '100px');
transition.updateCallbackDone.then(() => console.log('3. update callback finished'));
transition.ready.then(() => console.log('4. pseudo-elements exist, animation starts'));
transition.finished.then(() => console.log('5. animation done, pseudo-elements removed'));
}
document.querySelector('#toggle').addEventListener('click', toggle);
</script>
</body>
</html>The console prints 1, 2, 3, 4, 5 in that order. Line 1 shows the consequence of deferred capture. Code placed right after startViewTransition() still sees the old DOM, so do not read the new state there. Put state-dependent work inside the callback, or wait for updateCallbackDone. The next chapters cover these three promises in detail.
Phase 1: what gets captured
The browser always captures the page as a whole, which is the root snapshot. It also captures every element that has a view-transition-name. Each named element is captured separately and removed from the root snapshot, so the browser can animate it on its own.
An old snapshot is a static image. The new side behaves differently. It is a live view of the new element, so a video or a CSS animation in the new state keeps playing during the transition. The old side never updates.
Elements with no name are part of the root image. They cannot move independently. Any shared-element effect needs a name on that element, as a later chapter shows.
Phase 2: the update callback
Between capture and animation, the browser pauses rendering. This has several consequences:
- Your callback can make as many DOM changes as it needs. None of them is painted until the callback is done.
- If the callback returns a promise, the browser waits for it. An
asynccallback can fetch data and render it before the new state is captured. - A slow callback delays the animation and leaves the page frozen. Keep the work in it short, and do not wait on slow network requests unless you accept a frozen page for that long.
- The update runs even if the animation never happens, for example when the transition is skipped. The DOM update is the part that must always succeed.
async function openItem(id) {
const transition = document.startViewTransition(async () => {
const data = await fetchItem(id); // page stays frozen until this resolves
document.querySelector('#content').innerHTML = renderItem(data);
});
await transition.finished;
}Here fetchItem and renderItem stand for your own functions. If the fetch takes two seconds, the user sees a frozen page for two seconds. A common pattern is to show a loading state in the DOM first, and run the transition only when data is ready.
Phase 3: capturing the new state
When the callback is finished, the browser captures the new state. For each name, it checks whether the element exists in the old state, the new state, or both. This decides the animation:
- Present in both: the browser animates the old image into the new one, including position and size changes.
- Only in the old state: the old image fades out.
- Only in the new state: the new image fades in.
The browser then builds the pseudo-element tree and positions it above the page. At this point ready resolves, and the animations begin.
Phase 4: animation and cleanup
The default animation is a cross-fade of the root snapshots. A later chapter on the default cross-fade explains it. For this chapter, remember that the animations are ordinary CSS animations on pseudo-elements. When all of them end, the browser removes the pseudo-elements, the live page shows again, and finished resolves.
While the animation runs, you are not looking at the live DOM. This causes some visible effects:
- Pointer input goes to the pseudo-element layer, not to the elements underneath. Clicks during a long animation do nothing. Keep animations short, typically under 400 ms.
- Text selection, hover states, and focus rings are not shown on the snapshot.
- If the animation never finishes, the page stays blocked. Always test custom animations with a finite
duration.
When the lifecycle is cut short
The browser skips a transition when it cannot or should not run it. Examples are a hidden document, a new startViewTransition() call that interrupts an earlier one, or an exception in the callback. A skipped transition still runs the callback when the update has not run yet, so the DOM ends up in the right state. Only the animation is dropped. A later chapter covers the promise behavior in this case.
Summary of what to remember
- The call returns immediately. Capture and callback happen on later frames.
- The old state is a frozen image. The new state is a live view.
- Rendering is paused while the callback runs, so keep the callback fast.
- Animations run on pseudo-elements and block input to the live page until they end.
Exercise
A developer writes document.startViewTransition(() => setTheme('dark')); followed on the next line by console.log(document.body.dataset.theme);. The theme attribute is set inside setTheme. What does the log show, and how can the developer read the new value correctly?
Show answer
The log shows the old theme, because the browser runs the callback only after it captures the old state at the next frame. To read the new value, log it inside the callback after setTheme runs, or wait for transition.updateCallbackDone and read it in the then handler or after an await.
Exercise
Your update callback fetches data and takes 1.5 seconds. Users report that the page seems frozen when they click. Which phase causes this, and what is one way to fix it?
Show answer
Phase 2. Rendering is paused while the callback's promise is pending, so the user sees the old frame frozen. One fix is to show a loading state in the DOM without a transition, fetch the data, and then call startViewTransition() with a callback that only renders the data already fetched.