-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathCircularProgress.js
321 lines (296 loc) · 9.24 KB
/
CircularProgress.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
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import chainPropTypes from '@mui/utils/chainPropTypes';
import composeClasses from '@mui/utils/composeClasses';
import { keyframes, css, styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import capitalize from '../utils/capitalize';
import createSimplePaletteValueFilter from '../utils/createSimplePaletteValueFilter';
import { getCircularProgressUtilityClass } from './circularProgressClasses';
const SIZE = 44;
const circularRotateKeyframe = keyframes`
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
`;
const circularDashKeyframe = keyframes`
0% {
stroke-dasharray: 1px, 200px;
stroke-dashoffset: 0;
}
50% {
stroke-dasharray: 100px, 200px;
stroke-dashoffset: -15px;
}
100% {
stroke-dasharray: 1px, 200px;
stroke-dashoffset: -126px;
}
`;
// This implementation is for supporting both Styled-components v4+ and Pigment CSS.
// A global animation has to be created here for Styled-components v4+ (https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/utils/errors.md#12).
// which can be done by checking typeof indeterminate1Keyframe !== 'string' (at runtime, Pigment CSS transform keyframes`` to a string).
const rotateAnimation =
typeof circularRotateKeyframe !== 'string'
? css`
animation: ${circularRotateKeyframe} 1.4s linear infinite;
`
: null;
const dashAnimation =
typeof circularDashKeyframe !== 'string'
? css`
animation: ${circularDashKeyframe} 1.4s ease-in-out infinite;
`
: null;
const useUtilityClasses = (ownerState) => {
const { classes, variant, color, disableShrink } = ownerState;
const slots = {
root: ['root', variant, `color${capitalize(color)}`],
svg: ['svg'],
circle: ['circle', `circle${capitalize(variant)}`, disableShrink && 'circleDisableShrink'],
};
return composeClasses(slots, getCircularProgressUtilityClass, classes);
};
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)}`],
];
},
})(
memoTheme(({ theme }) => ({
display: 'inline-block',
variants: [
{
props: {
variant: 'determinate',
},
style: {
transition: theme.transitions.create('transform'),
},
},
{
props: {
variant: 'indeterminate',
},
style: rotateAnimation || {
animation: `${circularRotateKeyframe} 1.4s linear infinite`,
},
},
...Object.entries(theme.palette)
.filter(createSimplePaletteValueFilter())
.map(([color]) => ({
props: { color },
style: {
color: (theme.vars || theme).palette[color].main,
},
})),
],
})),
);
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,
];
},
})(
memoTheme(({ theme }) => ({
stroke: 'currentColor',
variants: [
{
props: {
variant: 'determinate',
},
style: {
transition: theme.transitions.create('stroke-dashoffset'),
},
},
{
props: {
variant: 'indeterminate',
},
style: {
// 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.
},
},
{
props: ({ ownerState }) =>
ownerState.variant === 'indeterminate' && !ownerState.disableShrink,
style: dashAnimation || {
// At runtime for Pigment CSS, `bufferAnimation` will be null and the generated keyframe will be used.
animation: `${circularDashKeyframe} 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.
*/
const CircularProgress = React.forwardRef(function CircularProgress(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiCircularProgress' });
const {
className,
color = 'primary',
disableShrink = false,
size = 40,
style,
thickness = 3.6,
value = 0,
variant = 'indeterminate',
...other
} = props;
const ownerState = {
...props,
color,
disableShrink,
size,
thickness,
value,
variant,
};
const classes = useUtilityClasses(ownerState);
const circleStyle = {};
const rootStyle = {};
const rootProps = {};
if (variant === 'determinate') {
const circumference = 2 * Math.PI * ((SIZE - thickness) / 2);
circleStyle.strokeDasharray = circumference.toFixed(3);
rootProps['aria-valuenow'] = Math.round(value);
circleStyle.strokeDashoffset = `${(((100 - value) / 100) * circumference).toFixed(3)}px`;
rootStyle.transform = 'rotate(-90deg)';
}
return (
<CircularProgressRoot
className={clsx(classes.root, className)}
style={{ width: size, height: size, ...rootStyle, ...style }}
ownerState={ownerState}
ref={ref}
role="progressbar"
{...rootProps}
{...other}
>
<CircularProgressSVG
className={classes.svg}
ownerState={ownerState}
viewBox={`${SIZE / 2} ${SIZE / 2} ${SIZE} ${SIZE}`}
>
<CircularProgressCircle
className={classes.circle}
style={circleStyle}
ownerState={ownerState}
cx={SIZE}
cy={SIZE}
r={(SIZE - thickness) / 2}
fill="none"
strokeWidth={thickness}
/>
</CircularProgressSVG>
</CircularProgressRoot>
);
});
CircularProgress.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['inherit', 'primary', 'secondary', 'error', 'info', 'success', 'warning']),
PropTypes.string,
]),
/**
* If `true`, the shrink animation is disabled.
* This only works if variant is `indeterminate`.
* @default false
*/
disableShrink: chainPropTypes(PropTypes.bool, (props) => {
if (props.disableShrink && props.variant && props.variant !== 'indeterminate') {
return new Error(
'MUI: You have provided the `disableShrink` prop ' +
'with a variant other than `indeterminate`. This will have no effect.',
);
}
return null;
}),
/**
* The size of the component.
* If using a number, the pixel unit is assumed.
* If using a string, you need to provide the CSS unit, for example '3rem'.
* @default 40
*/
size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* @ignore
*/
style: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The thickness of the circle.
* @default 3.6
*/
thickness: PropTypes.number,
/**
* The value of the progress indicator for the determinate variant.
* Value between 0 and 100.
* @default 0
*/
value: PropTypes.number,
/**
* The variant to use.
* Use indeterminate when there is no progress value.
* @default 'indeterminate'
*/
variant: PropTypes.oneOf(['determinate', 'indeterminate']),
};
export default CircularProgress;