import type { ComponentProps, CSSProperties } from "react"; export type RollingNumberProps = Omit, "children"> & { format: Intl.NumberFormat; value: number; }; type Part = { char: string; digit: boolean; key: string }; const DIGITS = [..."0123456789"]; function numberParts(value: number, format: Intl.NumberFormat): Part[] { let chars = format.formatToParts(value).flatMap(part => [...part.value].map(char => ({ char, digit: (part.type === "integer" || part.type === "fraction") && /\d/.test(char), })) ); // Keys count from the right so the ones column keeps its identity, and keeps rolling, when the // number gains or loses a leading digit. return chars.map((part, index) => ({ ...part, key: String(chars.length - index) })); } export function RollingNumber({ className, format, value, ...props }: RollingNumberProps) { return ( {/* Screen readers hear the formatted value once rather than ten digits per column. */} {format.format(value)} {numberParts(value, format).map(part => part.digit ? ( ) : ( ) )} ); }