-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCounter.jsx
More file actions
154 lines (137 loc) · 3.89 KB
/
Counter.jsx
File metadata and controls
154 lines (137 loc) · 3.89 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { motion, useSpring, useTransform } from 'motion/react';
import { useEffect } from 'react';
function Number({ mv, number, height }) {
const y = useTransform(mv, latest => {
const placeValue = latest % 10;
const offset = (10 + number - placeValue) % 10;
let memo = offset * height;
if (offset > 5) {
memo -= 10 * height;
}
return memo;
});
const baseStyle = {
position: 'absolute',
inset: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
};
return <motion.span style={{ ...baseStyle, 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="relative inline-flex items-center justify-center"
style={{ height, width: 'fit-content', ...digitStyle }}
>
.
</span>
);
}
const defaultStyle = {
height,
position: 'relative',
width: '1ch',
fontVariantNumeric: 'tabular-nums'
};
return (
<span className="relative inline-flex overflow-hidden" style={{ ...defaultStyle, ...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,
// same refactored default as your CSS version
places = [...value.toString()].map((ch, i, a) => {
if (ch === '.') return '.';
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 = 'white',
fontWeight = 'bold',
containerStyle,
counterStyle,
digitStyle,
gradientHeight = 16,
gradientFrom = 'black',
gradientTo = 'transparent',
topGradientStyle,
bottomGradientStyle
}) {
const height = fontSize + padding;
const defaultContainerStyle = {
position: 'relative',
display: 'inline-block'
};
const defaultCounterStyle = {
fontSize,
display: 'flex',
gap,
overflow: 'hidden',
borderRadius,
paddingLeft: horizontalPadding,
paddingRight: horizontalPadding,
lineHeight: 1,
color: textColor,
fontWeight,
direction: "ltr"
};
const gradientContainerStyle = {
pointerEvents: 'none',
position: 'absolute',
inset: 0,
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-between'
};
const defaultTopGradientStyle = {
height: gradientHeight,
background: `linear-gradient(to bottom, ${gradientFrom}, ${gradientTo})`
};
const defaultBottomGradientStyle = {
height: gradientHeight,
background: `linear-gradient(to top, ${gradientFrom}, ${gradientTo})`
};
return (
<span style={{ ...defaultContainerStyle, ...containerStyle }}>
<span style={{ ...defaultCounterStyle, ...counterStyle }}>
{places.map(place => (
<Digit key={place} place={place} value={value} height={height} digitStyle={digitStyle} />
))}
</span>
<span style={gradientContainerStyle}>
<span style={topGradientStyle ?? defaultTopGradientStyle} />
<span style={bottomGradientStyle ?? defaultBottomGradientStyle} />
</span>
</span>
);
}