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
Add view transitions on navigating history
The view transitions are added when the user navigates back and forth
through the history for configs that have enabled globalViewTransitions.
  • Loading branch information
davidwilde committed Sep 5, 2024
commit 12d75f00e75435baceafafaf4866ac942e39bdc8
40 changes: 27 additions & 13 deletions src/htmx.js
Original file line number Diff line number Diff line change
@@ -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
document.startViewTransition) {
const innerRestoreHistoryFromCache = restoreHistoryFromCache
// wrap the original doRestoreHistory() in a call to startViewTransition()
// @ts-ignore experimental feature atm
restoreHistoryFromCache = document.startViewTransition(
() => innerRestoreHistoryFromCache()
)
}
restoreHistoryFromCache()
} else {
if (htmx.config.refreshOnHistoryMiss) {
// @ts-ignore: optional parameter in reload() function throws error