RollingNumber

RollingNumber

HkRollingNumber renders a value as one cell per character, and when a digit changes the old digit slides out upward while the new one slides in — a pure CSS keyframe, no counters, no dependencies. It is the readout for figures that move while the user is looking at them: a live token count, a timer, a credit balance. Everything that is not a changed ASCII digit stays a plain character in place, so 1,234.56 keeps its separators and only the digits that actually moved roll.

  • The value is cut into characters, not digits: a change in length rebuilds the cell list and nothing animates, and a changed ,, . or - swaps in place. Only /^[0-9]$/ rolls.
  • A figure that never changes is plain text; a figure that must align digit-for-digit in a column is HkPrice's tabular-nums.
  • The animation is decoration: under prefers-reduced-motion the roll is switched off and the new digit appears at rest, which is the same end state.

Import

tsx
1
import { HkRollingNumber } from "@celestia-island/hikari";
rust
1
2
use hikari_components::*;          // RollingNumber
use hikari_components::prelude::*; // rsx!, VNode

The Vue form is controlled: pass value (number | string, required) and re-render it when the figure changes. The Rust form is the same character-by-character state machine in a still — it renders the value it is handed, so the example below shows a static reading.

Characters and cells

Every character is its own cell, so the component never guesses where the digits are. A money figure keeps its grouping comma and its decimal point as ordinary cells; a clock keeps its colons; a percentage keeps its sign. Digits are the only cells that can roll.

1,234.5699.9%04:59

What rolls

Two rules decide the animation, and both come from the character cut. If the character count changed — 9 to 10, 99 to 100 — the cell list is rebuilt and nothing animates, because there is no position that can be said to hold "the same" digit. If the count stayed, every position whose character changed becomes a rolling slot carrying the old and the new digit, and the untouched positions stay at rest.

The roll then commits: the keyframe is declared forwards, so the resting DOM already shows the new digit where the committed cell would put it, and the next render emits the plain character cell. A 350 ms fallback commits a roll whose animationend never fires — the global animation switch parks the keyframe at 0%, so without it the old digit would stay visible forever.

Animation cannot reach a still snapshot, so the Rust example below renders committed readings; in a browser the same two figures would roll where they differ.

1281291,000

Instance keys

The Rust mirror tracks the previous character of each cell outside the render, keyed by instance_key; without a key every unkeyed instance shares one slot, so two rolling numbers rendering different values can briefly show each other's previous digit while a roll plays. Pass a distinct key per figure when that can happen. The key is mirror-only: Vue's state is per component instance and needs no prop.

8,23116,462

API

Vue

Props

NameTypeDefaultRequired
value<code>[Number, String] as PropType<number | string></code>—✓

Rust

Props — RollingNumber

PropTypeDefaultDescription
valueString""The rendered text (Vue: value, <code>number | string</code>, required there; "" here because Rust props need a Default).
instance_keyOption<String>NoneRegistry slot for the previous-character tracking (mirror-only; see the header's state note).
classString""Host class appended last (Vue attribute fallthrough).

Accessibility

  • The root is a plain <span> with the digits as text, so assistive technology reads the value as one string (1,234.56), not one announcement per character. Nothing in the DOM is focusable and the component emits no events.
  • The roll is decoration only: no state lives in the DOM that the animation exposes, and the committed cell carries the same character the roll ends on.
  • prefers-reduced-motion: reduce switches the keyframe off; the same commit path applies, so the new digit is on screen either way.
  • Give the figure a label in the surrounding text (a caption, a table header) — the component names nothing itself.

Related components

  • HkPrice — the money display atom, with tabular figures and an optional reference conversion.
  • HkShareBar — the proportion bar for an "X of Y" breakdown.
  • Comment — the threaded comment block for the text around a figure.
  • HkProgressBar — a bounded proportion drawn as a bar rather than as digits.
  • HkSpinner — the indeterminate "still working" indicator, when a number has nothing to show yet.