-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use view transitions where preferred & possible
Adds the ability to use the View Transition API. It's based around the MPA View Transition support which is currently becoming available in Chrome. When navigating between pages, we check for the presence of `view-transition` meta tags in both documents, with a value of `same-origin`. If those tags are present, and the browser supports the View Transition API, we can render the page update within a transition. When not opted in with the meta tags, or when support is not available, we fallback to the previous behaviour. This mimics the behaviour that a supporting browser would have when performing a full-page navigation between those pages. We also suppress snapshot caching on pages that specify these meta tags, since the snapshots would interfere with the animations. Note that while the API is based on MPA view transitions, the implementation on requires SPA view transitions, which is already available in the latest Chrome versions. Also note that this is based on an API that is very new, and not yet widely supported. We'll want to keep an eye on how that develops and update our implementation accordingly.
- Loading branch information
1 parent
96a4f58
commit ade4ed3
Showing
7 changed files
with
132 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
declare global { | ||
type ViewTransition = { | ||
finished: Promise<void> | ||
} | ||
|
||
interface Document { | ||
startViewTransition?(callback: () => void): ViewTransition | ||
} | ||
} | ||
|
||
export function withViewTransition(shouldTransition: boolean, callback: () => Promise<void>): Promise<void> { | ||
if (shouldTransition && document.startViewTransition) { | ||
return document.startViewTransition(callback).finished | ||
} else { | ||
return callback() | ||
} | ||
} |
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,31 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Left</title> | ||
<meta name="view-transition" content="same-origin" /> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
|
||
<style> | ||
.square { | ||
display: block; | ||
width: 100px; | ||
height: 100px; | ||
border-radius: 6px; | ||
background-color: blue; | ||
view-transition-name: square; | ||
} | ||
|
||
.square.right { | ||
margin-left: auto; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body style="background-color: orange"> | ||
<h1>Left</h1> | ||
<p><a id="go-right" href="/src/tests/fixtures/transitions/right.html">go right</a></p> | ||
<div class="square"></div> | ||
<p><a id="go-other" href="/src/tests/fixtures/transitions/other.html">go other</a></p> | ||
</body> | ||
</html> |
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,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Other</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
</head> | ||
|
||
<body style="background-color: yellow"> | ||
<h1>Other</h1> | ||
<p><a id="go-left" href="/src/tests/fixtures/transitions/left.html">go left</a></p> | ||
</body> | ||
</html> |
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,30 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Right</title> | ||
<meta name="view-transition" content="same-origin" /> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
|
||
<style> | ||
.square { | ||
display: block; | ||
width: 100px; | ||
height: 100px; | ||
border-radius: 6px; | ||
background-color: blue; | ||
view-transition-name: square; | ||
} | ||
|
||
.square.right { | ||
margin-left: auto; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body style="background-color: red"> | ||
<h1>Right</h1> | ||
<p><a id="go-left" href="/src/tests/fixtures/transitions/left.html">go left</a></p> | ||
<div class="square right"></div> | ||
</body> | ||
</html> |
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,30 @@ | ||
import { test } from "@playwright/test" | ||
import { assert } from "chai" | ||
import { nextBody } from "../helpers/page" | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/src/tests/fixtures/transitions/left.html") | ||
|
||
await page.evaluate(` | ||
document.startViewTransition = (callback) => { | ||
window.startViewTransitionCalled = true | ||
callback() | ||
} | ||
`) | ||
}) | ||
|
||
test("navigating triggers the view transition", async ({ page }) => { | ||
await page.locator("#go-right").click() | ||
await nextBody(page) | ||
|
||
const called = await page.evaluate(`window.startViewTransitionCalled`) | ||
assert.isTrue(called) | ||
}) | ||
|
||
test("navigating does not trigger a view transition when meta tag not present", async ({ page }) => { | ||
await page.locator("#go-other").click() | ||
await nextBody(page) | ||
|
||
const called = await page.evaluate(`window.startViewTransitionCalled`) | ||
assert.isUndefined(called) | ||
}) |