UNPKG

6.63 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _reactNative = require("react-native");
11
12var _color = _interopRequireDefault(require("color"));
13
14var _theming = require("../core/theming");
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _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
20function _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
22function _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
24const INDETERMINATE_DURATION = 2000;
25const INDETERMINATE_MAX_WIDTH = 0.6;
26const {
27 isRTL
28} = _reactNative.I18nManager;
29/**
30 * Progress bar is an indicator used to present progress of some activity in the app.
31 *
32 * <div class="screenshots">
33 * <img src="screenshots/progress-bar.png" />
34 * </div>
35 *
36 * ## Usage
37 * ```js
38 * import * as React from 'react';
39 * import { ProgressBar, Colors } from 'react-native-paper';
40 *
41 * const MyComponent = () => (
42 * <ProgressBar progress={0.5} color={Colors.red800} />
43 * );
44 *
45 * export default MyComponent;
46 * ```
47 */
48
49const 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 // Show progress bar
73 _reactNative.Animated.timing(fade, {
74 duration: 200 * scale,
75 toValue: 1,
76 useNativeDriver: true,
77 isInteraction: false
78 }).start(); // Animate progress bar
79
80
81 if (indeterminate) {
82 if (!indeterminateAnimation.current) {
83 indeterminateAnimation.current = _reactNative.Animated.timing(timer, {
84 duration: INDETERMINATE_DURATION,
85 toValue: 1,
86 // Animated.loop does not work if useNativeDriver is true on web
87 useNativeDriver: _reactNative.Platform.OS !== 'web',
88 isInteraction: false
89 });
90 } // Reset timer to the beginning
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 // Stop indeterminate animation
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 // Start animation the very first time when previously the width was unclear
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 /*#__PURE__*/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 }), /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
149 style: [styles.container, {
150 backgroundColor: trackTintColor,
151 opacity: fade
152 }, style]
153 }, width ? /*#__PURE__*/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 // Workaround for workaround for https://github.com/facebook/react-native/issues/6278
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
179const styles = _reactNative.StyleSheet.create({
180 container: {
181 height: 4,
182 overflow: 'hidden'
183 },
184 progressBar: {
185 flex: 1
186 }
187});
188
189var _default = (0, _theming.withTheme)(ProgressBar);
190
191exports.default = _default;
192//# sourceMappingURL=ProgressBar.js.map
\No newline at end of file