diff --git a/files/en-us/web/api/view_transition_api/using/index.md b/files/en-us/web/api/view_transition_api/using/index.md index b09960e462f6499..e35ea0abea8daea 100644 --- a/files/en-us/web/api/view_transition_api/using/index.md +++ b/files/en-us/web/api/view_transition_api/using/index.md @@ -30,7 +30,7 @@ Let's walk through the process by which a view transition works: At this point, the view transition is about to run, and the {{domxref("ViewTransition.ready")}} promise fulfills, allowing you to respond by running a custom JavaScript animation instead of the default, for example. 5. The old page snapshots animate "out", while the new view snapshots animate "in". By default, the old view snapshots animate from {{cssxref("opacity")}} 1 to 0, and the new view snapshots animate from `opacity` 0 to 1, which creates a cross-fade. -6. When the transition animations have reached their end states, the {{domxref("ViewTransition.finished")}} promise fulfills, allowing you to respond. +6. When the transition animations have reached their end states, the snapshots are destroyed, and the {{domxref("ViewTransition.finished")}} promise fulfills, allowing you to respond. You can delay a view transition from reaching its finished state until a specified {{domxref("Promise")}} is resolved using the {{domxref("ViewTransition.waitUntil()")}} method, which has utility in advanced use cases. > [!NOTE] > If the document's [page visibility state](/en-US/docs/Web/API/Page_Visibility_API) is `hidden` (for example if the document is obscured by a window, the browser is minimized, or another browser tab is active) during a {{domxref("Document.startViewTransition()", "document.startViewTransition()")}} call, the view transition is skipped entirely. diff --git a/files/en-us/web/api/viewtransition/index.md b/files/en-us/web/api/viewtransition/index.md index b04f52d0cd5c929..d6bd505cd3376b5 100644 --- a/files/en-us/web/api/viewtransition/index.md +++ b/files/en-us/web/api/viewtransition/index.md @@ -34,6 +34,8 @@ When a view transition is triggered by a `startViewTransition()` call (or a page - {{domxref("ViewTransition.skipTransition", "skipTransition()")}} - : Skips the animation part of the view transition, but doesn't skip running the {{domxref("Document.startViewTransition()", "document.startViewTransition()")}} callback that updates the DOM. +- {{domxref("ViewTransition.waitUntil", "waitUntil()")}} + - : Delays finishing the view transition and the destruction of the associated pseudo-element tree until a {{jsxref("Promise")}} passed into the method has resolved. ## Examples diff --git a/files/en-us/web/api/viewtransition/waituntil/index.md b/files/en-us/web/api/viewtransition/waituntil/index.md new file mode 100644 index 000000000000000..da3db075e52d983 --- /dev/null +++ b/files/en-us/web/api/viewtransition/waituntil/index.md @@ -0,0 +1,214 @@ +--- +title: "ViewTransition: waitUntil() method" +short-title: waitUntil() +slug: Web/API/ViewTransition/waitUntil +page-type: web-api-instance-method +browser-compat: api.ViewTransition.waitUntil +--- + +{{APIRef("View Transition API")}} + +The **`waitUntil()`** method of the {{domxref("ViewTransition")}} interface delays finishing the view transition and the destruction of the associated pseudo-element tree until a {{jsxref("Promise")}} passed into the method has resolved. + +## Syntax + +```js-nolint +waitUntil(promise) +``` + +### Parameters + +- `promise` + - : A {{jsxref("Promise")}} that, when resolved, causes the view transition to finish and the associated pseudo-element tree to be destroyed. This can be any promise. + +### Return value + +None ({{jsxref("undefined")}}). + +## Description + +When a same-document view transition is started (typically via {{domxref("Document.startViewTransition()")}}), the browser automatically constructs a [pseudo-element tree](/en-US/docs/Web/API/View_Transition_API/Using#the_view_transition_pseudo-element_tree) to display and animate outgoing and inbound changes to the DOM. This tree is constructed when the view transition starts animating and is destroyed when the animations associated with all view transition pseudo-elements reach the finished state ({{domxref("ViewTransition.finished", "finished")}} is resolved). + +This works fine in most cases. However, some advanced use cases benefit from the pseudo-tree persisting beyond the animation finish state. This can be achieved using the `waitUntil()` method, which is passed a {{jsxref("Promise")}} as an argument. Calling `waitUntil()` causes the pseudo-tree to persist until the promise is resolved. At this point, the `finished` promise also resolves. + +Repeated calls to `waitUntil()` specifying multiple different promises will delay the finish state until all the given promises are resolved. + +Use cases include: + +- Combining a view transition with [scroll-driven animations](/en-US/docs/Web/CSS/Guides/Scroll-driven_animations). When the transition animation is controlled by a scroll progress or view progress timeline, the subtree should persist when the animations finish since scrolling back should be able to animate the pseudo-elements in reverse. +- Combining a view transition with {{domxref("Window.requestAnimationFrame", "requestAnimationFrame()")}}. When you're updating the state of your elements in `requestAnimationFrame()` callbacks, the view transition system does not know how long to persist the pseudo-element tree for, and will do it immediately as soon as the CSS animations are finished. +- Any situation in which you want to delay the view transition finishing until an event has occurred. You might for example want to start the view transition on `pointerdown` and not finish it until `pointerup`. + +## Examples + +### Basic `waitUntil()` usage + +This example demonstrates basic usage of the `waitUntil()` method to delay a view transition started by a button or key press from finishing until the button or key press ends. + +#### HTML + +We include a {{htmlelement("div")}} element containing page content, which includes a {{htmlelement("p")}} element and a {{htmlelement("button")}} element that when pressed will change the displayed content. The paragraph includes an [`aria-live`](/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-live) attribute so that DOM updates are announced to screenreader users. + +```html live-sample___basic-waituntil +
Hello! This is the first page.
+ +` element to log status messages into: + +```html live-sample___basic-waituntil +
+``` + +#### CSS + +First, we apply a {{cssxref("view-transition-name")}} of `page` to our `