diff --git a/files/en-us/web/api/document/startviewtransition/index.md b/files/en-us/web/api/document/startviewtransition/index.md index 82e052ae16058fa..49d4f43b8b161d7 100644 --- a/files/en-us/web/api/document/startviewtransition/index.md +++ b/files/en-us/web/api/document/startviewtransition/index.md @@ -10,9 +10,9 @@ browser-compat: api.Document.startViewTransition {{APIRef("View Transitions API")}}{{SeeCompatTable}} -The **`startViewTransition()`** method of the {{domxref("Document")}} interface starts a new view transition and returns a {{domxref("ViewTransition")}} object to represent it. +The **`startViewTransition()`** method of the {{domxref("Document")}} interface starts a new same-document (SPA) [view transition](/en-US/docs/Web/API/View_Transitions_API) and returns a {{domxref("ViewTransition")}} object to represent it. -When `startViewTransition()` is invoked, a sequence of steps is followed as explained in [The view transition process](/en-US/docs/Web/API/View_Transitions_API#the_view_transition_process). +When `startViewTransition()` is invoked, a sequence of steps is followed as explained in [The view transition process](/en-US/docs/Web/API/View_Transitions_API/Using#the_view_transition_process). ## Syntax @@ -24,7 +24,7 @@ startViewTransition(updateCallback) ### Parameters - `updateCallback` {{optional_inline}} - - : An optional callback function typically invoked to update the DOM during the view transition process, which returns a {{jsxref("Promise")}}. The callback is invoked once the API has taken a screenshot of the current page. When the promise returned by the callback fulfills, the view transition begins in the next frame. If the promise returned by the callback rejects, the transition is abandoned. + - : An optional callback function typically invoked to update the DOM during the SPA view transition process, which returns a {{jsxref("Promise")}}. The callback is invoked once the API has taken a snapshot of the current page. When the promise returned by the callback fulfills, the view transition begins in the next frame. If the promise returned by the callback rejects, the transition is abandoned. ### Return value @@ -34,7 +34,7 @@ A {{domxref("ViewTransition")}} object instance. ### Basic usage -In our [Basic View Transitions demo](https://mdn.github.io/dom-examples/view-transitions/), the `updateView()` function handles both browsers that do and don't support the View Transitions API. In supporting browsers, we invoke `startViewTransition()` to set off the view transition process without worrying about the return value. +In our [Basic SPA View Transitions demo](https://mdn.github.io/dom-examples/view-transitions/spa/), the `updateView()` function handles both browsers that do and don't support the View Transitions API. In supporting browsers, we invoke `startViewTransition()` to trigger the view transition process without worrying about the return value. ```js function updateView(event) { diff --git a/files/en-us/web/api/html_dom_api/index.md b/files/en-us/web/api/html_dom_api/index.md index 39a2943c9d02186..0345e3976b80294 100644 --- a/files/en-us/web/api/html_dom_api/index.md +++ b/files/en-us/web/api/html_dom_api/index.md @@ -235,6 +235,8 @@ The History API interfaces let you access information about the browser's histor - {{DOMxRef("HashChangeEvent")}} - {{DOMxRef("History")}} - {{DOMxRef("Location")}} +- {{DOMxRef("PageRevealEvent")}} +- {{DOMxRef("PageSwapEvent")}} - {{DOMxRef("PageTransitionEvent")}} - {{DOMxRef("PopStateEvent")}} diff --git a/files/en-us/web/api/navigation_api/index.md b/files/en-us/web/api/navigation_api/index.md index 3fe25516a83fc62..da09e490ea75c09 100644 --- a/files/en-us/web/api/navigation_api/index.md +++ b/files/en-us/web/api/navigation_api/index.md @@ -90,6 +90,8 @@ There are a few perceived limitations with the Navigation API: - : Event object for the {{domxref("Navigation/navigate_event", "navigate")}} event, which fires when [any type of navigation](https://github.com/WICG/navigation-api#appendix-types-of-navigations) is initiated. It provides access to information about that navigation, and most notably the {{domxref("NavigateEvent.intercept", "intercept()")}}, which allows you to control what happens when the navigation is initiated. - {{domxref("Navigation")}} {{Experimental_Inline}} - : Allows control over all navigation actions for the current `window` in one central place, including initiating navigations programmatically, examining navigation history entries, and managing navigations as they happen. +- {{domxref("NavigationActivation")}} + - : Represents a recent cross-document navigation. It contains the navigation type and current and destination document history entries. - {{domxref("NavigationCurrentEntryChangeEvent")}} {{Experimental_Inline}} - : Event object for the {{domxref("Navigation/currententrychange_event", "currententrychange")}} event, which fires when the {{domxref("Navigation.currentEntry")}} has changed. It provides access to the navigation type, and the previous history entry that was navigated from. - {{domxref("NavigationDestination")}} {{Experimental_Inline}} diff --git a/files/en-us/web/api/navigationactivation/entry/index.md b/files/en-us/web/api/navigationactivation/entry/index.md new file mode 100644 index 000000000000000..09ba309845a24d4 --- /dev/null +++ b/files/en-us/web/api/navigationactivation/entry/index.md @@ -0,0 +1,36 @@ +--- +title: "NavigationActivation: entry property" +short-title: entry +slug: Web/API/NavigationActivation/entry +page-type: web-api-instance-property +status: + - experimental +browser-compat: api.NavigationActivation.entry +--- + +{{APIRef("Navigation API")}}{{SeeCompatTable}} + +The **`entry`** read-only property of the {{domxref("NavigationActivation")}} interface contains a {{domxref("NavigationHistoryEntry")}} object representing the history entry for the inbound ("to") document in the navigation. This is equivalent to the {{domxref("Navigation.currentEntry")}} property at the moment the inbound document was activated. + +There are some cases in which either the from or entry NavigationHistoryEntry objects would not be viable targets for the traverseTo() method, as they might not be retained in history. For example, the Document can be activated using location.replace() or its initial entry could be replaced by history.replaceState(). However, those entries' url property and getState() method are still accessible. + +## Value + +A {{domxref("NavigationHistoryEntry")}} object. + +## Examples + +See the main {{domxref("NavigationActivation")}} page. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [Navigation API](/en-US/docs/Web/API/Navigation_API) +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/navigationactivation/from/index.md b/files/en-us/web/api/navigationactivation/from/index.md new file mode 100644 index 000000000000000..682834b202b24de --- /dev/null +++ b/files/en-us/web/api/navigationactivation/from/index.md @@ -0,0 +1,37 @@ +--- +title: "NavigationActivation: from property" +short-title: from +slug: Web/API/NavigationActivation/from +page-type: web-api-instance-property +status: + - experimental +browser-compat: api.NavigationActivation.from +--- + +{{APIRef("Navigation API")}}{{SeeCompatTable}} + +The **`from`** read-only property of the {{domxref("NavigationActivation")}} interface contains a {{domxref("NavigationHistoryEntry")}} object representing the history entry for the outgoing ("from") document in the navigation. + +## Value + +A {{domxref("NavigationHistoryEntry")}} object, or `null` if the outgoing document is: + +- Not same origin as the inbound document. +- The initial `about:blank` document. + +## Examples + +See the main {{domxref("NavigationActivation")}} page. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [Navigation API](/en-US/docs/Web/API/Navigation_API) +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/navigationactivation/index.md b/files/en-us/web/api/navigationactivation/index.md new file mode 100644 index 000000000000000..ce6fd9b07073157 --- /dev/null +++ b/files/en-us/web/api/navigationactivation/index.md @@ -0,0 +1,94 @@ +--- +title: NavigationActivation +slug: Web/API/NavigationActivation +page-type: web-api-interface +status: + - experimental +browser-compat: api.NavigationActivation +--- + +{{APIRef("Navigation API")}}{{SeeCompatTable}} + +The **`NavigationActivation`** interface of the [Navigation API](/en-US/docs/Web/API/Navigation_API) represents a recent cross-document navigation. It contains the navigation type and outgoing and inbound document history entries. + +This object is accessed via the {{domxref("PageSwapEvent.activation")}} and {{domxref("Navigation.activation")}} properties. Note that, in each case, the `NavigationActivation` represents a different navigation: + +- `Navigation.activation` represents information about the navigation to the current page. +- `PageSwapEvent.activation` represents information about the navigation to the next page. + +## Instance properties + +- {{domxref("NavigationActivation.entry", "entry")}} {{ReadOnlyInline}} {{Experimental_Inline}} + - : Contains a {{domxref("NavigationHistoryEntry")}} object representing the history entry for the inbound ("to") document in the navigation. This is equivalent to the {{domxref("Navigation.currentEntry")}} property at the moment the inbound document was activated. +- {{domxref("NavigationActivation.from", "from")}} {{ReadOnlyInline}} {{Experimental_Inline}} + - : Contains a {{domxref("NavigationHistoryEntry")}} object representing the history entry for the outgoing ("from") document in the navigation. +- {{domxref("NavigationActivation.navigationType", "navigationType")}} {{ReadOnlyInline}} {{Experimental_Inline}} + - : Contains a string indicating the type of navigation. + +## Examples + +```js +window.addEventListener("pagereveal", async (e) => { + // If the "from" history entry does not exist, return + if (!navigation.activation.from) return; + + // Only run this if an active view transition exists + if (e.viewTransition) { + const fromUrl = new URL(navigation.activation.from.url); + const currentUrl = new URL(navigation.activation.entry.url); + + // Went from profile page to homepage + // ~> Set VT names on the relevant list item + if (isProfilePage(fromUrl) && isHomePage(currentUrl)) { + const profile = extractProfileNameFromUrl(fromUrl); + + // Set view-transition-name values on the elements to animate + document.querySelector(`#${profile} span`).style.viewTransitionName = + "name"; + document.querySelector(`#${profile} img`).style.viewTransitionName = + "avatar"; + + // Remove names after snapshots have been taken + // so that we're ready for the next navigation + await e.viewTransition.ready; + document.querySelector(`#${profile} span`).style.viewTransitionName = + "none"; + document.querySelector(`#${profile} img`).style.viewTransitionName = + "none"; + } + + // Went to profile page + // ~> Set VT names on the main title and image + if (isProfilePage(currentUrl)) { + // Set view-transition-name values on the elements to animate + document.querySelector(`#detail main h1`).style.viewTransitionName = + "name"; + document.querySelector(`#detail main img`).style.viewTransitionName = + "avatar"; + + // Remove names after snapshots have been taken + // so that we're ready for the next navigation + await e.viewTransition.ready; + document.querySelector(`#detail main h1`).style.viewTransitionName = + "none"; + document.querySelector(`#detail main img`).style.viewTransitionName = + "none"; + } + } +}); +``` + +> **Note:** See [List of Chrome Dev Rel team members](https://view-transitions.netlify.app/profiles/mpa/) for the live demo this code is taken from. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [Navigation API](/en-US/docs/Web/API/Navigation_API) +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/navigationactivation/navigationtype/index.md b/files/en-us/web/api/navigationactivation/navigationtype/index.md new file mode 100644 index 000000000000000..98743993dda7743 --- /dev/null +++ b/files/en-us/web/api/navigationactivation/navigationtype/index.md @@ -0,0 +1,49 @@ +--- +title: "NavigationActivation: navigationType property" +short-title: navigationType +slug: Web/API/NavigationActivation/navigationType +page-type: web-api-instance-property +status: + - experimental +browser-compat: api.NavigationActivation.navigationType +--- + +{{APIRef("Navigation API")}}{{SeeCompatTable}} + +The **`navigationType`** read-only property of the {{domxref("NavigationActivation")}} interface contains a string indicating the type of navigation. + +## Value + +A string representing the type of navigation the {{domxref("NavigationActivation")}} relates to. Possible values are: + +- `push`: A new location was navigated to, causing a new entry to be pushed onto the history list. +- `reload`: The {{domxref("NavigationActivation.entry")}} was reloaded. +- `replace`: The {{domxref("NavigationActivation.entry")}} was replaced with a new history entry. This new entry will reuse the same {{domxref("NavigationHistoryEntry.key", "key")}}, but be assigned a different {{domxref("NavigationHistoryEntry.id", "id")}}. +- `traverse`: The browser navigated from one existing history entry to another existing history entry. + +## Examples + +```js +window.addEventListener("pageswap", (event) => { + // For example, the page was hidden, or the navigation is cross-document. + if (!event.viewTransition) return; + + // Skip the view transition for back/forward navigations. + if (event.activation.navigationType === "traverse") { + event.viewTransition.skipTransition(); + } +}); +``` + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [Navigation API](/en-US/docs/Web/API/Navigation_API) +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pagerevealevent/index.md b/files/en-us/web/api/pagerevealevent/index.md new file mode 100644 index 000000000000000..655646b72508531 --- /dev/null +++ b/files/en-us/web/api/pagerevealevent/index.md @@ -0,0 +1,104 @@ +--- +title: PageRevealEvent +slug: Web/API/PageRevealEvent +page-type: web-api-interface +status: + - experimental +browser-compat: api.PageRevealEvent +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`PageRevealEvent`** event object is made available inside handler functions for the {{domxref("Window.pagereveal_event", "pagereveal")}} event. + +During a cross-document navigation, it allows you to manipulate a related [view transition](/en-US/docs/Web/API/View_Transitions_API) (providing access to the relevant {{domxref("ViewTransition")}} object) from the document being navigated _to_, if a view transition was triggered by the navigation. + +Outside view transitions, this event is also useful for cases such as triggering a startup animation, or reporting a page view. It's equivalent to the first {{domxref("Window.requestAnimationFrame()")}} run after a cross-document navigation, if you were to trigger `requestAnimationFrame()` in the {{htmlelement("head")}} of the document. For example, if you ran the following `reveal()` function in the `
`: + +```js +function reveal() { + // Include startup animation here +} +/* This will fire in the first rendered frame after loading */ +requestAnimationFrame(() => reveal()); + +/* This will fire if the page is restored from BFCache */ +window.onpagehide = () => requestAnimationFrame(() => reveal()); +``` + +## Constructor + +- {{domxref("PageRevealEvent.PageRevealEvent", "PageRevealEvent()")}} + - : Creates a new {{domxref("PageRevealEvent")}} object instance. + +## Instance properties + +- {{domxref("PageRevealEvent.viewTransition", "viewTransition")}} {{ReadOnlyInline}} {{Experimental_Inline}} + - : Contains a {{domxref("ViewTransition")}} object representing the active view transition for the cross-document navigation. + +## Examples + +```js +window.addEventListener("pagereveal", async (e) => { + // If the "from" history entry does not exist, return + if (!navigation.activation.from) return; + + // Only run this if an active view transition exists + if (e.viewTransition) { + const fromUrl = new URL(navigation.activation.from.url); + const currentUrl = new URL(navigation.activation.entry.url); + + // Went from profile page to homepage + // ~> Set VT names on the relevant list item + if (isProfilePage(fromUrl) && isHomePage(currentUrl)) { + const profile = extractProfileNameFromUrl(fromUrl); + + // Set view-transition-name values on the elements to animate + document.querySelector(`#${profile} span`).style.viewTransitionName = + "name"; + document.querySelector(`#${profile} img`).style.viewTransitionName = + "avatar"; + + // Remove names after snapshots have been taken + // so that we're ready for the next navigation + await e.viewTransition.ready; + document.querySelector(`#${profile} span`).style.viewTransitionName = + "none"; + document.querySelector(`#${profile} img`).style.viewTransitionName = + "none"; + } + + // Went to profile page + // ~> Set VT names on the main title and image + if (isProfilePage(currentUrl)) { + // Set view-transition-name values on the elements to animate + document.querySelector(`#detail main h1`).style.viewTransitionName = + "name"; + document.querySelector(`#detail main img`).style.viewTransitionName = + "avatar"; + + // Remove names after snapshots have been taken + // so that we're ready for the next navigation + await e.viewTransition.ready; + document.querySelector(`#detail main h1`).style.viewTransitionName = + "none"; + document.querySelector(`#detail main img`).style.viewTransitionName = + "none"; + } + } +}); +``` + +> **Note:** See [List of Chrome Dev Rel team members](https://view-transitions.netlify.app/profiles/mpa/) for the live demo this code is taken from. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pagerevealevent/pagerevealevent/index.md b/files/en-us/web/api/pagerevealevent/pagerevealevent/index.md new file mode 100644 index 000000000000000..3b87cbb0858abcc --- /dev/null +++ b/files/en-us/web/api/pagerevealevent/pagerevealevent/index.md @@ -0,0 +1,45 @@ +--- +title: "PageRevealEvent: PageRevealEvent() constructor" +short-title: PageRevealEvent() +slug: Web/API/PageRevealEvent/PageRevealEvent +page-type: web-api-constructor +status: + - experimental +browser-compat: api.PageRevealEvent.PageRevealEvent +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`PageRevealEvent()`** constructor creates a new +{{domxref("PageRevealEvent")}} object instance. + +## Syntax + +```js-nolint +new PageRevealEvent(type, init) +``` + +### Parameters + +- `type` + - : A string representing the type of event. In the case of `PageRevealEvent` this is always `pagereveal`. +- `init` + - : An object containing the following properties: + - `viewTransition` {{optional_inline}} + - : A {{domxref("ViewTransition")}} object representing the active view transition for the related navigation. Defaults to `null` if there is no active view transition. + +## Examples + +A developer would not use this constructor manually. A new `PageRevealEvent` object is constructed when a handler is invoked as a result of the {{domxref("Window.pagereveal_event", "pagereveal")}} event firing. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pagerevealevent/viewtransition/index.md b/files/en-us/web/api/pagerevealevent/viewtransition/index.md new file mode 100644 index 000000000000000..c8ae71d6b4b284a --- /dev/null +++ b/files/en-us/web/api/pagerevealevent/viewtransition/index.md @@ -0,0 +1,34 @@ +--- +title: "PageRevealEvent: viewTransition property" +short-title: viewTransition +slug: Web/API/PageRevealEvent/viewTransition +page-type: web-api-instance-property +status: + - experimental +browser-compat: api.PageRevealEvent.viewTransition +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`viewTransition`** read-only property of the {{domxref("PageRevealEvent")}} interface contains a {{domxref("ViewTransition")}} object representing the active view transition for the cross-document navigation. + +## Value + +A {{domxref("ViewTransition")}} object, or `null` if no view transition is active when the event is fired. + +## Examples + +See the main {{domxref("PageRevealEvent")}} page. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [Navigation API](/en-US/docs/Web/API/Navigation_API) +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pageswapevent/activation/index.md b/files/en-us/web/api/pageswapevent/activation/index.md new file mode 100644 index 000000000000000..ad5231f67d895dd --- /dev/null +++ b/files/en-us/web/api/pageswapevent/activation/index.md @@ -0,0 +1,33 @@ +--- +title: "PageSwapEvent: activation property" +short-title: activation +slug: Web/API/PageSwapEvent/activation +page-type: web-api-instance-property +status: + - experimental +browser-compat: api.PageSwapEvent.activation +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`activation`** read-only property of the {{domxref("PageSwapEvent")}} interface contains a {{domxref("NavigationActivation")}} object containing the navigation type and current and destination document history entries for a same-origin navigation. + +## Value + +A {{domxref("NavigationActivation")}} object, or `null` if the associated navigation has a cross-origin URL anywhere in the redirect chain. + +## Examples + +See the main {{domxref("PageSwapEvent")}} page. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pageswapevent/index.md b/files/en-us/web/api/pageswapevent/index.md new file mode 100644 index 000000000000000..1a2b48ae6a52cfb --- /dev/null +++ b/files/en-us/web/api/pageswapevent/index.md @@ -0,0 +1,92 @@ +--- +title: PageSwapEvent +slug: Web/API/PageSwapEvent +page-type: web-api-interface +status: + - experimental +browser-compat: api.PageSwapEvent +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`PageSwapEvent`** event object is made available inside handler functions for the {{domxref("Window.pageswap_event", "pageswap")}} event. + +The pageswap event is fired when you navigate across documents, when the previous document is about to unload. During a cross-document navigation, the `PageSwapEvent`\*\* event object allows you to manipulate the related [view transition](/en-US/docs/Web/API/View_Transitions_API) (providing access to the relevant {{domxref("ViewTransition")}} object) from the document being navigated _from_, if a view transition was triggered by the navigation. It also provides access to information on the navigation type and current and destination documents. + +## Constructor + +- {{domxref("PageSwapEvent.PageSwapEvent", "PageSwapEvent()")}} + - : Creates a new {{domxref("PageSwapEvent")}} object instance. + +## Instance properties + +- {{domxref("PageSwapEvent.activation", "activation")}} {{ReadOnlyInline}} {{Experimental_Inline}} + - : Contains a {{domxref("NavigationActivation")}} object containing the navigation type and current and destination document history entries for a same-origin navigation. If the navigation has a cross-origin URL anywhere in the redirect chain, it returns `null`. +- {{domxref("PageSwapEvent.viewTransition", "viewTransition")}} {{ReadOnlyInline}} {{Experimental_Inline}} + - : Contains a {{domxref("ViewTransition")}} object representing the active view transition for the cross-document navigation. + +## Examples + +```js +window.addEventListener("pageswap", async (e) => { + // Only run this if an active view transition exists + if (e.viewTransition) { + const currentUrl = e.activation.from?.url + ? new URL(e.activation.from.url) + : null; + const targetUrl = new URL(e.activation.entry.url); + + // Going from profile page to homepage + // ~> The big img and title are the ones! + if (isProfilePage(currentUrl) && isHomePage(targetUrl)) { + // Set view-transition-name values on the elements to animate + document.querySelector(`#detail main h1`).style.viewTransitionName = + "name"; + document.querySelector(`#detail main img`).style.viewTransitionName = + "avatar"; + + // Remove view-transition-names after snapshots have been taken + // Stops naming conflicts resulting from the page state persisting in BFCache + await e.viewTransition.finished; + document.querySelector(`#detail main h1`).style.viewTransitionName = + "none"; + document.querySelector(`#detail main img`).style.viewTransitionName = + "none"; + } + + // Going to profile page + // ~> The clicked items are the ones! + if (isProfilePage(targetUrl)) { + const profile = extractProfileNameFromUrl(targetUrl); + + // Set view-transition-name values on the elements to animate + document.querySelector(`#${profile} span`).style.viewTransitionName = + "name"; + document.querySelector(`#${profile} img`).style.viewTransitionName = + "avatar"; + + // Remove view-transition-names after snapshots have been taken + // Stops naming conflicts resulting from the page state persisting in BFCache + await e.viewTransition.finished; + document.querySelector(`#${profile} span`).style.viewTransitionName = + "none"; + document.querySelector(`#${profile} img`).style.viewTransitionName = + "none"; + } + } +}); +``` + +> **Note:** See [List of Chrome Dev Rel team members](https://view-transitions.netlify.app/profiles/mpa/) for the live demo this code is taken from. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pageswapevent/pageswapevent/index.md b/files/en-us/web/api/pageswapevent/pageswapevent/index.md new file mode 100644 index 000000000000000..e5336b3a418f2cf --- /dev/null +++ b/files/en-us/web/api/pageswapevent/pageswapevent/index.md @@ -0,0 +1,47 @@ +--- +title: "PageSwapEvent: PageSwapEvent() constructor" +short-title: PageSwapEvent() +slug: Web/API/PageSwapEvent/PageSwapEvent +page-type: web-api-constructor +status: + - experimental +browser-compat: api.PageSwapEvent.PageSwapEvent +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`PageSwapEvent()`** constructor creates a new +{{domxref("PageSwapEvent")}} object instance. + +## Syntax + +```js-nolint +new PageSwapEvent(type, init) +``` + +### Parameters + +- `type` + - : A string representing the type of event. In the case of `PageSwapEvent` this is always `pageswap`. +- `init` + - : An object containing the following properties: + - `activation` + - : A {{domxref("NavigationActivation")}} object representing the navigation type and current and destination document history entries. Defaults to `null` if the associated navigation is a cross-origin navigation. + - `viewTransition` + - : A {{domxref("ViewTransition")}} object representing the active view transition for the related navigation. Defaults to `null` if there is no active view transition. + +## Examples + +A developer would not use this constructor manually. A new `PageSwapEvent` object is constructed when a handler is invoked as a result of the {{domxref("Window.pageswap_event", "pageswap")}} event firing. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/pageswapevent/viewtransition/index.md b/files/en-us/web/api/pageswapevent/viewtransition/index.md new file mode 100644 index 000000000000000..350a758553abaf7 --- /dev/null +++ b/files/en-us/web/api/pageswapevent/viewtransition/index.md @@ -0,0 +1,33 @@ +--- +title: "PageSwapEvent: viewTransition property" +short-title: viewTransition +slug: Web/API/PageSwapEvent/viewTransition +page-type: web-api-instance-property +status: + - experimental +browser-compat: api.PageSwapEvent.viewTransition +--- + +{{APIRef("HTML DOM")}}{{SeeCompatTable}} + +The **`viewTransition`** read-only property of the {{domxref("PageRevealEvent")}} interface contains a {{domxref("ViewTransition")}} object representing the active view transition for the cross-document navigation. + +## Value + +A {{domxref("ViewTransition")}} object, or `null` if no view transition is active when the event is fired. + +## Examples + +See the main {{domxref("PageSwapEvent")}} page. + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) diff --git a/files/en-us/web/api/view_transitions_api/index.md b/files/en-us/web/api/view_transitions_api/index.md index 04d3fd0ef96b315..881bc23db331a11 100644 --- a/files/en-us/web/api/view_transitions_api/index.md +++ b/files/en-us/web/api/view_transitions_api/index.md @@ -6,265 +6,33 @@ status: - experimental browser-compat: - api.Document.startViewTransition - - api.ViewTransition + - css.at-rules.view-transition spec-urls: https://drafts.csswg.org/css-view-transitions/ --- {{SeeCompatTable}}{{DefaultAPISidebar("View Transitions API")}} -The **View Transitions API** provides a mechanism for easily creating animated transitions between different DOM states while also updating the DOM contents in a single step. +The **View Transitions API** provides a mechanism for easily creating animated transitions between different website views. This includes animating between DOM states in a single-page app (SPA), and animating the navigation between documents in a multi-page app (MPA). ## Concepts and usage View transitions are a popular design choice for reducing users' cognitive load, helping them stay in context, and reducing perceived loading latency as they move between states or views of an application. -However, creating view transitions on the web has historically been difficult. Transitions between states in single-page apps (SPAs) tend to involve writing significant CSS and JavaScript to: +However, creating view transitions on the web has historically been difficult: -- Handle the loading and positioning of the old and new content. -- Animate the old and new states to create the transition. -- Stop accidental user interactions with the old content from causing problems. -- Remove the old content once the transition is complete. +- Transitions between states in single-page apps (SPAs) tend to involve writing significant CSS and JavaScript to: + - Handle the loading and positioning of the old and new content. + - Animate the old and new states to create the transition. + - Stop accidental user interactions with the old content from causing problems. + - Remove the old content once the transition is complete. + Accessibility issues like loss of reading position, focus confusion, and strange live region announcement behavior can also result from having the new and old content both present in the DOM at once. +- Cross-document view transitions (i.e. across navigations between different pages in MPAs) have historically been impossible. -Accessibility issues like loss of reading position, focus confusion, and strange live region announcement behavior can also result from having the new and old content both present in the DOM at once. In addition, cross-document view transitions (i.e. across different pages in regular, non-SPA websites) are impossible. +The View Transitions API provides an easy way of handling the required view changes and transition animations for both the above use cases. -The View Transitions API provides a much easier way of handling the required DOM changes and transition animations. +Creating a view transition that uses the browser's default transition animations is very quick to do, and there are features that allow you to both customize the transition animation and manipulate the view transition itself (for example specify circumstances under which the animation is skipped), for both SPA and MPA view transitions. -> **Note:** The View Transitions API doesn't currently enable cross-document view transitions, but this is planned for a future level of the spec and is actively being worked on. - -### Creating a basic view transition - -As an example, an SPA may include functionality to fetch new content and update the DOM in response to an event of some kind, such as a navigation link being clicked or an update being pushed from the server. In our [Basic View Transitions demo](https://mdn.github.io/dom-examples/view-transitions/) we've simplified this to a `displayNewImage()` function that shows a new full-size image based on the thumbnail that was clicked. We've encapsulated this inside an `updateView()` function that only calls the View Transition API if the browser supports it: - -```js -function updateView(event) { - // Handle the difference in whether the event is fired on the or the