-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCounter.jsx
More file actions
126 lines (117 loc) · 3.43 KB
/
Counter.jsx
File metadata and controls
126 lines (117 loc) · 3.43 KB
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
import { motion, useSpring, useTransform } from 'motion/react';
import { useEffect } from 'react';
import './Counter.css';
function Number({ mv, number, height }) {
let y = useTransform(mv, latest => {
let placeValue = latest % 10;
let offset = (10 + number - placeValue) % 10;
let memo = offset * height;
if (offset > 5) {
memo -= 10 * height;
}
return memo;
});
return (
<motion.span className="counter-number" style={{ y }}>
{number}
</motion.span>
);
}
function normalizeNearInteger(num) {
const nearest = Math.round(num);
const tolerance = 1e-9 * Math.max(1, Math.abs(num));
return Math.abs(num - nearest) < tolerance ? nearest : num;
}
function getValueRoundedToPlace(value, place) {
const scaled = value / place;
return Math.floor(normalizeNearInteger(scaled));
}
function Digit({ place, value, height, digitStyle }) {
const isDecimal = place === '.';
const valueRoundedToPlace = isDecimal ? 0 : getValueRoundedToPlace(value, place);
const animatedValue = useSpring(valueRoundedToPlace);
useEffect(() => {
if (!isDecimal) {
animatedValue.set(valueRoundedToPlace);
}
}, [animatedValue, valueRoundedToPlace, isDecimal]);
if (isDecimal) {
return (
<span className="counter-digit" style={{ height, ...digitStyle, width: 'fit-content' }}>
.
</span>
);
}
return (
<span className="counter-digit" style={{ height, ...digitStyle }}>
{Array.from({ length: 10 }, (_, i) => (
<Number key={i} mv={animatedValue} number={i} height={height} />
))}
</span>
);
}
export default function Counter({
value,
fontSize = 100,
padding = 0,
places = [...value.toString()].map((ch, i, a) => {
ch == '.';
if (ch === '.') {
return '.';
} else {
return (
10 **
(a.indexOf('.') === -1 ? a.length - i - 1 : i < a.indexOf('.') ? a.indexOf('.') - i - 1 : -(i - a.indexOf('.')))
);
}
}),
gap = 8,
borderRadius = 4,
horizontalPadding = 8,
textColor = 'inherit',
fontWeight = 'inherit',
containerStyle,
counterStyle,
digitStyle,
gradientHeight = 16,
gradientFrom = 'black',
gradientTo = 'transparent',
topGradientStyle,
bottomGradientStyle
}) {
const height = fontSize + padding;
const defaultCounterStyle = {
fontSize,
gap: gap,
borderRadius: borderRadius,
paddingLeft: horizontalPadding,
paddingRight: horizontalPadding,
color: textColor,
fontWeight: fontWeight,
direction: "ltr"
};
const defaultTopGradientStyle = {
height: gradientHeight,
background: `linear-gradient(to bottom, ${gradientFrom}, ${gradientTo})`
};
const defaultBottomGradientStyle = {
height: gradientHeight,
background: `linear-gradient(to top, ${gradientFrom}, ${gradientTo})`
};
return (
<span className="counter-container" style={containerStyle}>
<span className="counter-counter" style={{ ...defaultCounterStyle, ...counterStyle }}>
{places.map(place => (
<Digit key={place} place={place} value={value} height={height} digitStyle={digitStyle} />
))}
</span>
<span className="gradient-container">
<span className="top-gradient" style={topGradientStyle ? topGradientStyle : defaultTopGradientStyle}></span>
<span
className="bottom-gradient"
style={bottomGradientStyle ? bottomGradientStyle : defaultBottomGradientStyle}
></span>
</span>
</span>
);
}