Sub topic 3
Respecting `prefers-reduced-motion` in View Transitions
Assume you know JavaScript promises, CSS animations, DOM updates, and browser DevTools.
Respect reduced motion
A reduced-motion preference is a user setting that asks websites to limit animation. Disable transition animations while keeping the DOM update:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}This keeps navigation functional and makes the visual change immediate.
Promises and JavaScript animation
startViewTransition() returns a ViewTransition object. Its promises describe different stages:
updateCallbackDoneresolves when the update callback finishes.readyresolves when the new snapshot and transition pseudo-elements are ready.finishedresolves when the animation ends.
Use ready before animating a pseudo-element with the Web Animations API:
const transition = document.startViewTransition(() => updateView());
await transition.ready;
document.documentElement.animate(
{ clipPath: ["inset(0 0 100% 0)", "inset(0)"] },
{
duration: 400,
easing: "ease-in",
pseudoElement: "::view-transition-new(root)"
}
);
await transition.finished;The default CSS animation can interfere with this effect. Disable it first:
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}Skipping, interruption, and shared styles
Call transition.skipTransition() when the transition is no longer useful, such as when the user starts a newer navigation. An interrupted transition may reject finished, so handle cleanup in finally().
view-transition-class lets several named elements share styles without repeating selectors. The feature requires browser support, so keep ordinary selectors as a fallback.
.vt-card {
view-transition-class: card;
}
::view-transition-group(.card) {
animation-duration: 300ms;
}Transition types and debugging
Transition types label a transition so CSS can choose a direction-specific effect:
document.startViewTransition({
update: () => updateView(),
types: ["slide-forward"]
});Inspect the transition pseudo-elements in DevTools. Check for duplicate view-transition-name values, missing names in either state, unexpected stacking, clipping, and animations that remain active under reduced motion.
Same-document versus cross-document
Same-document transitions animate an update inside one page with document.startViewTransition(). Cross-document transitions animate navigation between pages. They use navigation-related CSS and do not give the same update callback or ViewTransition control.
Exercise
Which promise should you await before targeting a transition pseudo-element?
Show answer
Await transition.ready; the pseudo-elements exist after that stage.