The PageSwapEvent
event object is made available inside handler functions for the pageswap
event.
The pageswap
event is fired when you navigate across documents, when the previous document is about to unload. During a cross-document navigation, the PageSwapEvent
event object allows you to manipulate the related view transition (providing access to the relevant ViewTransition
object) from the document being navigated from, if a view transition was triggered by the navigation. It also provides access to information on the navigation type and current and destination documents.
window.addEventListener("pageswap", async (e) => {
if (e.viewTransition) {
const currentUrl = e.activation.from?.url
? new URL(e.activation.from.url)
: null;
const targetUrl = new URL(e.activation.entry.url);
if (isProfilePage(currentUrl) && isHomePage(targetUrl)) {
document.querySelector(`#detail main h1`).style.viewTransitionName =
"name";
document.querySelector(`#detail main img`).style.viewTransitionName =
"avatar";
await e.viewTransition.finished;
document.querySelector(`#detail main h1`).style.viewTransitionName =
"none";
document.querySelector(`#detail main img`).style.viewTransitionName =
"none";
}
if (isProfilePage(targetUrl)) {
const profile = extractProfileNameFromUrl(targetUrl);
document.querySelector(`#${profile} span`).style.viewTransitionName =
"name";
document.querySelector(`#${profile} img`).style.viewTransitionName =
"avatar";
await e.viewTransition.finished;
document.querySelector(`#${profile} span`).style.viewTransitionName =
"none";
document.querySelector(`#${profile} img`).style.viewTransitionName =
"none";
}
}
});