--- title: The Element Crossing description: "@vtbag/element-crossing allows you carry over state to the next page when using cross-document view transitions" tableOfContents: minHeadingLevel: 2 maxHeadingLevel: 3 head: - tag: meta attrs: property: "og:image" content: "/crossing-og.png" --- import { Steps } from "@astrojs/starlight/components"; import Logo from "@/components/Logo.astro"; import crossing from "@/assets/crossing.png"; import CTA from "@/components/CTA.astro"; import Media from "@/components/Media.astro"; Transfer selected element states across cross-document view transitions. The Element Crossing allows you to preserve state of the elements of your current page and carry it over to the next page when using cross-document view transitions. This also works independent of view transitions as long as all pages have the same origin. Tag your DOM elements for crossing and find their current dynamic state on the next page. [Watch the Demo!](/crossing/vanilla/1/) ## PREREQUISITE :::note[Best when used with the View Transition API] The Element Crossing is designed for use with cross-document view transitions. It might also be used independently of view transitions and on browsers without native support for Level 2 of the View Transition API. In that case, the fallback might appear jumpy, depending on the element state it's applied to. ::: ## State Loss on Cross-Document Transitions You can easily activate and customize cross-document view transitions using just CSS! But keep in mind that swapping in a new page will reset all your current dynamic DOM state. Check out [this example](/crossing/plain/1/) to see which parts of the DOM are impacted: - The state of collapsed link groups - Changes to the color scheme - The position of the video and animation in the footer - The content of the form at the bottom of the page In those cases, navigation not only results in a regrettable loss of information but can also be visually disrupt the seamless and smooth effect of view transitions. ## Using the Element Crossing The Element Crossing script helps you preserve state that matters and minimize visual disruptions. It comes in two flavors: **Vanilla** and **Over-the-Top**. Here, **Vanilla** stands for _reliable_, _well-understood_ state of the art. And **Over-the-Top** is… well, very much over the top: _experimental_, _fancy_ and ...\ ...**Disclaimer: might not work at all in your settings.** From the feature list, there are only few difference: - Vanilla stores the state of DOM and CSS properties in the `sessionStorage` to move them to the new page across a full page load. - Over-the-Top can also preserve whole DOM trees across cross-document view transitions and offers a `crossingStorage` to preserve arbitrary JavaScript objects across navigation. Coincidentally, the implementation of the crossingStorage literally works _over the top (window)_. ### Element Crossing Demo: Vanilla The [Vanilla demo](/crossing/vanilla/1/) showcases how this version of the Element Crossing can maintain state across cross-document view transitions. You can directly incorporate it into your own projects. ### Element Crossing Demo: Experimental Over-the-Top Experimental and maybe never mature for production: The [Over-the-top demo](/crossing/over-the-top/1/) demonstrates the additional features of the over-the-top version. ## Installation :::tip If you want to use the ElementCrossing with an Astro project, see [astro-vtbot](https://events-3bg.pages.dev/library/ElementCrossing/) for reusable components you can easily use in your project. ::: To use the Element Crossing, you can install the npm package in your project. Alternatively you can load the script from one of the global content delivery networks that provide npm packages. 1. Install `@vtbag/element-crossing` from npm. 2. In your project, add `@vtbag/element-crossing/lib/vanilla.js` as an external script at the beginning of the `` element on all pages where you want to retrieve or restore state during navigation. Details depend on your project setup and the frameworks used, but with a bundler like `vite` it can be as simple as: ```jsx import crossing from "@vtbag/element-crossing?url"; ... ... ; ``` To experiment with the over-the-top flavour include the `@vtbag/element-crossing/experimental` and the `@vtbag/element-crossing` script in that order. ## Vanilla Configuration To move state to the next page, you need to tell the Element Crossing for which HTML elements to move state and what state to move. Assigning the `data-vtbag-x` attribute to identify the elements that should have some state carried over. ### Expressions The `data-vtbag-x` attribute accepts a string that has to fulfil the following grammar:

```awk data-vtbag-x ::= 'id:' identifier specification ; specification ::= ( space+ ( 'prop:' | 'class:' | 'style:' | 'anim:' ) name )* ; ```

{/* bottlecaps.de/rr/ui */} id: identifier space prop: class: style: anim: name
```awk data-vtbag-x ::= 'id:' identifier specification ; specification ::= ( space+ ( 'prop:' | 'class:' | 'style:' | 'anim:' ) name )* ; ``` #### id The `id` expression is used to link elements between the old and new pages, allowing state to be transferred from the current HTML element to the target element with the same id on the subsequent page. If an id value appears exclusively on either the old or the new page, no state transfer occurs. It's important that id values remain unique on their respective pages. If the current HTML element already has an `id` attribute, the id expression within the `data-vtbag-x` string can be omitted. #### prop The `prop` expression identifies a property of the current HTML element. The value of this property is then transferred to the corresponding property of the target element on the next page. #### class The `class` expression specifies an entry in the current HTML element's `classList` property. If the class is present on the current element, it will be added to the target element's class list. Conversely, if the class is absent on the current element, it will be removed from the target element's class list. #### style The style expression identifies an entry of the current HTML element's `style` property. The value of this entry is transferred to the `style` property off the target element on the next page. This does not give access to CSS properties only available in computed styles. #### anim The anim expression references a CSSAnimation of the current HTML element. If the target element on the next page has an animation with the same `animationName`, the current playback time is transferred. Note these current restrictions of the Element Crossing: The `animationName` values must be unique per HTML element, and only CSSAnimations are supported, not CSSTransitions. The `anim` expression can also be used to persist SVG animation states. Using the special `anim:/svg` expression, it transfers the current animation playback time to the target element. The expression needs to be declared on the `` element, not on the `` element. ### Abbreviated Forms The long forms `id:`, `prop:`, `class:`, `style:`, and `anim:` can be abbreviated as `#`, `@`, `.`, `-`, and `~`, respectively. For example, instead of writing `data-vtbag-x="id:group class:active prop:value"`, you could write `data-vtbag-x="#group .active @value"`, or use a combination of both forms. ### Applications with Real-World Examples Although the expressions may seem basic, it's amazing how easily you can create the desired effects. Address any CSS property or DOM element property, any CSS class, or CSS animation. Below is a table that outlines the expressions used in the example above: | Effect | HTML (on the old and new page) | |---|---| | Do not reset the scroll position of the sidebar on navigation | `
` | | Keep the current `dark` CSS class, the `data-theme` property and the `color-scheme` CSS property | `` | | Do not lose the input of the form elements or the textarea dimensions | ``, ``, `