This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
682 lines (504 loc) · 22.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
'use strict';
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _assign = require('babel-runtime/core-js/object/assign');
var _assign2 = _interopRequireDefault(_assign);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _class, _temp;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var React = require('react');
var T = require('prop-types');
var _require = require('react-motion'),
spring = _require.spring;
var Utils = require('./utils');
var _merge = require('lodash/merge');
module.exports = (_temp = _class = function (_React$PureComponent) {
(0, _inherits3.default)(StrangeMotion, _React$PureComponent);
function StrangeMotion(props) {
(0, _classCallCheck3.default)(this, StrangeMotion);
var _this = (0, _possibleConstructorReturn3.default)(this, (StrangeMotion.__proto__ || (0, _getPrototypeOf2.default)(StrangeMotion)).call(this));
var animConfig = void 0;
if (!props.animConfig) {
animConfig = _this._getDefaultAnimConfig();
} else {
animConfig = _this.processInitAnimConfig(props.animConfig);
}
var model = void 0;
var childIsFunc = void 0;
if (props.model) {
model = props.model;
}
if (typeof props.children === 'function') {
childIsFunc = true;
if (!props.model) {
throw new Error('StrangeMotion: Must pass in model if children is a function');
}
model = props.model;
} else {
childIsFunc = false;
if (props.model) {
throw new Error('StrangeMotion: Must pass function as child if model is provided in props');
}
model = Utils.getElementsFromChildren(props.children);
}
_this.state = {
animConfig: animConfig,
model: model,
childIsFunc: childIsFunc,
childWrapperComponent: props.childWrapperComponent,
childWrapperProps: props.childWrapperProps
};
_this.willEnter = _this._willEnter.bind(_this);
_this.willLeave = _this._willLeave.bind(_this);
_this.getStyles = _this._getStyles.bind(_this);
_this.getDefaultStyles = _this._getDefaultStyles.bind(_this);
_this.applyInterpolatedStyles = _this._applyInterpolatedStyles.bind(_this);
_this.genId = _this._genId.bind(_this);
_this.applyPlugins = _this._applyPlugins.bind(_this);
return _this;
}
(0, _createClass3.default)(StrangeMotion, [{
key: 'getChildren',
value: function getChildren(interpolatedStyles) {
var _this2 = this;
var _state = this.state,
animWrapperComponent = _state.animWrapperComponent,
animWrapperProps = _state.animWrapperProps,
childWrapperComponent = _state.childWrapperComponent,
childWrapperProps = _state.childWrapperProps,
childProps = _state.childProps,
model = _state.model;
var interpolatedAsArray = [].concat(interpolatedStyles);
if ((typeof interpolatedStyles === 'undefined' ? 'undefined' : (0, _typeof3.default)(interpolatedStyles)) === 'object' && !Array.isArray(interpolatedStyles)) {
// it's a Motion object
var interpolatedChild = interpolatedAsArray.map(function (style) {
var newChildPropsVal = {};
if (childProps) {
newChildPropsVal = childProps(style);
}
// Use model right here
var newChild = Array.isArray(model) ? model[0] : model;
return _this2.applyInterpolatedStyles({
style: style,
child: newChild,
childWrapperComponent: childWrapperComponent,
childWrapperProps: childWrapperProps,
newChildPropsVal: newChildPropsVal
});
})[0]; // Grab the first and only item here
if (!animWrapperComponent && !animWrapperProps) {
return interpolatedChild;
}
if (animWrapperComponent) {
return React.createElement(animWrapperComponent, animWrapperProps && animWrapperProps(interpolatedStyles) || {}, interpolatedChild);
} else {
return React.cloneElement(interpolatedChild, animWrapperProps && animWrapperProps(interpolatedStyles) || {});
}
}
// Apply new styles to children
var interpolatedChildren = interpolatedAsArray.map(function (_ref) {
var style = _ref.style,
data = _ref.data,
key = _ref.key;
var newChildPropsVal = {};
if (childProps) {
newChildPropsVal = childProps({ style: style, data: data, key: key });
}
return _this2.applyInterpolatedStyles({
style: style,
child: data,
key: key,
childWrapperComponent: childWrapperComponent,
childWrapperProps: childWrapperProps,
newChildPropsVal: newChildPropsVal
});
});
return React.createElement(animWrapperComponent || 'div', animWrapperProps && animWrapperProps(interpolatedStyles) || {}, interpolatedChildren);
}
}, {
key: '_applyInterpolatedStyles',
value: function _applyInterpolatedStyles(_ref2) {
var style = _ref2.style,
child = _ref2.child,
key = _ref2.key,
childWrapperComponent = _ref2.childWrapperComponent,
childWrapperProps = _ref2.childWrapperProps,
newChildPropsVal = _ref2.newChildPropsVal;
var stylePluginsApplied = this.applyPlugins({
pluginFunc: 'toCss',
applyTo: style
});
if (!key) {
key = this.genId();
}
var children = this.props.children;
var childStyle = child.props && child.props.style;
var newStyle = stylePluginsApplied;
newStyle = (0, _assign2.default)({}, childStyle || {}, stylePluginsApplied);
var newChild = void 0;
var newChildProps = newChildPropsVal;
if (this.state.childIsFunc && !childWrapperComponent) {
newChild = React.cloneElement(children({ style: newStyle, child: child, key: key }), (0, _extends3.default)({}, newChildProps, { style: style }));
} else if (this.state.childIsFunc && childWrapperComponent) {
newChild = React.cloneElement(children({ style: newStyle, child: child, key: key }), newChildProps);
}
if (!this.state.childIsFunc && !childWrapperComponent) {
newChild = React.cloneElement(child, (0, _extends3.default)({}, newChildProps, { style: newStyle }));
} else if (!this.state.childIsFunc && childWrapperComponent) {
newChild = React.cloneElement(child, newChildProps);
}
if (childWrapperComponent) {
var childWrapperPropsVal = {};
if (typeof childWrapperProps === 'function') {
childWrapperPropsVal = childWrapperProps({ child: child, key: key });
} else if (childWrapperProps) {
childWrapperPropsVal = childWrapperProps;
}
newChild = React.createElement(childWrapperComponent, (0, _extends3.default)({ key: key }, childWrapperPropsVal), newChild);
}
return newChild;
}
}, {
key: '_getDefaultAnimConfig',
value: function _getDefaultAnimConfig() {
return {
start: {},
beforeEnter: {},
enter: {},
leave: {}
};
}
}, {
key: 'processInitAnimConfig',
value: function processInitAnimConfig(animConfig) {
var animConfigWithDefaults = Utils.assignDefaultsToAnimConfig(animConfig);
var self = this;
var animConfigPluginsApplied = this._applyPlugins({
pluginFunc: 'assignAnimConfig',
applyTo: animConfigWithDefaults
});
var _Utils$assignAnimConf = Utils.assignAnimConfig({
beginAnimConfig: self.state && self.state.animConfig,
newAnimConfig: animConfigPluginsApplied
}),
assignedAnimConfig = _Utils$assignAnimConf.assignedAnimConfig,
delays = _Utils$assignAnimConf.delays;
if (delays) {
this.waitingDelays = delays;
}
return assignedAnimConfig;
}
}, {
key: '_applyPlugins',
value: function _applyPlugins(_ref3) {
var _this3 = this;
var pluginFunc = _ref3.pluginFunc,
applyTo = _ref3.applyTo;
if (!this.props) {
return applyTo;
}
var animPlugins = this.props.animPlugins;
// Plugin validation
var filteredAnimPlugins = [].concat(animPlugins).filter(function (plugin) {
return plugin ? true : false;
});
var pluginsApplied = applyTo;
// TODO implement Topo sorting for plugins
filteredAnimPlugins.forEach(function (plugin) {
if (plugin[pluginFunc]) {
if (pluginFunc === 'getStyles') {
pluginsApplied = plugin[pluginFunc](applyTo, _this3.reactMotion);
} else {
pluginsApplied = plugin[pluginFunc](applyTo);
}
}
if (plugin['getReactMotion']) {
plugin['getReactMotion'](_this3.reactMotion);
}
});
return pluginsApplied;
}
// Why do I have newAnimConfig here if nobody is using it?
// it's for documentation to make sure you know what you're passing in.
}, {
key: 'assignAnimConfig',
value: function assignAnimConfig(_ref4, setStateCb) {
var _this4 = this;
var passedInAnimConfig = _ref4.newAnimConfig;
var newAnimConfig = JSON.parse((0, _stringify2.default)(passedInAnimConfig));
if (passedInAnimConfig.enter && passedInAnimConfig.enter.$delay) {
var _passedInAnimConfig$e = passedInAnimConfig.enter,
$enterDelay = _passedInAnimConfig$e.$delay,
enterWithoutDelay = (0, _objectWithoutProperties3.default)(_passedInAnimConfig$e, ['$delay']);
// setup the $delay
setTimeout(function () {
var newAnimConfigClone = JSON.parse((0, _stringify2.default)(newAnimConfig));
_this4.assignAnimConfig({
newAnimConfig: { enter: enterWithoutDelay }
}, _this4._setNewAnimConfig(passedInAnimConfig, newAnimConfigClone));
}, $enterDelay);
var enter = passedInAnimConfig.enter,
rest = (0, _objectWithoutProperties3.default)(passedInAnimConfig, ['enter']);
if ((0, _keys2.default)(rest).length !== 0) {
newAnimConfig = rest;
}
}
this._setNewAnimConfig(passedInAnimConfig, newAnimConfig, setStateCb)();
}
}, {
key: '_setNewAnimConfig',
value: function _setNewAnimConfig(passedInAnimConfig, newAnimConfig, setStateCb) {
var _this5 = this;
var reactMotion = this.reactMotion;
var beginAnimConfig = this.state.animConfig;
return function () {
newAnimConfig = (0, _keys2.default)(newAnimConfig).reduce(function (collector, animType) {
var currentAnimType = passedInAnimConfig[animType];
var newAnimType = (0, _keys2.default)(currentAnimType).reduce(function (collector, cssPropName) {
var cssPropVal = currentAnimType[cssPropName];
if (cssPropVal === 'getLastIdealStyle') {
var lastIdealStyle = reactMotion.state.lastIdealStyle[cssPropName];
if (typeof lastIdealStyle !== 'undefined') {
collector[cssPropName] = lastIdealStyle;
}
} else {
collector[cssPropName] = cssPropVal;
}
return collector;
}, {});
collector[animType] = newAnimType;
return collector;
}, {});
//
var animConfigPluginsApplied = _this5.applyPlugins({
pluginFunc: 'assignAnimConfig',
applyTo: newAnimConfig
});
var _Utils$assignAnimConf2 = Utils.assignAnimConfig({
beginAnimConfig: beginAnimConfig,
newAnimConfig: animConfigPluginsApplied,
reactMotion: reactMotion
}),
assignedAnimConfig = _Utils$assignAnimConf2.assignedAnimConfig,
delays = _Utils$assignAnimConf2.delays;
if (newAnimConfig.start) {
return _this5.setState({
animConfig: (0, _extends3.default)({}, assignedAnimConfig, {
enter: newAnimConfig.start
})
}, function () {
_this5.setState({
animConfig: assignedAnimConfig
}, function () {
if (typeof setStateCb === 'function') {
setStateCb();
}
_this5.handleDelays(delays);
});
});
}
_this5.setState({
animConfig: assignedAnimConfig
}, function () {
if (typeof setStateCb === 'function') {
setStateCb();
}
_this5.handleDelays(delays);
});
};
}
}, {
key: 'handleDelays',
value: function handleDelays(delays) {
var _this6 = this;
// if there weren't any delays, '$delay' will be set to undefined
// from Utils.assignAnimConfig
if (delays) {
/*
Delays object is organized by its delay times. ex:
{
400: [{ enter: { top: 100, left: 100 } }],
10000: [{ enter: { top: 500, left: 300 }] }
}
}
*/
(0, _keys2.default)(delays).forEach(function (delay) {
setTimeout(function () {
[].concat(delays[delay]).forEach(function (currentDelay) {
_this6.assignAnimConfig({
newAnimConfig: currentDelay
});
});
}, delay);
});
};
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var _this7 = this;
var waitingDelays = this.waitingDelays;
if (waitingDelays) {
(0, _keys2.default)(waitingDelays).forEach(function ($delay) {
setTimeout(function () {
_this7.assignAnimConfig({ newAnimConfig: waitingDelays[$delay] });
}, $delay);
});
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var children = nextProps.children,
model = nextProps.model,
animConfig = nextProps.animConfig;
if (children && typeof children !== 'function') {
this.setState({
model: Utils.getElementsFromChildren(children)
});
}
if (model) {
this.setState({ model: model });
}
// Thanks to React, we won't have to worry
// about the same animConfig being passed in twice,
// so we can safely assume that whatever animConfig
// props the user assigns via the `animController` will
// take precedence in `getStyles()`, and won't be
// immediately overwritten with styles here.
// NOTE: This does mean that any animations defined with
// the animController will be overwritten with this one.
// Of course they can be changed again via the animController
// which will take over. This is basically now an alias of
// animController.mergeAnimConfig()
if (animConfig) {
this.assignAnimConfig({ newAnimConfig: animConfig });
}
}
}, {
key: 'filterChildrenForType',
value: function filterChildrenForType(children) {
return children;
}
}, {
key: '_genId',
value: function _genId() {
return Math.random().toString(16).slice(2);
}
}, {
key: '_willEnter',
// Only used with TransitionMotion
value: function _willEnter() {
var animConfig = this.state.animConfig;
return animConfig.beforeEnter;
}
}, {
key: '_willLeave',
// Only used with TransitionMotion
value: function _willLeave() {
var animConfig = this.state.animConfig;
return animConfig.leave;
}
}, {
key: '_getDefaultStyles',
value: function _getDefaultStyles() {
var _this8 = this;
var defaultStyles = this.getStyles('start').map(function (interpolatedStyle) {
var newCssVals = _this8.applyPlugins({
pluginFunc: 'getDefaultStyles',
applyTo: interpolatedStyle.style
});
// Do NOT deep merge this. applyPlugins is allowed to do
// transformations on the styles, and we want the `style`
// prop to be preserved in `{ style: newCssVals }`
var newInterpolatedStyle = (0, _assign2.default)({}, interpolatedStyle, { style: newCssVals });
return newInterpolatedStyle;
});
return defaultStyles;
}
}, {
key: '_getStyles',
value: function _getStyles(animConfigKey) {
var _this9 = this;
var _state2 = this.state,
model = _state2.model,
animConfig = _state2.animConfig;
var mergedAnimConfig = animConfig;
if (this.animController && this.animControllerAnimConfig) {
mergedAnimConfig = (0, _assign2.default)({}, animConfig, this.animControllerAnimConfig);
};
var filteredModel = void 0;
if (this.props.model) {
filteredModel = model;
} else {
// TODO This doesn't make any sense, it's only here for the toggle-motion
filteredModel = this.filterChildrenForType(model);
}
var newStyles = filteredModel.map(function (child, i) {
var key = child.key,
itemId = child.id,
name = child.name;
var newKey = key || '';
if (!key) {
if (itemId) {
newKey += itemId;
}
if (name) {
newKey += name + i;
}
}
if (newKey === '') {
newKey = _this9._genId();
}
var newCssVals = _this9.applyPlugins({
pluginFunc: 'getStyles',
applyTo: animConfigKey ? mergedAnimConfig[animConfigKey] : mergedAnimConfig.enter
});
return {
data: child,
style: newCssVals,
key: String(newKey)
};
});
return newStyles;
}
}]);
return StrangeMotion;
}(React.PureComponent), _class.propTypes = {
model: T.array,
animConfig: T.shape({
start: T.object,
beforeEnter: T.object,
enter: T.object.isRequired,
leave: T.object
}),
animPlugins: T.array,
children: T.any.isRequired,
animWrapperComponent: T.any,
animWrapperComponentRef: T.func,
animWrapperProps: T.func,
childWrapperComponent: T.any,
childWrapperProps: T.func,
childProps: T.func
}, _class.defaultSpring = {
stiffness: 170,
damping: 26,
precision: 0.01
}, _temp);