API in Capabilities
View Transition
Published Updated
The document.startViewTransition() method captures a document before and after a DOM change, then presents the captures in a pseudo-element tree that CSS animations can style. Pass the DOM update as its callback; the returned ViewTransition exposes separate promises for the update, animation preparation, and animation completion stages.
Browser support
Section titled “Browser support”BCD api.Document.startViewTransition and api.ViewTransition record support from Chrome 111, Firefox 144, and Safari 18, with the corresponding mobile and Chromium-derived browser entries mirrored from those engines. Firefox 144 release notes describe View Transition API and related CSS support for single-page applications; Safari 18 release notes describe the Level 1 API. Check the broader API compatibility data alongside runtime detection when older browser versions remain in your support range.
Testing "startViewTransition" in document answers whether the method is present. It does not establish support for every Level 2 option, because BCD records the options dictionary and its members as separate subfeatures. Keep the callback form as the fallback when code also uses types.
Syntax
Section titled “Syntax”const transition = document.startViewTransition(updateCallback)const transition = document.startViewTransition({ update, types })
await transition.updateCallbackDoneawait transition.readyawait transition.finishedtransition.skipTransition()Level 1 defines the callback form. The callback runs asynchronously after the browser captures the old state; its DOM changes establish the new state. Level 2 adds the options form. The method returns immediately, before the callback or animation completes.
Parameters
Section titled “Parameters”| Parameter | Type | Meaning |
|---|---|---|
updateCallback |
function | Changes the DOM for the new state. It may return a promise, and capture of the new state waits for that promise. |
options.update |
function | Supplies the same update callback when the Level 2 options form is used. |
options.types |
sequence of strings | Sets active transition types for CSS selection in the Level 2 model. |
Call the callback form when no transition type is needed. When an asynchronous update fetches or prepares state, return its promise from the callback so the transition lifecycle can report a rejection through updateCallbackDone.
Members
Section titled “Members”The returned object separates the DOM update from preparation of the visual transition and completion of its animations.
| Member | Type | Meaning |
|---|---|---|
updateCallbackDone |
Promise<undefined> |
Fulfills when the update callback fulfills and rejects with the callback’s rejection reason. |
ready |
Promise<undefined> |
Fulfills after the transition pseudo-elements are created and before their animations start. It rejects when the transition cannot begin. |
finished |
Promise<undefined> |
Fulfills when the end state is visible and interactive. It rejects only when the update callback rejects. |
skipTransition() |
method | Skips or finishes the visual transition without preventing the update callback from running. |
The browser captures the old state, pauses rendering, runs the update callback, captures the new state, creates the ::view-transition pseudo-element tree, and runs its CSS animations. The default transition is a page-wide cross-fade. Assign the same unique view-transition-name to the old and new forms of an element when that element needs its own capture. Style ::view-transition-old(name) and ::view-transition-new(name) with CSS animations to control the outgoing and incoming captures.
updateCallbackDone is the promise to await when later application work depends on the DOM update itself. Await ready before starting a Web Animations API animation against the generated pseudo-elements. Await finished when work must follow the point at which the new state is visible and interactive. Calling skipTransition() still lets updateCallbackDone reflect the callback result and lets the application reach the new state.
Exceptions
Section titled “Exceptions”startViewTransition() returns a ViewTransition; the lifecycle promises report failures asynchronously. The specification defines the following paths.
| Promise | Failure condition |
|---|---|
updateCallbackDone |
The update callback throws or returns a rejected promise. |
ready |
The callback rejects, the document is hidden, another condition prevents capture, or two rendered elements use the same view-transition-name. |
finished |
The update callback rejects. A skipped visual transition otherwise reaches the end state and fulfills. |
Starting a transition while the document’s visibility state is hidden skips it with InvalidStateError. Starting another transition while one is active skips the earlier transition with AbortError; both asynchronous callbacks can still run. Application code that must serialize state changes therefore needs its own queue or cancellation mechanism.
Examples
Section titled “Examples”Treat the animation as an enhancement around a state change. Each example performs the same DOM update when the API is absent, so the button or selected view remains usable without transition support.
Animating a named panel with a working fallback
Section titled “Animating a named panel with a working fallback”This complete document assigns a stable view-transition-name to the panel and styles its old and new captures. The fallback calls selectPanel() directly; a supporting browser wraps the same call in startViewTransition().
<!doctype html><html lang="en"> <meta charset="utf-8" /> <title>Panel transition</title> <style> #panel { view-transition-name: selected-panel; } ::view-transition-old(selected-panel) { animation: 180ms ease-out both panel-out; } ::view-transition-new(selected-panel) { animation: 180ms ease-in both panel-in; } @keyframes panel-out { to { opacity: 0; transform: translateX(-1rem); } } @keyframes panel-in { from { opacity: 0; transform: translateX(1rem); } } </style> <button id="switch" type="button">Show details</button> <main id="panel">Summary</main> <script> const button = document.querySelector("#switch"); const panel = document.querySelector("#panel");
function selectPanel() { panel.textContent = "Details"; button.disabled = true; }
button.addEventListener("click", () => { if (!("startViewTransition" in document)) { selectPanel(); return; } document.startViewTransition(selectPanel); }); </script></html>The expected end state is the text Details and a disabled button in either branch. In a supporting browser, the named old capture moves left while the named new capture moves in from the right. If no animation appears, first confirm that the click still produces the end state, then inspect the computed view-transition-name and the two pseudo-element rules.
Recovering when a named transition fails
Section titled “Recovering when a named transition fails”Two visible cards with the same view-transition-name make ready reject. This example prevents that failure by assigning a unique name from each card’s data-id, handles a rejected update, and reports a skipped animation without undoing a successful DOM change.
const status = document.querySelector("#status");const cards = [...document.querySelectorAll(".card")];
function assignUniqueNames() { for (const card of cards) { card.style.viewTransitionName = `card-${card.dataset.id}`; }}
async function reorderCards() { assignUniqueNames(); if (!("startViewTransition" in document)) { cards[0].parentNode.append(...[...cards].reverse()); status.textContent = "Reordered without animation"; return; }
const transition = document.startViewTransition(() => { cards[0].parentNode.append(...[...cards].reverse()); });
const [update, ready] = await Promise.allSettled([ transition.updateCallbackDone, transition.ready, transition.finished, ]);
if (update.status === "rejected") { status.textContent = `Update failed: ${update.reason.name}`; } else if (ready.status === "rejected") { status.textContent = `Reordered; animation skipped: ${ready.reason.name}`; } else { status.textContent = "Reordered; transition lifecycle ended"; }}Give every .card a distinct data-id. Read the reported outcome in lifecycle order:
- If
updateCallbackDonerejects, repair the callback first;finishedrejects with the same callback failure. - If the update fulfills but
readyreportsInvalidStateError, inspect the computedview-transition-namevalues for duplicates. The DOM update can still have succeeded. - If all promises fulfill, the transition reached the updated state and its lifecycle ended. This result does not prove that the animation played to completion: calling
skipTransition()afterreadyfulfills leavesreadyfulfilled, andfinishedstill fulfills after a successful update.
Promise.allSettled() installs handlers for all lifecycle promises immediately. There is no separate finished rejection branch because an update callback failure also rejects updateCallbackDone and is reported by the first branch.
See also
Section titled “See also”- Navigation API, for intercepting and managing same-document navigation
- User activation, for APIs whose calls are tied to user gestures
- CSS View Transitions Module Level 1 (W3C)
- Same-document view transitions for single-page applications (Chrome for Developers)
- WebKit Features in Safari 18.0 (WebKit)
Specifications
| Specification | Status |
|---|---|
| CSS View Transitions Module Level 1: API | W3C |
| CSS View Transitions Module Level 2 | Other |