UNPKG

7.5 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Motion = {}, global.React));
5}(this, (function (exports, React) { 'use strict';
6
7 function _interopNamespace(e) {
8 if (e && e.__esModule) return e;
9 var n = Object.create(null);
10 if (e) {
11 Object.keys(e).forEach(function (k) {
12 if (k !== 'default') {
13 var d = Object.getOwnPropertyDescriptor(e, k);
14 Object.defineProperty(n, k, d.get ? d : {
15 enumerable: true,
16 get: function () {
17 return e[k];
18 }
19 });
20 }
21 });
22 }
23 n['default'] = e;
24 return Object.freeze(n);
25 }
26
27 var React__namespace = /*#__PURE__*/_interopNamespace(React);
28
29 /*! *****************************************************************************
30 Copyright (c) Microsoft Corporation.
31
32 Permission to use, copy, modify, and/or distribute this software for any
33 purpose with or without fee is hereby granted.
34
35 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
36 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
37 AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
38 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
40 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
41 PERFORMANCE OF THIS SOFTWARE.
42 ***************************************************************************** */
43
44 var __assign = function() {
45 __assign = Object.assign || function __assign(t) {
46 for (var s, i = 1, n = arguments.length; i < n; i++) {
47 s = arguments[i];
48 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
49 }
50 return t;
51 };
52 return __assign.apply(this, arguments);
53 };
54
55 function __rest(s, e) {
56 var t = {};
57 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
58 t[p] = s[p];
59 if (s != null && typeof Object.getOwnPropertySymbols === "function")
60 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
61 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
62 t[p[i]] = s[p[i]];
63 }
64 return t;
65 }
66
67 function __read(o, n) {
68 var m = typeof Symbol === "function" && o[Symbol.iterator];
69 if (!m) return o;
70 var i = m.call(o), r, ar = [], e;
71 try {
72 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
73 }
74 catch (error) { e = { error: error }; }
75 finally {
76 try {
77 if (r && !r.done && (m = i["return"])) m.call(i);
78 }
79 finally { if (e) throw e.error; }
80 }
81 return ar;
82 }
83
84 function convertToBezierString(_a) {
85 var _b = __read(_a, 4), a = _b[0], b = _b[1], c = _b[2], d = _b[3];
86 return "cubic-bezier(" + a + ", " + b + ", " + c + ", " + d + ")";
87 }
88
89 function secondsToMilliseconds(seconds) {
90 return seconds * 1000;
91 }
92
93 function getTargetKeyframe(keyframes) {
94 return Array.isArray(keyframes) ? keyframes[keyframes.length - 1] : keyframes;
95 }
96
97 function animate(element,
98 // elements: Element | Element[] | NodeListOf<Element> | string,
99 keyframes, options) {
100 // if (typeof elements === "string") {
101 // elements = document.querySelectorAll(elements)
102 // }
103 if (options === void 0) { options = {}; }
104 // if (Array.isArray(elements) || elements instanceof NodeList) {
105 // const { stagger = 0, delay = 0 } = options
106 // return Array.from(elements).map((element, i) => {
107 // animate(element, keyframes, { ...options, delay: delay + stagger * i })
108 // })
109 // }
110 // const element = elements as HTMLElement
111 var _a = options.delay, delay = _a === void 0 ? 0 : _a, _b = options.duration, duration = _b === void 0 ? 0.3 : _b, _c = options.repeat, repeat = _c === void 0 ? 0 : _c, _d = options.initialProgress, iterationStart = _d === void 0 ? 0 : _d, _e = options.easing, easing = _e === void 0 ? "linear" : _e;
112 delay = secondsToMilliseconds(delay);
113 duration = secondsToMilliseconds(duration);
114 console.log({
115 delay: delay,
116 duration: duration,
117 easing: Array.isArray(easing) ? convertToBezierString(easing) : easing,
118 iterations: repeat + 1,
119 iterationStart: iterationStart,
120 });
121 function onComplete() {
122 Object.assign(element.style, getTargetKeyframe(keyframes));
123 }
124 var animation = element.animate(keyframes, {
125 delay: delay,
126 duration: duration,
127 easing: Array.isArray(easing) ? convertToBezierString(easing) : easing,
128 iterations: repeat + 1,
129 iterationStart: iterationStart,
130 });
131 animation.finished.then(onComplete);
132 }
133
134 function useAnimation(ref, target, options) {
135 var prevTarget = React.useRef({});
136 React.useEffect(function () {
137 if (!target)
138 return;
139 var targetKeyframe = {};
140 for (var key in target) {
141 if (target !== prevTarget.current[key]) {
142 targetKeyframe[key] = target[key];
143 }
144 }
145 if (Object.keys(targetKeyframe).length && ref.current) {
146 animate(ref.current, targetKeyframe, options);
147 }
148 prevTarget.current = target;
149 });
150 }
151 function useInitialRender() {
152 var isInitialRender = React.useRef(true);
153 React.useEffect(function () {
154 isInitialRender.current = false;
155 }, []);
156 return isInitialRender.current;
157 }
158 function createAnimatedComponent(Component) {
159 function Animated(_a, _externalRef) {
160 var options = _a.options, style = _a.style, first = _a.first, props = __rest(_a, ["options", "style", "first"]);
161 var ref = React.useRef(null);
162 useAnimation(ref, style, options);
163 var isInitialRender = useInitialRender();
164 return React__namespace.createElement(Component, __assign(__assign({}, props), { style: isInitialRender && first ? first : style, ref: ref }));
165 }
166 return React__namespace.forwardRef(Animated);
167 }
168 var components = new Map();
169 var animated = new Proxy({}, {
170 get: function (_, key) {
171 if (!components.has(key)) {
172 components.set(key, createAnimatedComponent(key));
173 }
174 return components.get(key);
175 },
176 });
177
178 exports.animate = animate;
179 exports.animated = animated;
180 exports.useAnimation = useAnimation;
181
182 Object.defineProperty(exports, '__esModule', { value: true });
183
184})));