A number that counts up to its value. It is a server component with no client JavaScript: a registered custom property is animated and every digit is printed with a CSS counter. Two triggers: "mount" counts once by time on first paint, "scroll" ties the count to the scroll of the nearest scrollable container. The final value is in the server HTML, so search engines, screen readers and browsers without the needed CSS get it as is.
Browser Support
Ties the count to scroll (trigger="scroll").
Animates the counter value.
Preview
Scroll the window: the counters finish counting as they come into its visible area.
Our numbers
Scroll the window down. The counters below start from zero and finish as they come fully into view.
Scroll back up and they count down again: the number follows the scroll position, not the clock.
Installation
Usage
import { StatCounter } from "@/components/ui/stat-counter"
export default function Example() {
return (
<div className="text-center">
<StatCounter value={12480} suffix="+" className="text-4xl font-bold" />
{/* or follow the scroll of the nearest scroll container: */}
{/* <StatCounter trigger="scroll" value={12480} /> */}
<p className="text-sm text-muted-foreground">Active users</p>
</div>
)
}Examples
Currency and compact notation
formatOptions goes straight to Intl.NumberFormat, and locale picks separators and symbols. Currency signs, percent and compact suffixes stay as static text around the counting digits. Thousand separators come from the locale in groups of three; formats CSS cannot rebuild (negative numbers, exponents, other grouping schemes) show the final value without a count.
<StatCounter
value={84250}
locale="de-DE"
formatOptions={{ style: "currency", currency: "EUR", maximumFractionDigits: 0 }}
/>
<StatCounter
value={2400000}
formatOptions={{ notation: "compact", maximumFractionDigits: 1 }}
/>Count on mount, with replay
trigger="mount" counts once by time and does not depend on scroll. The button remounts the counter with a new key.
const [run, setRun] = useState(0)
<StatCounter key={run} trigger="mount" value={1284} />
<Button onClick={() => setRun((n) => n + 1)}>Replay</Button>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | - | Final value |
| from | number | 0 | Value the count starts from |
| prefix | ReactNode | - | Content before the number |
| suffix | ReactNode | - | Content after the number |
| decimals | number | 0 | Digits after the decimal point |
| locale | string | "en-US" | Locale for Intl.NumberFormat |
| formatOptions | Intl.NumberFormatOptions | - | Extra Intl.NumberFormat options |
| trigger | "mount" | "scroll" | "mount" | mount: once by time on first paint. scroll: follows the nearest scrollable container |
| duration | number | 2500 | Count duration in ms (mount only, default 2500) |
| scrollRange | string | "entry 0% cover 65%" | animation-range for scroll, default "entry 0% cover 65%" |
| className | string | - | Additional CSS classes |
Features
- No JavaScript: works in Server Components, nothing to hydrate
- SEO friendly: the final value is in the server HTML
- Stable width: tabular-nums and a hidden copy of the final value hold the width
- Reduced motion: shows the final value right away
- Screen readers: hear only the final value, not every frame