nativebits-animated-text 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/LICENSE +17 -0
  2. package/README.md +564 -2
  3. package/docs/android-demo.gif +0 -0
  4. package/lib/commonjs/index.js +146 -0
  5. package/lib/commonjs/index.js.map +1 -0
  6. package/lib/commonjs/package.json +1 -0
  7. package/lib/commonjs/text/BlurText.js +146 -0
  8. package/lib/commonjs/text/BlurText.js.map +1 -0
  9. package/lib/commonjs/text/BounceText.js +83 -0
  10. package/lib/commonjs/text/BounceText.js.map +1 -0
  11. package/lib/commonjs/text/CountUpText.js +77 -0
  12. package/lib/commonjs/text/CountUpText.js.map +1 -0
  13. package/lib/commonjs/text/ElasticText.js +85 -0
  14. package/lib/commonjs/text/ElasticText.js.map +1 -0
  15. package/lib/commonjs/text/ExplosionText.js +191 -0
  16. package/lib/commonjs/text/ExplosionText.js.map +1 -0
  17. package/lib/commonjs/text/FlipText.js +101 -0
  18. package/lib/commonjs/text/FlipText.js.map +1 -0
  19. package/lib/commonjs/text/GlitchText.js +329 -0
  20. package/lib/commonjs/text/GlitchText.js.map +1 -0
  21. package/lib/commonjs/text/GradientText.js +73 -0
  22. package/lib/commonjs/text/GradientText.js.map +1 -0
  23. package/lib/commonjs/text/MorphText.js +192 -0
  24. package/lib/commonjs/text/MorphText.js.map +1 -0
  25. package/lib/commonjs/text/NeonText.js +134 -0
  26. package/lib/commonjs/text/NeonText.js.map +1 -0
  27. package/lib/commonjs/text/ParticleText.js +328 -0
  28. package/lib/commonjs/text/ParticleText.js.map +1 -0
  29. package/lib/commonjs/text/RevealText.js +142 -0
  30. package/lib/commonjs/text/RevealText.js.map +1 -0
  31. package/lib/commonjs/text/RotateText.js +99 -0
  32. package/lib/commonjs/text/RotateText.js.map +1 -0
  33. package/lib/commonjs/text/ScrambleText.js +82 -0
  34. package/lib/commonjs/text/ScrambleText.js.map +1 -0
  35. package/lib/commonjs/text/ShinyText.js +87 -0
  36. package/lib/commonjs/text/ShinyText.js.map +1 -0
  37. package/lib/commonjs/text/SplitText.js +81 -0
  38. package/lib/commonjs/text/SplitText.js.map +1 -0
  39. package/lib/commonjs/text/TextWave.js +130 -0
  40. package/lib/commonjs/text/TextWave.js.map +1 -0
  41. package/lib/commonjs/text/ThreeDText.js +145 -0
  42. package/lib/commonjs/text/ThreeDText.js.map +1 -0
  43. package/lib/commonjs/text/TypewriterText.js +119 -0
  44. package/lib/commonjs/text/TypewriterText.js.map +1 -0
  45. package/lib/commonjs/text/WaveText.js +79 -0
  46. package/lib/commonjs/text/WaveText.js.map +1 -0
  47. package/lib/module/index.js +23 -0
  48. package/lib/module/index.js.map +1 -0
  49. package/lib/module/text/BlurText.js +139 -0
  50. package/lib/module/text/BlurText.js.map +1 -0
  51. package/lib/module/text/BounceText.js +77 -0
  52. package/lib/module/text/BounceText.js.map +1 -0
  53. package/lib/module/text/CountUpText.js +70 -0
  54. package/lib/module/text/CountUpText.js.map +1 -0
  55. package/lib/module/text/ElasticText.js +78 -0
  56. package/lib/module/text/ElasticText.js.map +1 -0
  57. package/lib/module/text/ExplosionText.js +184 -0
  58. package/lib/module/text/ExplosionText.js.map +1 -0
  59. package/lib/module/text/FlipText.js +94 -0
  60. package/lib/module/text/FlipText.js.map +1 -0
  61. package/lib/module/text/GlitchText.js +322 -0
  62. package/lib/module/text/GlitchText.js.map +1 -0
  63. package/lib/module/text/GradientText.js +66 -0
  64. package/lib/module/text/GradientText.js.map +1 -0
  65. package/lib/module/text/MorphText.js +185 -0
  66. package/lib/module/text/MorphText.js.map +1 -0
  67. package/lib/module/text/NeonText.js +127 -0
  68. package/lib/module/text/NeonText.js.map +1 -0
  69. package/lib/module/text/ParticleText.js +321 -0
  70. package/lib/module/text/ParticleText.js.map +1 -0
  71. package/lib/module/text/RevealText.js +135 -0
  72. package/lib/module/text/RevealText.js.map +1 -0
  73. package/lib/module/text/RotateText.js +93 -0
  74. package/lib/module/text/RotateText.js.map +1 -0
  75. package/lib/module/text/ScrambleText.js +76 -0
  76. package/lib/module/text/ScrambleText.js.map +1 -0
  77. package/lib/module/text/ShinyText.js +80 -0
  78. package/lib/module/text/ShinyText.js.map +1 -0
  79. package/lib/module/text/SplitText.js +74 -0
  80. package/lib/module/text/SplitText.js.map +1 -0
  81. package/lib/module/text/TextWave.js +123 -0
  82. package/lib/module/text/TextWave.js.map +1 -0
  83. package/lib/module/text/ThreeDText.js +138 -0
  84. package/lib/module/text/ThreeDText.js.map +1 -0
  85. package/lib/module/text/TypewriterText.js +113 -0
  86. package/lib/module/text/TypewriterText.js.map +1 -0
  87. package/lib/module/text/WaveText.js +72 -0
  88. package/lib/module/text/WaveText.js.map +1 -0
  89. package/lib/typescript/index.d.ts +41 -0
  90. package/lib/typescript/index.d.ts.map +1 -0
  91. package/lib/typescript/text/BlurText.d.ts +16 -0
  92. package/lib/typescript/text/BlurText.d.ts.map +1 -0
  93. package/lib/typescript/text/BounceText.d.ts +15 -0
  94. package/lib/typescript/text/BounceText.d.ts.map +1 -0
  95. package/lib/typescript/text/CountUpText.d.ts +20 -0
  96. package/lib/typescript/text/CountUpText.d.ts.map +1 -0
  97. package/lib/typescript/text/ElasticText.d.ts +17 -0
  98. package/lib/typescript/text/ElasticText.d.ts.map +1 -0
  99. package/lib/typescript/text/ExplosionText.d.ts +23 -0
  100. package/lib/typescript/text/ExplosionText.d.ts.map +1 -0
  101. package/lib/typescript/text/FlipText.d.ts +17 -0
  102. package/lib/typescript/text/FlipText.d.ts.map +1 -0
  103. package/lib/typescript/text/GlitchText.d.ts +19 -0
  104. package/lib/typescript/text/GlitchText.d.ts.map +1 -0
  105. package/lib/typescript/text/GradientText.d.ts +12 -0
  106. package/lib/typescript/text/GradientText.d.ts.map +1 -0
  107. package/lib/typescript/text/MorphText.d.ts +18 -0
  108. package/lib/typescript/text/MorphText.d.ts.map +1 -0
  109. package/lib/typescript/text/NeonText.d.ts +16 -0
  110. package/lib/typescript/text/NeonText.d.ts.map +1 -0
  111. package/lib/typescript/text/ParticleText.d.ts +23 -0
  112. package/lib/typescript/text/ParticleText.d.ts.map +1 -0
  113. package/lib/typescript/text/RevealText.d.ts +19 -0
  114. package/lib/typescript/text/RevealText.d.ts.map +1 -0
  115. package/lib/typescript/text/RotateText.d.ts +17 -0
  116. package/lib/typescript/text/RotateText.d.ts.map +1 -0
  117. package/lib/typescript/text/ScrambleText.d.ts +14 -0
  118. package/lib/typescript/text/ScrambleText.d.ts.map +1 -0
  119. package/lib/typescript/text/ShinyText.d.ts +15 -0
  120. package/lib/typescript/text/ShinyText.d.ts.map +1 -0
  121. package/lib/typescript/text/SplitText.d.ts +28 -0
  122. package/lib/typescript/text/SplitText.d.ts.map +1 -0
  123. package/lib/typescript/text/TextWave.d.ts +15 -0
  124. package/lib/typescript/text/TextWave.d.ts.map +1 -0
  125. package/lib/typescript/text/ThreeDText.d.ts +24 -0
  126. package/lib/typescript/text/ThreeDText.d.ts.map +1 -0
  127. package/lib/typescript/text/TypewriterText.d.ts +21 -0
  128. package/lib/typescript/text/TypewriterText.d.ts.map +1 -0
  129. package/lib/typescript/text/WaveText.d.ts +14 -0
  130. package/lib/typescript/text/WaveText.d.ts.map +1 -0
  131. package/package.json +48 -4
  132. package/src/index.ts +45 -0
  133. package/src/text/BlurText.tsx +243 -0
  134. package/src/text/BounceText.tsx +158 -0
  135. package/src/text/CountUpText.tsx +153 -0
  136. package/src/text/ElasticText.tsx +143 -0
  137. package/src/text/ExplosionText.tsx +242 -0
  138. package/src/text/FlipText.tsx +161 -0
  139. package/src/text/GlitchText.tsx +525 -0
  140. package/src/text/GradientText.tsx +118 -0
  141. package/src/text/MorphText.tsx +305 -0
  142. package/src/text/NeonText.tsx +205 -0
  143. package/src/text/ParticleText.tsx +489 -0
  144. package/src/text/RevealText.tsx +255 -0
  145. package/src/text/RotateText.tsx +179 -0
  146. package/src/text/ScrambleText.tsx +137 -0
  147. package/src/text/ShinyText.tsx +163 -0
  148. package/src/text/SplitText.tsx +122 -0
  149. package/src/text/TextWave.tsx +223 -0
  150. package/src/text/ThreeDText.tsx +268 -0
  151. package/src/text/TypewriterText.tsx +189 -0
  152. package/src/text/WaveText.tsx +132 -0
