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

Global view transitions for history restore #2873

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
40 changes: 27 additions & 13 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3205,19 +3205,33 @@ var htmx = (function() {
path = path || location.pathname + location.search
const cached = getCachedHistory(path)
if (cached) {
const fragment = makeFragment(cached.content)
const historyElement = getHistoryElement()
const settleInfo = makeSettleInfo(historyElement)
handleTitle(cached.title)
handlePreservedElements(fragment)
swapInnerHTML(historyElement, fragment, settleInfo)
restorePreservedElements()
settleImmediately(settleInfo.tasks)
getWindow().setTimeout(function() {
window.scrollTo(0, cached.scroll)
}, 0) // next 'tick', so browser has time to render layout
currentPathForHistory = path
triggerEvent(getDocument().body, 'htmx:historyRestore', { path, item: cached })
let restoreHistoryFromCache = function() {
const fragment = makeFragment(cached.content)
const historyElement = getHistoryElement()
const settleInfo = makeSettleInfo(historyElement)
handleTitle(cached.title)
handlePreservedElements(fragment)
swapInnerHTML(historyElement, fragment, settleInfo)
restorePreservedElements()
settleImmediately(settleInfo.tasks)
getWindow().setTimeout(function() {
window.scrollTo(0, cached.scroll)
}, 0) // next 'tick', so browser has time to render layout
currentPathForHistory = path
triggerEvent(getDocument().body, 'htmx:historyRestore', { path, item: cached })
}
const shouldTransition = htmx.config.globalViewTransitions
if (shouldTransition &&
// @ts-ignore experimental feature atm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably trigger the htmx:beforeTransition event to allow users to cancel if they want to, see the other place where we are using the Vue transitions API:

... && triggerEvent(elt, 'htmx:beforeTransition', responseInfo) && ...

document.startViewTransition) {
const innerRestoreHistoryFromCache = restoreHistoryFromCache
// wrap the original doRestoreHistory() in a call to startViewTransition()
// @ts-ignore experimental feature atm
restoreHistoryFromCache = document.startViewTransition(
() => innerRestoreHistoryFromCache()
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no potential delay here I think this be an if/else instead of wrapping the restoreHistoryFromCache function: pass it directly to startViewTransition else invoke it

restoreHistoryFromCache()
} else {
if (htmx.config.refreshOnHistoryMiss) {
// @ts-ignore: optional parameter in reload() function throws error
Expand Down