Data
Animated number
Number that animates when its value changes, as an odometer, a split-flap board, a blur, a stagger, a decode or a count.
Example
cr-number shows value, formatted in the page language. It is an inline element that takes the size, weight and colour of the text around it: style it like text, and write the unit next to it. All digits have the same width, so the number does not jitter while it changes.
<CrNumber value={steps} style={{ fontSize: 56, fontWeight: 800 }} /> stepsNothing moves on first display: only changes to value animate. animation picks the effect.
Steps today
'use client';
import { useState } from 'react';
import { CrBox, CrNumber, CrProgress, CrSegmented } from '@cristal-ui/react';
const animations = [
['roll', 'Roll'],
['flap', 'Flap'],
['blur', 'Blur'],
['stagger', 'Stagger'],
['decode', 'Decode'],
['count', 'Count'],
] as const;
const days = [
['mon', 'Mon', 6482],
['tue', 'Tue', 8215],
['wed', 'Wed', 4307],
['thu', 'Thu', 10024],
] as const;
const GOAL = 10000;
export default function NumberSteps() {
const [animation, setAnimation] = useState(0);
const [day, setDay] = useState(0);
const steps = days[day][2];
return (
<CrBox display="flex" direction="column" gap="xl" align="center">
<CrSegmented label="Animation" value={String(animation)} onCrChange={(event) => setAnimation(Number(event.detail))}>
{animations.map(([value, label], index) => (
<option key={value} value={index}>
{label}
</option>
))}
</CrSegmented>
<div style={{ width: '100%', maxWidth: 360, padding: 24, borderRadius: 'var(--cr-radius-l)', background: 'var(--cr-color-surface)', boxShadow: 'var(--cr-shadow-m)' }}>
<CrSegmented label="Day" value={String(day)} onCrChange={(event) => setDay(Number(event.detail))}>
{days.map(([value, label], index) => (
<option key={value} value={index}>
{label}
</option>
))}
</CrSegmented>
<p style={{ margin: '24px 0 0', color: 'var(--cr-color-text-muted)' }}>Steps today</p>
<p style={{ margin: '0 0 16px' }}>
<CrNumber value={steps} animation={animations[animation][0]} style={{ fontSize: 56, fontWeight: 800, lineHeight: 1.1 }} /> steps
</p>
<CrProgress label="Goal of 10,000 steps" value={Math.min(100, (steps / GOAL) * 100)} />
</div>
</CrBox>
);
}animation | Effect |
|---|---|
roll | odometer: each changed digit rolls vertically through the digits in between (default) |
flap | split-flap board: each digit is a tile whose top half flips down in 3D onto the new digit |
blur | the number slides away, blurred by the speed, up when it increases and down when it decreases; the new one comes in from the other side |
stagger | the changed digits are replaced one by one, left to right, and land with a bounce |
decode | the digits scramble through random digits, then lock in left to right |
count | the number counts to the new value, tinted green when it goes up and red when it goes down, with an arrow before it that fades away |
If the value changes again during an animation, the number heads for the latest value.
Format and decimals
The number is formatted with Intl.NumberFormat in the component language (lang on the page, a section or the tag): 6,482.50 in English, 6 482,50 in French. decimals sets the number of decimals (0 by default); the number is rounded. Separators and the minus sign stay still, only the digits animate; when the number gains a digit (999 → 1,000), the new place appears on the left without shifting the others.
Total: $
Total :
Gap: $
'use client';
import { useState } from 'react';
import { CrBox, CrButton, CrNumber } from '@cristal-ui/react';
export default function NumberFormat() {
const [price, setPrice] = useState(1249.9);
return (
<CrBox display="flex" direction="column" gap="lg" align="start">
<p style={{ margin: 0, fontSize: 24 }}>
Total: $<CrNumber value={price} decimals={2} />
</p>
<p lang="fr" style={{ margin: 0, fontSize: 24 }}>
Total : <CrNumber value={price} decimals={2} /> €
</p>
<p style={{ margin: 0, fontSize: 24 }}>
Gap: $<CrNumber value={price - 1300} decimals={1} animation="count" />
</p>
<CrButton size="s" variant="secondary" onClick={() => setPrice(price === 1249.9 ? 1302.45 : 1249.9)}>
Change the price
</CrButton>
</CrBox>
);
}With @cristal-ui/react/ssr, the server HTML already contains the formatted number. The component is rendered there on its own, without seeing the page language: give it lang (<CrNumber lang="fr" … />) so the server already uses the French separators.
If the user asked their system to reduce motion, the number switches to the new value at once, whatever the animation. Screen readers always read the final value, never the digits in between.
Accessibility
- The animated digits are hidden from screen readers (
aria-hidden). An invisible text holds the final value, updated as soon as it changes: screen readers read it and copy and paste copies it. - The change is not announced on its own. If it must be, put the number with its unit in a
role="status"(oraria-live="polite") region. - No digit fades in from transparent: the digits slide, flip or blur, and keep the text contrast. The
countanddecodetints keep a contrast of at least 4.5:1 on--cr-color-surface. - The
countcolour only backs up the direction of the change; the arrow is decorative. In forced colours, the digits take the system text colour.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
animation | "blur" | "count" | "decode" | "flap" | "roll" | "stagger" | 'roll' | Animation quand value change : roll (compteur kilométrique, par défaut), flap (tableau d'aéroport), blur (le nombre glisse, flouté), stagger (les chiffres changent un à un, avec rebond), decode (les chiffres défilent au hasard puis se figent), count (le nombre compte jusqu'à la nouvelle valeur). |
decimals | number | 0 | Nombre de décimales affichées (le nombre est arrondi). |
value | number | 0 | Le nombre affiché. |
CSS parts
To style it from outside: cr-number::part(name) { … }
| Part | Description |
|---|
Sparkline
Small chart without axes that shows the shape of a series of numbers. Tag cr-sparkline, React component CrSparkline.
Alert and toast
Message for information, success, warning or error, placed in the page or as a toast in a corner. Tags cr-alert and cr-toaster, React components CrAlert and CrToaster.