-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.tsx
133 lines (120 loc) · 3.5 KB
/
index.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
import React from "react";
import { motion, Transition, useAnimation, useInView } from "motion/react";
const NUMBERS = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9,
];
interface Props {
className?: string;
animateToNumber: number;
fontStyle?: React.CSSProperties;
transitions?: (index: number) => Transition;
includeComma?: boolean;
locale?: string;
}
const AnimatedNumber = ({
className,
animateToNumber,
fontStyle,
transitions,
includeComma,
locale,
}: Props) => {
const ref = React.useRef(null);
const isInView = useInView(ref, { once: true });
const controls = useAnimation();
const animateTonumberString = includeComma
? Math.abs(animateToNumber).toLocaleString(locale || "en-US")
: String(Math.abs(animateToNumber));
const animateToNumbersArr = Array.from(animateTonumberString, Number).map(
(x, idx) => (isNaN(x) ? animateTonumberString[idx] : x)
);
const [numberHeight, setNumberHeight] = React.useState(0);
const [numberWidth, setNumberWidth] = React.useState(0);
const numberDivRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
const rect = numberDivRef.current?.getClientRects()?.[0];
if (rect) {
setNumberHeight(rect.height);
setNumberWidth(rect.width);
}
}, []);
React.useEffect(() => {
if (isInView) {
controls.start("visible");
}
}, [isInView, animateToNumber, controls]);
return (
<span ref={ref}>
{numberHeight !== 0 && (
<div
style={{
display: "flex",
flexDirection: "row",
overflow: "hidden",
}}
className={className}
>
{animateToNumbersArr.map((n, index) => {
if (typeof n === "string") {
return (
<div
key={index}
style={{ ...fontStyle, fontVariantNumeric: "tabular-nums" }}
>
{n}
</div>
);
}
return (
<motion.div
key={`${n}_${index}`}
style={{
height: numberHeight,
width: numberWidth,
}}
initial="hidden"
variants={{
hidden: { y: 0 },
visible: {
y:
-1 *
(numberHeight *
(typeof animateToNumbersArr[index] === "number"
? animateToNumbersArr[index]
: 0)) -
numberHeight * 20,
},
}}
animate={controls}
transition={transitions?.(index)}
>
{NUMBERS.map((number) => (
<div
style={{ ...fontStyle, fontVariantNumeric: "tabular-nums" }}
>
{number}
</div>
))}
</motion.div>
);
})}
</div>
)}
<div
ref={numberDivRef}
style={{ position: "absolute", top: -9999, ...fontStyle }}
>
{0}
</div>
</span>
);
};
const Enhanced = React.memo(AnimatedNumber, (prevProps, nextProps) => {
return (
prevProps.animateToNumber === nextProps.animateToNumber &&
prevProps.fontStyle === nextProps.fontStyle &&
prevProps.includeComma === nextProps.includeComma
);
});
export default Enhanced;