package/src/index.ts ADDED
@@ -0,0 +1,45 @@
1
+ export { SplitText } from './text/SplitText';
2
+ export { WaveText } from './text/WaveText';
3
+ export { BlurText } from './text/BlurText';
4
+ export { GradientText } from './text/GradientText';
5
+ export { ShinyText } from './text/ShinyText';
6
+ export { TypewriterText } from './text/TypewriterText';
7
+ export { BounceText } from './text/BounceText';
8
+ export { RotateText } from './text/RotateText';
9
+ export { ScrambleText } from './text/ScrambleText';
10
+ export { FlipText } from './text/FlipText';
11
+ export { TextWave } from './text/TextWave';
12
+ export { ThreeDText } from './text/ThreeDText';
13
+ export { NeonText } from './text/NeonText';
14
+ export { RevealText } from './text/RevealText';
15
+ export { ElasticText } from './text/ElasticText';
16
+ export { CountUpText } from './text/CountUpText';
17
+ export { MorphText } from './text/MorphText';
18
+ export { GlitchText } from './text/GlitchText';
19
+ export { ParticleText } from './text/ParticleText';
20
+ export { ExplosionText } from './text/ExplosionText';
21
+
22
+
23
+ export type { SplitTextProps } from './text/SplitText';
24
+ export type { WaveTextProps } from './text/WaveText';
25
+ export type { BlurTextProps } from './text/BlurText';
26
+ export type { GradientTextProps } from './text/GradientText';
27
+ export type { ShinyTextProps } from './text/ShinyText';
28
+ export type { TypewriterTextProps } from './text/TypewriterText';
29
+ export type { BounceTextProps } from './text/BounceText';
30
+ export type { RotateTextProps } from './text/RotateText';
31
+ export type { ScrambleTextProps } from './text/ScrambleText';
32
+ export type { FlipTextProps } from './text/FlipText';
33
+ export type { TextWaveProps } from './text/TextWave';
34
+ export type { ThreeDTextProps } from './text/ThreeDText';
35
+ export type { NeonTextProps } from './text/NeonText';
36
+ export type { RevealTextProps } from './text/RevealText';
37
+ export type { ElasticTextProps } from './text/ElasticText';
38
+ export type {
39
+ CountUpTextProps,
40
+ CountUpEasing,
41
+ } from './text/CountUpText';
42
+ export type { MorphTextProps } from './text/MorphText';
43
+ export type { GlitchTextProps } from './text/GlitchText';
44
+ export type { ParticleTextProps } from './text/ParticleText';
45
+ export type { ExplosionTextProps } from './text/ExplosionText';
@@ -0,0 +1,243 @@
1
+
2
+ import React, { useEffect, useMemo } from 'react';
3
+ import {
4
+ View,
5
+ type TextStyle,
6
+ type ViewStyle,
7
+ } from 'react-native';
8
+
9
+ import {
10
+ BlurMask,
11
+ Canvas,
12
+ Group,
13
+ LinearGradient,
14
+ matchFont,
15
+ Text as SkiaText,
16
+ vec,
17
+ } from '@shopify/react-native-skia';
18
+
19
+
20
+
21
+ import {
22
+ cancelAnimation,
23
+ Easing,
24
+ useDerivedValue,
25
+ useSharedValue,
26
+ withDelay,
27
+ withTiming,
28
+ } from 'react-native-reanimated';
29
+
30
+ export interface BlurTextProps {
31
+ text: string;
32
+ duration?: number;
33
+ stagger?: number;
34
+ delay?: number;
35
+ fromY?: number;
36
+ blur?: number;
37
+ style?: TextStyle;
38
+ containerStyle?: ViewStyle;
39
+ blurColor?: string;
40
+ blurGradient?: string[];
41
+ }
42
+
43
+ interface AnimatedCharacterProps {
44
+ character: string;
45
+ x: number;
46
+ y: number;
47
+ font: ReturnType<typeof matchFont>;
48
+ color: string;
49
+ duration: number;
50
+ delay: number;
51
+ fromY: number;
52
+ blur: number;
53
+ blurColor: string;
54
+ blurGradient?: string[];
55
+ gradientWidth: number;
56
+ }
57
+
58
+ interface AnimatedCharacterProps {
59
+ character: string;
60
+ x: number;
61
+ y: number;
62
+ font: ReturnType<typeof matchFont>;
63
+ color: string;
64
+ blurColor: string;
65
+ blurGradient?: string[];
66
+ gradientWidth: number;
67
+ duration: number;
68
+ delay: number;
69
+ fromY: number;
70
+ blur: number;
71
+ }
72
+
73
+ const AnimatedCharacter = ({
74
+ character,
75
+ x,
76
+ y,
77
+ font,
78
+ color,
79
+ blurColor,
80
+ blurGradient,
81
+ gradientWidth,
82
+ duration,
83
+ delay,
84
+ fromY,
85
+ blur,
86
+ }: AnimatedCharacterProps) => {
87
+ const progress = useSharedValue(0);
88
+
89
+ useEffect(() => {
90
+ progress.value = withDelay(
91
+ delay,
92
+ withTiming(1, {
93
+ duration,
94
+ easing: Easing.out(Easing.cubic),
95
+ }),
96
+ );
97
+
98
+ return () => cancelAnimation(progress);
99
+ }, [delay, duration, progress]);
100
+
101
+ const animatedBlur = useDerivedValue(() =>
102
+ Math.max(0.001, blur * (1 - progress.value)),
103
+ );
104
+
105
+ const animatedY = useDerivedValue(
106
+ () => y + fromY * (1 - progress.value),
107
+ );
108
+
109
+ const blurOpacity = useDerivedValue(
110
+ () => 1 - progress.value,
111
+ );
112
+
113
+ return (
114
+ <>
115
+ {/* Colored blurred layer */}
116
+ <Group opacity={blurOpacity}>
117
+ <SkiaText
118
+ x={x}
119
+ y={animatedY}
120
+ text={character}
121
+ font={font}
122
+ color={blurColor}
123
+ >
124
+ <BlurMask blur={animatedBlur} style="normal" />
125
+
126
+ {blurGradient && blurGradient.length >= 2 && (
127
+ <LinearGradient
128
+ start={vec(0, 0)}
129
+ end={vec(gradientWidth, 0)}
130
+ colors={blurGradient}
131
+ />
132
+ )}
133
+ </SkiaText>
134
+ </Group>
135
+
136
+ {/* Sharp text layer */}
137
+ <Group opacity={progress}>
138
+ <SkiaText
139
+ x={x}
140
+ y={animatedY}
141
+ text={character}
142
+ font={font}
143
+ color={color}
144
+ />
145
+ </Group>
146
+ </>
147
+ );
148
+ };
149
+
150
+ export const BlurText = ({
151
+ text,
152
+ duration = 600,
153
+ stagger = 50,
154
+ delay = 0,
155
+ fromY = 20,
156
+ blur = 10,
157
+ style,
158
+ containerStyle,
159
+ blurColor,
160
+ blurGradient,
161
+ }: BlurTextProps) => {
162
+ const fontSize = Number(style?.fontSize ?? 28);
163
+ const fontFamily = style?.fontFamily ?? 'Arial';
164
+ const fontWeight =
165
+ style?.fontWeight === 'bold' ? 'bold' : 'normal';
166
+ const fontStyle =
167
+ style?.fontStyle === 'italic' ? 'italic' : 'normal';
168
+ const color = String(style?.color ?? '#111827');
169
+ const effectiveBlurColor = blurColor ?? color;
170
+ const font = useMemo(
171
+ () =>
172
+ matchFont({
173
+ fontFamily,
174
+ fontSize,
175
+ fontWeight,
176
+ fontStyle,
177
+ }),
178
+ [fontFamily, fontSize, fontWeight, fontStyle],
179
+ );
180
+
181
+ const characters = Array.from(text);
182
+
183
+ const { positions, textWidth } = useMemo(() => {
184
+ const glyphIds = font.getGlyphIDs(text, characters.length);
185
+ const glyphWidths = font.getGlyphWidths(glyphIds);
186
+
187
+ let currentX = 0;
188
+
189
+ const positions = characters.map((character, index) => {
190
+ const position = {
191
+ character,
192
+ x: currentX,
193
+ };
194
+
195
+ currentX += glyphWidths[index] ?? 0;
196
+
197
+ return position;
198
+ });
199
+
200
+ return {
201
+ positions,
202
+ textWidth: currentX,
203
+ };
204
+ }, [text, font]);
205
+
206
+ const metrics = font.getMetrics();
207
+ const canvasHeight =
208
+ metrics.descent - metrics.ascent + 2 * blur + Math.abs(fromY);
209
+ const baseline = -metrics.ascent + blur;
210
+
211
+ const canvasWidth = Math.max(1, textWidth);
212
+
213
+ return (
214
+ <View style={containerStyle}>
215
+ <Canvas
216
+ style={{
217
+ width: canvasWidth + blur * 2,
218
+ height: canvasHeight,
219
+ }}
220
+ >
221
+ {positions.map(({ character, x }, index) => (
222
+ character === ' ' ? null : (
223
+ <AnimatedCharacter
224
+ key={`${index}-${character}`}
225
+ character={character}
226
+ x={x + blur}
227
+ y={baseline}
228
+ font={font}
229
+ color={color}
230
+ blurColor={effectiveBlurColor}
231
+ blurGradient={blurGradient}
232
+ gradientWidth={textWidth}
233
+ duration={duration}
234
+ delay={delay + index * stagger}
235
+ fromY={fromY}
236
+ blur={blur}
237
+ />
238
+ )
239
+ ))}
240
+ </Canvas>
241
+ </View>
242
+ );
243
+ };
@@ -0,0 +1,158 @@
1
+
2
+ import React, { useEffect } from 'react';
3
+ import {
4
+ View,
5
+ Text,
6
+ type TextStyle,
7
+ type ViewStyle,
8
+ } from 'react-native';
9
+
10
+ import Animated, {
11
+ cancelAnimation,
12
+ Easing,
13
+ useAnimatedStyle,
14
+ useSharedValue,
15
+ withDelay,
16
+ withRepeat,
17
+ withSequence,
18
+ withSpring,
19
+ withTiming,
20
+ } from 'react-native-reanimated';
21
+
22
+ export interface BounceTextProps {
23
+ text: string;
24
+ duration?: number;
25
+ stagger?: number;
26
+ delay?: number;
27
+ amplitude?: number;
28
+ loop?: boolean;
29
+ style?: TextStyle;
30
+ characterStyle?: TextStyle;
31
+ containerStyle?: ViewStyle;
32
+ }
33
+
34
+ interface AnimatedCharacterProps {
35
+ character: string;
36
+ index: number;
37
+ duration: number;
38
+ stagger: number;
39
+ delay: number;
40
+ amplitude: number;
41
+ loop: boolean;
42
+ style?: TextStyle;
43
+ characterStyle?: TextStyle;
44
+ }
45
+
46
+ function AnimatedCharacter({
47
+ character,
48
+ index,
49
+ duration,
50
+ stagger,
51
+ delay,
52
+ amplitude,
53
+ loop,
54
+ style,
55
+ characterStyle,
56
+ }: AnimatedCharacterProps) {
57
+ const translateY = useSharedValue(0);
58
+
59
+ useEffect(() => {
60
+ cancelAnimation(translateY);
61
+ translateY.value = 0;
62
+
63
+ const bounce = withSequence(
64
+ withSpring(-amplitude, {
65
+ damping: 7,
66
+ stiffness: 180,
67
+ mass: 0.5,
68
+ }),
69
+ withSpring(0, {
70
+ damping: 7,
71
+ stiffness: 180,
72
+ mass: 0.5,
73
+ })
74
+ );
75
+
76
+ translateY.value = withDelay(
77
+ delay + index * stagger,
78
+ loop
79
+ ? withRepeat(
80
+ withSequence(
81
+ bounce,
82
+ withTiming(0, { duration })
83
+ ),
84
+ -1,
85
+ false
86
+ )
87
+ : bounce
88
+ );
89
+
90
+ return () => cancelAnimation(translateY);
91
+ }, [
92
+ index,
93
+ duration,
94
+ stagger,
95
+ delay,
96
+ amplitude,
97
+ loop,
98
+ translateY,
99
+ ]);
100
+
101
+ const animatedStyle = useAnimatedStyle(() => ({
102
+ transform: [{ translateY: translateY.value }],
103
+ }));
104
+
105
+ return (
106
+ <Animated.Text
107
+ style={[
108
+ style,
109
+ characterStyle,
110
+ animatedStyle,
111
+ ]}
112
+ >
113
+ {character === ' ' ? '\u00A0' : character}
114
+ </Animated.Text>
115
+ );
116
+ }
117
+
118
+ export function BounceText({
119
+ text,
120
+ duration = 500,
121
+ stagger = 80,
122
+ delay = 0,
123
+ amplitude = 15,
124
+ loop = false,
125
+ style,
126
+ characterStyle,
127
+ containerStyle,
128
+ }: BounceTextProps) {
129
+ const characters = Array.from(text);
130
+
131
+ return (
132
+ <View
133
+ style={[
134
+ {
135
+ flexDirection: 'row',
136
+ flexWrap: 'wrap',
137
+ alignItems: 'center',
138
+ },
139
+ containerStyle,
140
+ ]}
141
+ >
142
+ {characters.map((character, index) => (
143
+ <AnimatedCharacter
144
+ key={`${index}-${character}`}
145
+ character={character}
146
+ index={index}
147
+ duration={duration}
148
+ stagger={stagger}
149
+ delay={delay}
150
+ amplitude={amplitude}
151
+ loop={loop}
152
+ style={style}
153
+ characterStyle={characterStyle}
154
+ />
155
+ ))}
156
+ </View>
157
+ );
158
+ }
@@ -0,0 +1,153 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import {
3
+ View,
4
+ type TextStyle,
5
+ type ViewStyle,
6
+ } from 'react-native';
7
+
8
+ import Animated, {
9
+ cancelAnimation,
10
+ Easing,
11
+ useSharedValue,
12
+ withDelay,
13
+ withRepeat,
14
+ withSequence,
15
+ withTiming,
16
+ useAnimatedReaction,
17
+ runOnJS,
18
+ } from 'react-native-reanimated';
19
+
20
+
21
+ export type CountUpEasing =
22
+ | 'linear'
23
+ | 'easeIn'
24
+ | 'easeOut'
25
+ | 'easeInOut';
26
+
27
+ export interface CountUpTextProps {
28
+ from?: number;
29
+ to: number;
30
+ duration?: number;
31
+ delay?: number;
32
+ decimals?: number;
33
+ prefix?: string;
34
+ suffix?: string;
35
+ easing?: CountUpEasing;
36
+ loop?: boolean;
37
+ loopDelay?: number;
38
+ style?: TextStyle;
39
+ containerStyle?: ViewStyle;
40
+ }
41
+
42
+
43
+
44
+ export function CountUpText({
45
+ from = 0,
46
+ to,
47
+ duration = 1500,
48
+ delay = 0,
49
+ decimals = 0,
50
+ prefix = '',
51
+ suffix = '',
52
+ easing = 'easeOut',
53
+ loop = false,
54
+ loopDelay = 1000,
55
+ style,
56
+ containerStyle,
57
+ }: CountUpTextProps) {
58
+ const progress = useSharedValue(from);
59
+
60
+ useEffect(() => {
61
+ cancelAnimation(progress);
62
+ progress.value = from;
63
+
64
+ const easingFunction = {
65
+ linear: Easing.linear,
66
+ easeIn: Easing.in(Easing.quad),
67
+ easeOut: Easing.out(Easing.quad),
68
+ easeInOut: Easing.inOut(Easing.quad),
69
+ }[easing];
70
+
71
+ const countAnimation = withTiming(to, {
72
+ duration,
73
+ easing: easingFunction,
74
+ });
75
+
76
+ if (loop) {
77
+ progress.value = withDelay(
78
+ delay,
79
+ withRepeat(
80
+ withSequence(
81
+ countAnimation,
82
+ withDelay(
83
+ loopDelay,
84
+ withTiming(from, { duration: 1 }),
85
+ ),
86
+ ),
87
+ -1,
88
+ false,
89
+ ),
90
+ );
91
+ } else {
92
+ progress.value = withDelay(delay, countAnimation);
93
+ }
94
+
95
+ return () => {
96
+ cancelAnimation(progress);
97
+ };
98
+ }, [
99
+ from,
100
+ to,
101
+ duration,
102
+ delay,
103
+ easing,
104
+ loop,
105
+ loopDelay,
106
+ progress,
107
+ ]);
108
+
109
+ const [displayValue, setDisplayValue] = useState(
110
+ `${prefix}${from.toFixed(decimals)}${suffix}`,
111
+ );
112
+
113
+ useAnimatedReaction(
114
+ () => {
115
+ const factor = Math.pow(10, decimals);
116
+ const roundedValue =
117
+ Math.round(progress.value * factor) / factor;
118
+
119
+ return `${prefix}${roundedValue.toFixed(decimals)}${suffix}`;
120
+ },
121
+ (current, previous) => {
122
+ if (current !== previous) {
123
+ runOnJS(setDisplayValue)(current);
124
+ }
125
+ },
126
+ [decimals, prefix, suffix],
127
+ );
128
+
129
+ return (
130
+ <View
131
+ style={[
132
+ {
133
+ alignItems: 'flex-start',
134
+ justifyContent: 'center',
135
+ },
136
+ containerStyle,
137
+ ]}
138
+ >
139
+ <Animated.Text
140
+ style={[
141
+ {
142
+ includeFontPadding: false,
143
+ },
144
+ style,
145
+ ]}
146
+ >
147
+ {displayValue}
148
+ </Animated.Text>
149
+ </View>
150
+ );
151
+ }
152
+
153
+ export default CountUpText;