# View Transition

> Use document.startViewTransition() for same-document DOM updates, inspect its lifecycle promises, diagnose skipped animations, and keep a fallback.

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

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](/compatibility/api/) 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

```js
const transition = document.startViewTransition(updateCallback)
const transition = document.startViewTransition({ update, types })

await transition.updateCallbackDone
await transition.ready
await transition.finished
transition.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

| 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

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

`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.

:::caution
The Level 1 specification requires each rendered element participating in the named capture to have a unique `view-transition-name`. If more than one rendered element has the same name while the old or new state is captured, `ready` rejects with `InvalidStateError`; the update callback still runs, and `finished` still fulfills after the end state is reached ([capturing the old state](https://www.w3.org/TR/css-view-transitions-1/#capture-the-old-state), W3C).
:::

## 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

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()`.

```html
<!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

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.

```js
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:

1. If `updateCallbackDone` rejects, repair the callback first; `finished` rejects with the same callback failure.
2. If the update fulfills but `ready` reports `InvalidStateError`, inspect the computed `view-transition-name` values for duplicates. The DOM update can still have succeeded.
3. 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()` after `ready` fulfills leaves `ready` fulfilled, and `finished` still 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

- [Navigation API](/reference/capabilities/navigation-api/), for intercepting and managing same-document navigation
- [User activation](/reference/capabilities/user-activation/), for APIs whose calls are tied to user gestures
- [CSS View Transitions Module Level 1](https://www.w3.org/TR/css-view-transitions-1/) (W3C)
- [Same-document view transitions for single-page applications](https://developer.chrome.com/docs/web-platform/view-transitions/same-document) (Chrome for Developers)
- [WebKit Features in Safari 18.0](https://webkit.org/blog/15865/webkit-features-in-safari-18-0/) (WebKit)