1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
|
8 | var _inheritsLoose = _interopDefault(require('@babel/runtime/helpers/inheritsLoose'));
|
9 | var _assertThisInitialized = _interopDefault(require('@babel/runtime/helpers/assertThisInitialized'));
|
10 | var _objectWithoutPropertiesLoose = _interopDefault(require('@babel/runtime/helpers/objectWithoutPropertiesLoose'));
|
11 | var React = require('react');
|
12 | var React__default = _interopDefault(React);
|
13 |
|
14 | var bugfixes = undefined;
|
15 | var applyAnimatedValues = undefined;
|
16 | var colorNames = [];
|
17 | var requestFrame = function requestFrame(cb) {
|
18 | return typeof window !== 'undefined' && window.requestAnimationFrame(cb);
|
19 | };
|
20 | var cancelFrame = function cancelFrame(cb) {
|
21 | return typeof window !== 'undefined' && window.cancelAnimationFrame(cb);
|
22 | };
|
23 | var interpolation = undefined;
|
24 | var now = function now() {
|
25 | return Date.now();
|
26 | };
|
27 | var defaultElement = undefined;
|
28 | var createAnimatedStyle = undefined;
|
29 | var injectApplyAnimatedValues = function injectApplyAnimatedValues(fn, transform) {
|
30 | return applyAnimatedValues = {
|
31 | fn: fn,
|
32 | transform: transform
|
33 | };
|
34 | };
|
35 | var injectColorNames = function injectColorNames(names) {
|
36 | return colorNames = names;
|
37 | };
|
38 | var injectBugfixes = function injectBugfixes(fn) {
|
39 | return bugfixes = fn;
|
40 | };
|
41 | var injectInterpolation = function injectInterpolation(cls) {
|
42 | return interpolation = cls;
|
43 | };
|
44 | var injectFrame = function injectFrame(raf, caf) {
|
45 | var _ref;
|
46 |
|
47 | return _ref = [raf, caf], requestFrame = _ref[0], cancelFrame = _ref[1], _ref;
|
48 | };
|
49 | var injectNow = function injectNow(nowFn) {
|
50 | return now = nowFn;
|
51 | };
|
52 | var injectDefaultElement = function injectDefaultElement(el) {
|
53 | return defaultElement = el;
|
54 | };
|
55 | var injectCreateAnimatedStyle = function injectCreateAnimatedStyle(factory) {
|
56 | return createAnimatedStyle = factory;
|
57 | };
|
58 |
|
59 | var Globals = Object.freeze({
|
60 | get bugfixes () { return bugfixes; },
|
61 | get applyAnimatedValues () { return applyAnimatedValues; },
|
62 | get colorNames () { return colorNames; },
|
63 | get requestFrame () { return requestFrame; },
|
64 | get cancelFrame () { return cancelFrame; },
|
65 | get interpolation () { return interpolation; },
|
66 | get now () { return now; },
|
67 | get defaultElement () { return defaultElement; },
|
68 | get createAnimatedStyle () { return createAnimatedStyle; },
|
69 | injectApplyAnimatedValues: injectApplyAnimatedValues,
|
70 | injectColorNames: injectColorNames,
|
71 | injectBugfixes: injectBugfixes,
|
72 | injectInterpolation: injectInterpolation,
|
73 | injectFrame: injectFrame,
|
74 | injectNow: injectNow,
|
75 | injectDefaultElement: injectDefaultElement,
|
76 | injectCreateAnimatedStyle: injectCreateAnimatedStyle
|
77 | });
|
78 |
|
79 | var Animated =
|
80 |
|
81 | function () {
|
82 | function Animated() {}
|
83 |
|
84 | var _proto = Animated.prototype;
|
85 |
|
86 | _proto.attach = function attach() {};
|
87 |
|
88 | _proto.detach = function detach() {};
|
89 |
|
90 | _proto.getValue = function getValue() {};
|
91 |
|
92 | _proto.getAnimatedValue = function getAnimatedValue() {
|
93 | return this.getValue();
|
94 | };
|
95 |
|
96 | _proto.addChild = function addChild(child) {};
|
97 |
|
98 | _proto.removeChild = function removeChild(child) {};
|
99 |
|
100 | _proto.getChildren = function getChildren() {
|
101 | return [];
|
102 | };
|
103 |
|
104 | return Animated;
|
105 | }();
|
106 |
|
107 | var getValues = function getValues(object) {
|
108 | return Object.keys(object).map(function (k) {
|
109 | return object[k];
|
110 | });
|
111 | };
|
112 |
|
113 | var AnimatedWithChildren =
|
114 |
|
115 | function (_Animated) {
|
116 | _inheritsLoose(AnimatedWithChildren, _Animated);
|
117 |
|
118 | function AnimatedWithChildren() {
|
119 | var _this;
|
120 |
|
121 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
122 | args[_key] = arguments[_key];
|
123 | }
|
124 |
|
125 | _this = _Animated.call.apply(_Animated, [this].concat(args)) || this;
|
126 | _this.children = [];
|
127 |
|
128 | _this.getChildren = function () {
|
129 | return _this.children;
|
130 | };
|
131 |
|
132 | _this.getPayload = function (index) {
|
133 | if (index === void 0) {
|
134 | index = undefined;
|
135 | }
|
136 |
|
137 | return index !== void 0 && _this.payload ? _this.payload[index] : _this.payload || _assertThisInitialized(_assertThisInitialized(_this));
|
138 | };
|
139 |
|
140 | return _this;
|
141 | }
|
142 |
|
143 | var _proto = AnimatedWithChildren.prototype;
|
144 |
|
145 | _proto.addChild = function addChild(child) {
|
146 | if (this.children.length === 0) this.attach();
|
147 | this.children.push(child);
|
148 | };
|
149 |
|
150 | _proto.removeChild = function removeChild(child) {
|
151 | var index = this.children.indexOf(child);
|
152 | this.children.splice(index, 1);
|
153 | if (this.children.length === 0) this.detach();
|
154 | };
|
155 |
|
156 | return AnimatedWithChildren;
|
157 | }(Animated);
|
158 | var AnimatedArrayWithChildren =
|
159 |
|
160 | function (_AnimatedWithChildren) {
|
161 | _inheritsLoose(AnimatedArrayWithChildren, _AnimatedWithChildren);
|
162 |
|
163 | function AnimatedArrayWithChildren() {
|
164 | var _this2;
|
165 |
|
166 | for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
167 | args[_key2] = arguments[_key2];
|
168 | }
|
169 |
|
170 | _this2 = _AnimatedWithChildren.call.apply(_AnimatedWithChildren, [this].concat(args)) || this;
|
171 | _this2.payload = [];
|
172 |
|
173 | _this2.getAnimatedValue = function () {
|
174 | return _this2.getValue();
|
175 | };
|
176 |
|
177 | _this2.attach = function () {
|
178 | return _this2.payload.forEach(function (p) {
|
179 | return p instanceof Animated && p.addChild(_assertThisInitialized(_assertThisInitialized(_this2)));
|
180 | });
|
181 | };
|
182 |
|
183 | _this2.detach = function () {
|
184 | return _this2.payload.forEach(function (p) {
|
185 | return p instanceof Animated && p.removeChild(_assertThisInitialized(_assertThisInitialized(_this2)));
|
186 | });
|
187 | };
|
188 |
|
189 | return _this2;
|
190 | }
|
191 |
|
192 | return AnimatedArrayWithChildren;
|
193 | }(AnimatedWithChildren);
|
194 | var AnimatedObjectWithChildren =
|
195 |
|
196 | function (_AnimatedWithChildren2) {
|
197 | _inheritsLoose(AnimatedObjectWithChildren, _AnimatedWithChildren2);
|
198 |
|
199 | function AnimatedObjectWithChildren() {
|
200 | var _this3;
|
201 |
|
202 | for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
203 | args[_key3] = arguments[_key3];
|
204 | }
|
205 |
|
206 | _this3 = _AnimatedWithChildren2.call.apply(_AnimatedWithChildren2, [this].concat(args)) || this;
|
207 | _this3.payload = {};
|
208 |
|
209 | _this3.getAnimatedValue = function () {
|
210 | return _this3.getValue(true);
|
211 | };
|
212 |
|
213 | _this3.attach = function () {
|
214 | return getValues(_this3.payload).forEach(function (s) {
|
215 | return s instanceof Animated && s.addChild(_assertThisInitialized(_assertThisInitialized(_this3)));
|
216 | });
|
217 | };
|
218 |
|
219 | _this3.detach = function () {
|
220 | return getValues(_this3.payload).forEach(function (s) {
|
221 | return s instanceof Animated && s.removeChild(_assertThisInitialized(_assertThisInitialized(_this3)));
|
222 | });
|
223 | };
|
224 |
|
225 | return _this3;
|
226 | }
|
227 |
|
228 | var _proto2 = AnimatedObjectWithChildren.prototype;
|
229 |
|
230 | _proto2.getValue = function getValue(animated) {
|
231 | if (animated === void 0) {
|
232 | animated = false;
|
233 | }
|
234 |
|
235 | var payload = {};
|
236 |
|
237 | for (var key in this.payload) {
|
238 | var value = this.payload[key];
|
239 | if (animated && !(value instanceof Animated)) continue;
|
240 | payload[key] = value instanceof Animated ? value[animated ? 'getAnimatedValue' : 'getValue']() : value;
|
241 | }
|
242 |
|
243 | return payload;
|
244 | };
|
245 |
|
246 | return AnimatedObjectWithChildren;
|
247 | }(AnimatedWithChildren);
|
248 |
|
249 | var Interpolation$1 =
|
250 |
|
251 | function () {
|
252 | function Interpolation() {}
|
253 |
|
254 |
|
255 |
|
256 | Interpolation.create = function create(config, output, extra) {
|
257 | if (typeof config === 'function') return config;else if (interpolation && config.output && typeof config.output[0] === 'string') return interpolation(config);else if (Array.isArray(config)) return Interpolation.create({
|
258 | range: config,
|
259 | output: output,
|
260 | extrapolate: extra || 'extend'
|
261 | });
|
262 | var outputRange = config.output;
|
263 | var inputRange = config.range || [0, 1];
|
264 |
|
265 | var easing = config.easing || function (t) {
|
266 | return t;
|
267 | };
|
268 |
|
269 | var extrapolateLeft = 'extend';
|
270 | var map = config.map;
|
271 | if (config.extrapolateLeft !== undefined) extrapolateLeft = config.extrapolateLeft;else if (config.extrapolate !== undefined) extrapolateLeft = config.extrapolate;
|
272 | var extrapolateRight = 'extend';
|
273 | if (config.extrapolateRight !== undefined) extrapolateRight = config.extrapolateRight;else if (config.extrapolate !== undefined) extrapolateRight = config.extrapolate;
|
274 | return function (input) {
|
275 | var range = findRange(input, inputRange);
|
276 | return interpolate(input, inputRange[range], inputRange[range + 1], outputRange[range], outputRange[range + 1], easing, extrapolateLeft, extrapolateRight, map);
|
277 | };
|
278 | };
|
279 |
|
280 | return Interpolation;
|
281 | }();
|
282 |
|
283 | function interpolate(input, inputMin, inputMax, outputMin, outputMax, easing, extrapolateLeft, extrapolateRight, map) {
|
284 | var result = map ? map(input) : input;
|
285 |
|
286 | if (result < inputMin) {
|
287 | if (extrapolateLeft === 'identity') return result;else if (extrapolateLeft === 'clamp') result = inputMin;
|
288 | }
|
289 |
|
290 | if (result > inputMax) {
|
291 | if (extrapolateRight === 'identity') return result;else if (extrapolateRight === 'clamp') result = inputMax;
|
292 | }
|
293 |
|
294 | if (outputMin === outputMax) return outputMin;
|
295 | if (inputMin === inputMax) return input <= inputMin ? outputMin : outputMax;
|
296 |
|
297 | if (inputMin === -Infinity) result = -result;else if (inputMax === Infinity) result = result - inputMin;else result = (result - inputMin) / (inputMax - inputMin);
|
298 |
|
299 | result = easing(result);
|
300 |
|
301 | if (outputMin === -Infinity) result = -result;else if (outputMax === Infinity) result = result + outputMin;else result = result * (outputMax - outputMin) + outputMin;
|
302 | return result;
|
303 | }
|
304 |
|
305 | function findRange(input, inputRange) {
|
306 | for (var i = 1; i < inputRange.length - 1; ++i) {
|
307 | if (inputRange[i] >= input) break;
|
308 | }
|
309 |
|
310 | return i - 1;
|
311 | }
|
312 |
|
313 | var AnimatedInterpolation =
|
314 |
|
315 | function (_AnimatedArrayWithChi) {
|
316 | _inheritsLoose(AnimatedInterpolation, _AnimatedArrayWithChi);
|
317 |
|
318 | function AnimatedInterpolation(parents, _config, _arg) {
|
319 | var _this;
|
320 |
|
321 | _this = _AnimatedArrayWithChi.call(this) || this;
|
322 |
|
323 | _this.getValue = function () {
|
324 | var _this2;
|
325 |
|
326 | return (_this2 = _this).calc.apply(_this2, _this.payload.map(function (value) {
|
327 | return value.getValue();
|
328 | }));
|
329 | };
|
330 |
|
331 | _this.updateConfig = function (config, arg) {
|
332 | return _this.calc = Interpolation$1.create(config, arg);
|
333 | };
|
334 |
|
335 | _this.interpolate = function (config, arg) {
|
336 | return new AnimatedInterpolation(_assertThisInitialized(_assertThisInitialized(_this)), config, arg);
|
337 | };
|
338 |
|
339 | _this.payload =
|
340 | parents instanceof AnimatedArrayWithChildren && !parents.updateConfig ? parents.payload : Array.isArray(parents) ? parents : [parents];
|
341 | _this.calc = Interpolation$1.create(_config, _arg);
|
342 | return _this;
|
343 | }
|
344 |
|
345 | return AnimatedInterpolation;
|
346 | }(AnimatedArrayWithChildren);
|
347 | var interpolate$1 = function interpolate(parents, config, arg) {
|
348 | return parents && new AnimatedInterpolation(parents, config, arg);
|
349 | };
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | function findAnimatedStyles(node, styles) {
|
375 | if (typeof node.update === 'function') styles.add(node);else node.getChildren().forEach(function (child) {
|
376 | return findAnimatedStyles(child, styles);
|
377 | });
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | var AnimatedValue =
|
388 |
|
389 | function (_AnimatedWithChildren) {
|
390 | _inheritsLoose(AnimatedValue, _AnimatedWithChildren);
|
391 |
|
392 | function AnimatedValue(_value) {
|
393 | var _this;
|
394 |
|
395 | _this = _AnimatedWithChildren.call(this) || this;
|
396 |
|
397 | _this.setValue = function (value, flush) {
|
398 | if (flush === void 0) {
|
399 | flush = true;
|
400 | }
|
401 |
|
402 | _this.value = value;
|
403 | if (flush) _this.flush();
|
404 | };
|
405 |
|
406 | _this.getValue = function () {
|
407 | return _this.value;
|
408 | };
|
409 |
|
410 | _this.updateStyles = function () {
|
411 | return findAnimatedStyles(_assertThisInitialized(_assertThisInitialized(_this)), _this.animatedStyles);
|
412 | };
|
413 |
|
414 | _this.updateValue = function (value) {
|
415 | return _this.flush(_this.value = value);
|
416 | };
|
417 |
|
418 | _this.interpolate = function (config, arg) {
|
419 | return new AnimatedInterpolation(_assertThisInitialized(_assertThisInitialized(_this)), config, arg);
|
420 | };
|
421 |
|
422 | _this.value = _value;
|
423 | _this.animatedStyles = new Set();
|
424 | _this.done = false;
|
425 | _this.startPosition = _value;
|
426 | _this.lastPosition = _value;
|
427 | _this.lastVelocity = undefined;
|
428 | _this.lastTime = undefined;
|
429 | _this.controller = undefined;
|
430 | return _this;
|
431 | }
|
432 |
|
433 | var _proto = AnimatedValue.prototype;
|
434 |
|
435 | _proto.flush = function flush() {
|
436 | if (this.animatedStyles.size === 0) this.updateStyles();
|
437 | this.animatedStyles.forEach(function (animatedStyle) {
|
438 | return animatedStyle.update();
|
439 | });
|
440 | };
|
441 |
|
442 | _proto.prepare = function prepare(controller) {
|
443 |
|
444 |
|
445 | if (this.controller === undefined) this.controller = controller;
|
446 |
|
447 | if (this.controller === controller) {
|
448 | this.startPosition = this.value;
|
449 | this.lastPosition = this.value;
|
450 | this.lastVelocity = controller.isActive ? this.lastVelocity : undefined;
|
451 | this.lastTime = controller.isActive ? this.lastTime : undefined;
|
452 | this.done = false;
|
453 | this.animatedStyles.clear();
|
454 | }
|
455 | };
|
456 |
|
457 | return AnimatedValue;
|
458 | }(AnimatedWithChildren);
|
459 |
|
460 | var AnimatedArray =
|
461 |
|
462 | function (_AnimatedArrayWithChi) {
|
463 | _inheritsLoose(AnimatedArray, _AnimatedArrayWithChi);
|
464 |
|
465 | function AnimatedArray(array) {
|
466 | var _this;
|
467 |
|
468 | _this = _AnimatedArrayWithChi.call(this) || this;
|
469 |
|
470 | _this.setValue = function (value, flush) {
|
471 | if (flush === void 0) {
|
472 | flush = true;
|
473 | }
|
474 |
|
475 | if (Array.isArray(value)) {
|
476 | if (value.length === _this.payload.length) value.forEach(function (v, i) {
|
477 | return _this.payload[i].setValue(v, flush);
|
478 | });
|
479 | } else _this.payload.forEach(function (v, i) {
|
480 | return _this.payload[i].setValue(value, flush);
|
481 | });
|
482 | };
|
483 |
|
484 | _this.getValue = function () {
|
485 | return _this.payload.map(function (v) {
|
486 | return v.getValue();
|
487 | });
|
488 | };
|
489 |
|
490 | _this.interpolate = function (config, arg) {
|
491 | return new AnimatedInterpolation(_assertThisInitialized(_assertThisInitialized(_this)), config, arg);
|
492 | };
|
493 |
|
494 | _this.payload = array.map(function (n) {
|
495 | return new AnimatedValue(n);
|
496 | });
|
497 | return _this;
|
498 | }
|
499 |
|
500 | return AnimatedArray;
|
501 | }(AnimatedArrayWithChildren);
|
502 |
|
503 | var active = false;
|
504 | var controllers = new Set();
|
505 |
|
506 | var frameLoop = function frameLoop() {
|
507 | var time = now();
|
508 |
|
509 | for (var _iterator = controllers, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
510 | var _ref;
|
511 |
|
512 | if (_isArray) {
|
513 | if (_i >= _iterator.length) break;
|
514 | _ref = _iterator[_i++];
|
515 | } else {
|
516 | _i = _iterator.next();
|
517 | if (_i.done) break;
|
518 | _ref = _i.value;
|
519 | }
|
520 |
|
521 | var controller = _ref;
|
522 | var isDone = true;
|
523 | var noChange = true;
|
524 |
|
525 | for (var configIdx = 0; configIdx < controller.configs.length; configIdx++) {
|
526 | var config = controller.configs[configIdx];
|
527 | var endOfAnimation = void 0,
|
528 | lastTime = void 0;
|
529 |
|
530 | for (var valIdx = 0; valIdx < config.animatedValues.length; valIdx++) {
|
531 | var animation = config.animatedValues[valIdx];
|
532 |
|
533 | if (animation.done) continue;
|
534 | var from = config.fromValues[valIdx];
|
535 | var to = config.toValues[valIdx];
|
536 | var position = animation.lastPosition;
|
537 | var isAnimated = to instanceof Animated;
|
538 |
|
539 | var _velocity = Array.isArray(config.initialVelocity) ? config.initialVelocity[valIdx] : config.initialVelocity;
|
540 |
|
541 | if (isAnimated) to = to.getValue();
|
542 |
|
543 | if (config.immediate || !isAnimated && !config.decay && from === to) {
|
544 | animation.updateValue(to);
|
545 | animation.done = true;
|
546 | continue;
|
547 | }
|
548 |
|
549 |
|
550 | if (config.delay && time - controller.startTime < config.delay) {
|
551 | isDone = false;
|
552 | continue;
|
553 | }
|
554 |
|
555 |
|
556 | noChange = false;
|
557 |
|
558 | if (typeof from === 'string' || typeof to === 'string') {
|
559 | animation.updateValue(to);
|
560 | animation.done = true;
|
561 | continue;
|
562 | }
|
563 |
|
564 | if (config.duration !== void 0) {
|
565 |
|
566 | position = from + config.easing((time - controller.startTime - config.delay) / config.duration) * (to - from);
|
567 | endOfAnimation = time >= controller.startTime + config.delay + config.duration;
|
568 | } else if (config.decay) {
|
569 |
|
570 | position = from + _velocity / (1 - 0.998) * (1 - Math.exp(-(1 - 0.998) * (time - controller.startTime)));
|
571 | endOfAnimation = Math.abs(animation.lastPosition - position) < 0.1;
|
572 | if (endOfAnimation) to = position;
|
573 | } else {
|
574 |
|
575 | lastTime = animation.lastTime !== void 0 ? animation.lastTime : time;
|
576 | _velocity = animation.lastVelocity !== void 0 ? animation.lastVelocity : config.initialVelocity;
|
577 |
|
578 | if (time > lastTime + 64) lastTime = time;
|
579 |
|
580 | var numSteps = Math.floor(time - lastTime);
|
581 |
|
582 | for (var i = 0; i < numSteps; ++i) {
|
583 | var force = -config.tension * (position - to);
|
584 | var damping = -config.friction * _velocity;
|
585 | var acceleration = (force + damping) / config.mass;
|
586 | _velocity = _velocity + acceleration * 1 / 1000;
|
587 | position = position + _velocity * 1 / 1000;
|
588 | }
|
589 |
|
590 |
|
591 | var isOvershooting = config.clamp && config.tension !== 0 ? from < to ? position > to : position < to : false;
|
592 | var isVelocity = Math.abs(_velocity) <= config.precision;
|
593 | var isDisplacement = config.tension !== 0 ? Math.abs(to - position) <= config.precision : true;
|
594 | endOfAnimation = isOvershooting || isVelocity && isDisplacement;
|
595 | animation.lastVelocity = _velocity;
|
596 | animation.lastTime = time;
|
597 | }
|
598 |
|
599 |
|
600 | if (isAnimated && !config.toValues[valIdx].done) endOfAnimation = false;
|
601 |
|
602 | if (endOfAnimation) {
|
603 |
|
604 | if (animation.value !== to) position = to;
|
605 | animation.done = true;
|
606 | } else isDone = false;
|
607 |
|
608 | animation.updateValue(position);
|
609 | animation.lastPosition = position;
|
610 | }
|
611 |
|
612 |
|
613 | if (controller.props.onFrame || !controller.props.native) controller.animatedProps[config.name] = config.interpolation.getValue();
|
614 | }
|
615 |
|
616 |
|
617 | if (controller.props.onFrame || !controller.props.native) {
|
618 | if (!controller.props.native && controller.onUpdate) controller.onUpdate();
|
619 | if (controller.props.onFrame) controller.props.onFrame(controller.animatedProps);
|
620 | }
|
621 |
|
622 |
|
623 | if (isDone) {
|
624 | controllers.delete(controller);
|
625 | controller.debouncedOnEnd({
|
626 | finished: true,
|
627 | noChange: noChange
|
628 | });
|
629 | }
|
630 | }
|
631 |
|
632 |
|
633 | if (controllers.size) requestFrame(frameLoop);else active = false;
|
634 | };
|
635 |
|
636 | var addController = function addController(controller) {
|
637 | if (!controllers.has(controller)) {
|
638 | controllers.add(controller);
|
639 | if (!active) requestFrame(frameLoop);
|
640 | active = true;
|
641 | }
|
642 | };
|
643 |
|
644 | var removeController = function removeController(controller) {
|
645 | if (controllers.has(controller)) {
|
646 | controllers.delete(controller);
|
647 | }
|
648 | };
|
649 |
|
650 | function withDefault(value, defaultValue) {
|
651 | return value === undefined || value === null ? defaultValue : value;
|
652 | }
|
653 | function toArray(a) {
|
654 | return a !== void 0 ? Array.isArray(a) ? a : [a] : [];
|
655 | }
|
656 | function shallowEqual(a, b) {
|
657 | if (typeof a !== typeof b) return false;
|
658 | if (typeof a === 'string' || typeof a === 'number') return a === b;
|
659 | var i;
|
660 |
|
661 | for (i in a) {
|
662 | if (!(i in b)) return false;
|
663 | }
|
664 |
|
665 | for (i in b) {
|
666 | if (a[i] !== b[i]) return false;
|
667 | }
|
668 |
|
669 | return i === void 0 ? a === b : true;
|
670 | }
|
671 | function callProp(obj) {
|
672 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
673 | args[_key - 1] = arguments[_key];
|
674 | }
|
675 |
|
676 | return typeof obj === 'function' ? obj.apply(void 0, args) : obj;
|
677 | }
|
678 | function getValues$1(object) {
|
679 | return Object.keys(object).map(function (k) {
|
680 | return object[k];
|
681 | });
|
682 | }
|
683 | function getForwardProps(props) {
|
684 | var to = props.to,
|
685 | from = props.from,
|
686 | config = props.config,
|
687 | native = props.native,
|
688 | onStart = props.onStart,
|
689 | onRest = props.onRest,
|
690 | onFrame = props.onFrame,
|
691 | children = props.children,
|
692 | reset = props.reset,
|
693 | reverse = props.reverse,
|
694 | force = props.force,
|
695 | immediate = props.immediate,
|
696 | impl = props.impl,
|
697 | inject = props.inject,
|
698 | delay = props.delay,
|
699 | attach = props.attach,
|
700 | destroyed = props.destroyed,
|
701 | interpolateTo = props.interpolateTo,
|
702 | autoStart = props.autoStart,
|
703 | ref = props.ref,
|
704 | forward = _objectWithoutPropertiesLoose(props, ["to", "from", "config", "native", "onStart", "onRest", "onFrame", "children", "reset", "reverse", "force", "immediate", "impl", "inject", "delay", "attach", "destroyed", "interpolateTo", "autoStart", "ref"]);
|
705 |
|
706 | return forward;
|
707 | }
|
708 | function interpolateTo(props) {
|
709 | var forward = getForwardProps(props);
|
710 | var rest = Object.keys(props).reduce(function (a, k) {
|
711 | var _extends2;
|
712 |
|
713 | return forward[k] !== void 0 ? a : _extends({}, a, (_extends2 = {}, _extends2[k] = props[k], _extends2));
|
714 | }, {});
|
715 | return _extends({
|
716 | to: forward
|
717 | }, rest);
|
718 | }
|
719 | function convertToAnimatedValue(acc, _ref) {
|
720 | var _extends3;
|
721 |
|
722 | var name = _ref[0],
|
723 | value = _ref[1];
|
724 | return _extends({}, acc, (_extends3 = {}, _extends3[name] = new (Array.isArray(value) ? AnimatedArray : AnimatedValue)(value), _extends3));
|
725 | }
|
726 | function convertValues(props) {
|
727 | var from = props.from,
|
728 | to = props.to,
|
729 | native = props.native;
|
730 | var allProps = Object.entries(_extends({}, from, to));
|
731 | return native ? allProps.reduce(convertToAnimatedValue, {}) : _extends({}, from, to);
|
732 | }
|
733 | function handleRef(ref, forward) {
|
734 | if (forward) {
|
735 |
|
736 | if (typeof forward === 'function') forward(ref);else if (typeof forward === 'object') {
|
737 |
|
738 | forward.current = ref;
|
739 | }
|
740 | }
|
741 |
|
742 | return ref;
|
743 | }
|
744 |
|
745 | var Controller =
|
746 |
|
747 | function () {
|
748 | function Controller(props, config) {
|
749 | var _this = this;
|
750 |
|
751 | if (config === void 0) {
|
752 | config = {
|
753 | native: true,
|
754 | interpolateTo: true,
|
755 | autoStart: true
|
756 | };
|
757 | }
|
758 |
|
759 | this.getValues = function () {
|
760 | return _this.props.native ? _this.interpolations : _this.animatedProps;
|
761 | };
|
762 |
|
763 | this.dependents = new Set();
|
764 | this.isActive = false;
|
765 | this.hasChanged = false;
|
766 | this.props = {};
|
767 | this.merged = {};
|
768 | this.animations = {};
|
769 | this.interpolations = {};
|
770 | this.animatedProps = {};
|
771 | this.configs = [];
|
772 | this.frame = undefined;
|
773 | this.startTime = undefined;
|
774 | this.lastTime = undefined;
|
775 | this.update(_extends({}, props, config));
|
776 | }
|
777 |
|
778 | var _proto = Controller.prototype;
|
779 |
|
780 | _proto.update = function update(props) {
|
781 | var _this2 = this;
|
782 |
|
783 | this.props = _extends({}, this.props, props);
|
784 |
|
785 | var _ref = this.props.interpolateTo ? interpolateTo(this.props) : this.props,
|
786 | _ref$from = _ref.from,
|
787 | from = _ref$from === void 0 ? {} : _ref$from,
|
788 | _ref$to = _ref.to,
|
789 | to = _ref$to === void 0 ? {} : _ref$to,
|
790 | _ref$config = _ref.config,
|
791 | config = _ref$config === void 0 ? {} : _ref$config,
|
792 | _ref$delay = _ref.delay,
|
793 | delay = _ref$delay === void 0 ? 0 : _ref$delay,
|
794 | reverse = _ref.reverse,
|
795 | attach = _ref.attach,
|
796 | reset = _ref.reset,
|
797 | immediate = _ref.immediate,
|
798 | autoStart = _ref.autoStart,
|
799 | ref = _ref.ref;
|
800 |
|
801 |
|
802 | if (reverse) {
|
803 | var _ref2 = [to, from];
|
804 | from = _ref2[0];
|
805 | to = _ref2[1];
|
806 | }
|
807 |
|
808 | this.hasChanged = false;
|
809 |
|
810 | var target = attach && attach(this);
|
811 |
|
812 | var extra = reset ? {} : this.merged;
|
813 |
|
814 | this.merged = _extends({}, from, extra, to);
|
815 |
|
816 | this.animations = Object.entries(this.merged).reduce(function (acc, _ref3, i) {
|
817 | var name = _ref3[0],
|
818 | value = _ref3[1];
|
819 |
|
820 | var entry = !reset && acc[name] || {};
|
821 |
|
822 | var isNumber = typeof value === 'number';
|
823 | var isString = typeof value === 'string' && !value.startsWith('#') && !/\d/.test(value) && !colorNames[value];
|
824 | var isArray = !isNumber && !isString && Array.isArray(value);
|
825 | var fromValue = from[name] !== undefined ? from[name] : value;
|
826 | var toValue = isNumber || isArray ? value : isString ? value : 1;
|
827 | var toConfig = callProp(config, name);
|
828 | if (target) toValue = target.animations[name].parent;
|
829 |
|
830 | if (toConfig.decay !== void 0 || !shallowEqual(entry.changes, value)) {
|
831 | var _extends2;
|
832 |
|
833 | _this2.hasChanged = true;
|
834 | var parent, interpolation$$1;
|
835 | if (isNumber || isString) parent = interpolation$$1 = entry.parent || new AnimatedValue(fromValue);else if (isArray) parent = interpolation$$1 = entry.parent || new AnimatedArray(fromValue);else {
|
836 | var prev = entry.interpolation && entry.interpolation.calc(entry.parent.value);
|
837 |
|
838 | if (entry.parent) {
|
839 | parent = entry.parent;
|
840 | parent.setValue(0, false);
|
841 | } else parent = new AnimatedValue(0);
|
842 |
|
843 | var range = {
|
844 | output: [prev !== void 0 ? prev : fromValue, value]
|
845 | };
|
846 |
|
847 | if (entry.interpolation) {
|
848 | interpolation$$1 = entry.interpolation;
|
849 | entry.interpolation.updateConfig(range);
|
850 | } else interpolation$$1 = parent.interpolate(range);
|
851 | }
|
852 |
|
853 | if (callProp(immediate, name)) parent.setValue(value, false);
|
854 |
|
855 | var animatedValues = toArray(parent.getPayload());
|
856 | animatedValues.forEach(function (value) {
|
857 | return value.prepare(_this2);
|
858 | });
|
859 | return _extends({}, acc, (_extends2 = {}, _extends2[name] = _extends({}, entry, {
|
860 | name: name,
|
861 | parent: parent,
|
862 | interpolation: interpolation$$1,
|
863 | animatedValues: animatedValues,
|
864 | changes: value,
|
865 | fromValues: toArray(parent.getValue()),
|
866 | toValues: toArray(target ? toValue.getPayload() : toValue),
|
867 | immediate: callProp(immediate, name),
|
868 | delay: withDefault(toConfig.delay, delay || 0),
|
869 | initialVelocity: withDefault(toConfig.velocity, 0),
|
870 | clamp: withDefault(toConfig.clamp, false),
|
871 | precision: withDefault(toConfig.precision, 0.01),
|
872 | tension: withDefault(toConfig.tension, 170),
|
873 | friction: withDefault(toConfig.friction, 26),
|
874 | mass: withDefault(toConfig.mass, 1),
|
875 | duration: toConfig.duration,
|
876 | easing: withDefault(toConfig.easing, function (t) {
|
877 | return t;
|
878 | }),
|
879 | decay: toConfig.decay
|
880 | }), _extends2));
|
881 | } else return acc;
|
882 | }, this.animations);
|
883 |
|
884 | if (this.hasChanged) {
|
885 | this.configs = getValues$1(this.animations);
|
886 | this.animatedProps = {};
|
887 | this.interpolations = {};
|
888 |
|
889 | for (var key in this.animations) {
|
890 | this.interpolations[key] = this.animations[key].interpolation;
|
891 | this.animatedProps[key] = this.animations[key].interpolation.getValue();
|
892 | }
|
893 | }
|
894 |
|
895 |
|
896 | for (var _len = arguments.length, start = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
897 | start[_key - 1] = arguments[_key];
|
898 | }
|
899 |
|
900 | if (!ref && (autoStart || start.length)) this.start.apply(this, start);
|
901 | var onEnd = start[0],
|
902 | onUpdate = start[1];
|
903 | this.onEnd = typeof onEnd === 'function' && onEnd;
|
904 | this.onUpdate = onUpdate;
|
905 | return this.getValues();
|
906 | };
|
907 |
|
908 | _proto.start = function start(onEnd, onUpdate) {
|
909 | var _this3 = this;
|
910 |
|
911 | this.startTime = now();
|
912 | if (this.isActive) this.stop();
|
913 | this.isActive = true;
|
914 | this.onEnd = typeof onEnd === 'function' && onEnd;
|
915 | this.onUpdate = onUpdate;
|
916 | if (this.props.onStart) this.props.onStart();
|
917 | addController(this);
|
918 | return new Promise(function (res) {
|
919 | return _this3.resolve = res;
|
920 | });
|
921 | };
|
922 |
|
923 | _proto.stop = function stop(finished) {
|
924 | if (finished === void 0) {
|
925 | finished = false;
|
926 | }
|
927 |
|
928 |
|
929 | if (finished) getValues$1(this.animations).forEach(function (a) {
|
930 | return a.changes = undefined;
|
931 | });
|
932 | this.debouncedOnEnd({
|
933 | finished: finished
|
934 | });
|
935 | };
|
936 |
|
937 | _proto.destroy = function destroy() {
|
938 | removeController(this);
|
939 | this.props = {};
|
940 | this.merged = {};
|
941 | this.animations = {};
|
942 | this.interpolations = {};
|
943 | this.animatedProps = {};
|
944 | this.configs = [];
|
945 | };
|
946 |
|
947 | _proto.debouncedOnEnd = function debouncedOnEnd(result) {
|
948 | removeController(this);
|
949 | this.isActive = false;
|
950 | var onEnd = this.onEnd;
|
951 | this.onEnd = null;
|
952 | if (onEnd) onEnd(result);
|
953 | if (this.resolve) this.resolve();
|
954 | this.resolve = null;
|
955 | };
|
956 |
|
957 | return Controller;
|
958 | }();
|
959 |
|
960 | var AnimatedProps =
|
961 |
|
962 | function (_AnimatedObjectWithCh) {
|
963 | _inheritsLoose(AnimatedProps, _AnimatedObjectWithCh);
|
964 |
|
965 | function AnimatedProps(props, callback) {
|
966 | var _this;
|
967 |
|
968 | _this = _AnimatedObjectWithCh.call(this) || this;
|
969 | if (props.style) props = _extends({}, props, {
|
970 | style: createAnimatedStyle(props.style)
|
971 | });
|
972 | _this.payload = props;
|
973 | _this.update = callback;
|
974 |
|
975 | _this.attach();
|
976 |
|
977 | return _this;
|
978 | }
|
979 |
|
980 | return AnimatedProps;
|
981 | }(AnimatedObjectWithChildren);
|
982 |
|
983 | function createAnimatedComponent(Component) {
|
984 | var AnimatedComponent =
|
985 |
|
986 | function (_React$Component) {
|
987 | _inheritsLoose(AnimatedComponent, _React$Component);
|
988 |
|
989 | function AnimatedComponent(props) {
|
990 | var _this;
|
991 |
|
992 | _this = _React$Component.call(this) || this;
|
993 |
|
994 | _this.callback = function () {
|
995 | if (_this.node) {
|
996 | var didUpdate = applyAnimatedValues.fn(_this.node, _this.propsAnimated.getAnimatedValue(), _assertThisInitialized(_assertThisInitialized(_this)));
|
997 | if (didUpdate === false) _this.forceUpdate();
|
998 | }
|
999 | };
|
1000 |
|
1001 | _this.attachProps(props);
|
1002 |
|
1003 | return _this;
|
1004 | }
|
1005 |
|
1006 | var _proto = AnimatedComponent.prototype;
|
1007 |
|
1008 | _proto.componentWillUnmount = function componentWillUnmount() {
|
1009 | this.propsAnimated && this.propsAnimated.detach();
|
1010 | };
|
1011 |
|
1012 | _proto.setNativeProps = function setNativeProps(props) {
|
1013 | var didUpdate = applyAnimatedValues.fn(this.node, props, this);
|
1014 | if (didUpdate === false) this.forceUpdate();
|
1015 | }
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 | ;
|
1022 |
|
1023 | _proto.attachProps = function attachProps(_ref) {
|
1024 | var forwardRef = _ref.forwardRef,
|
1025 | nextProps = _objectWithoutPropertiesLoose(_ref, ["forwardRef"]);
|
1026 |
|
1027 | var oldPropsAnimated = this.propsAnimated;
|
1028 | this.propsAnimated = new AnimatedProps(nextProps, this.callback);
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 | oldPropsAnimated && oldPropsAnimated.detach();
|
1038 | };
|
1039 |
|
1040 | _proto.shouldComponentUpdate = function shouldComponentUpdate(props) {
|
1041 | var style = props.style,
|
1042 | nextProps = _objectWithoutPropertiesLoose(props, ["style"]);
|
1043 |
|
1044 | var _this$props = this.props,
|
1045 | currentStyle = _this$props.style,
|
1046 | currentProps = _objectWithoutPropertiesLoose(_this$props, ["style"]);
|
1047 |
|
1048 | if (!shallowEqual(currentProps, nextProps) || !shallowEqual(currentStyle, style)) {
|
1049 | this.attachProps(props);
|
1050 | return true;
|
1051 | }
|
1052 |
|
1053 | return false;
|
1054 | };
|
1055 |
|
1056 | _proto.render = function render() {
|
1057 | var _this2 = this;
|
1058 |
|
1059 | var _this$propsAnimated$g = this.propsAnimated.getValue(),
|
1060 | scrollTop = _this$propsAnimated$g.scrollTop,
|
1061 | scrollLeft = _this$propsAnimated$g.scrollLeft,
|
1062 | animatedProps = _objectWithoutPropertiesLoose(_this$propsAnimated$g, ["scrollTop", "scrollLeft"]);
|
1063 |
|
1064 | return React__default.createElement(Component, _extends({}, animatedProps, {
|
1065 | ref: function ref(node) {
|
1066 | return _this2.node = handleRef(node, _this2.props.forwardRef);
|
1067 | }
|
1068 | }));
|
1069 | };
|
1070 |
|
1071 | return AnimatedComponent;
|
1072 | }(React__default.Component);
|
1073 |
|
1074 | return React__default.forwardRef(function (props, ref) {
|
1075 | return React__default.createElement(AnimatedComponent, _extends({}, props, {
|
1076 | forwardRef: ref
|
1077 | }));
|
1078 | });
|
1079 | }
|
1080 |
|
1081 | var config = {
|
1082 | default: {
|
1083 | tension: 170,
|
1084 | friction: 26
|
1085 | },
|
1086 | gentle: {
|
1087 | tension: 120,
|
1088 | friction: 14
|
1089 | },
|
1090 | wobbly: {
|
1091 | tension: 180,
|
1092 | friction: 12
|
1093 | },
|
1094 | stiff: {
|
1095 | tension: 210,
|
1096 | friction: 20
|
1097 | },
|
1098 | slow: {
|
1099 | tension: 280,
|
1100 | friction: 60
|
1101 | },
|
1102 | molasses: {
|
1103 | tension: 280,
|
1104 | friction: 120
|
1105 | }
|
1106 | };
|
1107 |
|
1108 | var Spring =
|
1109 |
|
1110 | function (_React$Component) {
|
1111 | _inheritsLoose(Spring, _React$Component);
|
1112 |
|
1113 | function Spring() {
|
1114 | var _this;
|
1115 |
|
1116 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
1117 | args[_key] = arguments[_key];
|
1118 | }
|
1119 |
|
1120 | _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
1121 | _this.state = {
|
1122 | lastProps: {
|
1123 | from: {},
|
1124 | to: {}
|
1125 | },
|
1126 | propsChanged: false,
|
1127 | internal: false
|
1128 | };
|
1129 | _this.controller = new Controller(null, null);
|
1130 | _this.didUpdate = false;
|
1131 | _this.didInject = false;
|
1132 | _this.finished = true;
|
1133 |
|
1134 | _this.start = function () {
|
1135 | _this.finished = false;
|
1136 | var wasMounted = _this.mounted;
|
1137 |
|
1138 | _this.controller.start(function (props) {
|
1139 | return _this.finish(_extends({}, props, {
|
1140 | wasMounted: wasMounted
|
1141 | }));
|
1142 | }, _this.update);
|
1143 | };
|
1144 |
|
1145 | _this.stop = function () {
|
1146 | return _this.controller.stop(true);
|
1147 | };
|
1148 |
|
1149 | _this.update = function () {
|
1150 | return _this.mounted && _this.setState({
|
1151 | internal: true
|
1152 | });
|
1153 | };
|
1154 |
|
1155 | _this.finish = function (_ref) {
|
1156 | var finished = _ref.finished,
|
1157 | noChange = _ref.noChange,
|
1158 | wasMounted = _ref.wasMounted;
|
1159 | _this.finished = true;
|
1160 |
|
1161 | if (_this.mounted && finished) {
|
1162 |
|
1163 | if (_this.props.onRest && (wasMounted || !noChange)) _this.props.onRest(_this.controller.merged);
|
1164 |
|
1165 | if (_this.mounted && _this.didInject) {
|
1166 | _this.afterInject = convertValues(_this.props);
|
1167 |
|
1168 | _this.setState({
|
1169 | internal: true
|
1170 | });
|
1171 | }
|
1172 |
|
1173 |
|
1174 | if (_this.mounted && (_this.didInject || _this.props.after)) _this.setState({
|
1175 | internal: true
|
1176 | });
|
1177 | _this.didInject = false;
|
1178 | }
|
1179 | };
|
1180 |
|
1181 | return _this;
|
1182 | }
|
1183 |
|
1184 | var _proto = Spring.prototype;
|
1185 |
|
1186 | _proto.componentDidMount = function componentDidMount() {
|
1187 |
|
1188 | this.componentDidUpdate();
|
1189 | this.mounted = true;
|
1190 | };
|
1191 |
|
1192 | _proto.componentWillUnmount = function componentWillUnmount() {
|
1193 |
|
1194 | this.mounted = false;
|
1195 | this.stop();
|
1196 | };
|
1197 |
|
1198 | Spring.getDerivedStateFromProps = function getDerivedStateFromProps(props, _ref2) {
|
1199 | var internal = _ref2.internal,
|
1200 | lastProps = _ref2.lastProps;
|
1201 |
|
1202 | var from = props.from,
|
1203 | to = props.to,
|
1204 | reset = props.reset,
|
1205 | force = props.force;
|
1206 | var propsChanged = !shallowEqual(to, lastProps.to) || !shallowEqual(from, lastProps.from) || reset && !internal || force && !internal;
|
1207 | return {
|
1208 | propsChanged: propsChanged,
|
1209 | lastProps: props,
|
1210 | internal: false
|
1211 | };
|
1212 | };
|
1213 |
|
1214 | _proto.render = function render() {
|
1215 | var _this2 = this;
|
1216 |
|
1217 | var children = this.props.children;
|
1218 | var propsChanged = this.state.propsChanged;
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 | if (this.props.inject && propsChanged && !this.injectProps) {
|
1224 | var frame = this.props.inject(this.props, function (injectProps) {
|
1225 |
|
1226 | _this2.injectProps = injectProps;
|
1227 |
|
1228 | _this2.setState({
|
1229 | internal: true
|
1230 | });
|
1231 | });
|
1232 |
|
1233 | if (frame) return frame;
|
1234 | }
|
1235 |
|
1236 |
|
1237 | if (this.injectProps || propsChanged) {
|
1238 |
|
1239 | this.didInject = false;
|
1240 |
|
1241 |
|
1242 | if (this.injectProps) {
|
1243 | this.controller.update(this.injectProps);
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 | this.didInject = true;
|
1249 | } else if (propsChanged) this.controller.update(this.props);
|
1250 |
|
1251 |
|
1252 | this.didUpdate = true;
|
1253 | this.afterInject = undefined;
|
1254 | this.injectProps = undefined;
|
1255 | }
|
1256 |
|
1257 |
|
1258 |
|
1259 | var values = _extends({}, this.controller.getValues(), this.afterInject);
|
1260 |
|
1261 | if (this.finished) values = _extends({}, values, this.props.after);
|
1262 | return Object.keys(values).length ? children(values) : null;
|
1263 | };
|
1264 |
|
1265 | _proto.componentDidUpdate = function componentDidUpdate() {
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 | if (this.didUpdate) this.start();
|
1271 | this.didUpdate = false;
|
1272 | };
|
1273 |
|
1274 | return Spring;
|
1275 | }(React__default.Component);
|
1276 |
|
1277 | Spring.defaultProps = {
|
1278 | from: {},
|
1279 | to: {},
|
1280 | config: config.default,
|
1281 | native: false,
|
1282 | immediate: false,
|
1283 | reset: false,
|
1284 | force: false,
|
1285 | inject: bugfixes
|
1286 | };
|
1287 |
|
1288 | var Trail =
|
1289 |
|
1290 | function (_React$PureComponent) {
|
1291 | _inheritsLoose(Trail, _React$PureComponent);
|
1292 |
|
1293 | function Trail() {
|
1294 | var _this;
|
1295 |
|
1296 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
1297 | args[_key] = arguments[_key];
|
1298 | }
|
1299 |
|
1300 | _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
|
1301 | _this.first = true;
|
1302 | _this.instances = new Set();
|
1303 |
|
1304 | _this.hook = function (instance, index, length, reverse) {
|
1305 |
|
1306 | _this.instances.add(instance);
|
1307 |
|
1308 |
|
1309 | if (reverse ? index === length - 1 : index === 0) return undefined;else return Array.from(_this.instances)[reverse ? index + 1 : index - 1];
|
1310 | };
|
1311 |
|
1312 | return _this;
|
1313 | }
|
1314 |
|
1315 | var _proto = Trail.prototype;
|
1316 |
|
1317 | _proto.render = function render() {
|
1318 | var _this2 = this;
|
1319 |
|
1320 | var _this$props = this.props,
|
1321 | items = _this$props.items,
|
1322 | _children = _this$props.children,
|
1323 | _this$props$from = _this$props.from,
|
1324 | from = _this$props$from === void 0 ? {} : _this$props$from,
|
1325 | initial = _this$props.initial,
|
1326 | reverse = _this$props.reverse,
|
1327 | keys = _this$props.keys,
|
1328 | delay = _this$props.delay,
|
1329 | onRest = _this$props.onRest,
|
1330 | props = _objectWithoutPropertiesLoose(_this$props, ["items", "children", "from", "initial", "reverse", "keys", "delay", "onRest"]);
|
1331 |
|
1332 | var array = toArray(items);
|
1333 | return toArray(array).map(function (item, i) {
|
1334 | return React__default.createElement(Spring, _extends({
|
1335 | onRest: i === 0 ? onRest : null,
|
1336 | key: typeof keys === 'function' ? keys(item) : toArray(keys)[i],
|
1337 | from: _this2.first && initial !== void 0 ? initial || {} : from
|
1338 | }, props, {
|
1339 | delay: i === 0 && delay || undefined,
|
1340 | attach: function attach(instance) {
|
1341 | return _this2.hook(instance, i, array.length, reverse);
|
1342 | },
|
1343 | children: function children(props) {
|
1344 | var child = _children(item, i);
|
1345 |
|
1346 | return child ? child(props) : null;
|
1347 | }
|
1348 | }));
|
1349 | });
|
1350 | };
|
1351 |
|
1352 | _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
1353 | this.first = false;
|
1354 | if (prevProps.items !== this.props.items) this.instances.clear();
|
1355 | };
|
1356 |
|
1357 | return Trail;
|
1358 | }(React__default.PureComponent);
|
1359 |
|
1360 | Trail.defaultProps = {
|
1361 | keys: function keys(item) {
|
1362 | return item;
|
1363 | }
|
1364 | };
|
1365 |
|
1366 | var DEFAULT = '__default';
|
1367 |
|
1368 | var KeyframesImpl =
|
1369 |
|
1370 | function (_React$PureComponent) {
|
1371 | _inheritsLoose(KeyframesImpl, _React$PureComponent);
|
1372 |
|
1373 | function KeyframesImpl() {
|
1374 | var _this;
|
1375 |
|
1376 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
1377 | args[_key] = arguments[_key];
|
1378 | }
|
1379 |
|
1380 | _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
|
1381 | _this.guid = 0;
|
1382 | _this.state = {
|
1383 | props: {},
|
1384 | resolve: function resolve() {
|
1385 | return null;
|
1386 | },
|
1387 | last: true,
|
1388 | index: 0
|
1389 | };
|
1390 |
|
1391 | _this.next = function (props, last, index) {
|
1392 | if (last === void 0) {
|
1393 | last = true;
|
1394 | }
|
1395 |
|
1396 | if (index === void 0) {
|
1397 | index = 0;
|
1398 | }
|
1399 |
|
1400 | _this.running = true;
|
1401 | return new Promise(function (resolve) {
|
1402 | _this.mounted && _this.setState(function (state) {
|
1403 | return {
|
1404 | props: props,
|
1405 | resolve: resolve,
|
1406 | last: last,
|
1407 | index: index
|
1408 | };
|
1409 | }, function () {
|
1410 | return _this.running = false;
|
1411 | });
|
1412 | });
|
1413 | };
|
1414 |
|
1415 | return _this;
|
1416 | }
|
1417 |
|
1418 | var _proto = KeyframesImpl.prototype;
|
1419 |
|
1420 | _proto.componentDidMount = function componentDidMount() {
|
1421 | this.mounted = true;
|
1422 | this.componentDidUpdate({});
|
1423 | };
|
1424 |
|
1425 | _proto.componentWillUnmount = function componentWillUnmount() {
|
1426 | this.mounted = false;
|
1427 | };
|
1428 |
|
1429 | _proto.componentDidUpdate = function componentDidUpdate(previous) {
|
1430 | var _this2 = this;
|
1431 |
|
1432 | var _this$props = this.props,
|
1433 | states = _this$props.states,
|
1434 | f = _this$props.filter,
|
1435 | state = _this$props.state;
|
1436 |
|
1437 | if (previous.state !== this.props.state || this.props.reset && !this.running || !shallowEqual(states[state], previous.states[previous.state])) {
|
1438 | if (states && state && states[state]) {
|
1439 | (function () {
|
1440 | var localId = ++_this2.guid;
|
1441 | var slots = states[state];
|
1442 |
|
1443 | if (slots) {
|
1444 | if (Array.isArray(slots)) {
|
1445 | var q = Promise.resolve();
|
1446 |
|
1447 | var _loop = function _loop(i) {
|
1448 | var index = i;
|
1449 | var slot = slots[index];
|
1450 | var last = index === slots.length - 1;
|
1451 | q = q.then(function () {
|
1452 | return localId === _this2.guid && _this2.next(f(slot), last, index);
|
1453 | });
|
1454 | };
|
1455 |
|
1456 | for (var i = 0; i < slots.length; i++) {
|
1457 | _loop(i);
|
1458 | }
|
1459 | } else if (typeof slots === 'function') {
|
1460 | var index = 0;
|
1461 | slots(
|
1462 | function (props, last) {
|
1463 | if (last === void 0) {
|
1464 | last = false;
|
1465 | }
|
1466 |
|
1467 | return localId === _this2.guid && _this2.next(f(props), last, index++);
|
1468 | },
|
1469 | function () {
|
1470 | return requestFrame(function () {
|
1471 | return _this2.instance && _this2.instance.stop();
|
1472 | });
|
1473 | },
|
1474 | _this2.props);
|
1475 | } else {
|
1476 | _this2.next(f(states[state]));
|
1477 | }
|
1478 | }
|
1479 | })();
|
1480 | }
|
1481 | }
|
1482 | };
|
1483 |
|
1484 | _proto.render = function render() {
|
1485 | var _this3 = this;
|
1486 |
|
1487 | var _this$state = this.state,
|
1488 | props = _this$state.props,
|
1489 | resolve = _this$state.resolve,
|
1490 | last = _this$state.last,
|
1491 | index = _this$state.index;
|
1492 | if (!props || Object.keys(props).length === 0) return null;
|
1493 |
|
1494 | var _this$props2 = this.props,
|
1495 | state = _this$props2.state,
|
1496 | filter = _this$props2.filter,
|
1497 | states = _this$props2.states,
|
1498 | config = _this$props2.config,
|
1499 | Component = _this$props2.primitive,
|
1500 | _onRest = _this$props2.onRest,
|
1501 | forwardRef = _this$props2.forwardRef,
|
1502 | rest = _objectWithoutPropertiesLoose(_this$props2, ["state", "filter", "states", "config", "primitive", "onRest", "forwardRef"]);
|
1503 |
|
1504 |
|
1505 | if (Array.isArray(config)) config = config[index];
|
1506 | return React__default.createElement(Component, _extends({
|
1507 | ref: function ref(_ref) {
|
1508 | return _this3.instance = handleRef(_ref, forwardRef);
|
1509 | },
|
1510 | config: config
|
1511 | }, rest, props, {
|
1512 | onRest: function onRest(args) {
|
1513 | resolve(args);
|
1514 | if (_onRest && last) _onRest(args);
|
1515 | }
|
1516 | }));
|
1517 | };
|
1518 |
|
1519 | return KeyframesImpl;
|
1520 | }(React__default.PureComponent);
|
1521 |
|
1522 | KeyframesImpl.defaultProps = {
|
1523 | state: DEFAULT
|
1524 | };
|
1525 | var Keyframes = React__default.forwardRef(function (props, ref) {
|
1526 | return React__default.createElement(KeyframesImpl, _extends({}, props, {
|
1527 | forwardRef: ref
|
1528 | }));
|
1529 | });
|
1530 |
|
1531 | Keyframes.create = function (primitive) {
|
1532 | return function (states, filter) {
|
1533 | var _states;
|
1534 |
|
1535 | if (filter === void 0) {
|
1536 | filter = function filter(states) {
|
1537 | return states;
|
1538 | };
|
1539 | }
|
1540 |
|
1541 | if (typeof states === 'function' || Array.isArray(states)) states = (_states = {}, _states[DEFAULT] = states, _states);
|
1542 | return function (props) {
|
1543 | return React__default.createElement(KeyframesImpl, _extends({
|
1544 | primitive: primitive,
|
1545 | states: states,
|
1546 | filter: filter
|
1547 | }, props));
|
1548 | };
|
1549 | };
|
1550 | };
|
1551 |
|
1552 | Keyframes.Spring = function (states) {
|
1553 | return Keyframes.create(Spring)(states, interpolateTo);
|
1554 | };
|
1555 |
|
1556 | Keyframes.Trail = function (states) {
|
1557 | return Keyframes.create(Trail)(states, interpolateTo);
|
1558 | };
|
1559 |
|
1560 | var guid = 0;
|
1561 |
|
1562 | var get = function get(props) {
|
1563 | var items = props.items,
|
1564 | keys = props.keys,
|
1565 | rest = _objectWithoutPropertiesLoose(props, ["items", "keys"]);
|
1566 |
|
1567 | items = toArray(items !== void 0 ? items : null);
|
1568 | keys = typeof keys === 'function' ? items.map(keys) : toArray(keys);
|
1569 |
|
1570 | return _extends({
|
1571 | items: items,
|
1572 | keys: keys.map(function (key) {
|
1573 | return String(key);
|
1574 | })
|
1575 | }, rest);
|
1576 | };
|
1577 |
|
1578 | var Transition =
|
1579 |
|
1580 | function (_React$PureComponent) {
|
1581 | _inheritsLoose(Transition, _React$PureComponent);
|
1582 |
|
1583 | var _proto = Transition.prototype;
|
1584 |
|
1585 | _proto.componentDidMount = function componentDidMount() {
|
1586 | this.mounted = true;
|
1587 | };
|
1588 |
|
1589 | _proto.componentWillUnmount = function componentWillUnmount() {
|
1590 | this.mounted = false;
|
1591 | };
|
1592 |
|
1593 | function Transition(prevProps) {
|
1594 | var _this;
|
1595 |
|
1596 | _this = _React$PureComponent.call(this, prevProps) || this;
|
1597 |
|
1598 | _this.destroyItem = function (item, key, state) {
|
1599 | return function (values) {
|
1600 | var _this$props = _this.props,
|
1601 | onRest = _this$props.onRest,
|
1602 | onDestroyed = _this$props.onDestroyed;
|
1603 |
|
1604 | if (_this.mounted) {
|
1605 | onDestroyed && onDestroyed(item);
|
1606 |
|
1607 | _this.setState(function (_ref) {
|
1608 | var deleted = _ref.deleted;
|
1609 | return {
|
1610 | deleted: deleted.filter(function (t) {
|
1611 | return t.key !== key;
|
1612 | })
|
1613 | };
|
1614 | });
|
1615 |
|
1616 | onRest && onRest(item, state, values);
|
1617 | }
|
1618 | };
|
1619 | };
|
1620 |
|
1621 | _this.state = {
|
1622 | first: true,
|
1623 | transitions: [],
|
1624 | current: {},
|
1625 | deleted: [],
|
1626 | prevProps: prevProps
|
1627 | };
|
1628 | return _this;
|
1629 | }
|
1630 |
|
1631 | Transition.getDerivedStateFromProps = function getDerivedStateFromProps(props, _ref2) {
|
1632 | var first = _ref2.first,
|
1633 | prevProps = _ref2.prevProps,
|
1634 | state = _objectWithoutPropertiesLoose(_ref2, ["first", "prevProps"]);
|
1635 |
|
1636 | var _get = get(props),
|
1637 | items = _get.items,
|
1638 | keys = _get.keys,
|
1639 | initial = _get.initial,
|
1640 | from = _get.from,
|
1641 | enter = _get.enter,
|
1642 | leave = _get.leave,
|
1643 | update = _get.update,
|
1644 | _get$trail = _get.trail,
|
1645 | trail = _get$trail === void 0 ? 0 : _get$trail,
|
1646 | unique = _get.unique,
|
1647 | config = _get.config;
|
1648 |
|
1649 | var _get2 = get(prevProps),
|
1650 | _keys = _get2.keys,
|
1651 | _items = _get2.items;
|
1652 |
|
1653 | var current = _extends({}, state.current);
|
1654 |
|
1655 | var deleted = [].concat(state.deleted);
|
1656 |
|
1657 | var currentKeys = Object.keys(current);
|
1658 | var currentSet = new Set(currentKeys);
|
1659 | var nextSet = new Set(keys);
|
1660 | var added = keys.filter(function (item) {
|
1661 | return !currentSet.has(item);
|
1662 | });
|
1663 | var removed = state.transitions.filter(function (item) {
|
1664 | return !item.destroyed && !nextSet.has(item.originalKey);
|
1665 | }).map(function (i) {
|
1666 | return i.originalKey;
|
1667 | });
|
1668 | var updated = keys.filter(function (item) {
|
1669 | return currentSet.has(item);
|
1670 | });
|
1671 | var delay = 0;
|
1672 | added.forEach(function (key) {
|
1673 |
|
1674 | if (unique && deleted.find(function (d) {
|
1675 | return d.originalKey === key;
|
1676 | })) deleted = deleted.filter(function (t) {
|
1677 | return t.originalKey !== key;
|
1678 | });
|
1679 | var keyIndex = keys.indexOf(key);
|
1680 | var item = items[keyIndex];
|
1681 | var state = 'enter';
|
1682 | current[key] = {
|
1683 | state: state,
|
1684 | originalKey: key,
|
1685 | key: unique ? String(key) : guid++,
|
1686 | item: item,
|
1687 | trail: delay = delay + trail,
|
1688 | config: callProp(config, item, state),
|
1689 | from: callProp(first ? initial !== void 0 ? initial || {} : from : from, item),
|
1690 | to: callProp(enter, item)
|
1691 | };
|
1692 | });
|
1693 | removed.forEach(function (key) {
|
1694 | var keyIndex = _keys.indexOf(key);
|
1695 |
|
1696 | var item = _items[keyIndex];
|
1697 | var state = 'leave';
|
1698 | deleted.push(_extends({}, current[key], {
|
1699 | state: state,
|
1700 | destroyed: true,
|
1701 | left: _keys[Math.max(0, keyIndex - 1)],
|
1702 | right: _keys[Math.min(_keys.length, keyIndex + 1)],
|
1703 | trail: delay = delay + trail,
|
1704 | config: callProp(config, item, state),
|
1705 | to: callProp(leave, item)
|
1706 | }));
|
1707 | delete current[key];
|
1708 | });
|
1709 | updated.forEach(function (key) {
|
1710 | var keyIndex = keys.indexOf(key);
|
1711 | var item = items[keyIndex];
|
1712 | var state = 'update';
|
1713 | current[key] = _extends({}, current[key], {
|
1714 | item: item,
|
1715 | state: state,
|
1716 | trail: delay = delay + trail,
|
1717 | config: callProp(config, item, state),
|
1718 | to: callProp(update, item)
|
1719 | });
|
1720 | });
|
1721 |
|
1722 | var out = keys.map(function (key) {
|
1723 | return current[key];
|
1724 | });
|
1725 | deleted.forEach(function (_ref3) {
|
1726 | var left = _ref3.left,
|
1727 | right = _ref3.right,
|
1728 | transition = _objectWithoutPropertiesLoose(_ref3, ["left", "right"]);
|
1729 |
|
1730 | var pos;
|
1731 |
|
1732 | if ((pos = out.findIndex(function (t) {
|
1733 | return t.originalKey === left;
|
1734 | })) !== -1) pos += 1;
|
1735 |
|
1736 | if (pos === -1) pos = out.findIndex(function (t) {
|
1737 | return t.originalKey === right;
|
1738 | });
|
1739 |
|
1740 | if (pos === -1) pos = deleted.findIndex(function (t) {
|
1741 | return t.originalKey === left;
|
1742 | });
|
1743 |
|
1744 | if (pos === -1) pos = deleted.findIndex(function (t) {
|
1745 | return t.originalKey === right;
|
1746 | });
|
1747 |
|
1748 | pos = Math.max(0, pos);
|
1749 | out = [].concat(out.slice(0, pos), [transition], out.slice(pos));
|
1750 | });
|
1751 | return {
|
1752 | first: first && added.length === 0,
|
1753 | transitions: out,
|
1754 | current: current,
|
1755 | deleted: deleted,
|
1756 | prevProps: props
|
1757 | };
|
1758 | };
|
1759 |
|
1760 | _proto.render = function render() {
|
1761 | var _this2 = this;
|
1762 |
|
1763 | var _this$props2 = this.props,
|
1764 | initial = _this$props2.initial,
|
1765 | _this$props2$from = _this$props2.from,
|
1766 | _this$props2$enter = _this$props2.enter,
|
1767 | _this$props2$leave = _this$props2.leave,
|
1768 | _this$props2$update = _this$props2.update,
|
1769 | onDestroyed = _this$props2.onDestroyed,
|
1770 | keys = _this$props2.keys,
|
1771 | items = _this$props2.items,
|
1772 | onFrame = _this$props2.onFrame,
|
1773 | onRest = _this$props2.onRest,
|
1774 | onStart = _this$props2.onStart,
|
1775 | trail = _this$props2.trail,
|
1776 | config = _this$props2.config,
|
1777 | _children = _this$props2.children,
|
1778 | unique = _this$props2.unique,
|
1779 | reset = _this$props2.reset,
|
1780 | extra = _objectWithoutPropertiesLoose(_this$props2, ["initial", "from", "enter", "leave", "update", "onDestroyed", "keys", "items", "onFrame", "onRest", "onStart", "trail", "config", "children", "unique", "reset"]);
|
1781 |
|
1782 | return this.state.transitions.map(function (_ref4, i) {
|
1783 | var _ref5;
|
1784 |
|
1785 | var state = _ref4.state,
|
1786 | key = _ref4.key,
|
1787 | item = _ref4.item,
|
1788 | from = _ref4.from,
|
1789 | to = _ref4.to,
|
1790 | trail = _ref4.trail,
|
1791 | config = _ref4.config,
|
1792 | destroyed = _ref4.destroyed;
|
1793 | return React__default.createElement(Keyframes, _extends({
|
1794 | reset: reset && state === 'enter',
|
1795 | primitive: Spring,
|
1796 | state: state,
|
1797 | filter: interpolateTo,
|
1798 | states: (_ref5 = {}, _ref5[state] = to, _ref5),
|
1799 | key: key,
|
1800 | onRest: destroyed ? _this2.destroyItem(item, key, state) : onRest && function (values) {
|
1801 | return onRest(item, state, values);
|
1802 | },
|
1803 | onStart: onStart && function () {
|
1804 | return onStart(item, state);
|
1805 | },
|
1806 | onFrame: onFrame && function (values) {
|
1807 | return onFrame(item, state, values);
|
1808 | },
|
1809 | delay: trail,
|
1810 | config: config
|
1811 | }, extra, {
|
1812 | from: from,
|
1813 | children: function children(props) {
|
1814 | var child = _children(item, state, i);
|
1815 |
|
1816 | return child ? child(props) : null;
|
1817 | }
|
1818 | }));
|
1819 | });
|
1820 | };
|
1821 |
|
1822 | return Transition;
|
1823 | }(React__default.PureComponent);
|
1824 |
|
1825 | Transition.defaultProps = {
|
1826 | keys: function keys(item) {
|
1827 | return item;
|
1828 | },
|
1829 | unique: false,
|
1830 | reset: false
|
1831 | };
|
1832 |
|
1833 |
|
1834 |
|
1835 | var stringShapeRegex = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
|
1836 |
|
1837 | function createInterpolation(config$$1) {
|
1838 | var outputRange = config$$1.output;
|
1839 | var outputRanges = outputRange[0].match(stringShapeRegex).map(function () {
|
1840 | return [];
|
1841 | });
|
1842 | outputRange.forEach(function (value) {
|
1843 | value.match(stringShapeRegex).forEach(function (number, i) {
|
1844 | return outputRanges[i].push(+number);
|
1845 | });
|
1846 | });
|
1847 | var interpolations = outputRange[0].match(stringShapeRegex).map(function (_, i) {
|
1848 | return Interpolation.create(_extends({}, config$$1, {
|
1849 | output: outputRanges[i]
|
1850 | }));
|
1851 | });
|
1852 | return function (input) {
|
1853 | var i = 0;
|
1854 | return outputRange[0].replace(stringShapeRegex, function () {
|
1855 | return interpolations[i++](input);
|
1856 | });
|
1857 | };
|
1858 | }
|
1859 |
|
1860 |
|
1861 | injectFrame(function (cb) {
|
1862 | return setTimeout(cb, 1000 / 30);
|
1863 | }, function (r) {
|
1864 | return clearTimeout(r);
|
1865 | });
|
1866 | injectInterpolation(createInterpolation);
|
1867 | injectApplyAnimatedValues(function () {
|
1868 | return false;
|
1869 | }, function (style) {
|
1870 | return style;
|
1871 | });
|
1872 |
|
1873 | exports.Spring = Spring;
|
1874 | exports.Keyframes = Keyframes;
|
1875 | exports.Transition = Transition;
|
1876 | exports.Trail = Trail;
|
1877 | exports.Controller = Controller;
|
1878 | exports.config = config;
|
1879 | exports.animated = createAnimatedComponent;
|
1880 | exports.interpolate = interpolate$1;
|
1881 | exports.Globals = Globals;
|