1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var _objectWithoutPropertiesLoose = require('@babel/runtime/helpers/objectWithoutPropertiesLoose');
|
6 | var _extends = require('@babel/runtime/helpers/extends');
|
7 | var React = require('react');
|
8 | var reactUse = require('react-use');
|
9 | var hoistNonReactStatics = require('hoist-non-react-statics');
|
10 |
|
11 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
12 |
|
13 | var _objectWithoutPropertiesLoose__default = _interopDefaultLegacy(_objectWithoutPropertiesLoose);
|
14 | var _extends__default = _interopDefaultLegacy(_extends);
|
15 | var hoistNonReactStatics__default = _interopDefaultLegacy(hoistNonReactStatics);
|
16 |
|
17 | var clamp = function clamp(num, lower, upper) {
|
18 | num = num <= upper ? num : upper;
|
19 | num = num >= lower ? num : lower;
|
20 | return num;
|
21 | };
|
22 |
|
23 | var increment = function increment(progress) {
|
24 | var amount = 0;
|
25 |
|
26 | if (progress >= 0 && progress < 0.2) {
|
27 | amount = 0.1;
|
28 | } else if (progress >= 0.2 && progress < 0.5) {
|
29 | amount = 0.04;
|
30 | } else if (progress >= 0.5 && progress < 0.8) {
|
31 | amount = 0.02;
|
32 | } else if (progress >= 0.8 && progress < 0.99) {
|
33 | amount = 0.005;
|
34 | }
|
35 |
|
36 | return clamp(progress + amount, 0, 0.994);
|
37 | };
|
38 |
|
39 | var isRunning = false;
|
40 | var pending = [];
|
41 |
|
42 | var next = function next() {
|
43 | isRunning = true;
|
44 | var cb = pending.shift();
|
45 |
|
46 | if (cb) {
|
47 | return cb(next);
|
48 | }
|
49 |
|
50 | isRunning = false;
|
51 | };
|
52 |
|
53 | var clear = function clear() {
|
54 | isRunning = false;
|
55 | pending = [];
|
56 | };
|
57 | var queue = function queue(cb) {
|
58 | pending.push(cb);
|
59 |
|
60 | if (!isRunning && pending.length === 1) {
|
61 | next();
|
62 | }
|
63 | };
|
64 |
|
65 | var handle;
|
66 | var cancel = function cancel() {
|
67 | if (handle) {
|
68 | window.cancelAnimationFrame(handle);
|
69 | }
|
70 | };
|
71 | var timeout = function timeout(callback, delay) {
|
72 | var deltaTime;
|
73 | var start;
|
74 |
|
75 | var frame = function frame(time) {
|
76 | start = start || time;
|
77 | deltaTime = time - start;
|
78 |
|
79 | if (deltaTime > delay) {
|
80 | callback();
|
81 | return;
|
82 | }
|
83 |
|
84 | handle = window.requestAnimationFrame(frame);
|
85 | };
|
86 |
|
87 | handle = window.requestAnimationFrame(frame);
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 | var noop = function noop() {
|
93 | return undefined;
|
94 | };
|
95 |
|
96 | var initialState = {
|
97 | isFinished: false,
|
98 | progress: 0,
|
99 | sideEffect: noop
|
100 | };
|
101 |
|
102 | var cleanup = function cleanup() {
|
103 | cancel();
|
104 | clear();
|
105 | };
|
106 |
|
107 | var useNProgress = function useNProgress(_temp) {
|
108 | var _ref = _temp === void 0 ? {} : _temp,
|
109 | _ref$animationDuratio = _ref.animationDuration,
|
110 | animationDuration = _ref$animationDuratio === void 0 ? 200 : _ref$animationDuratio,
|
111 | _ref$incrementDuratio = _ref.incrementDuration,
|
112 | incrementDuration = _ref$incrementDuratio === void 0 ? 800 : _ref$incrementDuratio,
|
113 | _ref$isAnimating = _ref.isAnimating,
|
114 | isAnimating = _ref$isAnimating === void 0 ? false : _ref$isAnimating,
|
115 | _ref$minimum = _ref.minimum,
|
116 | minimum = _ref$minimum === void 0 ? 0.08 : _ref$minimum;
|
117 |
|
118 | var _useGetSetState = reactUse.useGetSetState(initialState),
|
119 | get = _useGetSetState[0],
|
120 | setState = _useGetSetState[1];
|
121 |
|
122 | var set = React.useCallback(function (n) {
|
123 | n = clamp(n, minimum, 1);
|
124 |
|
125 | if (n === 1) {
|
126 | cleanup();
|
127 | queue(function (next) {
|
128 | setState({
|
129 | progress: n,
|
130 | sideEffect: function sideEffect() {
|
131 | return timeout(next, animationDuration);
|
132 | }
|
133 | });
|
134 | });
|
135 | queue(function () {
|
136 | setState({
|
137 | isFinished: true,
|
138 | sideEffect: cleanup
|
139 | });
|
140 | });
|
141 | return;
|
142 | }
|
143 |
|
144 | queue(function (next) {
|
145 | setState({
|
146 | progress: n,
|
147 | sideEffect: function sideEffect() {
|
148 | return timeout(next, animationDuration);
|
149 | }
|
150 | });
|
151 | });
|
152 | }, [animationDuration, minimum, setState]);
|
153 | var trickle = React.useCallback(function () {
|
154 | set(increment(get().progress));
|
155 | }, [get, set]);
|
156 | var start = React.useCallback(function () {
|
157 | var work = function work() {
|
158 | trickle();
|
159 | queue(function (next) {
|
160 | timeout(function () {
|
161 | work();
|
162 | next();
|
163 | }, incrementDuration);
|
164 | });
|
165 | };
|
166 |
|
167 | work();
|
168 | }, [incrementDuration, trickle]);
|
169 | var savedTrickle = React.useRef(noop);
|
170 | var sideEffect = get().sideEffect;
|
171 | React.useEffect(function () {
|
172 | savedTrickle.current = trickle;
|
173 | });
|
174 | reactUse.useEffectOnce(function () {
|
175 | if (isAnimating) {
|
176 | start();
|
177 | }
|
178 |
|
179 | return cleanup;
|
180 | });
|
181 | reactUse.useUpdateEffect(function () {
|
182 | get().sideEffect();
|
183 | }, [get, sideEffect]);
|
184 | reactUse.useUpdateEffect(function () {
|
185 | if (!isAnimating) {
|
186 | set(1);
|
187 | } else {
|
188 | setState(_extends__default['default']({}, initialState, {
|
189 | sideEffect: start
|
190 | }));
|
191 | }
|
192 | }, [isAnimating, set, setState, start]);
|
193 | return {
|
194 | animationDuration: animationDuration,
|
195 | isFinished: get().isFinished,
|
196 | progress: get().progress
|
197 | };
|
198 | };
|
199 |
|
200 | var NProgress = function NProgress(_ref) {
|
201 | var children = _ref.children,
|
202 | restProps = _objectWithoutPropertiesLoose__default['default'](_ref, ["children"]);
|
203 |
|
204 | var renderProps = useNProgress(restProps);
|
205 | return children(renderProps);
|
206 | };
|
207 |
|
208 | function withNProgress(BaseComponent) {
|
209 | var WithNProgress = function WithNProgress(props) {
|
210 | var hookProps = useNProgress(props);
|
211 | return React.createElement(BaseComponent, Object.assign({}, props, hookProps));
|
212 | };
|
213 |
|
214 | hoistNonReactStatics__default['default'](WithNProgress, BaseComponent);
|
215 | return WithNProgress;
|
216 | }
|
217 |
|
218 | exports.NProgress = NProgress;
|
219 | exports.useNProgress = useNProgress;
|
220 | exports.withNProgress = withNProgress;
|
221 |
|