-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persistent DOM in ViewTransitions (#7861)
* First pass at transition:persist * Persistent islands * Changeset * Updated compiler * Use official release * Upgrade again * Refactor to allow head content to persist untouched * >= * Specify the types for "astro:persist" * Automatically persist links * Use reference for array * Upgrade to latest compiler version * Explain the feature * Update .changeset/empty-experts-unite.md Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> * Update .changeset/empty-experts-unite.md Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> * Update .changeset/empty-experts-unite.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update .changeset/empty-experts-unite.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
4e651af
commit 41afb84
Showing
18 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Persistent DOM and Islands in Experimental View Transitions | ||
|
||
With `viewTransitions: true` enabled in your Astro config's experimental section, pages using the `<ViewTransition />` routing component can now access a new `transition:persist` directive. | ||
|
||
With this directive, you can keep the state of DOM elements and islands on the old page when transitioning to the new page. | ||
|
||
For example, to keep a video playing across page navigation, add `transition:persist` to the element: | ||
|
||
```astro | ||
<video controls="" autoplay="" transition:persist> | ||
<source src="https://ia804502.us.archive.org/33/items/GoldenGa1939_3/GoldenGa1939_3_512kb.mp4" type="video/mp4"> | ||
</video> | ||
``` | ||
|
||
This `<video>` element, with its current state, will be moved over to the next page (if the video also exists on that page). | ||
|
||
Likewise, this feature works with any client-side framework component island. In this example, a counter's state is preserved and moved to the new page: | ||
|
||
```astro | ||
<Counter count={5} client:load transition:persist /> | ||
``` | ||
|
||
See our [View Transitions Guide](https://docs.astro.build/en/guides/view-transitions/#maintaining-state) to learn more on usage. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/astro/e2e/fixtures/view-transitions/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import react from '@astrojs/react'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [react()], | ||
experimental: { | ||
viewTransitions: true, | ||
assets: true, | ||
}, | ||
vite: { | ||
build: { | ||
assetsInlineLimit: 0, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/astro/e2e/fixtures/view-transitions/src/components/Island.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.counter { | ||
display: grid; | ||
font-size: 2em; | ||
grid-template-columns: repeat(3, minmax(0, 1fr)); | ||
margin-top: 2em; | ||
place-items: center; | ||
} | ||
|
||
.counter-message { | ||
text-align: center; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/astro/e2e/fixtures/view-transitions/src/components/Island.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React, { useState } from 'react'; | ||
import './Island.css'; | ||
|
||
export default function Counter({ children, count: initialCount, id }) { | ||
const [count, setCount] = useState(initialCount); | ||
const add = () => setCount((i) => i + 1); | ||
const subtract = () => setCount((i) => i - 1); | ||
|
||
return ( | ||
<> | ||
<div id={id} className="counter"> | ||
<button className="decrement" onClick={subtract}>-</button> | ||
<pre>{count}</pre> | ||
<button className="increment" onClick={add}>+</button> | ||
</div> | ||
<div className="counter-message">{children}</div> | ||
</> | ||
); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<video controls="" autoplay="" name="media" transition:persist transition:name="video"> | ||
<source src="https://ia804502.us.archive.org/33/items/GoldenGa1939_3/GoldenGa1939_3_512kb.mp4" type="video/mp4"> | ||
</video> |
9 changes: 9 additions & 0 deletions
9
packages/astro/e2e/fixtures/view-transitions/src/pages/island-one.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
import Island from '../components/Island.jsx'; | ||
--- | ||
<Layout> | ||
<p id="island-one">Page 1</p> | ||
<a id="click-two" href="/island-two">go to 2</a> | ||
<Island count={5} client:load transition:persist transition:name="counter" /> | ||
</Layout> |
9 changes: 9 additions & 0 deletions
9
packages/astro/e2e/fixtures/view-transitions/src/pages/island-two.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
import Island from '../components/Island.jsx'; | ||
--- | ||
<Layout> | ||
<p id="island-two">Page 2</p> | ||
<a id="click-one" href="/island-one">go to 1</a> | ||
<Island count={2} client:load transition:persist transition:name="counter" /> | ||
</Layout> |
17 changes: 17 additions & 0 deletions
17
packages/astro/e2e/fixtures/view-transitions/src/pages/video-one.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
import Video from '../components/Video.astro'; | ||
--- | ||
<Layout> | ||
<p id="video-one">Page 1</p> | ||
<a id="click-two" href="/video-two">go to 2</a> | ||
<Video /> | ||
<script> | ||
const vid = document.querySelector('video'); | ||
vid.addEventListener('canplay', () => { | ||
// Jump to the 1 minute mark | ||
vid.currentTime = 60; | ||
vid.dataset.ready = ''; | ||
}, { once: true }); | ||
</script> | ||
</Layout> |
14 changes: 14 additions & 0 deletions
14
packages/astro/e2e/fixtures/view-transitions/src/pages/video-two.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
import Video from '../components/Video.astro'; | ||
--- | ||
<style> | ||
#video-two { | ||
color: blue; | ||
} | ||
</style> | ||
<Layout> | ||
<p id="video-two">Page 2</p> | ||
<a id="click-one" href="/video-one">go to 1</a> | ||
<Video /> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.