Sub topic 9
Hooking in with pageswap and pagereveal
This chapter assumes you can write CSS animations with @keyframes, you know that each link click on a multi-page site loads a new document, and you can attach JavaScript event listeners. You should also have opted in with @view-transition { navigation: auto; } on both pages.
CSS alone cannot see where a navigation is going. It styles the page it is on. Two events give JavaScript a view of both ends of the navigation: pageswap on the page being left and pagereveal on the page being opened.
The two events
| Event | Fires on | When | Useful properties |
|---|---|---|---|
| `pageswap` | The old page | Just before the old page is snapshotted | `event.viewTransition`, `event.activation` |
| `pagereveal` | The new page | Just before the first frame is rendered | `event.viewTransition`, `window.navigation.activation` |
Both events expose event.viewTransition. It is null when no transition will run. That happens when the navigation is not eligible, or when the browser skips the transition. Always check it first.
Where the navigation came from and went to
On the old page, event.activation describes the destination. Its entry.url is the URL being navigated to. On the new page, navigation.activation.from describes the page you came from. The ?. operators in the spine code guard against browsers where window.navigation is missing.
Because these are real URLs, you can decide what to animate by matching paths. A list page at / links to posts at /posts/some-slug/, so the destination path tells you which card was clicked.
Naming the clicked element
A shared element needs the same view-transition-name on both pages, and only one element per page may carry that name at a time. If many thumbnails shared one static name, the transition would be skipped. So set the name only on the thumbnail that matters.
// List page: tag the clicked card's thumbnail as the outgoing shared element.
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition || !event.activation) return;
const to = new URL(event.activation.entry.url);
const match = to.pathname.match(/^\/posts\/([^/]+)\/?$/);
if (!match) return;
const thumb = document.querySelector(`[data-post="${CSS.escape(match[1])}"] img`);
if (thumb) thumb.style.viewTransitionName = 'post-image';
});
// List page again: when coming back from a post, tag the matching thumbnail.
window.addEventListener('pagereveal', async (event) => {
if (!event.viewTransition) return;
const from = window.navigation?.activation?.from;
if (!from) return;
const match = new URL(from.url).pathname.match(/^\/posts\/([^/]+)\/?$/);
if (!match) return;
const thumb = document.querySelector(`[data-post="${CSS.escape(match[1])}"] img`);
if (!thumb) return;
thumb.style.viewTransitionName = 'post-image';
await event.viewTransition.finished;
thumb.style.viewTransitionName = 'none';
});The post page needs no script. Its stylesheet gives the hero image a fixed name with .post-hero img { view-transition-name: post-image; }.
The pageswap handler runs before the old snapshot, so the name is captured. The pagereveal handler runs on the way back, when the list page is new. It sets the name before the first render, then removes it after finished resolves so the name never ends up on two elements.
Setting transition types
The same events can add a transition type, a label that CSS can react to.
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition || !event.activation) return;
const to = new URL(event.activation.entry.url);
event.viewTransition.types.add(
to.pathname.startsWith('/posts/') ? 'forward' : 'back'
);
});A later chapter covers :active-view-transition-type(), which uses these labels.
Common errors
- No
viewTransitioncheck. The handler throws when the browser skips the transition. - Unescaped values in selectors. Use
CSS.escape()on any text taken from a URL. - Waiting too long. Keep these handlers fast and synchronous. The navigation waits for them.
- Leaving names behind. A stale name on a second element makes the transition fail.
Exercise
A list page has twenty thumbnails. Why is it wrong to give every thumbnail view-transition-name: post-image in the stylesheet, and how do pageswap and pagereveal fix it?
Show answer
A view-transition-name must be unique among the elements on a page. With twenty elements sharing it, the transition is skipped. In pageswap, the script reads the destination URL and sets the name only on the clicked post's thumbnail before the old snapshot is taken. In pagereveal, it reads the source URL, sets the name on the matching thumbnail before the first render, and clears it after viewTransition.finished.