🚀 Ship faster with premium components 🚀
Search documentation v1.3.0

Animated Stat

Animate metrics between two values

Source Report Issue
Command docs
How to add this block to your project
npm run add AnimatedStat

Use the AnimatedStat component to visually animate metrics between two values. They are useful for indicating the number of users, views, or other metrics in your application. They can be used for:

Users
Unlock code
How to use the AnimatedStat component in Astro
<AnimatedStat
to={100}
subText="Users"
/>

To use the component set a to prop for the ending value. You can optionally also set a subText prop to display additional secondary text under the stat. The component is only animated when it becomes visible in the viewport. The animation is based on CSS to keep the component simple and performant.

Custom Values

By default, the number is animated from 0. If you want to change this value, you can also specify a from prop:

Unlock code
How to specify a custom starting value
<AnimatedStat
from={-1000}
to={100}
/>

Duration and Delay

The animation timing can be changed using the duration and delay props:

Duration set to 10s
Delay set to 10s
Unlock code
How to use a custom duration
<AnimatedStat
title="Duration set to 10s"
to={100}
duration={10}
/>
Unlock code
How to delay the animation
<AnimatedStat
title="Delay set to 10s"
to={100}
delay={10}
/>

Pre and Postfixes

In case you want to animate formatted values, for example pricing or percentage, you can use the prefix and postfix props (you can also set both at the same time):

Monthly Sales
Uptime
Unlock code
How to use prefixes
<AnimatedStat
to={200}
subText="Monthly Sales"
prefix="$"
/>
Unlock code
How to use postfixes
<AnimatedStat
to={99}
subText="Uptime"
postfix="%"
/>

Card Styles

As the component is based on the Card component, you can use card-specific props, such as title or secondary:

Animated stat with title and secondary card
Users
Unlock code
How to use card props
<AnimatedStat
to={100}
subText="Users"
title="Animated stat with title and secondary card"
secondary={true}
/>

Color

In case you need the component to match your brand, you can specify the color of the stat through the color prop:

Colored stat
Colored stat
Colored stat
Unlock code
How to specify a custom color
<AnimatedStat
to={100}
subText="Colored stat"
color="var(--w-color-info)" {/* You can use any valid CSS value */}
/>

Configuration with CSS

If you would like to keep component configuration in one place and make them global, you can also set many of the props through the following CSS variables:

Unlock code
How to configure the component with CSS
html body {
--w-animated-stat-color: var(--w-color-primary);
--w-animated-stat-duration: 4s;
--w-animated-stat-delay: 0s;
--w-animated-stat-prefix: '';
--w-animated-stat-postfix: '';
}

API

Unlock code
Component API reference
type AnimatedStatProps = {
from?: number
to: number
duration?: number
delay?: number
prefix?: string
postfix?: string
subText?: string
color?: string
} & CardProps
PropDefaultPurpose
from 0 Sets the from value for the stat.
to 0 Sets the to value for the stat.
duration 4s Sets the duration of the animation in seconds.
delay 0s Sets the delay of the animation in seconds.
prefix - Sets a prefix in front of the stat.
postfix - Sets a postfix after the stat.
subText - Sets a secondary text under the stat.
color var(--w-color-primary) Sets the color for the stat.