Skip to content

Commit e04cf7d

Browse files
committed
Copy to additional destinations
1 parent c65254a commit e04cf7d

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

src/tailwind/TextAnimations/CountUp/CountUp.jsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useEffect, useRef } from 'react';
21
import { useInView, useMotionValue, useSpring } from 'motion/react';
2+
import { useCallback, useEffect, useRef } from 'react';
33

44
export default function CountUp({
55
to,
@@ -42,11 +42,25 @@ export default function CountUp({
4242

4343
const maxDecimals = Math.max(getDecimalPlaces(from), getDecimalPlaces(to));
4444

45+
const formatValue = useCallback(latest => {
46+
const hasDecimals = maxDecimals > 0;
47+
48+
const options = {
49+
useGrouping: !!separator,
50+
minimumFractionDigits: hasDecimals ? maxDecimals : 0,
51+
maximumFractionDigits: hasDecimals ? maxDecimals : 0
52+
};
53+
54+
const formattedNumber = Intl.NumberFormat('en-US', options).format(latest);
55+
56+
return separator ? formattedNumber.replace(/,/g, separator) : formattedNumber;
57+
}, [maxDecimals, separator])
58+
4559
useEffect(() => {
4660
if (ref.current) {
47-
ref.current.textContent = String(direction === 'down' ? to : from);
61+
ref.current.textContent = formatValue(direction === 'down' ? to : from);
4862
}
49-
}, [from, to, direction]);
63+
}, [from, to, direction, formatValue]);
5064

5165
useEffect(() => {
5266
if (isInView && startWhen) {
@@ -73,22 +87,12 @@ export default function CountUp({
7387
useEffect(() => {
7488
const unsubscribe = springValue.on('change', latest => {
7589
if (ref.current) {
76-
const hasDecimals = maxDecimals > 0;
77-
78-
const options = {
79-
useGrouping: !!separator,
80-
minimumFractionDigits: hasDecimals ? maxDecimals : 0,
81-
maximumFractionDigits: hasDecimals ? maxDecimals : 0
82-
};
83-
84-
const formattedNumber = Intl.NumberFormat('en-US', options).format(latest);
85-
86-
ref.current.textContent = separator ? formattedNumber.replace(/,/g, separator) : formattedNumber;
90+
ref.current.textContent = formatValue(latest);
8791
}
8892
});
8993

9094
return () => unsubscribe();
91-
}, [springValue, separator, maxDecimals]);
95+
}, [springValue, formatValue]);
9296

9397
return <span className={className} ref={ref} />;
9498
}

src/ts-tailwind/TextAnimations/CountUp/CountUp.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useEffect, useRef } from 'react';
21
import { useInView, useMotionValue, useSpring } from 'motion/react';
2+
import { useCallback, useEffect, useRef } from 'react';
33

44
interface CountUpProps {
55
to: number;
@@ -52,11 +52,25 @@ export default function CountUp({
5252

5353
const maxDecimals = Math.max(getDecimalPlaces(from), getDecimalPlaces(to));
5454

55+
const formatValue = useCallback((latest: number) => {
56+
const hasDecimals = maxDecimals > 0;
57+
58+
const options: Intl.NumberFormatOptions = {
59+
useGrouping: !!separator,
60+
minimumFractionDigits: hasDecimals ? maxDecimals : 0,
61+
maximumFractionDigits: hasDecimals ? maxDecimals : 0
62+
};
63+
64+
const formattedNumber = Intl.NumberFormat('en-US', options).format(latest);
65+
66+
return separator ? formattedNumber.replace(/,/g, separator) : formattedNumber;
67+
}, [maxDecimals, separator])
68+
5569
useEffect(() => {
5670
if (ref.current) {
57-
ref.current.textContent = String(direction === 'down' ? to : from);
71+
ref.current.textContent = formatValue(direction === 'down' ? to : from);
5872
}
59-
}, [from, to, direction]);
73+
}, [from, to, direction, formatValue]);
6074

6175
useEffect(() => {
6276
if (isInView && startWhen) {
@@ -85,24 +99,14 @@ export default function CountUp({
8599
}, [isInView, startWhen, motionValue, direction, from, to, delay, onStart, onEnd, duration]);
86100

87101
useEffect(() => {
88-
const unsubscribe = springValue.on('change', latest => {
102+
const unsubscribe = springValue.on('change', (latest: number) => {
89103
if (ref.current) {
90-
const hasDecimals = maxDecimals > 0;
91-
92-
const options: Intl.NumberFormatOptions = {
93-
useGrouping: !!separator,
94-
minimumFractionDigits: hasDecimals ? maxDecimals : 0,
95-
maximumFractionDigits: hasDecimals ? maxDecimals : 0
96-
};
97-
98-
const formattedNumber = Intl.NumberFormat('en-US', options).format(latest);
99-
100-
ref.current.textContent = separator ? formattedNumber.replace(/,/g, separator) : formattedNumber;
104+
ref.current.textContent = formatValue(latest);
101105
}
102106
});
103107

104108
return () => unsubscribe();
105-
}, [springValue, separator, maxDecimals]);
109+
}, [springValue, formatValue]);
106110

107111
return <span className={className} ref={ref} />;
108112
}

0 commit comments

Comments
 (0)