Skip to content

Support Directional View Transitions #1375

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/core/drive/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class Visit {
this.shouldCacheSnapshot = shouldCacheSnapshot
this.acceptsStreamResponse = acceptsStreamResponse
this.direction = direction || Direction[action]
this.viewTransitionDirection = this.calculateViewTransitionDirection()
}

get adapter() {
Expand Down Expand Up @@ -430,6 +431,16 @@ export class Visit {
delete this.frame
}
}

calculateViewTransitionDirection() {
const originalVisitDirection = this.restorationData.direction === "back" ? "back" : "forward"

if (this.direction === "forward") {
return originalVisitDirection
} else {
return originalVisitDirection === "forward" ? "back" : "forward"
}
}
}

function isSuccessful(statusCode) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export function registerAdapter(adapter) {
* @param options Options to apply
* @param options.action Type of history navigation to apply ("restore",
* "replace" or "advance")
* @param options.direction Logical direction of the navigation, applies to
* view transitions ("forward", "back")
* @param options.historyChanged Specifies whether the browser history has
* already been changed for this visit or not
* @param options.referrer Specifies the referrer of this visit such that
Expand Down
9 changes: 7 additions & 2 deletions src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export class Session {
visit(location, options = {}) {
const frameElement = options.frame ? document.getElementById(options.frame) : null

if (["forward", "back"].includes(options.direction)) {
this.history.updateRestorationData({ direction: options.direction })
}

if (frameElement instanceof FrameElement) {
const action = options.action || getVisitAction(frameElement)

Expand Down Expand Up @@ -250,8 +254,9 @@ export class Session {
followedLinkToLocation(link, location) {
const action = this.getActionForLink(link)
const acceptsStreamResponse = link.hasAttribute("data-turbo-stream")
const direction = link.getAttribute("data-turbo-visit-direction")

this.visit(location.href, { action, acceptsStreamResponse })
this.visit(location.href, { action, acceptsStreamResponse, direction })
}

// Navigator delegate
Expand All @@ -270,7 +275,7 @@ export class Session {
visitStarted(visit) {
if (!visit.acceptsStreamResponse) {
markAsBusy(document.documentElement)
this.view.markVisitDirection(visit.direction)
this.view.markVisitDirection(visit.viewTransitionDirection)
}
extendURLWithDeprecatedProperties(visit.location)
if (!visit.silent) {
Expand Down