Skip to content

Count-up

A figure that counts from zero to its value when it arrives; the server renders the final figure.

<p class="cap-specimen-row">Recoverable estimate: <b><span class="cap-count-up" id="cu-money" data-cap="count-up" data-value="12480" data-currency="USD">$12,480</span></b></p>
<p class="cap-specimen-row">Payments retried: <b><span class="cap-count-up" id="cu-count" data-cap="count-up" data-value="312">312</span></b></p>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/count-up.css";
import { enhance } from "capsomer/behavior/count-up";

enhance();

Usage

<span class="cap-count-up" data-cap="count-up" data-value="12480" data-currency="USD">$12,480</span>

API reference

Functions

FunctionSignatureDoes
useCountUp(key: string, run?: boolean, duration?: number): numberThe count-up's progress (0 to 1) for a figure: 1 on the server and with reduced motion, and on the client a count from 0 to 1 whenever key changes (key the figures, not an array, so a parent re-render does not restart it). run false shows the figure still.
easeOutCubic(t: number): number
countUp(onProgress: (progress: number) => void, duration?: number): () => voidCalls onProgress with 0..1 (eased) on each frame until done. Returns a function that stops it.
formatter(el: HTMLElement): (n: number) => stringThe figure's words: an Intl.NumberFormat from data-currency and data-decimals.
enhance(root?: ParentNode): () => voidA page with no React: every [data-cap="count-up"] with data-value counts up once it is seen.

Parts and data attributes

ClassIs
.cap-count-upthe root
AttributeValues
data-capcount-up

Accessibility

None.

  • Only text changes, and only for 600ms; a screen reader reaching it reads the final figure (no live region, so the counting is not announced).
  • Reduced motion is honored.