Sub topic 10
Naming elements per navigation (list to detail)
This chapter assumes you have enabled @view-transition { navigation: auto } on both pages and know how view-transition-name morphs a single element. It also assumes you can read a basic event listener.
Why static names break on a list page
A list page shows many thumbnails. The detail page shows one hero image. If every thumbnail had view-transition-name: post-image in CSS, the names would collide. The browser requires each name to be unique within a page. If two elements share a name, it skips the transition.
The fix is to name only the one thumbnail the user clicked, and only during that navigation. You decide which one at runtime, using the pageswap and pagereveal events.
The markup and the detail page
Each card carries a data-post attribute holding the post's slug. The detail page has only one hero image, so it can name it in plain CSS.
<article data-post="css-nesting">
<a href="/posts/css-nesting/">
<img src="/img/css-nesting-thumb.jpg" alt="CSS nesting diagram">
</a>
</article>.post-hero img {
view-transition-name: post-image;
}Naming the outgoing thumbnail
pageswap fires on the old page just before it is snapshotted. event.activation.entry.url is the destination. Parse it, find the matching card, and set the name inline.
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';
});The event.viewTransition check matters. It is null when no transition will run, for example when the navigation is cross-origin. CSS.escape keeps an odd slug from breaking the selector.
Naming the incoming thumbnail on the way back
When the user returns to the list, the detail page is the old page and its hero is already named. The list page must name the right thumbnail before its first render. pagereveal fires at that moment. navigation.activation.from describes the page the user came from.
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 last line removes the name once the animation ends. Otherwise the thumbnail keeps the name, and a later navigation could collide with it.
Common errors
- Two elements share a name. The transition is skipped and the page simply swaps. Name only one element per page.
- The name is set too late. Setting it after
pageswaporpagerevealhas returned means the snapshot has already been taken. Do it synchronously inside the handler, or at least before the first render. - The selector finds nothing. Check that the slug in the URL matches
data-postexactly, including case. - Unsupported browsers. Both events and
event.viewTransitionare absent there. The listeners never fire, and navigation works normally.
Exercise
On the list page, why must the pagereveal handler set view-transition-name back to none after event.viewTransition.finished resolves?
Show answer
If the name stayed on the thumbnail, the next navigation could find two elements sharing post-image. For example, pageswap may name a different thumbnail. Duplicate names make the browser skip the transition. Resetting to none leaves the page with no named elements until the next handler runs.