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

Directional View Transitions #1374

Open
jbarrieault opened this issue Mar 4, 2025 · 2 comments
Open

Directional View Transitions #1374

jbarrieault opened this issue Mar 4, 2025 · 2 comments

Comments

@jbarrieault
Copy link

jbarrieault commented Mar 4, 2025

What if Turbo allowed visits to specify their View Transition direction?

Turbo's view transition support is awesome—with just a little CSS, you can have rich, directional page transitions that slide pages in and out, in a visually directional manner.

However, view transition direction is currently coupled directly to a visit's action. Iinitial visits have a view transition direction of "forward". The view transition of restoration visits are "forward" when going up the history stack, and "back" when going down.

Allowing Turbo visits to specify their view transition direction independently of their action would allow multi-page applications to create a links to pages that—while advancing history—performs a visual backwards view transition. And, that transition direction is reversed when traveling back to that page.

@jbarrieault jbarrieault changed the title Control view transition direction? Directional View Transitions Mar 5, 2025
@oscarmrom
Copy link

oscarmrom commented Apr 1, 2025

My coworker helped me reach what could be a workaround or how it may be intended to be used,

data: {turbo_action: "restore"}

on a button or link_to will allow for this to "control" the direction being "back".

Per the docs: https://turbo.hotwired.dev/handbook/drive#restoration-visits

@4lllex
Copy link

4lllex commented Apr 2, 2025

There is a way to do it with turbo events:

// app/javascript/application.js

document.addEventListener("turbo:click", (event) => {
  if (event.target.dataset.turboVisitDirection === "back") {
    window.visitDirection = "back"
  }
});
document.addEventListener("turbo:visit", (event) => {
  // this way you don't override browser back button navigation
  if (window.visitDirection) {
    document.documentElement.dataset.turboVisitDirection = window.visitDirection
  }
  window.visitDirection = undefined;
});
<%= link_to "Cancel", post, data: {turbo_visit_direction: "back"} %>

https://stackoverflow.com/a/78786565/207090

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants