bc518174
王天杨
提交两个项目文件
|
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
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["children", "begin", "duration", "attributeName", "easing", "isActive", "steps", "from", "to", "canBegin", "onAnimationEnd", "shouldReAnimate", "onAnimationReStart"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
import React, { PureComponent, cloneElement, Children } from 'react';
import PropTypes from 'prop-types';
import { deepEqual } from 'fast-equals';
import createAnimateManager from './AnimateManager';
import { configEasing } from './easing';
import configUpdate from './configUpdate';
import { getTransitionVal, identity } from './util';
var Animate = /*#__PURE__*/function (_PureComponent) {
_inherits(Animate, _PureComponent);
var _super = _createSuper(Animate);
function Animate(props, context) {
var _this;
_classCallCheck(this, Animate);
_this = _super.call(this, props, context);
var _this$props = _this.props,
isActive = _this$props.isActive,
attributeName = _this$props.attributeName,
from = _this$props.from,
to = _this$props.to,
steps = _this$props.steps,
children = _this$props.children,
duration = _this$props.duration;
_this.handleStyleChange = _this.handleStyleChange.bind(_assertThisInitialized(_this));
_this.changeStyle = _this.changeStyle.bind(_assertThisInitialized(_this));
if (!isActive || duration <= 0) {
_this.state = {
style: {}
};
// if children is a function and animation is not active, set style to 'to'
if (typeof children === 'function') {
_this.state = {
style: to
};
}
return _possibleConstructorReturn(_this);
}
if (steps && steps.length) {
_this.state = {
style: steps[0].style
};
} else if (from) {
if (typeof children === 'function') {
_this.state = {
style: from
};
return _possibleConstructorReturn(_this);
}
_this.state = {
style: attributeName ? _defineProperty({}, attributeName, from) : from
};
} else {
_this.state = {
style: {}
};
}
return _this;
}
_createClass(Animate, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props2 = this.props,
isActive = _this$props2.isActive,
canBegin = _this$props2.canBegin;
this.mounted = true;
if (!isActive || !canBegin) {
return;
}
this.runAnimation(this.props);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props3 = this.props,
isActive = _this$props3.isActive,
canBegin = _this$props3.canBegin,
attributeName = _this$props3.attributeName,
shouldReAnimate = _this$props3.shouldReAnimate,
to = _this$props3.to,
currentFrom = _this$props3.from;
var style = this.state.style;
if (!canBegin) {
return;
}
if (!isActive) {
var newState = {
style: attributeName ? _defineProperty({}, attributeName, to) : to
};
if (this.state && style) {
if (attributeName && style[attributeName] !== to || !attributeName && style !== to) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState(newState);
}
}
return;
}
if (deepEqual(prevProps.to, to) && prevProps.canBegin && prevProps.isActive) {
return;
}
var isTriggered = !prevProps.canBegin || !prevProps.isActive;
if (this.manager) {
this.manager.stop();
}
if (this.stopJSAnimation) {
this.stopJSAnimation();
}
var from = isTriggered || shouldReAnimate ? currentFrom : prevProps.to;
if (this.state && style) {
var _newState = {
style: attributeName ? _defineProperty({}, attributeName, from) : from
};
if (attributeName && style[attributeName] !== from || !attributeName && style !== from) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState(_newState);
}
}
this.runAnimation(_objectSpread(_objectSpread({}, this.props), {}, {
from: from,
begin: 0
}));
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.mounted = false;
var onAnimationEnd = this.props.onAnimationEnd;
if (this.unSubscribe) {
this.unSubscribe();
}
if (this.manager) {
this.manager.stop();
this.manager = null;
}
if (this.stopJSAnimation) {
this.stopJSAnimation();
}
if (onAnimationEnd) {
onAnimationEnd();
}
}
}, {
key: "handleStyleChange",
value: function handleStyleChange(style) {
this.changeStyle(style);
}
}, {
key: "changeStyle",
value: function changeStyle(style) {
if (this.mounted) {
this.setState({
style: style
});
}
}
}, {
key: "runJSAnimation",
value: function runJSAnimation(props) {
var _this2 = this;
var from = props.from,
to = props.to,
duration = props.duration,
easing = props.easing,
begin = props.begin,
onAnimationEnd = props.onAnimationEnd,
onAnimationStart = props.onAnimationStart;
var startAnimation = configUpdate(from, to, configEasing(easing), duration, this.changeStyle);
var finalStartAnimation = function finalStartAnimation() {
_this2.stopJSAnimation = startAnimation();
};
this.manager.start([onAnimationStart, begin, finalStartAnimation, duration, onAnimationEnd]);
}
}, {
key: "runStepAnimation",
value: function runStepAnimation(props) {
var _this3 = this;
var steps = props.steps,
begin = props.begin,
onAnimationStart = props.onAnimationStart;
var _steps$ = steps[0],
initialStyle = _steps$.style,
_steps$$duration = _steps$.duration,
initialTime = _steps$$duration === void 0 ? 0 : _steps$$duration;
var addStyle = function addStyle(sequence, nextItem, index) {
if (index === 0) {
return sequence;
}
var duration = nextItem.duration,
_nextItem$easing = nextItem.easing,
easing = _nextItem$easing === void 0 ? 'ease' : _nextItem$easing,
style = nextItem.style,
nextProperties = nextItem.properties,
onAnimationEnd = nextItem.onAnimationEnd;
var preItem = index > 0 ? steps[index - 1] : nextItem;
var properties = nextProperties || Object.keys(style);
if (typeof easing === 'function' || easing === 'spring') {
return [].concat(_toConsumableArray(sequence), [_this3.runJSAnimation.bind(_this3, {
from: preItem.style,
to: style,
duration: duration,
easing: easing
}), duration]);
}
var transition = getTransitionVal(properties, duration, easing);
var newStyle = _objectSpread(_objectSpread(_objectSpread({}, preItem.style), style), {}, {
transition: transition
});
return [].concat(_toConsumableArray(sequence), [newStyle, duration, onAnimationEnd]).filter(identity);
};
return this.manager.start([onAnimationStart].concat(_toConsumableArray(steps.reduce(addStyle, [initialStyle, Math.max(initialTime, begin)])), [props.onAnimationEnd]));
}
}, {
key: "runAnimation",
value: function runAnimation(props) {
if (!this.manager) {
this.manager = createAnimateManager();
}
var begin = props.begin,
duration = props.duration,
attributeName = props.attributeName,
propsTo = props.to,
easing = props.easing,
onAnimationStart = props.onAnimationStart,
onAnimationEnd = props.onAnimationEnd,
steps = props.steps,
children = props.children;
var manager = this.manager;
this.unSubscribe = manager.subscribe(this.handleStyleChange);
if (typeof easing === 'function' || typeof children === 'function' || easing === 'spring') {
this.runJSAnimation(props);
return;
}
if (steps.length > 1) {
this.runStepAnimation(props);
return;
}
var to = attributeName ? _defineProperty({}, attributeName, propsTo) : propsTo;
var transition = getTransitionVal(Object.keys(to), duration, easing);
manager.start([onAnimationStart, begin, _objectSpread(_objectSpread({}, to), {}, {
transition: transition
}), duration, onAnimationEnd]);
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
children = _this$props4.children,
begin = _this$props4.begin,
duration = _this$props4.duration,
attributeName = _this$props4.attributeName,
easing = _this$props4.easing,
isActive = _this$props4.isActive,
steps = _this$props4.steps,
from = _this$props4.from,
to = _this$props4.to,
canBegin = _this$props4.canBegin,
onAnimationEnd = _this$props4.onAnimationEnd,
shouldReAnimate = _this$props4.shouldReAnimate,
onAnimationReStart = _this$props4.onAnimationReStart,
others = _objectWithoutProperties(_this$props4, _excluded);
var count = Children.count(children);
// eslint-disable-next-line react/destructuring-assignment
var stateStyle = this.state.style;
if (typeof children === 'function') {
return children(stateStyle);
}
if (!isActive || count === 0 || duration <= 0) {
return children;
}
var cloneContainer = function cloneContainer(container) {
var _container$props = container.props,
_container$props$styl = _container$props.style,
style = _container$props$styl === void 0 ? {} : _container$props$styl,
className = _container$props.className;
var res = /*#__PURE__*/cloneElement(container, _objectSpread(_objectSpread({}, others), {}, {
style: _objectSpread(_objectSpread({}, style), stateStyle),
className: className
}));
return res;
};
if (count === 1) {
return cloneContainer(Children.only(children));
}
return /*#__PURE__*/React.createElement("div", null, Children.map(children, function (child) {
return cloneContainer(child);
}));
}
}]);
return Animate;
}(PureComponent);
Animate.displayName = 'Animate';
Animate.defaultProps = {
begin: 0,
duration: 1000,
from: '',
to: '',
attributeName: '',
easing: 'ease',
isActive: true,
canBegin: true,
steps: [],
onAnimationEnd: function onAnimationEnd() {},
onAnimationStart: function onAnimationStart() {}
};
Animate.propTypes = {
from: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
to: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
attributeName: PropTypes.string,
// animation duration
duration: PropTypes.number,
begin: PropTypes.number,
easing: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
steps: PropTypes.arrayOf(PropTypes.shape({
duration: PropTypes.number.isRequired,
style: PropTypes.object.isRequired,
easing: PropTypes.oneOfType([PropTypes.oneOf(['ease', 'ease-in', 'ease-out', 'ease-in-out', 'linear']), PropTypes.func]),
// transition css properties(dash case), optional
properties: PropTypes.arrayOf('string'),
onAnimationEnd: PropTypes.func
})),
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
isActive: PropTypes.bool,
canBegin: PropTypes.bool,
onAnimationEnd: PropTypes.func,
// decide if it should reanimate with initial from style when props change
shouldReAnimate: PropTypes.bool,
onAnimationStart: PropTypes.func,
onAnimationReStart: PropTypes.func
};
export default Animate;
|