UNPKG

3.77 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import React from 'react';
13import { View, Animated, Platform, } from 'react-native';
14import Color from 'color';
15const LinearProgress = (_a) => {
16 var _b, _c;
17 var { value = 0, variant = 'indeterminate', color = 'secondary', style, theme, trackColor } = _a, props = __rest(_a, ["value", "variant", "color", "style", "theme", "trackColor"]);
18 const [width, setWidth] = React.useState(0);
19 const { current: animation } = React.useRef(new Animated.Value(0));
20 const intermediate = React.useRef();
21 const startAnimation = React.useCallback(() => {
22 if (variant === 'indeterminate') {
23 intermediate.current = Animated.timing(animation, {
24 duration: 2000,
25 toValue: 1,
26 useNativeDriver: true,
27 isInteraction: false,
28 });
29 animation.setValue(0);
30 Animated.loop(intermediate.current).start();
31 }
32 else {
33 Animated.timing(animation, {
34 duration: 1000,
35 toValue: value || 0,
36 useNativeDriver: Platform.OS !== 'web',
37 isInteraction: false,
38 }).start();
39 }
40 }, [animation, variant, value]);
41 const tintColor = color === 'secondary' || color === 'primary'
42 ? (_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b[color]
43 : Color(color).rgb().string() || ((_c = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _c === void 0 ? void 0 : _c.secondary);
44 const trackTintColor = trackColor || Color(tintColor).alpha(0.4).rgb().string();
45 React.useEffect(() => {
46 startAnimation();
47 }, [startAnimation, value]);
48 return (<View accessible accessibilityRole="progressbar" accessibilityValue={{
49 now: value,
50 min: 0,
51 max: 1,
52 }} {...props} onLayout={(e) => {
53 setWidth(e.nativeEvent.layout.width);
54 }} style={[
55 {
56 height: 4,
57 overflow: 'hidden',
58 width: '100%',
59 backgroundColor: trackTintColor,
60 },
61 style,
62 ]}>
63 <Animated.View style={{
64 transform: [
65 {
66 translateX: animation.interpolate(variant === 'indeterminate'
67 ? {
68 inputRange: [0, 1],
69 outputRange: [-width, 0.5 * width],
70 }
71 : {
72 inputRange: [0, 1],
73 outputRange: [-0.5 * width, 0],
74 }),
75 },
76 {
77 scaleX: animation.interpolate(variant === 'indeterminate'
78 ? {
79 inputRange: [0, 0.5, 1],
80 outputRange: [0.0001, 1, 0.001],
81 }
82 : {
83 inputRange: [0, 1],
84 outputRange: [0.0001, 1],
85 }),
86 },
87 ],
88 backgroundColor: tintColor,
89 flex: 1,
90 }}/>
91 </View>);
92};
93export default LinearProgress;
94
\No newline at end of file