-
-
Save cawa0505/dc523da2a227b8f1a01dc14d0a1741b3 to your computer and use it in GitHub Desktop.
htmx-astro-view-transition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
htmx.defineExtension("hx-astro-view-transition", { | |
onEvent: function (name, evt: any) { | |
if (name === "htmx:afterRequest") { | |
const attributes = evt.target.attributes; | |
const viewTransitionTarget = | |
attributes["hx-view-transition"]?.value; | |
if (!viewTransitionTarget) { | |
console.log(evt); | |
return; | |
} | |
const a = document.createElement("a"); | |
a.href = viewTransitionTarget; | |
console.count(viewTransitionTarget); | |
document.body.appendChild(a); | |
a.dispatchEvent( | |
new PointerEvent("mouseenter", { | |
bubbles: true, | |
cancelable: true, | |
}), | |
); | |
a.dispatchEvent( | |
new PointerEvent("click", { | |
bubbles: true, | |
cancelable: true, | |
}), | |
); | |
} | |
}, | |
}); | |
// Runs on view transitions navigation + process the new HTML | |
document.addEventListener("astro:after-swap", () => | |
htmx.process(document), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment