✨Animation Style
<AnimationStyle/>
gives the user more direct control over the style sheets for Astro’s view transitions. It is accompanied by a set of functions in animation-style.ts
that generate the CSS for native view transitions as well as Astro’s simulation of view transitions.
Note: While you will probably use the
<AnimationStyle/>
component in your own project. But most functions fromanimation-style.ts
are rather building blocks for other components that provide view transition animations and are typically not used by end users. SeeZoom
andSwing
for animations aimed at end users. A notable exception is theadapter()
function, see description below.
Contents
Why would I use this component?
For the basic animation use case, Move, Swing and Zoom animations can simply be plugged into Astro’s transition:animate={}
property. But for the more elaborated use cases with extended parametrization, the style sheets for animations during view transitions must be placed manually. The <AnimationStyle/>
component gives you full control over where to place style sheets for view transitions on your pages. It generates CSS for native view transition as well as Astro’s simulation of view transitions. The contents of the style sheets are specified using functions from animation-style.ts
.
Usage
Install astro-vtbot in your project running npx astro add astro-vtbot
or npm install astro-vtbot
.
Using <AnimationStyle>
Using this component is a two step process:
- define the CSS in the component script of your Astro component
- place the component in the component template of your Astro component
The <AnimationStyle/>
component should be placed in the <head>
of your page.
The call to setStyle
and the <AnimationStyle/>
component are linked by the common identifier superFade
. The name of the animation is arbitrary, but must be the same in both places. The second parameter to setStyles()
is a string with the CSS for the animation.
Creating CSS and transition scope
How do I get that CSS string for setStyles()
?
You can use the styleSheet
function from animation-style.ts
to generate both, a CSS string for view transition animations and a transition scope id that you can use to reference the style sheet from an HTML element for which you want to define a transition group.
The scope
is an identifier that is used to bind the style sheet to the <div>
. This is the same thing that happens behind the scenes when you use transition:animate
in Astro. It typically has the form astro-xxxxxxxx
where xxxxxxxx
is a random string.
The second parameter to styleSheet()
is an object that describes the CSS that should be used for the view transition. Here is an example for a fade animation:
You might recognize this example from the Astro Docs about View Transitions↗. The main difference between the input of styleSheet()
and the Astro custom animation example is that vtbot
uses kebab-case CSS property keys, while Astro uses a small set of convenient names. See below for details.
Here myFade
defines forwards
and backwards
animations. “Forwards” is what will normally happen when you click a link. “Backwards” is what is used when you hit the browser back button. Each branch defines the out animation for leaving the old
page and the in animation for entering the new
page. The animation for each branch is defined by an object with CSS properties.
Astro’s properties for custom animations are very convenient to use, while the kebab-case properties require more typing. However, supporting the full set of CSS properties is more powerful: for example, you can use transform-origin
with styleSheet()
. Astro’s custom animations do not support this.
NamedAnimationPairs vs. TransitionDirectionalAnimations
Here are the details about the differences between Astro’s TransitionDirectionalAnimations
and the NamedAnimationPairs
used by astro-bot’s styleSheet()
:
Object | Astro animation | vtbot animation |
---|---|---|
Object for CSS generation | TransitionDirectionalAnimations : an object with forwards and backwards properties | NamedAnimationPairs : an object that can have arbitrarily named properties including forwards and backwards |
Animation pairs | TransitionAnimation : old and new can be objects or arrays of objects | old and new can only be objects but not arrays |
Animation properties | TransitionAnimation : an object with entries for 6 CSS animation properties. Keys are name , delay , duration , easing , direction & fillmode | AnimationCSS : an object with any CSS properties. The keys are kebab-case CSS property names. |
Reusing Astro custom animations with extended()
The animation-style.ts
file also provides an extended()
function that can be used to convert Astro’s TransitionDirectionalAnimations
to NamedAnimationPairs
. This allows you to reuse Astro’s custom animations with styleSheet()
.
Note: An important limitation is that Astro’s
TransitionDirectionalAnimations
can contain arrays, whileNamedAnimationPairs
can not. This means that you can only use Astro custom animations withextend()
if they do not contain arrays.
Using custom direction names with transition:animate={}
One use case supported by animation-style.ts
is independent of the <AnimationStyle/>
component. It is about supporting custom animations with transition:animate={}
. Astro recognizes two direction names for view transitions: forwards
and backwards
. If you want to use other or additional names, you will get typing errors. It is possible to cast objects of type Record<string, TransitionAnimationPair>
to Astro’s TransitionDirectionalAnimations
. The more elegant and type safe way is to use the adapter()
function from animation-style.ts
.
Use your custom animation object or function with transition:animate
by sending it through the adapter.
Of course this also works with functions
Now you can use other direction names than forwards
and backwards
in your animation. Make sure to select proper direction values by setting e.direction
to one of your custom direction names in the event handler for astro:before-preparation
.
Incompatibility Notice For the
forwards
andbackwards
styles, you must use the divergent direction namesforward
andback
when settinge.direction
.
See this in action in the FishPond Tech Demo