Sub topic 13
Skipping and interrupted transitions
This chapter assumes you can use document.startViewTransition(), know the ViewTransition promises (ready, finished, updateCallbackDone), and are comfortable with async/await.
What "skipped" means
A view transition is skipped when the browser abandons the animation but still makes sure the page reaches its new state. The DOM update callback runs (or has already run), and the user sees the new view without the animation.
A transition is skipped in these cases:
- You call
transition.skipTransition(). - You call
document.startViewTransition()while an earlier transition is still running. The earlier one is skipped. - The document is hidden, for example in a background tab, when the transition starts.
- Two elements share the same
view-transition-namein the same state. - The update callback throws or returns a rejected promise.
How the promises behave
Skipping changes how the three promises settle.
| Situation | `updateCallbackDone` | `ready` | `finished` |
|---|---|---|---|
| Skipped, callback succeeds | fulfills | rejects | fulfills |
| Callback throws | rejects | rejects | rejects |
| Runs to the end | fulfills | fulfills | fulfills |
Because ready rejects whenever the animation never starts, any code that waits on it must handle that rejection. The next example does so.
async function navigate(nextId) {
if (!document.startViewTransition) {
updateView(nextId);
return;
}
const transition = document.startViewTransition(() => updateView(nextId));
try {
await transition.ready;
document.documentElement.animate(
{ clipPath: ['inset(0 0 100% 0)', 'inset(0)'] },
{
duration: 400,
easing: 'ease-in',
pseudoElement: '::view-transition-new(root)'
}
);
} catch {
// Skipped: no pseudo-elements exist. The DOM still updates.
}
await transition.updateCallbackDone;
}Without the try/catch, a skipped transition produces an unhandled promise rejection in the console, even though the page works.
Skipping on purpose
Call skipTransition() when the animation is no longer useful, for example when the user presses Escape or you must show an urgent state.
let activeTransition = null;
function navigateWithEscape(nextId) {
activeTransition = document.startViewTransition(() => updateView(nextId));
activeTransition.finished.finally(() => {
activeTransition = null;
});
}
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && activeTransition) {
activeTransition.skipTransition();
}
});Common errors
- Assuming the callback may not run. A skipped transition still runs the update callback. Put all state changes inside it, not in code that depends on the animation.
- Rapid clicks. Each click skips the previous transition and starts another from the current screen. The result usually looks fine, but cleanup code tied to
finishedmust tolerate that. Clear any temporaryview-transition-namein afinallyhandler, as the shared-element example does. - Starting animations after
await transition.readywithout a catch. See the code above.
Exercise
A user clicks a link twice in quick succession. The first transition has not finished when the second startViewTransition() call happens. Which promises of the first transition reject, and does the first update callback still run?
Show answer
Only ready rejects, and only if the first transition had not yet become ready. updateCallbackDone and finished fulfill if the callback succeeds. The first update callback still runs (or has already run), because skipping removes the animation but not the DOM update.