-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathprogressbar-rounded.tsx
More file actions
138 lines (127 loc) · 4.18 KB
/
progressbar-rounded.tsx
File metadata and controls
138 lines (127 loc) · 4.18 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
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
/* global window */
import * as React from 'react';
import { SIZE } from './constants';
import {
StyledProgressBarRoundedRoot,
StyledProgressBarRoundedSvg,
StyledProgressBarRoundedTrackBackground,
StyledProgressBarRoundedTrackForeground,
StyledProgressBarRoundedText,
} from './styled-components';
import { useOverrides } from '../helpers/overrides';
import type { ProgressBarRoundedProps } from './types';
const defaults = {
Root: StyledProgressBarRoundedRoot,
Svg: StyledProgressBarRoundedSvg,
TrackBackground: StyledProgressBarRoundedTrackBackground,
TrackForeground: StyledProgressBarRoundedTrackForeground,
Text: StyledProgressBarRoundedText,
};
// @ts-ignore
function roundTo(n, digits?) {
if (digits === undefined) {
digits = 0;
}
const multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
const test = Math.round(n) / multiplicator;
return +test.toFixed(digits);
}
function ProgressBarRounded({
progress = 0,
size = SIZE.medium,
animate = true,
inline = false,
overrides = {},
ariaLabel,
...restProps
}: ProgressBarRoundedProps) {
const {
Root: [Root, rootProps],
Svg: [Svg, svgProps],
TrackBackground: [TrackBackground, trackBackgroundProps],
TrackForeground: [TrackForeground, trackForegroundProps],
Text: [Text, textProps],
} = useOverrides(defaults, overrides);
// Get path length after initial render
const [pathLength, setPathLength] = React.useState(0);
const pathRef = React.useRef<SVGPathElement>();
React.useEffect(() => {
if (pathRef.current && pathRef.current.getTotalLength) {
setPathLength(pathRef.current.getTotalLength());
}
}, []);
// Animation
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const animationFrameRef = React.useRef<any>();
const [pathProgress, setPathProgress] = React.useState(0);
React.useEffect(() => {
if (!animate) {
setPathProgress(progress);
return;
}
if (window && animationFrameRef.current) {
window.cancelAnimationFrame(animationFrameRef.current);
}
let animationDuration = Math.max(1000 * (progress - pathProgress), 250);
// @ts-ignore
let animationTimeStarted;
function loop(now = 0) {
// @ts-ignore
if (!animationTimeStarted) {
animationTimeStarted = now;
}
// @ts-ignore
let animationTimeElapsed = now - animationTimeStarted;
// Move out of state - might need to reverse calculate the path progress for interruped animations
let currentPathProgress = Math.min(
(progress - pathProgress) * (animationTimeElapsed / animationDuration) + pathProgress,
1
);
currentPathProgress = Math.max(currentPathProgress, 0);
setPathProgress(currentPathProgress);
if (animationTimeElapsed <= animationDuration) {
animationFrameRef.current = window.requestAnimationFrame(loop);
}
}
loop();
}, [progress]); // We want *only* `progress` to trigger this effect
const propsAriaLabel = restProps['aria-label'] || ariaLabel;
const progressPercentage = `${roundTo(Math.min(progress * 100, 100))}%`;
const defaultAriaLabel = propsAriaLabel || `${progressPercentage} complete`;
return (
<Root
data-baseweb="progressbar-rounded"
role="progressbar"
aria-label={defaultAriaLabel}
aria-valuenow={progress}
aria-valuemin={0}
aria-valuemax={1}
aria-live="polite"
$size={size}
$inline={inline}
{...restProps}
{...rootProps}
>
<Svg $size={size} {...restProps} {...svgProps}>
<TrackBackground $size={size} {...trackBackgroundProps} />
<TrackForeground
ref={pathRef}
$size={size}
$visible={!!pathRef.current}
$pathLength={pathLength}
$pathProgress={pathProgress}
{...trackForegroundProps}
/>
</Svg>
{/* eslint-disable-next-line react/no-children-prop */}
<Text $size={size} children={progressPercentage} {...textProps} />
</Root>
);
}
export default ProgressBarRounded;