Sub topic 8
Support and graceful fallback
This chapter assumes you can write CSS and basic JavaScript event listeners. You should also know that each link click on a multi-page site loads a new document. You have already opted in with @view-transition. Now you will make sure the site still works in browsers that ignore it.
Transitions are an enhancement
Cross-document view transitions are supported in Chromium-based browsers and in recent Safari. Support in other browsers changes over time, so check a current compatibility table such as MDN or caniuse before you ship.
A browser without support does not break the site. It ignores the @view-transition at-rule, and links load the new page with no animation. This is the fallback you want. The page is the same, minus the motion.
Why the CSS opt-in degrades safely
CSS drops rules it does not understand. An unknown at-rule is skipped, and so are unknown pseudo-elements. This means the CSS from earlier chapters is already safe:
@view-transition {
navigation: auto;
}
::view-transition-old(root) {
animation: 200ms ease-in both slide-out;
}
::view-transition-new(root) {
animation: 300ms ease-out both slide-in;
}An unsupported browser skips all three rules. No code is needed to guard them.
One caution applies to custom properties and classes. If your normal styles depend on a transition running, such as an element hidden until an animation reveals it, unsupported browsers will show the wrong result. Keep the default state of every element visible and usable.
Guarding JavaScript
CSS needs no guard, but your JavaScript handlers do. The pageswap and pagereveal events only fire where cross-document transitions exist. The handlers from the previous chapters already begin with a check:
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition) return;
// Only reached when a transition is actually running.
event.viewTransition.types.add('forward');
});event.viewTransition is null when the navigation has no transition. That happens in unsupported browsers, for navigations that are not eligible, and when the user has turned transitions off. Always test it before touching it.
If you call a newer API directly, test for it first. For example, window.navigation?.activation uses optional chaining, so the code does not throw when the Navigation API is missing.
Feature queries with @supports
Sometimes you want styles that apply only where transitions work. Use @supports with the at-rule selector test:
@supports at-rule(@view-transition) {
.site-header {
view-transition-name: site-header;
}
}This keeps names such as site-header out of browsers that cannot use them. Not every browser supports the at-rule() test itself. In a browser that does not, the whole block is skipped, which is also a safe result. Use this for styles that exist only to serve the transition. Do not use it for styles the page needs to look correct.
Combining fallback with reduced motion
Two separate groups get no animation: browsers without support, and users who ask for less motion. Handle both with the same rule, which is that the page must be complete without motion.
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}Testing the fallback
Do not assume the fallback works. Test it.
- Open the site in a browser that lacks support and click through every page.
- In a supporting browser, comment out the
@view-transitionrule and reload. You should see plain navigation. - Turn on reduced motion in your operating system settings and confirm that no animation plays.
Exercise
A page hides its main content with opacity: 0 and relies on a ::view-transition-new(root) animation to fade it in. What goes wrong in a browser without support, and how do you fix it?
Show answer
The browser skips the transition rules, so nothing ever fades the content in and it stays invisible. Fix it by leaving the content visible by default (no opacity: 0 on the normal element). Let the transition animation handle only the pseudo-elements, which exist only when the transition runs.
Exercise
Why does a pageswap handler begin with if (!event.viewTransition) return;?
Show answer
event.viewTransition is null when no transition is running, for example in an ineligible navigation or when transitions are unavailable. Without the check, the code would throw an error when it tried to read properties of null.