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 = _interopNamespace(React);
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
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 |
|
101 |
|
102 |
|
103 | if (options === void 0) { options = {}; }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
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 | })));
|