Primitives
SlidingNumber
WAAPI-powered animated number component with slot-machine-style digit transitions and optional motion blur.
SlidingNumber
A high-performance animated number display using the Web Animations API (WAAPI). Digits transition with a slot-machine-style animation and optional motion blur effect.
Import
import { SlidingNumber } from '@mks2508/mks-ui/react';Features
- Slot-machine animation - Digits scroll vertically like a slot machine
- Motion blur - Optional SVG filter blur during animation
- Staggered digits - Each digit animates with configurable delay
- Trend support - Animate up, down, or auto-detect shortest path
- Format support - Currency, percentage, decimal via
Intl.NumberFormat - Spring physics - Natural-feeling animations with configurable overshoot
Basic Usage
Basic Number
.tsx
<SlidingNumber value={1234} />With Formatting
Currency Format
.tsx
<SlidingNumber
value={45678.90}
format={{ style: 'currency', currency: 'USD' }}
/>Percentage Format
.tsx
<SlidingNumber
value={0.856}
format={{ style: 'percent', minimumFractionDigits: 1 }}
/>Trend Direction
Control the animation direction:
Trend Directions
.tsx
<SlidingNumber value={100} trend={1} /> {/* Upward */}
<SlidingNumber value={100} trend={-1} /> {/* Downward */}
<SlidingNumber value={100} trend={0} /> {/* Auto */}Motion Blur
Enable blur effect during animation for a more dynamic feel:
<SlidingNumber value={9999} motionBlur={true} />Animation Configuration
Customize spring physics:
<SlidingNumber
value={1234}
duration={700}
stagger={30}
animationConfig={{
overshoot: 0.2,
mass: 1.0,
stiffness: 200,
}}
/>Dynamic Value Changes
The component animates smoothly when the value prop changes:
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<SlidingNumber value={count} fontSize="4rem" />
<button onClick={() => setCount(c => c + 1)}>Increment</button>
<button onClick={() => setCount(c => c - 1)}>Decrement</button>
</div>
);
}API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | — | The numeric value to display |
duration | number | 700 | Animation duration in ms |
fontSize | string | '3rem' | Font size for digits |
fontWeight | string | '700' | Font weight |
color | string | '#000' | Text color |
digitHeight | number | 60 | Height of each digit row in px |
stagger | number | 30 | Delay between digit animations (ms) |
motionBlur | boolean | true | Enable motion blur effect |
format | IFormatOptions | — | Number formatting options |
trend | -1 | 0 | 1 | 0 | Animation direction |
animationConfig | object | — | Spring physics config |
IFormatOptions
| Option | Type | Description |
|---|---|---|
style | 'decimal' | 'currency' | 'percent' | Number format style |
currency | string | Currency code (e.g., 'USD') |
locale | string | Locale string (e.g., 'en-US') |
minimumFractionDigits | number | Minimum decimal places |
maximumFractionDigits | number | Maximum decimal places |
useGrouping | boolean | Use thousand separators |
AnimationConfig
| Option | Type | Default | Description |
|---|---|---|---|
overshoot | number | — | Spring overshoot amount |
mass | number | — | Spring mass |
stiffness | number | — | Spring stiffness |
Related
- CountingNumber - Spring-based counter
- SlidingText - Text animations
- DataCard - Uses animated numbers