v160 · css · view transitions

Shadow card morph

A <profile-card> custom element names its avatar and title inside its own shadow stylesheet; the caption beside it is named by the page. Expand the card with a document-level view transition and the browser reports which names it captured. Pass shadowRoots: [card.shadowRoot] and the card’s internals morph with the page; leave it out and only the page’s names take part.

Probing the shadowRoots dictionary member…

Expand the card

Light-DOM caption: view-transition-name: card-caption declared in the page stylesheet.

Declared in the page (document tree)

  • card-caption

Declared in the card’s shadow tree

  • card-avatar
  • card-name
Press “Expand card” to run a real view transition and list what it captured.

the code

class ProfileCard extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" }).innerHTML = `
      <style>
        .avatar { view-transition-name: card-avatar; }
        .name   { view-transition-name: card-name; }
      </style> …`;
  }
}

const t = document.startViewTransition({
  update: () => card.toggleAttribute("expanded"),
  shadowRoots: [card.shadowRoot],   // without this, card-avatar/card-name are ignored
});
await t.ready;
// Which names were captured? Read the UA animations on the pseudo-elements:
document.getAnimations()
  .map(a => a.effect.pseudoElement)          // "::view-transition-group(card-avatar)" …

Browsers without the feature ignore the unknown dictionary member, so the call is safe to ship everywhere: the shadow names simply do not participate and the card cross-fades as part of the root snapshot.

see also