⮌ back | Overview | Home

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 from animation-style.ts are rather building blocks for other components that provide view transition animations and are typically not used by end users. See Zoom and Swing for animations aimed at end users. A notable exception is the adapter() 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:

MyComponent.astro
---
import { setStyles } from 'astro-vtbot/animations/animation-style';
import AnimationStyle from 'astro-vtbot/animations/AnimationStyle.astro';
setStyles("superFade", styles);
---
<AnimationStyle name="superFade">

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.

MyComponent.astro
---
import { ViewTransitions } from 'astro:transitions';
import { setStyles, styleSheet } from 'astro-vtbot/animations/animation-style';
import AnimationStyle from 'astro-vtbot/animations/AnimationStyle.astro';
const myFade = ...
const { scope, styles } = styleSheet({ transitionName: "superFade", animations: myFade });
setStyles("superFade", styles);
---
<html>
<head>
<ViewTransitions />
<AnimationStyle name="superFade">
...
</head>
<body>
...
<div data-astro-transition-scope={scope}>
...
</div>
</body>
</html>

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:

const anim = {
old: {
'animation-name': 'fadeIn',
'animation-duration': '0.2s',
'animation-timing-function': 'linear',
'animation-fill-mode': 'forwards',
},
new: {
'animation-name': 'fadeOut',
'animation-duration': '0.3s',
'animation-timing-function': 'linear',
'animation-fill-mode': 'backwards',
}
};
const myFade = {
forwards: anim,
backwards: anim,
};

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():

ObjectAstro animationvtbot animation
Object for CSS generationTransitionDirectionalAnimations: an object with forwards and backwards propertiesNamedAnimationPairs: an object that can have arbitrarily named properties including forwards and backwards
Animation pairsTransitionAnimation: old and new can be objects or arrays of objectsold and new can only be objects but not arrays
Animation propertiesTransitionAnimation: an object with entries for 6 CSS animation properties. Keys are name, delay, duration, easing, direction & fillmodeAnimationCSS: 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, while NamedAnimationPairs can not. This means that you can only use Astro custom animations with extend() if they do not contain arrays.

MyComponent.astro
---
import { setStyles, styleSheet, extend } from 'astro-vtbot/animations/animation-style';
import AnimationStyle from 'astro-vtbot/animations/AnimationStyle.astro';
import { fade } from 'astro:transitions';
const { scope, styles } = styleSheet({
transitionName: 'fade',
animations: extend(fade({ duration: 300 })),
});
setStyles('fade', styles);
---
<AnimationStyle name="fade">
<div data-astro-transition-scope={scope}>
...
</div>

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.

MyComponent.astro
---
import { adapter } from 'astro-vtbot/animations/animation-style';
const mySpecialAnimation = {...}
---
...
<div transition:animate={adapter(mySpecialAnimation)}>
...

Of course this also works with functions

MyComponent.astro
...
<div transition:animate={adapter(mySpecialAnimationFunction(withParameters))}>
...

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 and backwards styles, you must use the divergent direction names forward and back when setting e.direction.

See this in action in the FishPond Tech Demo