Components
AnimatedNumber
An animated number display with smooth transitions for both digits and separators
Features
- Smoothly animates number updates with layout transitions
- Animated comma or dot separators between every 3 digits
- Supports two animation strategies: "translate" and "swap"
- Supports customizable animation configs: "timing" and "spring"
Dependencies
This component requires React Native Reanimated:
npx expo install react-native-reanimated
Usage
This component is self-contained and can be used by simply copying the file.
Steps
-
Copy and paste the AnimatedNumber.tsx file into your project
-
Use it inside your component:
import AnimatedNumber from "./AnimatedNumber" import { TextInput, View } from "react-native" import { useRef, useState } from "react" export default function App() { const [number, setNumber] = useState("") const inputRef = useRef(null) return ( <View style={styles.container}> <AnimatedNumber number={number} textStyle={styles.text} containerStyle={{ width: "100%", justifyContent: "center" }} separatorAnimation="swap" separator="comma" animationConfig={{ type: "spring", damping: 20, stiffness: 120 }} /> <TextInput ref={inputRef} autoFocus style={{ width: "100%", height: 0, backgroundColor: "transparent", position: "absolute", bottom: 0 }} onChangeText={setNumber} keyboardType="numeric" /> </View> ) }
Props
Prop | Type | Default |
---|---|---|
number | string | - |
separator? | "comma" | "dot" | "comma" |
separatorAnimation? | "swap" | "translate" | "swap" |
textStyle? | TextStyle | - |
containerStyle? | ViewStyle | - |
animationConfig? | AnimationConfigs | { type: "timing", duration: 300 } |
prefix? | string | - |
suffix? | string | - |
AnimationConfig
AnimationConfig can be one of two types. The type
field determines which fields are applicable.
TimingOptions
Prop | Type | Default |
---|---|---|
type | "timing" | - |
duration? | number | 300 |
easing? | EasingFunction | - |
SpringOptions
Prop | Type | Default |
---|---|---|
type | "spring" | - |
damping? | number | 20 |
mass? | number | 1 |
stiffness? | number | 100 |
overshootClamp? | boolean | false |
For "timing"
type, duration is exact. For "spring"
type, duration is physics-based.
If you need exact sync with other animations, use "timing"
or calculate an approximate spring duration.
Examples
// timing
animationConfig={{
type: "timing",
duration: 400,
easing: Easing.out(Easing.exp)
}}
// spring
animationConfig={{
type: "spring",
damping: 20,
stiffness: 120,
overshootClamp: false
}}
Made with love by niche.guys
Let the numbers flow beautifully.