diff --git a/src/content/docs/en/guides/view-transitions.mdx b/src/content/docs/en/guides/view-transitions.mdx index 5ca16210a1291..f2c1ea2415e08 100644 --- a/src/content/docs/en/guides/view-transitions.mdx +++ b/src/content/docs/en/guides/view-transitions.mdx @@ -3,6 +3,7 @@ title: View transitions description: Enable seamless navigation between pages in Astro with view transitions. i18nReady: true --- +import ReadMore from '~/components/ReadMore.astro'; import Since from '~/components/Since.astro' import { Steps } from '@astrojs/starlight/components' @@ -319,7 +320,7 @@ Links with the `data-astro-reload` attribute will be ignored by the router and a ### Trigger navigation -You can also trigger client-side navigation via events not normally listened to by the `` router using `navigate`. This function from the `astro:transitions/client` module can be used in scripts, and in framework components that are hydrated with a [client directive](/en/reference/directives-reference/#client-directives). +You can also trigger client-side navigation via events not normally listened to by the `` router using [`navigate()`](/en/reference/modules/astro-transitions/#navigate). This function from the `astro:transitions/client` module can be used in scripts, and in framework components that are hydrated with a [client directive](/en/reference/directives-reference/#client-directives). The following example shows an Astro component that navigates a visitor to another page they select from a menu: @@ -390,27 +391,17 @@ import { ClientRouter } from "astro:transitions"; ``` -The `navigate` method takes these arguments: - -- `href` (required) - The new page to navigate to. -- `options` - An optional object with the following properties: - - `history`: `"push"` | `"replace"` | `"auto"` - - `"push"`: the router will use `history.pushState` to create a new entry in the browser history. - - `"replace"`: the router will use `history.replaceState` to update the URL without adding a new entry into navigation. - - `"auto"` (default): the router will attempt `history.pushState`, but if the URL is not one that can be transitioned to, the current URL will remain with no changes to the browser history. - - `formData`: A [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object for `POST` requests. - For backward and forward navigation through the browser history, you can combine `navigate()` with the built-in `history.back()`, `history.forward()` and `history.go()` functions of the browser. If `navigate()` is called during the server-side render of your component, it has no effect. +See the `astro:transitions` reference for more information about the [`navigate()` options](/en/reference/modules/astro-transitions/#navigate). + ### Replace entries in the browser history Normally, each time you navigate, a new entry is written to the browser's history. This allows navigation between pages using the browser's `back` and `forward` buttons. The `` router allows you to overwrite history entries by adding the `data-astro-history` attribute to any individual `` tag. -The `data-astro-history` attribute can be set to the same three values as the [`history` option of the `navigate()` function](#trigger-navigation): - -`data-astro-history`: `"push"` | `"replace"` | `"auto"` +The `data-astro-history` attribute can be set to the same three values as the [`history` option of the `navigate()` function](/en/reference/modules/astro-transitions/#history-option): - `"push"`: the router will use `history.pushState` to create a new entry in the browser history. - `"replace"`: the router will use `history.replaceState` to update the URL without adding a new entry into navigation. @@ -605,7 +596,7 @@ This event is used: - To alter loading, such as loading content you've defined in a template rather than from the external URL. - To change the `direction` of the navigation (which is usually either `forward` or `backward`) for custom animation. -Here is an example of using the `astro:before-preparation` event to load a spinner before the content is loaded and stop it immediately after loading. Note that using the `loader` callback in this way allows asynchronous execution of code. +Here is an example of using the `astro:before-preparation` event to load a spinner before the content is loaded and stop it immediately after loading. Note that using the [`loader` callback](/en/reference/modules/astro-transitions/#loader) in this way allows asynchronous execution of code. ```astro + + +``` + +## Imports from `astro:transitions/client` + +```ts +import { + getFallback, + isTransitionBeforePreparationEvent, + isTransitionBeforeSwapEvent, + navigate, + supportsViewTransitions, + swapFunctions, + transitionEnabledOnThisPage, + TRANSITION_AFTER_PREPARATION, + TRANSITION_AFTER_SWAP, + TRANSITION_BEFORE_PREPARATION, + TRANSITION_BEFORE_SWAP, + TRANSITION_PAGE_LOAD, +} from 'astro:transitions/client'; ``` ### `navigate()` @@ -115,7 +194,7 @@ import { slide } from 'astro:transitions';

