Guest User

Untitled

a guest
Jul 24th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import React from 'react';
  2. import { Text, View } from 'react-native';
  3. import Animated, { EasingNode } from 'react-native-reanimated';
  4.  
  5. const NUMBERS = Array(10).fill().map((_, i) => i);
  6.  
  7. const usePrevious = (value) => {
  8. const ref = React.useRef();
  9. React.useEffect(() => {
  10. ref.current = value;
  11. });
  12.  
  13. if (typeof ref.current === 'undefined') {
  14. return 0;
  15. }
  16.  
  17. return ref.current;
  18. };
  19.  
  20. const AnimatedNumber = ({
  21. animateToNumber,
  22. fontStyle,
  23. animationDuration,
  24. includeComma,
  25. easing,
  26. }) => {
  27. const prevNumber = usePrevious(animateToNumber);
  28. const animateToNumberString = String(Math.abs(animateToNumber));
  29. const prevNumberString = String(Math.abs(prevNumber));
  30.  
  31. const animateToNumbersArr = Array.from(animateToNumberString, Number);
  32. const prevNumberersArr = Array.from(prevNumberString, Number);
  33.  
  34. if (includeComma) {
  35. const reducedArray = new Array(
  36. Math.ceil(animateToNumberString.length / 3)
  37. ).fill(0);
  38.  
  39. const startReducedArray = new Array(
  40. Math.ceil(prevNumberString.length / 3)
  41. ).fill(0);
  42.  
  43. reducedArray.map((__, index) => {
  44. if (index === 0) {
  45. return;
  46. }
  47.  
  48. animateToNumbersArr.splice(
  49. animateToNumberString.length - index * 3,
  50. 0,
  51. ','
  52. );
  53. });
  54.  
  55. startReducedArray.map((__, index) => {
  56. if (index === 0) {
  57. return;
  58. }
  59.  
  60. prevNumberersArr.splice(prevNumberString.length - index * 3, 0, ',');
  61. });
  62. }
  63.  
  64. const [numberHeight, setNumberHeight] = React.useState(0);
  65. const animations = animateToNumbersArr.map((__, index) => {
  66. if (typeof prevNumberersArr[index] !== 'number') {
  67. return new Animated.Value(0);
  68. }
  69.  
  70. const animationHeight = -1 * (numberHeight * prevNumberersArr[index]);
  71. return new Animated.Value(animationHeight);
  72. });
  73.  
  74. const setButtonLayout = (e) => {
  75. setNumberHeight(e.nativeEvent.layout.height);
  76. };
  77.  
  78. React.useEffect(() => {
  79. animations.map((animation, index) => {
  80. if (typeof animateToNumbersArr[index] !== 'number') {
  81. return;
  82. }
  83.  
  84. Animated.timing(animation, {
  85. toValue: -1 * (numberHeight * animateToNumbersArr[index]),
  86. duration: animationDuration || 1400,
  87. useNativeDriver: true,
  88. easing: easing || EasingNode.elastic(1.2),
  89. }).start();
  90. });
  91. }, [animateToNumber, numberHeight]);
  92.  
  93. const getTranslateY = (index) => {
  94. return animations[index];
  95. };
  96.  
  97. return (
  98. <>
  99. {numberHeight !== 0 && (
  100. <View style={{ flexDirection: 'row' }}>
  101. {animateToNumber < 0 && (
  102. <Text style={[fontStyle, { height: numberHeight }]}>{'-'}</Text>
  103. )}
  104. {animateToNumbersArr.map((n, index) => {
  105. if (typeof n === 'string') {
  106. return (
  107. <Text key={index} style={[fontStyle, { height: numberHeight }]}>
  108. {n}
  109. </Text>
  110. );
  111. }
  112.  
  113. return (
  114. <View
  115. key={index}
  116. style={{ height: numberHeight, overflow: 'hidden' }}
  117. >
  118. <Animated.View
  119. style={[
  120. {
  121. transform: [
  122. {
  123. translateY: getTranslateY(index),
  124. },
  125. ],
  126. },
  127. ]}
  128. >
  129. {NUMBERS.map((number, i) => (
  130. <View style={{ flexDirection: 'row' }} key={i}>
  131. <Text style={[fontStyle, { height: numberHeight }]}>
  132. {number}
  133. </Text>
  134. </View>
  135. ))}
  136. </Animated.View>
  137. </View>
  138. );
  139. })}
  140. </View>
  141. )}
  142. <Text
  143. style={[fontStyle, { position: 'absolute', top: -999999 }]}
  144. onLayout={setButtonLayout}
  145. >
  146. {0}
  147. </Text>
  148. </>
  149. );
  150. };
  151.  
  152. export default AnimatedNumber;
Add Comment
Please, Sign In to add comment