UNPKG

6.39 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() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
19
20function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 = ({
50 color,
51 indeterminate,
52 style,
53 progress = 0,
54 visible = true,
55 theme,
56 ...rest
57}) => {
58 const {
59 current: timer
60 } = React.useRef(new _reactNative.Animated.Value(0));
61 const {
62 current: fade
63 } = React.useRef(new _reactNative.Animated.Value(0));
64 const [width, setWidth] = React.useState(0);
65 const [prevWidth, setPrevWidth] = React.useState(0);
66 const indeterminateAnimation = React.useRef(null);
67 const {
68 scale
69 } = theme.animation;
70 const startAnimation = React.useCallback(() => {
71 // Show progress bar
72 _reactNative.Animated.timing(fade, {
73 duration: 200 * scale,
74 toValue: 1,
75 useNativeDriver: true,
76 isInteraction: false
77 }).start(); // Animate progress bar
78
79
80 if (indeterminate) {
81 if (!indeterminateAnimation.current) {
82 indeterminateAnimation.current = _reactNative.Animated.timing(timer, {
83 duration: INDETERMINATE_DURATION,
84 toValue: 1,
85 // Animated.loop does not work if useNativeDriver is true on web
86 useNativeDriver: _reactNative.Platform.OS !== 'web',
87 isInteraction: false
88 });
89 } // Reset timer to the beginning
90
91
92 timer.setValue(0);
93
94 _reactNative.Animated.loop(indeterminateAnimation.current).start();
95 } else {
96 _reactNative.Animated.timing(timer, {
97 duration: 200 * scale,
98 toValue: progress ? progress : 0,
99 useNativeDriver: true,
100 isInteraction: false
101 }).start();
102 }
103 }, [scale, timer, progress, indeterminate, fade]);
104 const stopAnimation = React.useCallback(() => {
105 // Stop indeterminate animation
106 if (indeterminateAnimation.current) {
107 indeterminateAnimation.current.stop();
108 }
109
110 _reactNative.Animated.timing(fade, {
111 duration: 200 * scale,
112 toValue: 0,
113 useNativeDriver: true,
114 isInteraction: false
115 }).start();
116 }, [fade, scale]);
117 React.useEffect(() => {
118 if (visible) startAnimation();else stopAnimation();
119 }, [visible, startAnimation, stopAnimation]);
120 React.useEffect(() => {
121 // Start animation the very first time when previously the width was unclear
122 if (visible && prevWidth === 0) {
123 startAnimation();
124 }
125 }, [prevWidth, startAnimation, visible]);
126
127 const onLayout = event => {
128 setPrevWidth(width);
129 setWidth(event.nativeEvent.layout.width);
130 };
131
132 const tintColor = color || theme.colors.primary;
133 const trackTintColor = (0, _color.default)(tintColor).alpha(0.38).rgb().string();
134 return /*#__PURE__*/React.createElement(_reactNative.View, _extends({
135 onLayout: onLayout
136 }, rest, {
137 accessible: true,
138 accessibilityRole: "progressbar",
139 accessibilityState: {
140 busy: visible
141 },
142 accessibilityValue: indeterminate ? {} : {
143 min: 0,
144 max: 100,
145 now: progress * 100
146 }
147 }), /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
148 style: [styles.container, {
149 backgroundColor: trackTintColor,
150 opacity: fade
151 }, style]
152 }, /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
153 style: [styles.progressBar, {
154 backgroundColor: tintColor,
155 width,
156 transform: [{
157 translateX: timer.interpolate(indeterminate ? {
158 inputRange: [0, 0.5, 1],
159 outputRange: [(isRTL ? 1 : -1) * 0.5 * width, (isRTL ? 1 : -1) * 0.5 * INDETERMINATE_MAX_WIDTH * width, (isRTL ? -1 : 1) * 0.7 * width]
160 } : {
161 inputRange: [0, 1],
162 outputRange: [(isRTL ? 1 : -1) * 0.5 * width, 0]
163 })
164 }, {
165 // Workaround for workaround for https://github.com/facebook/react-native/issues/6278
166 scaleX: timer.interpolate(indeterminate ? {
167 inputRange: [0, 0.5, 1],
168 outputRange: [0.0001, INDETERMINATE_MAX_WIDTH, 0.0001]
169 } : {
170 inputRange: [0, 1],
171 outputRange: [0.0001, 1]
172 })
173 }]
174 }]
175 })));
176};
177
178const styles = _reactNative.StyleSheet.create({
179 container: {
180 height: 4,
181 overflow: 'hidden'
182 },
183 progressBar: {
184 flex: 1
185 }
186});
187
188var _default = (0, _theming.withTheme)(ProgressBar);
189
190exports.default = _default;
191//# sourceMappingURL=ProgressBar.js.map
\No newline at end of file