-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathCircularProgress.tsx
211 lines (200 loc) · 5.71 KB
/
CircularProgress.tsx
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
import { CircularProgressTypeMap } from ".";
import styled from "../styles/styled";
import capitalize from "../utils/capitalize";
import { getCircularProgressUtilityClass } from "./circularProgressClasses";
import createComponentFactory from "@suid/base/createComponentFactory";
import SxProps from "@suid/system/sxProps";
import { PropsOf } from "@suid/types";
import randomString from "@suid/utils/randomString";
import clsx from "clsx";
const $ = createComponentFactory<CircularProgressTypeMap>()({
name: "MuiCircularProgress",
propDefaults: ({ set }) =>
set({
color: "primary",
disableShrink: false,
size: 40,
thickness: 3.6,
value: 0,
variant: "indeterminate",
}),
selfPropNames: [
"classes",
"color",
"disableShrink",
"size",
"thickness",
"value",
"variant",
],
utilityClass: getCircularProgressUtilityClass,
slotClasses: (ownerState) => ({
root: ["root", ownerState.variant, `color${capitalize(ownerState.color)}`],
svg: ["svg"],
circle: [
"circle",
`circle${capitalize(ownerState.variant)}`,
ownerState.disableShrink && "circleDisableShrink",
],
}),
});
const SIZE = 44;
// [review] keyframe must have a static name
const animationId = randomString();
const CircularProgressRoot = styled("span", {
name: "MuiCircularProgress",
slot: "Root",
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
styles[ownerState.variant],
styles[`color${capitalize(ownerState.color)}`],
];
},
})(
({ ownerState, theme }) => ({
width: ownerState.size,
height: ownerState.size,
...(ownerState.variant === "determinate" && {
transform: "rotate(-90deg)",
}),
display: "inline-block",
...(ownerState.variant === "determinate" && {
transition: theme.transitions.create("transform"),
}),
...(ownerState.color !== "inherit" && {
color: theme.palette[ownerState.color].main,
}),
}),
({ ownerState }) =>
ownerState.variant === "indeterminate" && {
[`@keyframes circular-rotate-${animationId}`]: {
0: {
transform: "rotate(0deg)",
},
100: {
transform: "rotate(360deg)",
},
},
animation: `circular-rotate-${animationId} 1.4s linear infinite`,
}
);
const CircularProgressSVG = styled("svg", {
name: "MuiCircularProgress",
slot: "Svg",
overridesResolver: (props, styles) => styles.svg,
})({
display: "block", // Keeps the progress centered
});
const CircularProgressCircle = styled("circle", {
name: "MuiCircularProgress",
slot: "Circle",
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.circle,
styles[`circle${capitalize(ownerState.variant)}`],
ownerState.disableShrink && styles.circleDisableShrink,
];
},
})(
({ ownerState, theme }) => ({
stroke: "currentColor",
// Use butt to follow the specification, by chance, it's already the default CSS value.
// strokeLinecap: 'butt',
...(ownerState.variant === "determinate" && {
transition: theme.transitions.create("stroke-dashoffset"),
}),
...(ownerState.variant === "indeterminate" && {
// Some default value that looks fine waiting for the animation to kicks in.
strokeDasharray: "80px, 200px",
strokeDashoffset: 0, // Add the unit to fix a Edge 16 and below bug.
}),
}),
({ ownerState }) =>
ownerState.variant === "indeterminate" &&
!ownerState.disableShrink && {
[`@keyframes circular-dash-${animationId}`]: {
0: {
strokeDasharray: "1px, 200px",
strokeDashoffset: 0,
},
50: {
strokeDasharray: "100px, 200px",
strokeDashoffset: "-15px",
},
100: {
strokeDasharray: "100px, 200px",
strokeDashoffset: "-125px",
},
},
animation: `circular-dash-${animationId} 1.4s ease-in-out infinite`,
}
);
/**
* ## ARIA
*
* If the progress bar is describing the loading progress of a particular region of a page,
* you should use `aria-describedby` to point to the progress bar, and set the `aria-busy`
* attribute to `true` on that region until it has finished loading.
*
* Demos:
*
* - [Progress](https://mui.com/components/progress/)
*
* API:
*
* - [CircularProgress API](https://mui.com/api/circular-progress/)
*/
const CircularProgress = $.component(function CircularProgress({
allProps,
classes,
otherProps,
props,
}) {
const circleStyle = () => {
if (props.variant !== "determinate") return {};
const circumference = 2 * Math.PI * ((SIZE - props.thickness) / 2);
return {
strokeDasharray: circumference.toFixed(3),
strokeDashoffset: `${(
((100 - props.value) / 100) *
circumference
).toFixed(3)}px`,
} as SxProps;
};
const rootProps = () => {
if (props.variant !== "determinate") return {};
return {
"aria-valuenow": Math.round(props.value),
} as PropsOf<CircularProgressTypeMap>;
};
return (
<CircularProgressRoot
class={clsx(classes.root, otherProps.class)}
ownerState={allProps}
role="progressbar"
{...rootProps()}
{...otherProps}
>
<CircularProgressSVG
class={classes.svg}
ownerState={allProps}
viewBox={`${SIZE / 2} ${SIZE / 2} ${SIZE} ${SIZE}`}
>
<CircularProgressCircle
class={classes.circle}
sx={circleStyle()}
ownerState={allProps}
cx={SIZE}
cy={SIZE}
r={(SIZE - props.thickness) / 2}
fill="none"
stroke-width={props.thickness}
/>
</CircularProgressSVG>
</CircularProgressRoot>
);
});
export default CircularProgress;