framer-motion 5.4.1 → 5.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,362 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var useMotionValue = require('./use-motion-value-73ed7c77.js');
7
+ var React = require('react');
8
+ var popmotion = require('popmotion');
9
+ var three = require('three');
10
+ var mergeRefs = require('react-merge-refs');
11
+ var useMeasure = require('react-use-measure');
12
+ var fiber = require('@react-three/fiber');
13
+ require('hey-listen');
14
+ require('framesync');
15
+ require('style-value-types');
16
+
17
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
+
19
+ function _interopNamespace(e) {
20
+ if (e && e.__esModule) return e;
21
+ var n = Object.create(null);
22
+ if (e) {
23
+ Object.keys(e).forEach(function (k) {
24
+ if (k !== 'default') {
25
+ var d = Object.getOwnPropertyDescriptor(e, k);
26
+ Object.defineProperty(n, k, d.get ? d : {
27
+ enumerable: true,
28
+ get: function () { return e[k]; }
29
+ });
30
+ }
31
+ });
32
+ }
33
+ n["default"] = e;
34
+ return Object.freeze(n);
35
+ }
36
+
37
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
38
+ var mergeRefs__default = /*#__PURE__*/_interopDefaultLegacy(mergeRefs);
39
+ var useMeasure__default = /*#__PURE__*/_interopDefaultLegacy(useMeasure);
40
+
41
+ function useHover(isStatic, _a, visualElement) {
42
+ var whileHover = _a.whileHover, onHoverStart = _a.onHoverStart, onHoverEnd = _a.onHoverEnd, onPointerOver = _a.onPointerOver, onPointerOut = _a.onPointerOut;
43
+ var isHoverEnabled = whileHover || onHoverStart || onHoverEnd;
44
+ if (isStatic || !visualElement || !isHoverEnabled)
45
+ return {};
46
+ return {
47
+ onPointerOver: function (event) {
48
+ var _a;
49
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(useMotionValue.AnimationType.Hover, true);
50
+ onPointerOver === null || onPointerOver === void 0 ? void 0 : onPointerOver(event);
51
+ },
52
+ onPointerOut: function (event) {
53
+ var _a;
54
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(useMotionValue.AnimationType.Hover, false);
55
+ onPointerOut === null || onPointerOut === void 0 ? void 0 : onPointerOut(event);
56
+ },
57
+ };
58
+ }
59
+
60
+ function useTap(isStatic, _a, visualElement) {
61
+ var whileTap = _a.whileTap, onTapStart = _a.onTapStart, onTap = _a.onTap, onTapCancel = _a.onTapCancel, onPointerDown = _a.onPointerDown;
62
+ var isTapEnabled = onTap || onTapStart || onTapCancel || whileTap;
63
+ var isPressing = React.useRef(false);
64
+ var cancelPointerEndListeners = React.useRef(null);
65
+ if (isStatic || !visualElement || !isTapEnabled)
66
+ return {};
67
+ function removePointerEndListener() {
68
+ var _a;
69
+ (_a = cancelPointerEndListeners.current) === null || _a === void 0 ? void 0 : _a.call(cancelPointerEndListeners);
70
+ cancelPointerEndListeners.current = null;
71
+ }
72
+ function checkPointerEnd() {
73
+ var _a;
74
+ removePointerEndListener();
75
+ isPressing.current = false;
76
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(useMotionValue.AnimationType.Tap, false);
77
+ return !useMotionValue.isDragActive();
78
+ }
79
+ function onPointerUp(event, info) {
80
+ if (!checkPointerEnd())
81
+ return;
82
+ /**
83
+ * We only count this as a tap gesture if the event.target is the same
84
+ * as, or a child of, this component's element
85
+ */
86
+ onTap === null || onTap === void 0 ? void 0 : onTap(event, info);
87
+ }
88
+ function onPointerCancel(event, info) {
89
+ if (!checkPointerEnd())
90
+ return;
91
+ onTapCancel === null || onTapCancel === void 0 ? void 0 : onTapCancel(event, info);
92
+ }
93
+ return {
94
+ onPointerDown: useMotionValue.wrapHandler(function (event, info) {
95
+ var _a;
96
+ removePointerEndListener();
97
+ if (isPressing.current)
98
+ return;
99
+ isPressing.current = true;
100
+ cancelPointerEndListeners.current = popmotion.pipe(useMotionValue.addPointerEvent(window, "pointerup", onPointerUp), useMotionValue.addPointerEvent(window, "pointercancel", onPointerCancel));
101
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(useMotionValue.AnimationType.Tap, true);
102
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown(event);
103
+ onTapStart === null || onTapStart === void 0 ? void 0 : onTapStart(event, info);
104
+ }, true),
105
+ };
106
+ }
107
+
108
+ var useRender = function (Component, props, _projectionId, ref, _state, isStatic, visualElement) {
109
+ return React.createElement(Component, tslib.__assign(tslib.__assign(tslib.__assign(tslib.__assign({ ref: ref }, useMotionValue.filterProps(props, false, false)), { onUpdate: props.onInstanceUpdate }), useHover(isStatic, props, visualElement)), useTap(isStatic, props, visualElement)));
110
+ };
111
+
112
+ var setVector = function (name, defaultValue) {
113
+ return function (i) {
114
+ return function (instance, value) {
115
+ var _a;
116
+ (_a = instance[name]) !== null && _a !== void 0 ? _a : (instance[name] = new three.Vector3(defaultValue));
117
+ var vector = instance[name];
118
+ vector.setComponent(i, value);
119
+ };
120
+ };
121
+ };
122
+ var setEuler = function (name, defaultValue) {
123
+ return function (axis) {
124
+ return function (instance, value) {
125
+ var _a;
126
+ (_a = instance[name]) !== null && _a !== void 0 ? _a : (instance[name] = new three.Euler(defaultValue));
127
+ var euler = instance[name];
128
+ euler[axis] = value;
129
+ };
130
+ };
131
+ };
132
+ var setColor = function (name) { return function (instance, value) {
133
+ var _a;
134
+ (_a = instance[name]) !== null && _a !== void 0 ? _a : (instance[name] = new three.Color(value));
135
+ instance[name].set(value);
136
+ }; };
137
+ var setScale = setVector("scale", 1);
138
+ var setPosition = setVector("position", 0);
139
+ var setRotation = setEuler("rotation", 0);
140
+ var setters = {
141
+ x: setPosition(0),
142
+ y: setPosition(1),
143
+ z: setPosition(2),
144
+ scale: function (instance, value) {
145
+ var _a;
146
+ (_a = instance.scale) !== null && _a !== void 0 ? _a : (instance.scale = new three.Vector3(1));
147
+ var scale = instance.scale;
148
+ scale.set(value, value, value);
149
+ },
150
+ scaleX: setScale(0),
151
+ scaleY: setScale(1),
152
+ scaleZ: setScale(2),
153
+ rotateX: setRotation("x"),
154
+ rotateY: setRotation("y"),
155
+ rotateZ: setRotation("z"),
156
+ color: setColor("color"),
157
+ specular: setColor("specular"),
158
+ };
159
+ function setThreeValue(instance, key, values) {
160
+ if (setters[key]) {
161
+ setters[key](instance, values[key]);
162
+ }
163
+ else {
164
+ instance[key] = values[key];
165
+ }
166
+ }
167
+
168
+ var readVector = function (name, defaultValue) {
169
+ return function (axis) {
170
+ return function (instance) {
171
+ var value = instance[name];
172
+ return value ? value[axis] : defaultValue;
173
+ };
174
+ };
175
+ };
176
+ var readPosition = readVector("position", 0);
177
+ var readScale = readVector("scale", 1);
178
+ var readRotation = readVector("rotation", 0);
179
+ var readers = {
180
+ x: readPosition("x"),
181
+ y: readPosition("y"),
182
+ z: readPosition("z"),
183
+ scale: readScale("x"),
184
+ scaleX: readScale("x"),
185
+ scaleY: readScale("y"),
186
+ scaleZ: readScale("z"),
187
+ rotateX: readRotation("x"),
188
+ rotateY: readRotation("y"),
189
+ rotateZ: readRotation("z"),
190
+ };
191
+ function readAnimatableValue(value) {
192
+ if (!value)
193
+ return;
194
+ if (value instanceof three.Color) {
195
+ return value.getStyle();
196
+ }
197
+ else {
198
+ return value;
199
+ }
200
+ }
201
+ function readThreeValue(instance, name) {
202
+ var _a;
203
+ return readers[name]
204
+ ? readers[name](instance)
205
+ : (_a = readAnimatableValue(instance[name])) !== null && _a !== void 0 ? _a : 0;
206
+ }
207
+
208
+ var axes = ["x", "y", "z"];
209
+ var valueMap = {
210
+ "position-x": "x",
211
+ "position-y": "y",
212
+ "position-z": "z",
213
+ "rotation-x": "rotateX",
214
+ "rotation-y": "rotateY",
215
+ "rotation-z": "rotateZ",
216
+ "scale-x": "scaleX",
217
+ "scale-y": "scaleY",
218
+ "scale-z": "scaleZ",
219
+ };
220
+ var scrapeMotionValuesFromProps = function (props) {
221
+ var motionValues = {};
222
+ for (var key in props) {
223
+ var prop = props[key];
224
+ if (useMotionValue.isMotionValue(prop)) {
225
+ motionValues[valueMap[key] || key] = prop;
226
+ }
227
+ else if (Array.isArray(prop)) {
228
+ for (var i = 0; i < prop.length; i++) {
229
+ var value = prop[i];
230
+ if (useMotionValue.isMotionValue(value)) {
231
+ var name_1 = valueMap[key + "-" + axes[i]];
232
+ motionValues[name_1] = value;
233
+ }
234
+ }
235
+ }
236
+ }
237
+ return motionValues;
238
+ };
239
+
240
+ var createRenderState = function () { return ({}); };
241
+ var threeVisualElement = useMotionValue.visualElement({
242
+ treeType: "three",
243
+ readValueFromInstance: readThreeValue,
244
+ getBaseTarget: function () {
245
+ return undefined;
246
+ },
247
+ sortNodePosition: function (a, b) {
248
+ return a.id - b.id;
249
+ },
250
+ makeTargetAnimatable: function (element, target) {
251
+ useMotionValue.checkTargetForNewValues(element, target, {});
252
+ return target;
253
+ },
254
+ restoreTransform: function () { },
255
+ resetTransform: function () { },
256
+ removeValueFromRenderState: function (_key, _renderState) { },
257
+ measureViewportBox: useMotionValue.createBox,
258
+ scrapeMotionValuesFromProps: scrapeMotionValuesFromProps,
259
+ build: function (_element, state, latestValues) {
260
+ for (var key in latestValues) {
261
+ state[key] = latestValues[key];
262
+ }
263
+ },
264
+ render: function (instance, renderState) {
265
+ for (var key in renderState) {
266
+ setThreeValue(instance, key, renderState);
267
+ }
268
+ },
269
+ });
270
+ var createVisualElement = function (_, options) {
271
+ return threeVisualElement(options);
272
+ };
273
+
274
+ var useVisualState = useMotionValue.makeUseVisualState({
275
+ scrapeMotionValuesFromProps: scrapeMotionValuesFromProps,
276
+ createRenderState: createRenderState,
277
+ });
278
+ var preloadedFeatures = tslib.__assign({}, useMotionValue.animations);
279
+ function custom(Component) {
280
+ return useMotionValue.createMotionComponent({
281
+ Component: Component,
282
+ preloadedFeatures: preloadedFeatures,
283
+ useRender: useRender,
284
+ useVisualState: useVisualState,
285
+ createVisualElement: createVisualElement,
286
+ });
287
+ }
288
+ var componentCache = new Map();
289
+ var motion = new Proxy(custom, {
290
+ get: function (_, key) {
291
+ !componentCache.has(key) && componentCache.set(key, custom(key));
292
+ return componentCache.get(key);
293
+ },
294
+ });
295
+
296
+ function Block(_a) {
297
+ var set = _a.set;
298
+ useMotionValue.useIsomorphicLayoutEffect(function () {
299
+ set(new Promise(function () { return null; }));
300
+ return function () { return set(false); };
301
+ }, []);
302
+ return null;
303
+ }
304
+ var ErrorBoundary = /** @class */ (function (_super) {
305
+ tslib.__extends(ErrorBoundary, _super);
306
+ function ErrorBoundary() {
307
+ var _this = _super !== null && _super.apply(this, arguments) || this;
308
+ _this.state = { error: false };
309
+ return _this;
310
+ }
311
+ ErrorBoundary.prototype.componentDidCatch = function (error) {
312
+ this.props.set(error);
313
+ };
314
+ ErrorBoundary.prototype.render = function () {
315
+ return this.state.error ? null : this.props.children;
316
+ };
317
+ ErrorBoundary.getDerivedStateFromError = function () { return ({ error: true }); };
318
+ return ErrorBoundary;
319
+ }(React__namespace.Component));
320
+ /**
321
+ * A synchronously rendering version of R3F's Canvas component. We call render
322
+ * within the render function itself to ensure lifecycle events are called before
323
+ * parent reconcilers. This can be replaced with a simple MotionContext provider
324
+ * when R3F moves to the same model.
325
+ */
326
+ function SyncCanvasComponent(_a, forwardedRef) {
327
+ var children = _a.children, fallback = _a.fallback, tabIndex = _a.tabIndex, resize = _a.resize, id = _a.id, style = _a.style, className = _a.className, events = _a.events, props = tslib.__rest(_a, ["children", "fallback", "tabIndex", "resize", "id", "style", "className", "events"]);
328
+ var motionContext = React.useContext(useMotionValue.MotionContext);
329
+ var _b = tslib.__read(useMeasure__default["default"](tslib.__assign({ scroll: true, debounce: { scroll: 50, resize: 0 } }, resize)), 2), containerRef = _b[0], _c = _b[1], width = _c.width, height = _c.height;
330
+ var canvasRef = React__namespace.useRef(null);
331
+ var _d = tslib.__read(React__namespace.useState(false), 2), block = _d[0], setBlock = _d[1];
332
+ var _e = tslib.__read(React__namespace.useState(false), 2), error = _e[0], setError = _e[1];
333
+ // Suspend this component if block is a promise (2nd run)
334
+ if (block)
335
+ throw block;
336
+ // Throw exception outwards if anything within canvas throws
337
+ if (error)
338
+ throw error;
339
+ // Execute JSX in the reconciler as a layout-effect
340
+ if (width > 0 && height > 0) {
341
+ fiber.render(React__namespace.createElement(ErrorBoundary, { set: setError },
342
+ React__namespace.createElement(React__namespace.Suspense, { fallback: React__namespace.createElement(Block, { set: setBlock }) },
343
+ React__namespace.createElement(useMotionValue.MotionContext.Provider, { value: motionContext }, children))), canvasRef.current, tslib.__assign(tslib.__assign({}, props), { size: { width: width, height: height }, events: events || fiber.events }));
344
+ }
345
+ useMotionValue.useIsomorphicLayoutEffect(function () {
346
+ var container = canvasRef.current;
347
+ return function () { return fiber.unmountComponentAtNode(container); };
348
+ }, []);
349
+ return (React__namespace.createElement("div", { ref: containerRef, id: id, className: className, tabIndex: tabIndex, style: tslib.__assign({ position: "relative", width: "100%", height: "100%", overflow: "hidden" }, style) },
350
+ React__namespace.createElement("canvas", { ref: mergeRefs__default["default"]([canvasRef, forwardedRef]), style: { display: "block" } }, fallback)));
351
+ }
352
+ var MotionCanvas = React.forwardRef(SyncCanvasComponent);
353
+
354
+ function useTime() {
355
+ var time = useMotionValue.useMotionValue(0);
356
+ fiber.useFrame(function (state) { return time.set(state.clock.getElapsedTime()); });
357
+ return time;
358
+ }
359
+
360
+ exports.MotionCanvas = MotionCanvas;
361
+ exports.motion = motion;
362
+ exports.useTime = useTime;