UNPKG

59.5 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
8var _inheritsLoose = _interopDefault(require('@babel/runtime/helpers/inheritsLoose'));
9var _assertThisInitialized = _interopDefault(require('@babel/runtime/helpers/assertThisInitialized'));
10var _objectWithoutPropertiesLoose = _interopDefault(require('@babel/runtime/helpers/objectWithoutPropertiesLoose'));
11var React = require('react');
12var React__default = _interopDefault(React);
13
14var bugfixes = undefined;
15var applyAnimatedValues = undefined;
16var colorNames = [];
17var requestFrame = function requestFrame(cb) {
18 return typeof window !== 'undefined' && window.requestAnimationFrame(cb);
19};
20var cancelFrame = function cancelFrame(cb) {
21 return typeof window !== 'undefined' && window.cancelAnimationFrame(cb);
22};
23var interpolation = undefined;
24var now = function now() {
25 return Date.now();
26};
27var defaultElement = undefined;
28var createAnimatedStyle = undefined;
29var injectApplyAnimatedValues = function injectApplyAnimatedValues(fn, transform) {
30 return applyAnimatedValues = {
31 fn: fn,
32 transform: transform
33 };
34};
35var injectColorNames = function injectColorNames(names) {
36 return colorNames = names;
37};
38var injectBugfixes = function injectBugfixes(fn) {
39 return bugfixes = fn;
40};
41var injectInterpolation = function injectInterpolation(cls) {
42 return interpolation = cls;
43};
44var injectFrame = function injectFrame(raf, caf) {
45 var _ref;
46
47 return _ref = [raf, caf], requestFrame = _ref[0], cancelFrame = _ref[1], _ref;
48};
49var injectNow = function injectNow(nowFn) {
50 return now = nowFn;
51};
52var injectDefaultElement = function injectDefaultElement(el) {
53 return defaultElement = el;
54};
55var injectCreateAnimatedStyle = function injectCreateAnimatedStyle(factory) {
56 return createAnimatedStyle = factory;
57};
58
59var Globals = /*#__PURE__*/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
79var Animated =
80/*#__PURE__*/
81function () {
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
107var getValues = function getValues(object) {
108 return Object.keys(object).map(function (k) {
109 return object[k];
110 });
111};
112
113var AnimatedWithChildren =
114/*#__PURE__*/
115function (_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);
158var AnimatedArrayWithChildren =
159/*#__PURE__*/
160function (_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);
194var AnimatedObjectWithChildren =
195/*#__PURE__*/
196function (_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
249var Interpolation$1 =
250/*#__PURE__*/
251function () {
252 function Interpolation() {}
253
254 // Default config = config, args
255 // Short config = range, output, extrapolate
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
283function interpolate(input, inputMin, inputMax, outputMin, outputMax, easing, extrapolateLeft, extrapolateRight, map) {
284 var result = map ? map(input) : input; // Extrapolate
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; // Input Range
296
297 if (inputMin === -Infinity) result = -result;else if (inputMax === Infinity) result = result - inputMin;else result = (result - inputMin) / (inputMax - inputMin); // Easing
298
299 result = easing(result); // Output Range
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
305function 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
313var AnimatedInterpolation =
314/*#__PURE__*/
315function (_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 = // AnimatedArrays should unfold, except AnimatedInterpolation which is taken as is
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);
347var interpolate$1 = function interpolate(parents, config, arg) {
348 return parents && new AnimatedInterpolation(parents, config, arg);
349};
350
351/**
352 * Animated works by building a directed acyclic graph of dependencies
353 * transparently when you render your Animated components.
354 *
355 * new Animated.Value(0)
356 * .interpolate() .interpolate() new Animated.Value(1)
357 * opacity translateY scale
358 * style transform
359 * View#234 style
360 * View#123
361 *
362 * A) Top Down phase
363 * When an Animated.Value is updated, we recursively go down through this
364 * graph in order to find leaf nodes: the views that we flag as needing
365 * an update.
366 *
367 * B) Bottom Up phase
368 * When a view is flagged as needing an update, we recursively go back up
369 * in order to build the new value that it needs. The reason why we need
370 * this two-phases process is to deal with composite props such as
371 * transform which can receive values from multiple parents.
372 */
373
374function 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 * Standard value for driving animations. One `Animated.Value` can drive
381 * multiple properties in a synchronized fashion, but can only be driven by one
382 * mechanism at a time. Using a new mechanism (e.g. starting a new animation,
383 * or calling `setValue`) will stop any previous ones.
384 */
385
386
387var AnimatedValue =
388/*#__PURE__*/
389function (_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 // Values stay loyal to their original controller, this is also a way to
444 // detect trailing values originating from a foreign controller
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
460var AnimatedArray =
461/*#__PURE__*/
462function (_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
503var active = false;
504var controllers = new Set();
505
506var 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]; // If an animation is done, skip, until all of them conclude
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(); // Conclude animation if it's either immediate, or from-values match end-state
542
543 if (config.immediate || !isAnimated && !config.decay && from === to) {
544 animation.updateValue(to);
545 animation.done = true;
546 continue;
547 } // Doing delay here instead of setTimeout is one async worry less
548
549
550 if (config.delay && time - controller.startTime < config.delay) {
551 isDone = false;
552 continue;
553 } // Flag change
554
555
556 noChange = false; // Break animation when string values are involved
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 /** Duration easing */
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 /** Decay easing */
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 /** Spring easing */
575 lastTime = animation.lastTime !== void 0 ? animation.lastTime : time;
576 _velocity = animation.lastVelocity !== void 0 ? animation.lastVelocity : config.initialVelocity; // If we lost a lot of frames just jump to the end.
577
578 if (time > lastTime + 64) lastTime = time; // http://gafferongames.com/game-physics/fix-your-timestep/
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 } // Conditions for stopping the spring animation
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 } // Trails aren't done until their parents conclude
598
599
600 if (isAnimated && !config.toValues[valIdx].done) endOfAnimation = false;
601
602 if (endOfAnimation) {
603 // Ensure that we end up with a round value
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 } // Keep track of updated values only when necessary
611
612
613 if (controller.props.onFrame || !controller.props.native) controller.animatedProps[config.name] = config.interpolation.getValue();
614 } // Update callbacks in the end of the frame
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 } // Either call onEnd or next frame
621
622
623 if (isDone) {
624 controllers.delete(controller);
625 controller.debouncedOnEnd({
626 finished: true,
627 noChange: noChange
628 });
629 }
630 } // Loop over as long as there are controllers ...
631
632
633 if (controllers.size) requestFrame(frameLoop);else active = false;
634};
635
636var addController = function addController(controller) {
637 if (!controllers.has(controller)) {
638 controllers.add(controller);
639 if (!active) requestFrame(frameLoop);
640 active = true;
641 }
642};
643
644var removeController = function removeController(controller) {
645 if (controllers.has(controller)) {
646 controllers.delete(controller);
647 }
648};
649
650function withDefault(value, defaultValue) {
651 return value === undefined || value === null ? defaultValue : value;
652}
653function toArray(a) {
654 return a !== void 0 ? Array.isArray(a) ? a : [a] : [];
655}
656function 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}
671function 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}
678function getValues$1(object) {
679 return Object.keys(object).map(function (k) {
680 return object[k];
681 });
682}
683function 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}
708function 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}
719function 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}
726function 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}
733function handleRef(ref, forward) {
734 if (forward) {
735 // If it's a function, assume it's a ref callback
736 if (typeof forward === 'function') forward(ref);else if (typeof forward === 'object') {
737 // If it's an object and has a 'current' property, assume it's a ref object
738 forward.current = ref;
739 }
740 }
741
742 return ref;
743}
744
745var Controller =
746/*#__PURE__*/
747function () {
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; // Reverse values when requested
800
801
802 if (reverse) {
803 var _ref2 = [to, from];
804 from = _ref2[0];
805 to = _ref2[1];
806 }
807
808 this.hasChanged = false; // Attachment handling, trailed springs can "attach" themselves to a previous spring
809
810 var target = attach && attach(this); // Reset merged props when necessary
811
812 var extra = reset ? {} : this.merged; // This will collect all props that were ever set
813
814 this.merged = _extends({}, from, extra, to); // Reduces input { name: value } pairs into animated values
815
816 this.animations = Object.entries(this.merged).reduce(function (acc, _ref3, i) {
817 var name = _ref3[0],
818 value = _ref3[1];
819 // Issue cached entries, except on reset
820 var entry = !reset && acc[name] || {}; // Figure out what the value is supposed to be
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; // Detect changes, animated values will be checked in the raf-loop
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 } // Set immediate values
852
853 if (callProp(immediate, name)) parent.setValue(value, false); // Reset animated values
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 } // TODO: clean up ref in controller
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 // Reset collected changes since the animation has been stopped cold turkey
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
960var AnimatedProps =
961/*#__PURE__*/
962function (_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
983function createAnimatedComponent(Component) {
984 var AnimatedComponent =
985 /*#__PURE__*/
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 } // The system is best designed when setNativeProps is implemented. It is
1016 // able to avoid re-rendering and directly set the attributes that
1017 // changed. However, setNativeProps can only be implemented on leaf
1018 // native components. If you want to animate a composite component, you
1019 // need to re-render it. In this case, we have a fallback that uses
1020 // forceUpdate.
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); // When you call detach, it removes the element from the parent list
1029 // of children. If it goes to 0, then the parent also detaches itself
1030 // and so on.
1031 // An optimization is to attach the new elements and THEN detach the old
1032 // ones instead of detaching and THEN attaching.
1033 // This way the intermediate state isn't to go to 0 and trigger
1034 // this expensive recursive detaching to then re-attach everything on
1035 // the very next operation.
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
1081var 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
1108var Spring =
1109/*#__PURE__*/
1110function (_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 // Only call onRest if either we *were* mounted, or when there were changes
1163 if (_this.props.onRest && (wasMounted || !noChange)) _this.props.onRest(_this.controller.merged); // Restore end-state
1164
1165 if (_this.mounted && _this.didInject) {
1166 _this.afterInject = convertValues(_this.props);
1167
1168 _this.setState({
1169 internal: true
1170 });
1171 } // If we have an inject or values to apply after the animation we ping here
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 // componentDidUpdate isn't called on mount, we call it here to start animating
1188 this.componentDidUpdate();
1189 this.mounted = true;
1190 };
1191
1192 _proto.componentWillUnmount = function componentWillUnmount() {
1193 // Stop all ongoing animtions
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 // The following is a test against props that could alter the animation
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; // Inject phase -----------------------------------------------------------
1219 // Handle injected frames, for instance targets/web/fix-auto
1220 // An inject will return an intermediary React node which measures itself out
1221 // .. and returns a callback when the values sought after are ready, usually "auto".
1222
1223 if (this.props.inject && propsChanged && !this.injectProps) {
1224 var frame = this.props.inject(this.props, function (injectProps) {
1225 // The inject frame has rendered, now let's update animations...
1226 _this2.injectProps = injectProps;
1227
1228 _this2.setState({
1229 internal: true
1230 });
1231 }); // Render out injected frame
1232
1233 if (frame) return frame;
1234 } // Update phase -----------------------------------------------------------
1235
1236
1237 if (this.injectProps || propsChanged) {
1238 // We can potentially cause setState, but we're inside render, the flag prevents that
1239 this.didInject = false; // Update animations, this turns from/to props into AnimatedValues
1240 // An update can occur on injected props, or when own-props have changed.
1241
1242 if (this.injectProps) {
1243 this.controller.update(this.injectProps); // didInject is needed, because there will be a 3rd stage, where the original values
1244 // .. will be restored after the animation is finished. When someone animates towards
1245 // .. "auto", the end-result should be "auto", not "1999px", which would block nested
1246 // .. height/width changes.
1247
1248 this.didInject = true;
1249 } else if (propsChanged) this.controller.update(this.props); // Flag an update that occured, componentDidUpdate will start the animation later on
1250
1251
1252 this.didUpdate = true;
1253 this.afterInject = undefined;
1254 this.injectProps = undefined;
1255 } // Render phase -----------------------------------------------------------
1256 // Render out raw values or AnimatedValues depending on "native"
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 // The animation has to start *after* render, since at that point the scene
1267 // .. graph should be established, so we do it here. Unfortunatelly, non-native
1268 // .. animations as well as "auto"-injects call forceUpdate, so it's causing a loop.
1269 // .. didUpdate prevents that as it gets set only on prop changes.
1270 if (this.didUpdate) this.start();
1271 this.didUpdate = false;
1272 };
1273
1274 return Spring;
1275}(React__default.Component);
1276
1277Spring.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
1288var Trail =
1289/*#__PURE__*/
1290function (_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 // Add instance to set
1306 _this.instances.add(instance); // Return undefined on the first index and from then on the previous 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
1360Trail.defaultProps = {
1361 keys: function keys(item) {
1362 return item;
1363 }
1364};
1365
1366var DEFAULT = '__default';
1367
1368var KeyframesImpl =
1369/*#__PURE__*/
1370function (_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( // next
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 }, // cancel
1469 function () {
1470 return requestFrame(function () {
1471 return _this2.instance && _this2.instance.stop();
1472 });
1473 }, // ownprops
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"]); // Arrayed configs need an index to process
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
1522KeyframesImpl.defaultProps = {
1523 state: DEFAULT
1524};
1525var Keyframes = React__default.forwardRef(function (props, ref) {
1526 return React__default.createElement(KeyframesImpl, _extends({}, props, {
1527 forwardRef: ref
1528 }));
1529});
1530
1531Keyframes.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
1552Keyframes.Spring = function (states) {
1553 return Keyframes.create(Spring)(states, interpolateTo);
1554};
1555
1556Keyframes.Trail = function (states) {
1557 return Keyframes.create(Trail)(states, interpolateTo);
1558};
1559
1560var guid = 0;
1561
1562var 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); // Make sure numeric keys are interpreted as Strings (5 !== "5")
1569
1570 return _extends({
1571 items: items,
1572 keys: keys.map(function (key) {
1573 return String(key);
1574 })
1575 }, rest);
1576};
1577
1578var Transition =
1579/*#__PURE__*/
1580function (_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); // Compare next keys with current keys
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 // In unique mode, remove fading out transitions if their key comes in again
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 }); // This tries to restore order for deleted items by finding their last known siblings
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; // Was it the element on the left, if yes, move there ...
1731
1732 if ((pos = out.findIndex(function (t) {
1733 return t.originalKey === left;
1734 })) !== -1) pos += 1; // Or how about the element on the right ...
1735
1736 if (pos === -1) pos = out.findIndex(function (t) {
1737 return t.originalKey === right;
1738 }); // Maybe we'll find it in the list of deleted items
1739
1740 if (pos === -1) pos = deleted.findIndex(function (t) {
1741 return t.originalKey === left;
1742 }); // Checking right side as well
1743
1744 if (pos === -1) pos = deleted.findIndex(function (t) {
1745 return t.originalKey === right;
1746 }); // And if nothing else helps, move it to the start ¯\_(ツ)_/¯
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
1825Transition.defaultProps = {
1826 keys: function keys(item) {
1827 return item;
1828 },
1829 unique: false,
1830 reset: false
1831};
1832
1833// Solution: https://stackoverflow.com/questions/638565/parsing-scientific-notation-sensibly/658662
1834
1835var stringShapeRegex = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
1836
1837function 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} // Render 30/fps by default
1859
1860
1861injectFrame(function (cb) {
1862 return setTimeout(cb, 1000 / 30);
1863}, function (r) {
1864 return clearTimeout(r);
1865});
1866injectInterpolation(createInterpolation);
1867injectApplyAnimatedValues(function () {
1868 return false;
1869}, function (style) {
1870 return style;
1871});
1872
1873exports.Spring = Spring;
1874exports.Keyframes = Keyframes;
1875exports.Transition = Transition;
1876exports.Trail = Trail;
1877exports.Controller = Controller;
1878exports.config = config;
1879exports.animated = createAnimatedComponent;
1880exports.interpolate = interpolate$1;
1881exports.Globals = Globals;