This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-js-progressbar.js
622 lines (556 loc) · 23.8 KB
/
react-js-progressbar.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import React, { forwardRef, useCallback, useEffect, useRef, useState } from 'react';
import { requestNum } from 'request-animation-number';
export default function Progressbar(props) {
const pathRef = useRef();
const pathGradiantRef = useRef();
const textRef = useRef();
const trailRef = useRef();
const trailGradiantRef = useRef();
const backgroundColorRef = useRef();
const backgroundColorGradiantRef = useRef();
const [oldProgressValue, setoldProgressValue] = useState(null);
const [oldTextValue, setoldTextValue] = useState(null);
const [svgId] = useState(Math.random() * 1000);
let input = props.input;
input = Math.min(Math.max(input, 0), 100);
const type = props.shape || 'full circle'; // 'semi circle' | 'full circle' | 'arc'
const size = props.size ?? '100%';
const clockwise = props.clockwise ?? true;
const dashed = props.dashed ?? false;
const dashesSize = props.dashesSize ?? 15;
const dashesGap = props.dashesGap ?? 2;
const strokeWidth = props.pathWidth ?? 12;
const strokeColor = props.pathColor || ['#f4314a', '#fa5813'];
const strokeLinecap = props.pathLinecap || (type === 'arc' ? 'none' : 'round'); // 'butt' | 'round' | 'square' | 'none'
const strokeShadow = dashed
? 'none'
: props.pathShadow && props.pathShadow !== 'none'
? `drop-shadow(${props.pathShadow})`
: props.pathShadow === 'none'
? 'none'
: 'drop-shadow(0px 0px 2px #00000080)';
const trailWidth = props.trailWidth ?? 5;
const trailColor = props.trailColor || 'lightgray';
const backgroundColor = props.backgroundColor || 'none';
const textValue = props.customText ?? (oldTextValue ? oldTextValue.toFixed(0) : input.toFixed(0)) + '%';
const textPositionX = props.textPosition?.x ?? '50%';
const textPositionY = props.textPosition?.y ?? (type === 'arc' ? '40%' : '50%');
const textPosition = { x: textPositionX, y: textPositionY };
const textStyle = { fontSize: '50px', fill: 'black', ...props.textStyle };
const animateText = props.animateText ?? true;
const animationDuration = props.animation?.duration ?? 500;
const animationDelay = props.animation?.delay ?? 0;
const animationEase = props.animation?.ease || 'easeOutBack';
const animateOnMount = props.animateOnMount ?? true;
const animateOnInputChange = props.animateOnInputChange ?? true;
const animation = {
duration: animationDuration,
delay: animationDelay,
ease: animationEase,
animateOnMount,
animateOnInputChange,
};
const checkTypes = () => {
if (typeof input !== 'number' || input < 0) console.error('react-js-ProgressBar: props.input has invalid value.');
if (!new Set(['full circle', 'semi circle', 'full circle', 'arc']).has(type))
console.error('react-js-ProgressBar: props.shape has invalid value.');
if (typeof size !== 'number' && typeof size !== 'string')
console.error('react-js-ProgressBar: props.size has invalid value.');
if (typeof clockwise !== 'boolean') console.error('react-js-ProgressBar: props.clockwise has invalid value.');
if (typeof dashed !== 'boolean') console.error('react-js-ProgressBar: props.dashed has invalid value.');
if (typeof dashesSize !== 'number' || dashesSize < 0)
console.error('react-js-ProgressBar: props.dashesSize has invalid value.');
if (typeof dashesGap !== 'number' || dashesGap < 0) console.error('react-js-ProgressBar: props.dashesGap has invalid value.');
if (typeof strokeWidth !== 'number' || strokeWidth < 0)
console.error('react-js-ProgressBar: props.pathWidth has invalid value.');
if (typeof strokeColor !== 'string' && typeof strokeColor !== 'object')
console.error('react-js-ProgressBar: props.pathColor has invalid value.');
if (!new Set(['butt', 'round', 'square', 'none']).has(strokeLinecap))
console.error('react-js-ProgressBar: props.pathLinecap has invalid value.');
if (typeof strokeShadow !== 'string') console.error('react-js-ProgressBar: props.pathShadow has invalid value.');
if (typeof trailWidth !== 'number' || trailWidth < 0)
console.error('react-js-ProgressBar: props.trailWidth has invalid value.');
if (typeof trailColor !== 'string' && typeof trailColor !== 'object')
console.error('react-js-ProgressBar: props.trailColor has invalid value.');
if (typeof backgroundColor !== 'string' && typeof backgroundColor !== 'object')
console.error('react-js-ProgressBar: props.backgroundColor has invalid value.');
if (typeof textPositionX !== 'number' && typeof textPositionX !== 'string')
console.error('react-js-ProgressBar: props.textPosition.x has invalid value.');
if (typeof textPositionY !== 'number' && typeof textPositionY !== 'string')
console.error('react-js-ProgressBar: props.textPosition.y has invalid value.');
if (typeof textPosition !== 'object') console.error('react-js-ProgressBar: props.textPosition has invalid value.');
if (typeof textStyle !== 'object') console.error('react-js-ProgressBar: props.textStyle has invalid value.');
if (typeof animateText !== 'boolean') console.error('react-js-ProgressBar: props.animateText has invalid value.');
if (typeof animationDuration !== 'number' || animationDuration < 0)
console.error('react-js-ProgressBar: props.animation.duration has invalid value.');
if (typeof animationDelay !== 'number' || animationDelay < 0)
console.error('react-js-ProgressBar: props.animation.delay has invalid value.');
if (
(!new Set([
'linear',
'easeInSine',
'easeOutSine',
'easeInOutSine',
'easeInQuad',
'easeOutQuad',
'easeInOutQuad',
'easeInCubic',
'easeOutCubic',
'easeInOutCubic',
'easeInQuart',
'easeOutQuart',
'easeInOutQuart',
'easeInQuint',
'easeOutQuint',
'easeInOutQuint',
'easeInExpo',
'easeOutExpo',
'easeInOutExpo',
'easeInCirc',
'easeOutCirc',
'easeInOutCirc',
'easeInBack',
'easeOutBack',
'easeInOutBack',
'easeInElastic',
'easeOutElastic',
'easeInOutElastic',
'easeInBounce',
'easeOutBounce',
'easeInOutBounce',
]).has(animationEase) &&
typeof animationEase === 'string') ||
(typeof animationEase !== 'string' && typeof animationEase !== 'function')
)
console.error('react-js-ProgressBar: props.animation.ease has invalid value.');
if (typeof animateOnMount !== 'boolean')
console.error('react-js-ProgressBar: props.animation.animateOnMount has invalid value.');
if (typeof animateOnInputChange !== 'boolean')
console.error('react-js-ProgressBar: props.animation.animateOnInputChange has invalid value.');
if (typeof animation !== 'object') console.error('react-js-ProgressBar: props.animation has invalid value.');
};
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') checkTypes();
const R = 100;
const circumference =
type === 'full circle'
? 2 * Math.PI * R
: type === 'arc'
? (2 * Math.PI * 100) / 2
: type === 'semi circle'
? (2 * Math.PI * R) / 1.329
: 0;
const calcProgress = (circumference - (input * circumference) / 100) * (clockwise ? 1 : -1);
const pathShadowBlur = strokeShadow !== 'none' ? parseInt(strokeShadow?.split(' ')?.[2]) : 0;
const trailShadowBlur = Math.abs(pathShadowBlur - (trailWidth - strokeWidth));
const viewBoxExpand =
trailWidth > strokeWidth
? `${-(trailWidth / 2 + trailShadowBlur)} ${trailWidth / 2 + trailShadowBlur} ${200 + trailWidth + trailShadowBlur} ${
200 + trailWidth + trailShadowBlur
}`
: `${-(strokeWidth / 2 + pathShadowBlur)} ${strokeWidth / 2 + pathShadowBlur} ${200 + strokeWidth + pathShadowBlur} ${
200 + strokeWidth + pathShadowBlur
}`;
const maskCord = trailWidth > strokeWidth ? -trailWidth / 2 : -strokeWidth / 2;
const Gradiant = forwardRef((options, ref) => {
return (
<linearGradient ref={ref} id={options.id + svgId} x='0%' x2='0%' y1='0%' y2='100%'>
<stop offset='0%' stopColor={options.stop1} />
<stop offset='90%' stopColor={options.stop2} />
</linearGradient>
);
});
const [isFirstMount, setIsFirstMount] = useState(true);
const SemiCircle = useCallback(() => {
const translateY = trailWidth > strokeWidth ? trailWidth + trailShadowBlur : strokeWidth + pathShadowBlur;
const textShift = trailWidth > strokeWidth ? trailWidth / 2 + trailShadowBlur / 2 : strokeWidth / 2 + pathShadowBlur / 2;
return (
<svg width={size} height={size} viewBox={viewBoxExpand} fill='none' xmlns='http://www.w3.org/2000/svg'>
{/* background gradiant */}
{typeof backgroundColor === 'object' ? (
<Gradiant
ref={backgroundColorGradiantRef}
id='react-js-progrssBar-background'
stop1={backgroundColor[0]}
stop2={backgroundColor[1]}
/>
) : null}
{/* background */}
{backgroundColor !== 'none' ? (
<circle
ref={backgroundColorRef}
cx='100'
cy='100'
r={R - strokeWidth / 2 + 0.5}
transform='rotate(270)'
fill={typeof backgroundColor === 'object' ? `url(#react-js-progrssBar-background${svgId})` : backgroundColor}
style={{ transformOrigin: 'center' }}
/>
) : null}
{/* trail gradiant */}
{typeof trailColor === 'object' ? (
<Gradiant ref={trailGradiantRef} id='react-js-progrssBar-trail' stop1={trailColor[0]} stop2={trailColor[1]} />
) : null}
{/* trail */}
{trailColor === 'none' || trailWidth === 0 ? null : (
<path
d='M170.063 171.353C188.54 153.208 200 127.942 200 100C200 44.7715 155.228 0 100 0C44.7715 0 0 44.7715 0 100C0 127.942 11.4604 153.208 29.9371 171.353'
ref={trailRef}
strokeWidth={trailWidth + 'px'}
stroke={typeof trailColor === 'object' ? `url(#react-js-progrssBar-trail${svgId})` : trailColor}
strokeLinecap={strokeLinecap}
style={{ transform: `translateY(${translateY}px)` }}
/>
)}
{/* path gradiant */}
{typeof strokeColor === 'object' ? (
<Gradiant ref={pathGradiantRef} id='react-js-progrssBar-path' stop1={strokeColor[0]} stop2={strokeColor[1]} />
) : null}
{/* path */}
<path
d='M170.063 171.353C188.54 153.208 200 127.942 200 100C200 44.7715 155.228 0 100 0C44.7715 0 0 44.7715 0 100C0 127.942 11.4604 153.208 29.9371 171.353'
ref={pathRef}
strokeWidth={strokeWidth + 'px'}
stroke={typeof strokeColor === 'object' ? `url(#react-js-progrssBar-path${svgId})` : strokeColor}
strokeDashoffset={oldProgressValue !== null ? oldProgressValue : circumference + 'px'}
strokeDasharray={circumference + 'px'}
strokeLinecap={strokeLinecap}
mask={dashed ? `url(#react-js-progrssBar-dashes${svgId})` : null}
style={{
transform: `translateY(${translateY}px)`,
filter: strokeShadow,
}}
/>
{/* dashes */}
{dashed ? (
<mask maskUnits='userSpaceOnUse' x={maskCord} y={maskCord} id={'react-js-progrssBar-dashes' + svgId}>
<path
d='M170.063 171.353C188.54 153.208 200 127.942 200 100C200 44.7715 155.228 0 100 0C44.7715 0 0 44.7715 0 100C0 127.942 11.4604 153.208 29.9371 171.353'
stroke='white'
strokeWidth={strokeWidth + 'px'}
strokeDasharray={`${dashesSize}px ${dashesGap}px`}
/>
</mask>
) : null}
{/* text */}
{props.customText === '' ? null : (
<text
ref={textRef}
x={textPosition.x}
y={textPosition.y}
dy={textShift}
dx={-textShift}
textAnchor='middle'
dominantBaseline='central'
style={{ ...textStyle }}
>
{textValue}
</text>
)}
{/* children */}
{props.children ? (
<foreignObject
x={trailWidth > strokeWidth ? -trailWidth / 2 : -strokeWidth / 2}
y={trailWidth > strokeWidth ? trailWidth / 2 : strokeWidth / 2}
width='100%'
height='100%'
>
{props.children}
</foreignObject>
) : null}
</svg>
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.children]);
const Arc = useCallback(() => {
const textShift = trailWidth > strokeWidth ? trailWidth / 2 + trailShadowBlur / 2 : strokeWidth / 2 + pathShadowBlur / 2;
const translateY = trailWidth > strokeWidth ? trailWidth + trailShadowBlur : strokeWidth + pathShadowBlur;
const hs = strokeWidth / 2;
return (
<svg width={size} height={size} viewBox={viewBoxExpand} fill='none' xmlns='http://www.w3.org/2000/svg'>
{/* background gradiant */}
{typeof backgroundColor === 'object' ? (
<Gradiant
ref={backgroundColorGradiantRef}
id='react-js-progrssBar-background'
stop1={backgroundColor[0]}
stop2={backgroundColor[1]}
/>
) : null}
{/* background */}
{backgroundColor !== 'none' ? (
<path
ref={backgroundColorRef}
d={`M${200.5 - hs} 100 C${200.5 - hs} 45 ${155.5 - hs} ${hs - 0.5} 100 ${hs - 0.5} C45 ${hs} ${hs} ${
45 + hs
} ${hs} 100`}
fill={typeof backgroundColor === 'object' ? `url(#react-js-progrssBar-background${svgId})` : backgroundColor}
style={{ transform: `translateY(${translateY}px)` }}
/>
) : null}
{/* trail gradiant */}
{typeof trailColor === 'object' ? (
<Gradiant ref={trailGradiantRef} id='react-js-progrssBar-trail' stop1={trailColor[0]} stop2={trailColor[1]} />
) : null}
{/* trail */}
{trailColor === 'none' || trailWidth === 0 ? null : (
<path
ref={trailRef}
d='M200 100 C200 45 155 0 100 0 C45 0 0 45 0 100'
strokeWidth={trailWidth + 'px'}
stroke={typeof trailColor === 'object' ? `url(#react-js-progrssBar-trail${svgId})` : trailColor}
strokeLinecap={strokeLinecap}
style={{ transform: `translateY(${translateY}px)` }}
/>
)}
{/* path gradiant */}
{typeof strokeColor === 'object' ? (
<Gradiant ref={pathGradiantRef} id='react-js-progrssBar-path' stop1={strokeColor[0]} stop2={strokeColor[1]} />
) : null}
{/* path */}
<path
d='M200 100 C200 45 155 0 100 0 C45 0 0 45 0 100'
ref={pathRef}
strokeWidth={strokeWidth + 'px'}
stroke={typeof strokeColor === 'object' ? `url(#react-js-progrssBar-path${svgId})` : strokeColor}
strokeDashoffset={oldProgressValue !== null ? oldProgressValue : circumference + 'px'}
strokeDasharray={circumference + 'px'}
strokeLinecap={strokeLinecap}
mask={dashed ? `url(#react-js-progrssBar-dashes${svgId})` : null}
style={{ transform: `translateY(${translateY}px)` }}
/>
{/* dashes */}
{dashed ? (
<mask maskUnits='userSpaceOnUse' x={maskCord} y={maskCord} id={'react-js-progrssBar-dashes' + svgId}>
<path
d='M199.999 100C200 99.83 200 99.66 200 99.4898C200 44.5431 155.228 0 100 0C44.7715 0 0 44.5431 0 99.4898C0 99.66 0.000429398 99.83 0.00128722 100'
stroke='white'
strokeWidth={strokeWidth + 'px'}
strokeDasharray={`${dashesSize}px ${dashesGap}px`}
/>
</mask>
) : null}
{/* Text */}
{props.customText === '' ? null : (
<text
ref={textRef}
x={textPosition.x}
y={textPosition.y}
dy={textShift}
dx={-textShift}
textAnchor='middle'
dominantBaseline='central'
style={{ ...textStyle }}
>
{textValue}
</text>
)}
{/* children */}
{props.children ? (
<foreignObject
x={trailWidth > strokeWidth ? -trailWidth / 2 : -strokeWidth / 2}
y={trailWidth > strokeWidth ? trailWidth / 2 : strokeWidth / 2}
width='100%'
height='100%'
>
{props.children}
</foreignObject>
) : null}
</svg>
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.children]);
const FullCircle = useCallback(() => {
const textShift = trailWidth > strokeWidth ? trailWidth / 2 + trailShadowBlur / 2 : strokeWidth / 2 + pathShadowBlur / 2;
return (
<svg width={size} height={size} viewBox={viewBoxExpand} xmlns='http://www.w3.org/2000/svg' fill='none'>
{/* background gradiant */}
{typeof backgroundColor === 'object' ? (
<Gradiant
ref={backgroundColorGradiantRef}
id='react-js-progrssBar-background'
stop1={backgroundColor[0]}
stop2={backgroundColor[1]}
/>
) : null}
{/* background */}
{backgroundColor !== 'none' ? (
<circle
ref={backgroundColorRef}
cx='100'
cy='100'
r={R - strokeWidth / 2 + 0.5}
transform='rotate(270)'
fill={typeof backgroundColor === 'object' ? `url(#react-js-progrssBar-background${svgId})` : backgroundColor}
style={{ transformOrigin: 'center' }}
/>
) : null}
{/* trail gradiant */}
{typeof trailColor === 'object' ? (
<Gradiant ref={trailGradiantRef} id='react-js-progrssBar-trail' stop1={trailColor[0]} stop2={trailColor[1]} />
) : null}
{/* trail */}
{trailColor === 'none' || trailWidth === 0 ? null : (
<circle
ref={trailRef}
cx='100'
cy='100'
r={R}
strokeWidth={trailWidth + 'px'}
stroke={typeof trailColor === 'object' ? `url(#react-js-progrssBar-trail${svgId})` : trailColor}
transform='rotate(270)'
style={{ transformOrigin: 'center' }}
/>
)}
{/* path gradiant */}
{typeof strokeColor === 'object' ? (
<Gradiant ref={pathGradiantRef} id='react-js-progrssBar-path' stop1={strokeColor[0]} stop2={strokeColor[1]} />
) : null}
{/* path */}
<circle
ref={pathRef}
cx='100'
cy='100'
r={R}
strokeWidth={strokeWidth + 'px'}
stroke={typeof strokeColor === 'object' ? `url(#react-js-progrssBar-path${svgId})` : strokeColor}
strokeDashoffset={oldProgressValue !== null ? oldProgressValue : circumference + 'px'}
strokeDasharray={circumference + 'px'}
strokeLinecap={strokeLinecap}
transform='rotate(270)'
mask={dashed ? `url(#react-js-progrssBar-dashes${svgId})` : null}
style={{ transformOrigin: 'center', filter: strokeShadow }}
/>
{/* dashes */}
{dashed ? (
<mask maskUnits='userSpaceOnUse' x={maskCord} y={maskCord} id={'react-js-progrssBar-dashes' + svgId}>
<circle
cx='100'
cy='100'
r={R}
stroke='white'
strokeWidth={strokeWidth + 'px'}
strokeDasharray={`${dashesSize}px ${dashesGap}px`}
/>
</mask>
) : null}
{/* text */}
{props.customText === '' ? null : (
<text
ref={textRef}
x={textPosition.x}
y={textPosition.y}
dy={textShift}
dx={-textShift}
textAnchor='middle'
dominantBaseline='central'
style={{ ...textStyle }}
>
{textValue}
</text>
)}
{/* children */}
{props.children ? (
<foreignObject
x={trailWidth > strokeWidth ? -trailWidth / 2 - trailShadowBlur / 2 : -strokeWidth / 2 - pathShadowBlur / 2}
y={trailWidth > strokeWidth ? trailWidth / 2 + trailShadowBlur / 2 : strokeWidth / 2 + pathShadowBlur / 2}
width='100%'
height='100%'
>
{props.children}
</foreignObject>
) : null}
</svg>
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.children]);
// update props when input changes
const updateProps = useCallback(() => {
// path
if (pathRef.current) {
pathRef.current.style.stroke = typeof strokeColor === 'object' ? `url(#react-js-progrssBar-path${svgId})` : strokeColor;
}
if (pathGradiantRef.current && typeof strokeColor === 'object') {
pathGradiantRef.current.children[0].style.stopColor = strokeColor[0];
pathGradiantRef.current.children[1].style.stopColor = strokeColor[1];
}
// trail
if (trailRef.current) {
trailRef.current.style.stroke = typeof trailColor === 'object' ? `url(#react-js-progrssBar-trail${svgId})` : trailColor;
}
if (trailGradiantRef.current && typeof trailColor === 'object') {
trailGradiantRef.current.children[0].style.stopColor = trailColor[0];
trailGradiantRef.current.children[1].style.stopColor = trailColor[1];
}
// background
if (backgroundColorRef.current) {
backgroundColorRef.current.style.fill =
typeof backgroundColor === 'object' ? `url(#react-js-progrssBar-background${svgId})` : backgroundColor;
}
if (backgroundColorGradiantRef.current && typeof backgroundColor === 'object') {
backgroundColorGradiantRef.current.children[0].style.stopColor = backgroundColor[0];
backgroundColorGradiantRef.current.children[1].style.stopColor = backgroundColor[1];
}
// text
if (textRef.current) {
Object.keys(textStyle).forEach(key => (textRef.current.style[key] = textStyle[key]));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.input]);
useEffect(() => {
// update props when input changes without re-rendering
if (!isFirstMount) updateProps();
// exit if animate on mount disabled.
if (isFirstMount && !animation.animateOnMount) {
setoldProgressValue(calcProgress);
setoldTextValue(input);
setIsFirstMount(false);
return;
}
// exit if animate on input change disabled.
if (!isFirstMount && !animation.animateOnInputChange) {
setoldProgressValue(calcProgress);
setoldTextValue(input);
return;
}
// animate path
requestNum(
{
from: oldProgressValue ? oldProgressValue : circumference * (clockwise ? 1 : -1),
to: calcProgress,
duration: animation.duration,
delay: animation.delay,
easingFunction: animation.ease,
},
x => {
// do nothing if unmounted
if (pathRef.current) {
pathRef.current.style.strokeDashoffset = x + 'px';
if (x === calcProgress) setoldProgressValue(calcProgress);
}
}
);
// animate text if it's not cutom text
if (!props.customText && props.customText !== '' && animateText) {
requestNum(
{
from: oldTextValue ?? 0,
to: input,
duration: animation.duration,
delay: animation.delay,
easingFunction: animation.ease,
},
t => {
// do nothing if unmounted
if (textRef.current) {
textRef.current.innerHTML = t.toFixed(0) + '%';
if (t === input) setoldTextValue(input);
}
}
);
// update cutstom text
} else if (textRef.current) textRef.current.innerHTML = props.customText;
setIsFirstMount(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.input]);
return <>{type === 'semi circle' ? <SemiCircle /> : type === 'arc' ? <Arc /> : <FullCircle />}</>;
}