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

[Feature Request] Add or improve history actions #11

Open
pySilver opened this issue Nov 9, 2022 · 5 comments
Open

[Feature Request] Add or improve history actions #11

pySilver opened this issue Nov 9, 2022 · 5 comments

Comments

@pySilver
Copy link

pySilver commented Nov 9, 2022

Thanks for the great lib!

It would be nice to have a shortcut to modify Turbo.navigator.history. I find it an essential action since the Back button is not doing what is expected when the browser history is modified without touching Turbo.navigator.history. As a workaround, the invoke action can be used at the moment.

More on the issue: https://discuss.hotwired.dev/t/back-button-not-working/3012

P.S. I'd really love to help but I'm not very familiar with Typescript yet :/

@marcoroth
Copy link
Owner

Thanks for opening this!

I'm happy to take a look on adding such an action!

@pySilver
Copy link
Author

@marcoroth FYK navigation isn't that simple, unfortunately. See this: hotwired/turbo#792

@marcoroth
Copy link
Owner

I was wondering if expanding the push_state action would be worth thinking about:

export function push_state(this: StreamElement) {
const url = this.getAttribute("url")
const state = this.getAttribute("state")
const title = this.getAttribute("title") || ""
window.history.pushState(state, title, url)
}

@pySilver
Copy link
Author

Sounds OK if that would let one push state solely on window.history or the Turbo.navigator.history (sometime you want or another)

@marcoroth
Copy link
Owner

Yeah, I thought it could be similar to how redirect_to works.

export function redirect_to(this: StreamElement) {
const url = this.getAttribute("url") || "/"
const action = (this.getAttribute("action") || "advance") as Action
const turbo = this.getAttribute("turbo") === "true"
if (turbo) {
if (window.Turbo) window.Turbo.visit(url, { action })
if (window.Turbolinks) window.Turbolinks.visit(url, { action })
if (!window.Turbo && !window.Turbolinks) window.location.href = url
} else {
window.location.href = url
}
}

By default it uses the one on Turbo, but if you don't want that or if Turbo is not available it falls back to window

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

No branches or pull requests

2 participants