Compact Number
Compact notation counter where digits roll on springs while K, M, and B magnitude units swap smoothly via directional transitions.
1.2K
Customize
Installation
npx shadcn@latest add @kinetic/compact-numberUsage
Pass a numeric value to render compact notation (1.2K to 12.4K to 1.2M). CompactNumber wraps RollingDigits so digit wheels roll smoothly on springs while the compact magnitude unit swaps independently via directionalSwap whenever the value crosses a scale threshold.
import { CompactNumber } from "@/components/kinetic/ui/compact-number";
interface AudienceStatProps {
readonly subscribers: number;
}
export function AudienceStat({ subscribers }: AudienceStatProps) {
return (
<CompactNumber
as="p"
value={subscribers}
className="text-3xl font-semibold tracking-tight"
/>
);
}Formatting styles and locales
CompactNumber formats values with Intl.NumberFormat (notation: "compact") and formatToParts. Switch style between "decimal" (default), "currency" (paired with currency, default "USD"), and "percent". Control unit verbosity with compactDisplay ("short" or "long"), decimal precision with maximumFractionDigits (default 1) and minimumFractionDigits, and locale units via locale (for example "en-US" for K/M/B or "vi-VN" for N/Tr/T).
import { CompactNumber } from "@/components/kinetic/ui/compact-number";
interface LocalizedCompactMetricsProps {
readonly revenue: number;
readonly reach: number;
readonly growth: number;
}
export function LocalizedCompactMetrics({
revenue,
reach,
growth,
}: LocalizedCompactMetricsProps) {
return (
<div className="space-y-2">
<CompactNumber
value={revenue}
style="currency"
currency="USD"
locale="en-US"
maximumFractionDigits={1}
/>
<CompactNumber
value={reach}
locale="vi-VN"
compactDisplay="short"
maximumFractionDigits={1}
/>
<CompactNumber
value={growth}
style="percent"
locale="en-US"
direction="shortest"
/>
</div>
);
}Entrance and imperative control
Combine from with trigger="mount", trigger="inView", or trigger="manual" to roll in from a starting number. Attach a CompactNumberHandle ref to call play() or reset() programmatically.
import { useRef } from "react";
import {
CompactNumber,
type CompactNumberHandle,
} from "@/components/kinetic/ui/compact-number";
interface DownloadsCounterProps {
readonly total: number;
}
export function DownloadsCounter({ total }: DownloadsCounterProps) {
const ref = useRef<CompactNumberHandle>(null);
return (
<div className="space-y-3">
<CompactNumber
ref={ref}
from={0}
value={total}
trigger="manual"
className="text-3xl font-semibold"
/>
<div className="flex gap-2">
<button type="button" onClick={() => ref.current?.play()}>
Play
</button>
<button type="button" onClick={() => ref.current?.reset()}>
Reset
</button>
</div>
</div>
);
}Motion preferences
blurPx (default 2.4, clamped at 8px max), speed, and unit swap duration scale with the global Kinetic tempo. Under reduced motion, both the digit wheels and compact unit skip animation, snap immediately to the formatted value, and call onComplete. Visual wheels and unit spans stay aria-hidden="true" while an .sr-only span exposes the complete formatted string (aria-label overrides it); enable announceChanges to announce updates politely via role="status".
API Reference
CompactNumber
Intl.NumberFormat compact notation wrapper over RollingDigits with dedicated directional unit swaps.
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | - | Numeric value to format in compact notation. |
locale | Intl.LocalesArgument | "en-US" | BCP 47 locale such as en-US or vi-VN. |
style | "decimal" | "currency" | "percent" | "decimal" | Intl number formatting style. |
currency | string | "USD" | ISO currency code used when style is currency. |
compactDisplay | "short" | "long" | "short" | Short (1.2M) or long (1.2 million) unit display. |
maximumFractionDigits | number | 1 | Maximum fractional digits shown. |
direction | "auto" | "up" | "down" | "shortest" | "auto" | Rolling direction strategy for digit wheels. |
animateWidth | boolean | true | Smoothly resizes width when digits or units change. |