Guest User

Untitled

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