-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Expand file tree
/
Copy pathCircularProgress.js
More file actions
192 lines (167 loc) · 5.04 KB
/
CircularProgress.js
File metadata and controls
192 lines (167 loc) · 5.04 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
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
import React, {Component, PropTypes} from 'react';
import autoPrefix from '../utils/autoPrefix';
import transitions from '../styles/transitions';
const THICKNESS = 3.5;
function getRelativeValue(value, min, max) {
const clampedValue = Math.min(Math.max(min, value), max);
return clampedValue / (max - min);
}
function getArcLength(fraction, props) {
return fraction * Math.PI * (props.size - THICKNESS);
}
function getStyles(props, context) {
const {
max,
min,
size,
value,
} = props;
const {baseTheme: {palette}} = context.muiTheme;
const styles = {
root: {
position: 'relative',
display: 'inline-block',
width: size,
height: size,
},
wrapper: {
width: size,
height: size,
display: 'inline-block',
transition: transitions.create('transform', '20s', null, 'linear'),
transitionTimingFunction: 'linear',
},
svg: {
width: size,
height: size,
position: 'relative',
},
path: {
stroke: props.color || palette.primary1Color,
strokeLinecap: 'square',
transition: transitions.create('all', '1.5s', null, 'ease-in-out'),
},
};
if (props.mode === 'determinate') {
const relVal = getRelativeValue(value, min, max);
styles.path.transition = transitions.create('all', '0.3s', null, 'linear');
styles.path.strokeDasharray = `${getArcLength(relVal, props)}, ${getArcLength(1, props)}`;
}
return styles;
}
class CircularProgress extends Component {
static propTypes = {
/**
* Override the progress's color.
*/
color: PropTypes.string,
/**
* Style for inner wrapper div.
*/
innerStyle: PropTypes.object,
/**
* The max value of progress, only works in determinate mode.
*/
max: PropTypes.number,
/**
* The min value of progress, only works in determinate mode.
*/
min: PropTypes.number,
/**
* The mode of show your progress, indeterminate
* for when there is no value for progress.
*/
mode: PropTypes.oneOf(['determinate', 'indeterminate']),
/**
* The diameter of the progress in pixels.
*/
size: PropTypes.number,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* The value of progress, only works in determinate mode.
*/
value: PropTypes.number,
};
static defaultProps = {
mode: 'indeterminate',
value: 0,
min: 0,
max: 100,
size: 40,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
componentDidMount() {
this.scalePath(this.refs.path);
this.rotateWrapper(this.refs.wrapper);
}
componentWillUnmount() {
clearTimeout(this.scalePathTimer);
clearTimeout(this.rotateWrapperTimer);
}
scalePath(path, step = 0) {
if (this.props.mode !== 'indeterminate') return;
step %= 3;
if (step === 0) {
path.style.strokeDasharray = `${getArcLength(0, this.props)}, ${getArcLength(1, this.props)}`;
path.style.strokeDashoffset = 0;
path.style.transitionDuration = '0ms';
} else if (step === 1) {
path.style.strokeDasharray = `${getArcLength(0.7, this.props)}, ${getArcLength(1, this.props)}`;
path.style.strokeDashoffset = getArcLength(-0.3, this.props);
path.style.transitionDuration = '750ms';
} else {
path.style.strokeDasharray = `${getArcLength(0.7, this.props)}, ${getArcLength(1, this.props)}`;
path.style.strokeDashoffset = getArcLength(-1, this.props);
path.style.transitionDuration = '850ms';
}
this.scalePathTimer = setTimeout(() => this.scalePath(path, step + 1), step ? 750 : 250);
}
rotateWrapper(wrapper) {
if (this.props.mode !== 'indeterminate') return;
autoPrefix.set(wrapper.style, 'transform', 'rotate(0deg)');
autoPrefix.set(wrapper.style, 'transitionDuration', '0ms');
setTimeout(() => {
autoPrefix.set(wrapper.style, 'transform', 'rotate(1800deg)');
autoPrefix.set(wrapper.style, 'transitionDuration', '10s');
autoPrefix.set(wrapper.style, 'transitionTimingFunction', 'linear');
}, 50);
this.rotateWrapperTimer = setTimeout(() => this.rotateWrapper(wrapper), 10050);
}
render() {
const {
style,
innerStyle,
size,
...other,
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
return (
<div {...other} style={prepareStyles(Object.assign(styles.root, style))} >
<div ref="wrapper" style={prepareStyles(Object.assign(styles.wrapper, innerStyle))} >
<svg
viewBox={`0 0 ${size} ${size}`}
style={prepareStyles(styles.svg)}
>
<circle
ref="path"
style={prepareStyles(styles.path)}
cx={size / 2}
cy={size / 2}
r={(size - THICKNESS) / 2}
fill="none"
strokeWidth={THICKNESS}
strokeMiterlimit="20"
/>
</svg>
</div>
</div>
);
}
}
export default CircularProgress;