-A function that executes a navigation to the given `href` using the View Transitions API. +Executes a navigation to the given `href` using the View Transitions API. This function signature is based on the [`navigate` function from the browser Navigation API](https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate). Although based on the Navigation API, this function is implemented on top of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to allow for navigation without reloading the page. @@ -144,9 +223,7 @@ This option follows the [`history` option](https://developer.mozilla.org/en-US/d

-A `FormData` object for `POST` requests. - -When this option is provided, the requests to the navigation target page will be sent as a `POST` request with the form data object as the content. +When provided, the requests to the navigation target page will be sent as a `POST` request with the `FormData` object as the content. Submitting an HTML form with view transitions enabled will use this method instead of the default navigation with page reload. Calling this method allows triggering the same behavior programmatically. @@ -183,8 +260,8 @@ This option mimics the [`state` option](https://developer.mozilla.org/en-US/docs

The element that triggered this navigation, if any. This element will be available in the following events: -- `astro:before-preparation` -- `astro:before-swap` +- [`astro:before-preparation`](#astrobefore-preparation-event) +- [`astro:before-swap`](#astrobefore-swap-event) ### `supportsViewTransitions` @@ -196,11 +273,11 @@ The element that triggered this navigation, if any. This element will be availab Whether or not view transitions are supported and enabled in the current browser. -### `transitionEnabledOnThisPage` +### `transitionEnabledOnThisPage()`

-**Type:** `boolean`
+**Type:** `() => boolean`

@@ -210,18 +287,19 @@ Whether or not the current page has view transitions enabled for client-side nav

-**Type:** `() => 'none' | 'animate' | 'swap'`
+**Type:** () =>
Fallback

-Returns the fallback strategy to use in browsers that do not support view transitions. +Returns the fallback strategy to use in browsers that do not support view transitions. This returns `animate` by default when the strategy has not been overridden. -See the guide on [Fallback control](/en/guides/view-transitions/#fallback-control) for how to choose and configure the fallback behavior. +See the guide on [Fallback control](/en/guides/view-transitions/#fallback-control) for how to choose and configure the fallback behavior. ### `swapFunctions`

+**Type:** `object`

@@ -278,34 +356,331 @@ Stores the element in focus on the current page and returns a function that when Replaces the old body with the new body. Then, goes through every element in the old body that should be persisted and have a matching element in the new body and swaps the old element back in place. +### `isTransitionBeforePreparationEvent` + +

+ +**Type:** `(value: any) => boolean`
+ +

+ +Determines whether the given value matches a [`TransitionBeforePreparationEvent`](#transitionbeforepreparationevent). This can be useful when you need to narrow the type of an event in an event listener. + +```astro title="src/pages/index.astro" "isTransitionBeforePreparationEvent" +--- +--- + + +``` + +### `isTransitionBeforeSwapEvent` + +

+ +**Type:** `(value: any) => boolean`
+ +

+ +Determines whether the given value matches a [`TransitionBeforeSwapEvent`](#transitionbeforeswapevent). This can be useful when you need to narrow the type of an event in an event listener. + +```astro title="src/pages/index.astro" "isTransitionBeforeSwapEvent" +--- +--- + + +``` + +### `TRANSITION_BEFORE_PREPARATION` + +

+ +**Type:** `'astro:before-preparation'`
+ +

+ +A constant to avoid writing the `astro:before-preparation` event name in plain text when you define an event. + +```astro title="src/pages/index.astro" "TRANSITION_BEFORE_PREPARATION" +--- +--- + + +``` + +### `TRANSITION_AFTER_PREPARATION` + +

+ +**Type:** `'astro:after-preparation'`
+ +

+ +A constant to avoid writing the `astro:after-preparation` event name in plain text when you define an event. + +```astro title="src/pages/index.astro" "TRANSITION_AFTER_PREPARATION" +--- +--- + + +``` + +### `TRANSITION_BEFORE_SWAP` + +

+ +**Type:** `'astro:before-swap'`
+ +

+ +A constant to avoid writing the `astro:before-swap` event name in plain text when you define an event. + +```astro title="src/pages/index.astro" "TRANSITION_BEFORE_SWAP" +--- +--- + + +``` + +### `TRANSITION_AFTER_SWAP` + +

+ +**Type:** `'astro:after-swap'`
+ +

+ +A constant to avoid writing the `astro:after-swap` event name in plain text when you define an event. + +```astro title="src/pages/index.astro" "TRANSITION_AFTER_SWAP" +--- +--- + + +``` + +### `TRANSITION_PAGE_LOAD` + +

+ +**Type:** `'astro:page-load'`
+ +

+ +A constant to avoid writing the `astro:page-load` event name in plain text when you define an event. + +```astro title="src/pages/index.astro" "TRANSITION_PAGE_LOAD" +--- +--- + + +``` + +## `astro:transitions/client` types + +```ts +import type { + Direction, + Fallback, + NavigationTypeString, + Options, + TransitionBeforePreparationEvent, + TransitionBeforeSwapEvent, +} from 'astro:transitions/client'; +``` + +### `Direction` + +

+ +**Type:** `'forward' | 'back'`
+ +

+ +A union of animation directions: +- `forward`: navigating to the next page in the history or to a new page. +- `back`: navigating to the previous page in the history. + +### `Fallback` + +

+ +**Type:** `'none' | 'animate' | 'swap'`
+ +

+ +A union of fallback strategies to use in browsers that do not support view transitions: +- `animate`: Astro will simulate view transitions using custom attributes before updating page content. +- `swap`: Astro will not attempt to animate the page. Instead, the old page will be immediately replaced by the new one. +- `none`: Astro will not do any animated page transitions at all. Instead, you will get full page navigation in non-supporting browsers. + +Learn more about [controlling the fallback strategy](/en/guides/view-transitions/#fallback-control) with the `ClientRouter`. + +### `NavigationTypeString` + +

+ +**Type:** `'push' | 'replace' | 'traverse'`
+ +

+ +A union of supported history navigation events. + +### `TransitionBeforePreparationEvent` + +

+ +**Type:** `Event`
+ +

+ +Represents an [`astro:before-preparation` event](#astrobefore-preparation). This can be useful to type the event received by a listener: + +```astro title="src/pages/index.astro" "TransitionBeforePreparationEvent" +--- +--- + + +``` + +### `TransitionBeforeSwapEvent` + +

+ +**Type:** `Event`
+ +

+ +Represents an [`astro:before-swap` event](#astrobefore-swap). This can be useful to type the event received by a listener: + +```astro title="src/pages/index.astro" "TransitionBeforeSwapEvent" +--- +--- + + +``` + ## Lifecycle events ### `astro:before-preparation` event +

+ +**Type:** [`TransitionBeforePreparationEvent`](#transitionbeforepreparationevent)
+ +

+ An event dispatched at the beginning of a navigation using the View Transitions router. This event happens before any request is made and any browser state is changed. This event has the attributes: - [`info`](#info) - [`sourceElement`](#sourceelement) - [`navigationType`](#navigationtype) -- [`direction`](#direction) +- [`direction`](#direction-1) - [`from`](#from) - [`to`](#to) - [`formData`](#formdata) - [`loader()`](#loader) -Read more about how to use this event on the [View Transitions guide](/en/guides/view-transitions/#astrobefore-preparation). +Read more about how to use this event on the [View Transitions guide](/en/guides/view-transitions/#astrobefore-preparation). ### `astro:after-preparation` event +

+ +**Type:** `Event`
+ +

+ An event dispatched after the next page in a navigation using View Transitions router is loaded. This event has no attributes. -Read more about how to use this event on the [View Transitions guide](/en/guides/view-transitions/#astroafter-preparation). +Read more about how to use this event on the [View Transitions guide](/en/guides/view-transitions/#astroafter-preparation). ### `astro:before-swap` event +

+ +**Type:** [`TransitionBeforeSwapEvent`](#transitionbeforeswapevent)
+ +

+ An event dispatched after the next page is parsed, prepared, and linked into a document in preparation for the transition but before any content is swapped between the documents. This event can't be canceled. Calling `preventDefault()` is a no-op. @@ -314,22 +689,32 @@ This event has the attributes: - [`info`](#info) - [`sourceElement`](#sourceelement) - [`navigationType`](#navigationtype) -- [`direction`](#direction) +- [`direction`](#direction-1) - [`from`](#from) - [`to`](#to) - [`viewTransition`](#viewtransition) - [`swap()`](#swap) -Read more about how to use this event on the [View Transitions guide](/en/guides/view-transitions/#astrobefore-swap). +Read more about how to use this event on the [View Transitions guide](/en/guides/view-transitions/#astrobefore-swap). ### `astro:after-swap` event +

+ +**Type:** `Event` +

+ An event dispatched after the contents of the page have been swapped but before the view transition ends. The history entry and scroll position have already been updated when this event is triggered. ### `astro:page-load` event +

+ +**Type:** `Event` +

+ An event dispatched after a page completes loading, whether from a navigation using view transitions or native to the browser. When view transitions is enabled on the page, code that would normally execute on `DOMContentLoaded` should be changed to execute on this event. @@ -338,11 +723,13 @@ When view transitions is enabled on the page, code that would normally execute o

+The following attributes are common to both the [`astro:before-preparation`](#astrobefore-preparation-event) and [`astro:before-swap`](#astrobefore-swap-event) events, except for some that are only available with one or the other. + #### `info`

-**Type:** `URL` +**Type:** `any`

Arbitrary data defined during navigation. @@ -373,26 +760,24 @@ The document for the next page in the navigation. The contents of this document

-**Type:** `'push' | 'replace' | 'traverse'` +**Type:** [`NavigationTypeString`](#navigationtypestring)

Which kind of history navigation is happening. - `push`: a new `NavigationHistoryEntry` is being created for the new page. - `replace`: the current `NavigationHistoryEntry` is being replaced with an entry for the new page. -- `traverse`: no `NavigationHistoryEntry` is created. The position in the history is changing. - The direction of the traversal is given on the [`direction` attribute](#direction) +- `traverse`: no `NavigationHistoryEntry` is created. The position in the history is changing. The direction of the traversal is given on the [`direction` attribute](#direction-1). #### `direction`

-**Type:** `Direction` +**Type:** `string`

-The direction of the transition. -- `forward`: navigating to the next page in the history or to a new page. -- `back`: navigating to the previous page in the history. -- Anything else some other listener might have set. +The direction of the transition. This can be a predefined [`Direction`](#direction) or anything else some other listener might have set. + +In the [`astro:before-preparation` event](#astrobefore-swap-event), the value is writable and accepts any `string`. In the [`astro:before-swap` event](#astrobefore-swap-event), the value is readonly. #### `from` @@ -416,12 +801,11 @@ The URL of the page being navigated to. This property can be modified, the value

-**Type:** `FormData | undefined` +**Type:** `FormData | undefined`
+**Available in:** [`astro:before-preparation` event](#astrobefore-preparation-event)

-A `FormData` object for `POST` requests. - -When this attribute is set, a `POST` request will be sent to the [`to` URL](#to) with the given form data object as the content instead of the normal `GET` request. +When set, a `POST` request will be sent to the [`to` URL](#to) with the given `FormData` object as the content instead of the normal `GET` request. When submitting an HTML form with view transitions enabled, this field is automatically set to the data in the form. When using the [`navigate()` function](#navigate), this value is the same as given in the options. @@ -429,7 +813,8 @@ When submitting an HTML form with view transitions enabled, this field is automa

-**Type:** `() => Promise` +**Type:** `() => Promise`
+**Available in:** [`astro:before-preparation` event](#astrobefore-preparation-event)

Implementation of the following phase in the navigation (loading the next page). This implementation can be overridden to add extra behavior. @@ -438,7 +823,8 @@ Implementation of the following phase in the navigation (loading the next page).

-**Type:** [`ViewTransition`](https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition) +**Type:** [`ViewTransition`](https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition)
+**Available in:** [`astro:before-swap` event](#astrobefore-swap-event)

The view transition object used in this navigation. On browsers that do not support the [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API), this is an object implementing the same API for convenience but without the DOM integration. @@ -447,17 +833,16 @@ The view transition object used in this navigation. On browsers that do not supp

-**Type:** `() => void` +**Type:** `() => void`
+**Available in:** [`astro:before-swap` event](#astrobefore-swap-event)

-Implementation of the document swap logic. - -Read more about [building a custom swap function](/en/guides/view-transitions/#building-a-custom-swap-function) in the View Transitions guide. - -By default, this implementation will call the following functions in order: +Calls the default document swap logic. By default, this implementation will call the following functions in order: 1. [`deselectScripts()`](#deselectscripts) 2. [`swapRootAttributes()`](#swaprootattributes) 3. [`swapHeadElements()`](#swapheadelements) 4. [`saveFocus()`](#savefocus) 5. [`swapBodyElement()`](#swapbodyelement) + +Read more about [building a custom swap function](/en/guides/view-transitions/#building-a-custom-swap-function) in the View Transitions guide.