This requires some customization. Fortunately, Astro 3 has accounted for customizing animations in a few ways. The first is by importing their built-in animations and passing in any customization options. However, at the time of this writing, duration is the only property available for setting.
The second way is by allowing you to define your own animations for use within the transition:animate directive. This would be necessary for cases where you want to specify a custom delay, easing, fill mode, or custom CSS animations, and involves a few things:
Defining both old and new content animation settings for both the forwards and backwards navigation.
// components/Metadata/Value.astro
const { index } = Astro.props
const slideAnim = {
old: {
name: 'slideDown',
duration: '0.3s',
easing: 'ease-out',
fillMode: 'backwards',
},
new: {
name: 'slideUp',
duration: '0.3s',
delay: `${index * 0.1 + 0.1}s`,
easing: 'ease-out',
fillMode: 'backwards',
},
}
const verticalSlide = {
forwards: animSlideUp,
backwards: animSlideUp,
}
<!-- components/Metadata/Value.astro -->
<li transition:animate={verticalSlide}>
...
</li>
Defining the custom CSS animations in a global style tag.
@keyframes slideUp {
from {
transform: translateY(20%);
opacity: 0;
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes slideDown {
from {
transform: translateY(0);
opacity: 1;
}
to {
opacity: 0;
transform: translateY(20%);
}
}
Browser Support #
As of March ‘23, View Transitions are supported in Chromium-based browsers (Chrome, Edge, and Opera), with both Safari and Firefox signaling support for the API in the near future. In the meantime, Astro’s implementation provides cross browser support via a built-in polyfill; however, it’s important to note that Firefox and Safari receive slightly different transitions which are not a 1:1 match, but still perfectly acceptable. Astro refers to this as the fallback animation, which can also be controlled with a fallback property.
Accessibility #
Using View Transitions does come with some accessibility challenges; however, the Astro team has taken measures to ensure that accessibility features are built-in. They’ve done this in two ways: by including CSS media queries using prefer-reduced-motion to disable all view transitions animations, and by including a route announcer (as of 3.2.0) which automatically announces new pages for assistive technology. No configuration is needed to enable these features. Read more about them in Astro's documentation.
Future Improvements #
View Transitions in Astro will undoubtedly receive some good updates in upcoming releases, and we have our own small list of hopeful improvements we’d like to see:
- The ability to control the z-index of a transitioning element. In our work so far, we’ve seen some odd z-index things where elements sometimes appear like they are moving underneath other elements, when they should be moving over top of them.
- The ability to set different animations for transitioning into and out of view. For example, fade in, and slide out.
- Exposing additional animation properties for the built-in fade and slide animations, for example,
delay, easing, direction, and fillMode. Currently, duration is the only property that you can set.
Summary #
The potential for View Transitions on the web is promising! It has the potential to level the navigation transition playing field between MPAs and mobile apps and SPAs. Astro being the first framework to include support for View Transitions (with others currently working on it), has given the feature a huge boost. Astro’s batteries-included approach of wrapping the browser primitives and providing good defaults, with accessibility built-in, will prove to be a big win for developers looking to implement View Transitions. We’re certainly hopeful for and excited to see how View Transitions will begin to be used!
Resources #
If you’re curious and would like to see some other Astro & View Transitions resources, here are the best ones we’ve seen:
- Spotify App Clone
- Astro Movies
- Music Library
- Playlist
- Astro View Transitions: Fact vs Fiction – Fred K. Schott (ViteConf 23)