Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support View Transition API for navigations #935

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/core/drive/page_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ export class PageSnapshot extends Snapshot<HTMLBodyElement> {
}

get isPreviewable() {
return this.cacheControlValue != "no-preview"
return this.cacheControlValue != "no-preview" && !this.prefersViewTransitions
}

get isCacheable() {
return this.cacheControlValue != "no-cache"
return this.cacheControlValue != "no-cache" && !this.prefersViewTransitions
}

get isVisitable() {
return this.getSetting("visit-control") != "reload"
}

get prefersViewTransitions() {
return this.headSnapshot.getMetaValue("view-transition") === "same-origin"
}

// Private

getSetting(name: string) {
Expand Down
6 changes: 5 additions & 1 deletion src/core/drive/page_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ErrorRenderer } from "./error_renderer"
import { PageRenderer } from "./page_renderer"
import { PageSnapshot } from "./page_snapshot"
import { SnapshotCache } from "./snapshot_cache"
import { withViewTransition } from "./view_transitions"
import { Visit } from "./visit"

export type PageViewRenderOptions = ViewRenderOptions<HTMLBodyElement>
Expand All @@ -20,13 +21,16 @@ export class PageView extends View<HTMLBodyElement, PageSnapshot, PageViewRender
forceReloaded = false

renderPage(snapshot: PageSnapshot, isPreview = false, willRender = true, visit?: Visit) {
const shouldTransition = this.snapshot.prefersViewTransitions && snapshot.prefersViewTransitions
const renderer = new PageRenderer(this.snapshot, snapshot, PageRenderer.renderElement, isPreview, willRender)

if (!renderer.shouldRender) {
this.forceReloaded = true
} else {
visit?.changeHistory()
}
return this.render(renderer)

return withViewTransition(shouldTransition, () => this.render(renderer))
}

renderError(snapshot: PageSnapshot, visit?: Visit) {
Expand Down
17 changes: 17 additions & 0 deletions src/core/drive/view_transitions.ts
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
afcapel marked this conversation as resolved.
Show resolved Hide resolved
} else {
return callback()
}
}
31 changes: 31 additions & 0 deletions src/tests/fixtures/transitions/left.html
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>
13 changes: 13 additions & 0 deletions src/tests/fixtures/transitions/other.html
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>
30 changes: 30 additions & 0 deletions src/tests/fixtures/transitions/right.html
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>
30 changes: 30 additions & 0 deletions src/tests/functional/drive_view_transition_tests.ts
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)
})