-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCounter.tsx
More file actions
167 lines (147 loc) · 4.45 KB
/
Counter.tsx
File metadata and controls
167 lines (147 loc) · 4.45 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
155
156
157
158
159
160
161
162
163
164
165
166
167
import { MotionValue, motion, useSpring, useTransform } from 'motion/react';
import type React from 'react';
import { useEffect } from 'react';
import './Counter.css';
type PlaceValue = number | '.';
interface NumberProps {
mv: MotionValue<number>;
number: number;
height: number;
}
function Number({ mv, number, height }: NumberProps) {
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;
});
return (
<motion.span className="counter-number" style={{ y }}>
{number}
</motion.span>
);
}
function normalizeNearInteger(num: number): number {
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: number, place: number): number {
const scaled = value / place;
return Math.floor(normalizeNearInteger(scaled));
}
interface DigitProps {
place: PlaceValue;
value: number;
height: number;
digitStyle?: React.CSSProperties;
}
function Digit({ place, value, height, digitStyle }: DigitProps) {
if (place === '.') {
return (
<span className="counter-digit" style={{ height, ...digitStyle, width: 'fit-content' }}>
.
</span>
);
}
const valueRoundedToPlace = getValueRoundedToPlace(value, place);
const animatedValue = useSpring(valueRoundedToPlace);
useEffect(() => {
animatedValue.set(valueRoundedToPlace);
}, [animatedValue, valueRoundedToPlace]);
return (
<span className="counter-digit" style={{ height, ...digitStyle }}>
{Array.from({ length: 10 }, (_, i) => (
<Number key={i} mv={animatedValue} number={i} height={height} />
))}
</span>
);
}
interface CounterProps {
value: number;
fontSize?: number;
padding?: number;
/**
* An array of place values that determines which digit positions
* should be displayed. For decimal places, use "." to represent
* the decimal point. Leave this prop empty to enable automatic
* detection based on the current value.
*/
places?: PlaceValue[];
gap?: number;
borderRadius?: number;
horizontalPadding?: number;
textColor?: string;
fontWeight?: React.CSSProperties['fontWeight'];
containerStyle?: React.CSSProperties;
counterStyle?: React.CSSProperties;
digitStyle?: React.CSSProperties;
gradientHeight?: number;
gradientFrom?: string;
gradientTo?: string;
topGradientStyle?: React.CSSProperties;
bottomGradientStyle?: React.CSSProperties;
}
export default function Counter({
value,
fontSize = 100,
padding = 0,
places = [...value.toString()].map((ch, i, a) => {
if (ch === '.') {
return '.';
}
const dotIndex = a.indexOf('.');
const isInteger = dotIndex === -1;
const exponent = isInteger ? a.length - i - 1 : i < dotIndex ? dotIndex - i - 1 : -(i - dotIndex);
return 10 ** exponent;
}),
gap = 8,
borderRadius = 4,
horizontalPadding = 8,
textColor = 'inherit',
fontWeight = 'inherit',
containerStyle,
counterStyle,
digitStyle,
gradientHeight = 16,
gradientFrom = 'black',
gradientTo = 'transparent',
topGradientStyle,
bottomGradientStyle
}: CounterProps) {
const height = fontSize + padding;
const defaultCounterStyle: React.CSSProperties = {
fontSize,
gap,
borderRadius,
paddingLeft: horizontalPadding,
paddingRight: horizontalPadding,
color: textColor,
fontWeight,
direction: "ltr"
};
const defaultTopGradientStyle: React.CSSProperties = {
height: gradientHeight,
background: `linear-gradient(to bottom, ${gradientFrom}, ${gradientTo})`
};
const defaultBottomGradientStyle: React.CSSProperties = {
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 ?? defaultTopGradientStyle} />
<span className="bottom-gradient" style={bottomGradientStyle ?? defaultBottomGradientStyle} />
</span>
</span>
);
}