Skip to content

Commit

Permalink
Document ViewTransitions API reference (#9174)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Armand Philippot <[email protected]>
Co-authored-by: Yan <[email protected]>
Co-authored-by: Martin Trapp <[email protected]>
  • Loading branch information
5 people authored Aug 29, 2024
1 parent 979da67 commit dd35681
Showing 1 changed file with 345 additions and 1 deletion.
346 changes: 345 additions & 1 deletion src/content/docs/en/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,350 @@ export const onRequest = defineMiddleware(async (context, next) => {
})
```

## View Transitions client API (`astro:transitions/client`)

<p><Since v="3.2.0" /></p>

This module provides functions to control and interact with the View Transitions API and client-side router.

For features and usage examples, [see our View Transitions guide](/en/guides/view-transitions/).

### `navigate()`

<p>

**Type:** `(href: string, options?: Options) => void`<br />
<Since v="3.2.0" />
</p>

A function that 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.

#### `history` option

<p>

**Type:** `'auto' | 'push' | 'replace'`<br />
**Default:** `'auto'`<br />
<Since v="3.2.0" />
</p>

Defines how this navigation should be added to the browser history.

- `'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 cannot be transitioned to, the current URL will remain with no changes to the browser history.

This option follows the [`history` option](https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate#history) from the browser Navigation API but simplified for the cases that can happen on an Astro project.

#### `formData` option

<p>

**Type:** `FormData`<br />
<Since v="3.5.0" />
</p>

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.

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.

#### `info` option

<p>

**Type:** `any`<br />
<Since v="3.6.0" />
</p>

Arbitrary data to be included in the `astro:before-preparation` and `astro:before-swap` events caused by this navigation.

This option mimics the [`info` option](https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate#info) from the browser Navigation API.

#### `state` option

<p>

**Type:** `any`<br />
<Since v="3.6.0" />
</p>

Arbitrary data to be associated with the `NavitationHistoryEntry` object created by this navigation. This data can then be retrieved using the [`history.getState` function](https://developer.mozilla.org/en-US/docs/Web/API/NavigationHistoryEntry/getState) from the History API.

This option mimics the [`state` option](https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate#state) from the browser Navigation API.

#### `sourceElement` option

<p>

**Type:** `Element`<br />
<Since v="3.6.0" />
</p>

The element that triggered this navigation, if any. This element will be available in the following events:
- `astro:before-preparation`
- `astro:before-swap`

### `supportsViewTransitions`

<p>

**Type:** `boolean`<br />
<Since v="3.2.0" />
</p>

Whether or not view transitions are supported and enabled in the current browser.

### `transitionEnabledOnThisPage`

<p>

**Type:** `boolean`<br />
<Since v="3.2.0" />
</p>

Whether or not the current page has view transitions enabled for client-side navigation. This can be used to make components that behave differently when they are used on pages with view transitions.

### `getFallback`

<p>

**Type:** `() => 'none' | 'animate' | 'swap'`<br />
<Since v="3.6.0" />
</p>

Returns the fallback strategy to use in browsers that do not support view transitions.

See the guide on [Fallback control](/en/guides/view-transitions/#fallback-control) for how to choose and configure the fallback behavior.


### `astro:before-preparation` event

An event dispatched at the beginning of a navigation using View Transitions. 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)
- [`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).

### `astro:after-preparation` event

An event dispatched after the next page in a navigation using View Transitions 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).

### `astro:before-swap` event

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.

This event has the attributes:
- [`info`](#info)
- [`sourceElement`](#sourceelement)
- [`navigationType`](#navigationtype)
- [`direction`](#direction)
- [`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).

### `astro:after-swap` 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

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.

### View Transitions events attributes

<p><Since v="3.6.0" /></p>

#### `info`

<p>

**Type:** `URL`
</p>

Arbitrary data defined during navigation.

This is the literal value passed on the [`info` option](#info-option) of the [`navigate()` function](#navigate).

#### `sourceElement`

<p>

**Type:** `Element | undefined`
</p>

The element that triggered the navigation. This can be, for example, an `<a>` element that was clicked.

When using the [`navigate()` function](#navigate), this will be the element specified in the call.

#### `newDocument`

<p>

**Type:** `Document`
</p>

The document for the next page in the navigation. The contents of this document will be swapped in place of the contents of the current document.

#### `navigationType`

<p>

**Type:** `'push' | 'replace' | 'traverse'`
</p>

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)

#### `direction`

<p>

**Type:** `Direction`
</p>

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.

#### `from`

<p>

**Type:** `URL`
</p>

The URL of the page initiating the navigation.

#### `to`

<p>

**Type:** `URL`
</p>

The URL of the page being navigated to. This property can be modified, the value at the end of the lifecycle will be used in the `NavigationHistoryEntry` for the next page.

#### `formData`

<p>

**Type:** `FormData | undefined`
</p>

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

#### `loader`

<p>

**Type:** `() => Promise<void>`
</p>

Implementation of the following phase in the navigation (loading the next page). This implementation can be overridden to add extra behavior.

#### `viewTransition`

<p>

**Type:** [`ViewTransition`](https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition)
</p>

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.

#### `swap`

<p>

**Type:** `() => void`
</p>

Implementation of the document swap logic.

Read more on how to [build your own custom swap function](/en/guides/view-transitions/#building-a-custom-swap-function) on the View Transitions guide.

By default, this implementation will call the following functions in order:

##### `deselectScripts()`

<p>

**Type:** `(newDocument: Document) => void`
</p>

Marks scripts in the new document that should not be executed. Those scripts are already in the current document and are not flagged for re-execution using [`data-astro-rerun`](/en/guides/view-transitions/#data-astro-rerun).

##### `swapRootAttributes()`

<p>

**Type:** `(newDocument: Document) => void`
</p>

Swaps the attributes between the document roots, like the `lang` attribute. This also includes Astro-injected internal attributes like `data-astro-transition`, which makes the transition direction available to Astro-generated CSS rules.

When making a custom swap function, it is important to call this function so as not to break the view transition's animations.

##### `swapHeadElements()`

<p>

**Type:** `(newDocument: Document) => void`
</p>

Removes every element from the current document's `<head>` that is not persisted to the new document. Then appends all new elements from the new document's `<head>` to the current document's `<head>`.

##### `saveFocus()`

<p>

**Type:** `() => () => void`
</p>

Stores the element in focus on the current page and returns a function that when called, if the focused element was persisted, returns the focus to it.


##### `swapBodyElements()`

<p>

**Type:** `(newBody: Element, oldBody: Element) => void`
</p>

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.

## Actions (`astro:actions`)

<p>
Expand Down Expand Up @@ -2506,4 +2850,4 @@ const serverObject = {
This component provides a way to inspect values on the client-side, without any JavaScript.


[canonical]: https://en.wikipedia.org/wiki/Canonical_link_element
[canonical]: https://en.wikipedia.org/wiki/Canonical_link_element

0 comments on commit dd35681

Please sign in to comment.