Sub topic 8
Keeping names unique, including in lists
This chapter assumes you know how view-transition-name pairs an old element with a new one, and that you can build a shared-element transition. It covers the rule that keeps those pairings valid and how to apply it to repeated items.
The uniqueness rule
Within one captured state, each view-transition-name value may appear on only one rendered element. The browser checks the old state when it captures it, and checks the new state after your update callback runs. A name that appears once in each state links those two elements into a single animated group.
If a state contains the same name twice, the browser skips the transition. Your update callback still runs, so the DOM changes correctly, but the animation does not play. transition.ready rejects with an InvalidStateError, and Chrome logs a console warning.
Two other points matter:
- Names must be valid CSS identifiers.
item-42works.42anditem 42do not, and the browser ignores the invalid declaration. - Elements with
display: noneare not captured, so their names do not count and they do not animate.
Naming items in a list
A card list is the usual source of duplicates. If every card uses viewTransitionName = 'hero', every state has many hero elements. Derive the name from a stable ID instead.
This example names every item so each one slides to its new position when the list is sorted:
function renderList(items) {
const list = document.querySelector('#list');
list.replaceChildren(...items.map((item) => {
const li = document.createElement('li');
li.textContent = item.title;
li.style.viewTransitionName = `item-${item.id}`;
return li;
}));
}
function sortItems(items) {
const sorted = [...items].sort((a, b) => a.title.localeCompare(b.title));
if (!document.startViewTransition) {
renderList(sorted);
return;
}
document.startViewTransition(() => renderList(sorted));
}Each item keeps the same ID in both states, so the browser pairs the old and new elements and animates the move. Use a database ID or another stable key. An array index is a poor choice, because the index changes when the order changes.
Name only what needs to move
Each named element gets its own snapshot layer. Naming hundreds of items costs memory and time. Two approaches reduce the cost:
- Name only the element involved in the transition, as in the
heroexample from the previous chapter. Set the name just beforestartViewTransition()and clear it afterwards. - For reordering, name only the items that are visible in the viewport.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| No animation, console warning | Two elements share a name in one state | Include a unique ID in the name |
| Name has no effect | Invalid identifier, such as a leading digit | Add a prefix such as `item-` |
| Item jumps instead of moving | The name is missing in the old or the new state | Set the name on both elements |
| Old element still named after the transition | The name was never cleared | Clear it in the callback or in `finished` |
Checking for duplicates
When a transition does not play, log the rejection:
const transition = document.startViewTransition(() => renderList(sorted));
transition.ready.catch((error) => console.warn('Transition skipped:', error.name));An InvalidStateError here points to a duplicate name. Check the element IDs in the DevTools Elements panel.
Exercise
A page renders 20 product cards, and each one has the inline style view-transition-name: product. Sorting the cards with startViewTransition() produces no animation, but the cards do reorder. Why, and how do you fix it?
Show answer
Twenty rendered elements share the name product, so each state contains duplicates. The browser skips the transition, but the update callback still runs, which is why the cards reorder. Give each card a unique, valid name built from a stable ID, for example product-${product.id}.