1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _reactNative = require("react-native");
|
11 |
|
12 | var _color = _interopRequireDefault(require("color"));
|
13 |
|
14 | var _theming = require("../core/theming");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
19 |
|
20 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
21 |
|
22 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
23 |
|
24 | const INDETERMINATE_DURATION = 2000;
|
25 | const INDETERMINATE_MAX_WIDTH = 0.6;
|
26 | const {
|
27 | isRTL
|
28 | } = _reactNative.I18nManager;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | const ProgressBar = _ref => {
|
50 | let {
|
51 | color,
|
52 | indeterminate,
|
53 | style,
|
54 | progress = 0,
|
55 | visible = true,
|
56 | theme,
|
57 | ...rest
|
58 | } = _ref;
|
59 | const {
|
60 | current: timer
|
61 | } = React.useRef(new _reactNative.Animated.Value(0));
|
62 | const {
|
63 | current: fade
|
64 | } = React.useRef(new _reactNative.Animated.Value(0));
|
65 | const [width, setWidth] = React.useState(0);
|
66 | const [prevWidth, setPrevWidth] = React.useState(0);
|
67 | const indeterminateAnimation = React.useRef(null);
|
68 | const {
|
69 | scale
|
70 | } = theme.animation;
|
71 | const startAnimation = React.useCallback(() => {
|
72 |
|
73 | _reactNative.Animated.timing(fade, {
|
74 | duration: 200 * scale,
|
75 | toValue: 1,
|
76 | useNativeDriver: true,
|
77 | isInteraction: false
|
78 | }).start();
|
79 |
|
80 |
|
81 | if (indeterminate) {
|
82 | if (!indeterminateAnimation.current) {
|
83 | indeterminateAnimation.current = _reactNative.Animated.timing(timer, {
|
84 | duration: INDETERMINATE_DURATION,
|
85 | toValue: 1,
|
86 |
|
87 | useNativeDriver: _reactNative.Platform.OS !== 'web',
|
88 | isInteraction: false
|
89 | });
|
90 | }
|
91 |
|
92 |
|
93 | timer.setValue(0);
|
94 |
|
95 | _reactNative.Animated.loop(indeterminateAnimation.current).start();
|
96 | } else {
|
97 | _reactNative.Animated.timing(timer, {
|
98 | duration: 200 * scale,
|
99 | toValue: progress ? progress : 0,
|
100 | useNativeDriver: true,
|
101 | isInteraction: false
|
102 | }).start();
|
103 | }
|
104 | }, [scale, timer, progress, indeterminate, fade]);
|
105 | const stopAnimation = React.useCallback(() => {
|
106 |
|
107 | if (indeterminateAnimation.current) {
|
108 | indeterminateAnimation.current.stop();
|
109 | }
|
110 |
|
111 | _reactNative.Animated.timing(fade, {
|
112 | duration: 200 * scale,
|
113 | toValue: 0,
|
114 | useNativeDriver: true,
|
115 | isInteraction: false
|
116 | }).start();
|
117 | }, [fade, scale]);
|
118 | React.useEffect(() => {
|
119 | if (visible) startAnimation();else stopAnimation();
|
120 | }, [visible, startAnimation, stopAnimation]);
|
121 | React.useEffect(() => {
|
122 |
|
123 | if (visible && prevWidth === 0) {
|
124 | startAnimation();
|
125 | }
|
126 | }, [prevWidth, startAnimation, visible]);
|
127 |
|
128 | const onLayout = event => {
|
129 | setPrevWidth(width);
|
130 | setWidth(event.nativeEvent.layout.width);
|
131 | };
|
132 |
|
133 | const tintColor = color || theme.colors.primary;
|
134 | const trackTintColor = (0, _color.default)(tintColor).alpha(0.38).rgb().string();
|
135 | return React.createElement(_reactNative.View, _extends({
|
136 | onLayout: onLayout
|
137 | }, rest, {
|
138 | accessible: true,
|
139 | accessibilityRole: "progressbar",
|
140 | accessibilityState: {
|
141 | busy: visible
|
142 | },
|
143 | accessibilityValue: indeterminate ? {} : {
|
144 | min: 0,
|
145 | max: 100,
|
146 | now: progress * 100
|
147 | }
|
148 | }), React.createElement(_reactNative.Animated.View, {
|
149 | style: [styles.container, {
|
150 | backgroundColor: trackTintColor,
|
151 | opacity: fade
|
152 | }, style]
|
153 | }, width ? React.createElement(_reactNative.Animated.View, {
|
154 | style: [styles.progressBar, {
|
155 | width,
|
156 | backgroundColor: tintColor,
|
157 | transform: [{
|
158 | translateX: timer.interpolate(indeterminate ? {
|
159 | inputRange: [0, 0.5, 1],
|
160 | outputRange: [(isRTL ? 1 : -1) * 0.5 * width, (isRTL ? 1 : -1) * 0.5 * INDETERMINATE_MAX_WIDTH * width, (isRTL ? -1 : 1) * 0.7 * width]
|
161 | } : {
|
162 | inputRange: [0, 1],
|
163 | outputRange: [(isRTL ? 1 : -1) * 0.5 * width, 0]
|
164 | })
|
165 | }, {
|
166 |
|
167 | scaleX: timer.interpolate(indeterminate ? {
|
168 | inputRange: [0, 0.5, 1],
|
169 | outputRange: [0.0001, INDETERMINATE_MAX_WIDTH, 0.0001]
|
170 | } : {
|
171 | inputRange: [0, 1],
|
172 | outputRange: [0.0001, 1]
|
173 | })
|
174 | }]
|
175 | }]
|
176 | }) : null));
|
177 | };
|
178 |
|
179 | const styles = _reactNative.StyleSheet.create({
|
180 | container: {
|
181 | height: 4,
|
182 | overflow: 'hidden'
|
183 | },
|
184 | progressBar: {
|
185 | flex: 1
|
186 | }
|
187 | });
|
188 |
|
189 | var _default = (0, _theming.withTheme)(ProgressBar);
|
190 |
|
191 | exports.default = _default;
|
192 |
|
\ | No newline at end of file |