forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivityIndicator.tsx
286 lines (256 loc) · 7.1 KB
/
ActivityIndicator.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
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
import * as React from 'react';
import {
Animated,
Easing,
Platform,
StyleProp,
StyleSheet,
View,
ViewStyle,
} from 'react-native';
import { withTheme } from '../core/theming';
import { Theme } from '../types';
type Props = React.ComponentPropsWithRef<typeof View> & {
/**
* Whether to show the indicator or hide it.
*/
animating?: boolean;
/**
* The color of the spinner.
*/
color?: string;
/**
* Size of the indicator.
*/
size?: 'small' | 'large' | number;
/**
* Whether the indicator should hide when not animating.
*/
hidesWhenStopped?: boolean;
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme: Theme;
};
type State = {
timer: Animated.Value;
fade: Animated.Value;
};
const DURATION = 2400;
/**
* Activity indicator is used to present progress of some activity in the app.
* It can be used as a drop-in for the ActivityIndicator shipped with React Native.
*
* <div class="screenshots">
* <img src="screenshots/activity-indicator.gif" style="width: 100px;" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { ActivityIndicator, Colors } from 'react-native-paper';
*
* const MyComponent = () => (
* <ActivityIndicator animating={true} color={Colors.red800} />
* );
*
* export default MyComponent;
* ```
*/
class ActivityIndicator extends React.Component<Props, State> {
static defaultProps: Partial<Props> = {
animating: true,
size: 'small',
hidesWhenStopped: true,
};
state = {
timer: new Animated.Value(0),
fade: new Animated.Value(
!this.props.animating && this.props.hidesWhenStopped ? 0 : 1
),
};
rotation: Animated.CompositeAnimation | undefined = undefined;
componentDidMount() {
const { animating } = this.props;
const { timer } = this.state;
// Circular animation in loop
this.rotation = Animated.timing(timer, {
duration: DURATION,
easing: Easing.linear,
// Animated.loop does not work if useNativeDriver is true on web
useNativeDriver: Platform.OS !== 'web',
toValue: 1,
isInteraction: false,
});
if (animating) {
this.startRotation();
}
}
componentDidUpdate(prevProps: Props) {
const {
animating,
hidesWhenStopped,
theme: {
animation: { scale },
},
} = this.props;
const { fade } = this.state;
if (animating !== prevProps.animating) {
if (animating) {
this.startRotation();
} else if (hidesWhenStopped) {
// Hide indicator first and then stop rotation
Animated.timing(fade, {
duration: 200 * scale,
toValue: 0,
useNativeDriver: true,
isInteraction: false,
}).start(this.stopRotation.bind(this));
} else {
this.stopRotation();
}
}
}
private startRotation = () => {
const { fade, timer } = this.state;
const { scale } = this.props.theme.animation;
// Show indicator
Animated.timing(fade, {
duration: 200 * scale,
toValue: 1,
isInteraction: false,
useNativeDriver: true,
}).start();
// Circular animation in loop
if (this.rotation) {
timer.setValue(0);
// $FlowFixMe
Animated.loop(this.rotation).start();
}
};
private stopRotation = () => {
if (this.rotation) {
this.rotation.stop();
}
};
render() {
const { fade, timer } = this.state;
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
animating,
color: indicatorColor,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
hidesWhenStopped,
size: indicatorSize,
style,
theme,
...rest
} = this.props;
const color = indicatorColor || theme.colors.primary;
const size =
typeof indicatorSize === 'string'
? indicatorSize === 'small'
? 24
: 48
: indicatorSize
? indicatorSize
: 24;
const frames = (60 * DURATION) / 1000;
const easing = Easing.bezier(0.4, 0.0, 0.7, 1.0);
const containerStyle = {
width: size,
height: size / 2,
overflow: 'hidden',
};
return (
<View style={[styles.container, style]} {...rest}>
<Animated.View style={[{ width: size, height: size, opacity: fade }]}>
{[0, 1].map(index => {
// Thanks to https://github.com/n4kz/react-native-indicators for the great work
const inputRange = Array.from(
new Array(frames),
(_, frameIndex) => frameIndex / (frames - 1)
);
const outputRange = Array.from(
new Array(frames),
(_, frameIndex) => {
let progress = (2 * frameIndex) / (frames - 1);
const rotation = index ? +(360 - 15) : -(180 - 15);
if (progress > 1.0) {
progress = 2.0 - progress;
}
const direction = index ? -1 : +1;
return `${direction * (180 - 30) * easing(progress) +
rotation}deg`;
}
);
const layerStyle = {
width: size,
height: size,
transform: [
{
rotate: timer.interpolate({
inputRange: [0, 1],
outputRange: [
`${0 + 30 + 15}deg`,
`${2 * 360 + 30 + 15}deg`,
],
}),
},
],
};
const viewportStyle = {
width: size,
height: size,
transform: [
{
translateY: index ? -size / 2 : 0,
},
{
rotate: timer.interpolate({ inputRange, outputRange }),
},
],
};
const offsetStyle = index ? { top: size / 2 } : null;
const lineStyle = {
width: size,
height: size,
borderColor: color,
borderWidth: size / 10,
borderRadius: size / 2,
};
return (
<Animated.View key={index} style={[styles.layer]}>
<Animated.View style={layerStyle}>
<Animated.View
style={[containerStyle, offsetStyle]}
collapsable={false}
>
<Animated.View style={viewportStyle}>
<Animated.View style={containerStyle} collapsable={false}>
<Animated.View style={lineStyle} />
</Animated.View>
</Animated.View>
</Animated.View>
</Animated.View>
</Animated.View>
);
})}
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
},
layer: {
...StyleSheet.absoluteFillObject,
justifyContent: 'center',
alignItems: 'center',
},
});
export default withTheme(ActivityIndicator);