UNPKG

10.9 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createMotionAnimatedComponent = createMotionAnimatedComponent;
7exports.createMotionComponent = createMotionComponent;
8var _tools = require("@legendapp/tools");
9var _react = _interopRequireWildcard(require("react"));
10var _reactNative = require("react-native");
11var _configureMotion = require("./configureMotion");
12var _Constants = require("./Constants");
13var _MotionPressable = require("./MotionPressable");
14var _useTransformOrigin = require("./useTransformOrigin");
15function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
18const TransformKeys = {
19 x: 'translateX',
20 y: 'translateY',
21 scale: 'scale',
22 scaleX: 'scaleX',
23 scaleY: 'scaleY',
24 skewX: 'skewX',
25 skewY: 'skewY',
26 perspective: 'perspective',
27 rotate: 'rotate',
28 rotateX: 'rotateX',
29 rotateY: 'rotateY',
30 rotateZ: 'rotateZ',
31 matrix: 'matrix'
32};
33const OtherNativeKeys = {
34 opacity: 'opacity'
35};
36const DefaultValues = {
37 x: 0,
38 y: 0,
39 scale: 1,
40 scaleX: 1,
41 scaleY: 1,
42 skewX: '0deg',
43 skewY: '0deg',
44 perspective: 0,
45 rotate: '0deg',
46 rotateX: '0deg',
47 rotateY: '0deg',
48 rotateZ: '0deg',
49 matrix: [],
50 opacity: 1
51};
52const DefaultTransition = {
53 type: 'tween',
54 duration: _Constants.DefaultTransitionTime
55};
56const Eases = {
57 linear: _reactNative.Easing.linear,
58 easeIn: _reactNative.Easing.ease,
59 easeInOut: _reactNative.Easing.inOut(_reactNative.Easing.ease),
60 easeOut: _reactNative.Easing.out(_reactNative.Easing.ease),
61 circIn: _reactNative.Easing.circle,
62 circInOut: _reactNative.Easing.inOut(_reactNative.Easing.circle),
63 circOut: _reactNative.Easing.out(_reactNative.Easing.circle),
64 backIn: _reactNative.Easing.back(2),
65 backInOut: _reactNative.Easing.inOut(_reactNative.Easing.back(2)),
66 backOut: _reactNative.Easing.out(_reactNative.Easing.back(2))
67};
68function addKeysToSet() {
69 const set = new Set();
70 for (let i = 0; i < arguments.length; i++) {
71 const obj = i < 0 || arguments.length <= i ? undefined : arguments[i];
72 if (obj) {
73 const keys = Object.keys(obj);
74 for (let i = 0; i < keys.length; i++) {
75 set.add(keys[i]);
76 }
77 }
78 }
79 return set;
80}
81function createMotionComponent(Component) {
82 return /*#__PURE__*/(0, _react.forwardRef)(function MotionComponent(_ref,
83 // @ts-ignore
84 ref) {
85 let {
86 animate,
87 animateProps,
88 initial,
89 initialProps,
90 exit,
91 transition,
92 transformOrigin,
93 style: styleProp,
94 onLayout: onLayoutProp,
95 whileTap,
96 whileHover,
97 onAnimationComplete,
98 ...rest
99 } = _ref;
100 const refAnims = (0, _react.useRef)({});
101
102 // Generate the arrays of keys and values for transitioning. These are used as deps of useMemo
103 // so that it will update whenever a key or value changes.
104 const animKeysSet = addKeysToSet(initial, animate, animateProps, whileTap, whileHover, exit);
105 const values = Object.assign({}, animate);
106 if (animateProps) {
107 addKeysToSet(animKeysSet, animateProps);
108 Object.assign(values, animateProps);
109 }
110 if (whileTap || whileHover) {
111 const {
112 pressed,
113 hovered
114 } = (0, _react.useContext)(_MotionPressable.ContextPressable);
115 if (whileHover) {
116 addKeysToSet(animKeysSet, whileHover);
117 if (hovered) {
118 Object.assign(values, whileHover);
119 }
120 }
121 if (whileTap) {
122 addKeysToSet(animKeysSet, whileTap);
123 if (pressed) {
124 Object.assign(values, whileTap);
125 }
126 }
127 }
128 if (exit) {
129 addKeysToSet(animKeysSet, exit);
130 }
131 const animKeys = [...animKeysSet];
132 const animValues = animKeys.map(key => values[key]);
133 const update = () => {
134 const anims = refAnims.current;
135 const useNativeDriver = !animateProps && animKeys.every(key => !!OtherNativeKeys[key] || !!TransformKeys[key]);
136 for (let i = 0; i < animKeys.length; i++) {
137 var _ref2, _ref3;
138 const key = animKeys[i];
139 const isProp = (animateProps === null || animateProps === void 0 ? void 0 : animateProps[key]) !== undefined;
140 let value = values[key];
141 const valueInitial = (_ref2 = (_ref3 = isProp ? initialProps === null || initialProps === void 0 ? void 0 : initialProps[key] : initial === null || initial === void 0 ? void 0 : initial[key]) !== null && _ref3 !== void 0 ? _ref3 : value) !== null && _ref2 !== void 0 ? _ref2 : DefaultValues[key];
142 if (value === undefined) {
143 value = valueInitial !== null && valueInitial !== void 0 ? valueInitial : DefaultValues[key];
144 }
145 if (!anims[key] || anims[key].value !== value) {
146 const isStr = (0, _tools.isString)(valueInitial);
147 const isArr = (0, _tools.isArray)(valueInitial);
148
149 // If this is the first run or it's a new key, create the Animated.Value
150 if (!anims[key]) {
151 const startValue = isStr || isArr ? 1 : valueInitial;
152 const animValue = new _reactNative.Animated.Value(startValue);
153 anims[key] = {
154 value: valueInitial,
155 animValue,
156 valueInterp: isStr ? 1 : undefined
157 };
158 }
159 let toValue;
160 // If string or array it needs to interpolate, so toggle back and forth between 0 and 1,
161 // interpolating from current value to target value
162 if (isStr || isArr) {
163 const fromInterp = anims[key].valueInterp;
164 const from = anims[key].value;
165 anims[key].interpolation = anims[key].animValue.interpolate({
166 inputRange: [0, 1],
167 outputRange: fromInterp === 1 ? [value, from] : [from, value]
168 });
169 anims[key].valueInterp = toValue = 1 - fromInterp;
170 anims[key].value = value;
171 } else {
172 anims[key].value = toValue = value;
173 }
174
175 // Get the transition for this key, the 'default' key, the root transition, or default transition if no transition prop
176 const transitionForKey = (transition === null || transition === void 0 ? void 0 : transition[key]) || (transition === null || transition === void 0 ? void 0 : transition['default']) || transition || DefaultTransition;
177 if (_configureMotion.config.timing === 's' && transitionForKey !== DefaultTransition && (0, _tools.isNumber)(transitionForKey.duration)) {
178 transitionForKey.duration *= 1000;
179 }
180 if ((0, _tools.isString)(transitionForKey.easing)) {
181 transitionForKey.easing = Eases[transitionForKey.easing];
182 }
183 if ((0, _tools.isString)(transitionForKey.ease)) {
184 transitionForKey.ease = Eases[transitionForKey.ease];
185 }
186 const animOptions = Object.assign({
187 toValue,
188 useNativeDriver
189 }, transitionForKey);
190
191 // This typeof check is to make it work when rendered server-side like in Next.js
192 if (typeof requestAnimationFrame !== 'undefined') {
193 requestAnimationFrame(() => {
194 const callback = onAnimationComplete ? () => onAnimationComplete(key) : undefined;
195 const {
196 loop,
197 type
198 } = transitionForKey;
199 let animation;
200
201 // Spring or timing based on the transition prop
202 if (type === 'spring') {
203 animation = _reactNative.Animated.spring(anims[key].animValue, animOptions);
204 } else {
205 animation = _reactNative.Animated.timing(anims[key].animValue, animOptions);
206 }
207
208 // Loop based on the transition prop
209 if (loop !== undefined) {
210 animation = _reactNative.Animated.loop(animation, {
211 iterations: loop
212 });
213 }
214 animation.start(callback);
215 });
216 }
217 }
218 }
219 };
220 (0, _react.useMemo)(update, animValues); // eslint-disable-line react-hooks/exhaustive-deps
221
222 // Apply the animations to the style object
223 const style = {};
224 const animProps = {};
225 const transforms = [];
226 Object.entries(refAnims.current).forEach(_ref4 => {
227 let [key, value] = _ref4;
228 if ((animateProps === null || animateProps === void 0 ? void 0 : animateProps[key]) !== undefined) {
229 animProps[key] = value.interpolation || value.animValue;
230 } else if (TransformKeys[key]) {
231 transforms.push({
232 key,
233 value
234 });
235 } else {
236 style[key] = value.interpolation || value.animValue;
237 }
238 });
239
240 // Map the transforms into an Animated transforms array
241 if (transforms.length) {
242 style.transform = transforms.map(_ref5 => {
243 let {
244 key,
245 value
246 } = _ref5;
247 return {
248 [TransformKeys[key]]: value.interpolation || value.animValue
249 };
250 });
251 }
252 const onLayout = transformOrigin ? (0, _useTransformOrigin.useTransformOrigin)(transformOrigin, style.transform, onLayoutProp) : onLayoutProp;
253
254 // @ts-ignore
255 return /*#__PURE__*/_react.default.createElement(Component, _extends({
256 style: [styleProp, style],
257 onLayout: onLayout
258 }, rest, animProps, {
259 ref: ref
260 }));
261 });
262}
263function createMotionAnimatedComponent(component) {
264 return createMotionComponent(_reactNative.Animated.createAnimatedComponent(component));
265}
266//# sourceMappingURL=createMotionComponent.js.map
\No newline at end of file