-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedTextCounter.tsx
143 lines (130 loc) · 3.31 KB
/
AnimatedTextCounter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, {useEffect, useState, useMemo} from 'react';
import {
View,
Text,
StyleSheet,
Animated,
TextStyle,
ViewStyle,
} from 'react-native';
interface TextCounterProps {
from: number;
to: number;
textStyle?: TextStyle;
containerStyle?: ViewStyle;
}
interface DigitProps {
index: number;
animatedValues: Animated.Value[];
textStyle?: TextStyle;
containerStyle?: ViewStyle;
}
const calculateInitialPositions = (
fromFormatted: string,
toFormatted: string,
): number[] =>
toFormatted.split('').map((digit, index) => {
const fromDigit = parseInt(fromFormatted[index] || '0', 10);
const toDigit = parseInt(digit, 10);
if (!isNaN(toDigit)) {
const difference = toDigit - fromDigit;
return (
-(
fromDigit +
((difference > 0 && difference <= 5) || difference < -5 ? 0 : 10)
) * 30
);
}
return 0;
});
const useAnimatedValues = (
from: number,
to: number,
): {animatedValues: Animated.Value[]; toFormatted: string} => {
const fromFormatted = useMemo(
() => new Intl.NumberFormat('en-IN').format(from),
[from],
);
const toFormatted = useMemo(
() => new Intl.NumberFormat('en-IN').format(to),
[to],
);
const [animatedValues, setAnimatedValues] = useState<Animated.Value[]>([]);
useEffect(() => {
const initialValues = calculateInitialPositions(fromFormatted, toFormatted);
const newAnimatedValues = initialValues.map(
initialValue => new Animated.Value(initialValue),
);
setAnimatedValues(newAnimatedValues);
newAnimatedValues.forEach((animValue, index) => {
if (!isNaN(parseInt(toFormatted[index]))) {
Animated.timing(animValue, {
toValue: -parseInt(toFormatted[index], 10) * 30,
duration: 1000,
useNativeDriver: true,
}).start();
}
});
}, [fromFormatted, toFormatted]);
return {animatedValues, toFormatted};
};
const AnimatedDigit: React.FC<DigitProps> = ({index, animatedValues, textStyle}) => (
<View key={index} style={styles.digitContainer}>
<Animated.View
style={{
height: 300,
transform: [{translateY: animatedValues[index] || 0}],
}}>
{Array.from({length: 10}).map((_, num) => (
<Text key={num} style={[styles.digit, textStyle]}>
{num}
</Text>
))}
</Animated.View>
</View>
);
const AnimatedTextCounter: React.FC<TextCounterProps> = ({
from,
to,
containerStyle,
textStyle = {
fontSize: 24,
lineHeight: 30,
},
}) => {
const {animatedValues, toFormatted} = useAnimatedValues(from, to);
return (
<View style={[styles.container, containerStyle]}>
{toFormatted.split('').map((digit, index) =>
isNaN(parseInt(digit)) ? (
<Text key={index} style={[textStyle, styles.digit]}>
{digit}
</Text>
) : (
<AnimatedDigit
index={index}
animatedValues={animatedValues}
textStyle={textStyle}
/>
),
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
digitContainer: {
height: 30,
overflow: 'hidden',
width: 18,
},
digit: {
height: 30,
textAlign: 'center',
}
});
export default AnimatedTextCounter;