framer-motion 5.3.2 → 5.4.0-beta.2

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.
Files changed (58) hide show
  1. package/dist/es/animation/utils/easing.mjs +1 -1
  2. package/dist/es/animation/utils/transitions.mjs +1 -1
  3. package/dist/es/components/AnimatePresence/index.mjs +1 -1
  4. package/dist/es/components/AnimatePresence/use-presence.mjs +1 -27
  5. package/dist/es/components/AnimateSharedLayout.mjs +1 -1
  6. package/dist/es/components/MotionCanvas/index.mjs +14 -0
  7. package/dist/es/components/Reorder/Item.mjs +1 -1
  8. package/dist/es/context/MotionContext/index.mjs +1 -1
  9. package/dist/es/events/use-dom-event.mjs +2 -31
  10. package/dist/es/events/use-pointer-event.mjs +2 -5
  11. package/dist/es/gestures/utils/event-type.mjs +1 -8
  12. package/dist/es/motion/index.mjs +1 -1
  13. package/dist/es/projection/animation/mix-values.mjs +1 -1
  14. package/dist/es/projection/geometry/models.mjs +1 -11
  15. package/dist/es/projection/node/create-projection-node.mjs +4 -1167
  16. package/dist/es/projection/styles/scale-border-radius.mjs +1 -1
  17. package/dist/es/projection/styles/transform.mjs +5 -5
  18. package/dist/es/render/dom/motion-proxy.mjs +3 -0
  19. package/dist/es/render/dom/utils/css-variables-conversion.mjs +2 -2
  20. package/dist/es/render/dom/utils/filter-props.mjs +3 -1
  21. package/dist/es/render/dom/value-types/dimensions.mjs +1 -8
  22. package/dist/es/render/html/use-props.mjs +1 -1
  23. package/dist/es/render/html/utils/build-transform.mjs +2 -2
  24. package/dist/es/render/svg/utils/path.mjs +1 -1
  25. package/dist/es/render/svg/utils/transform-origin.mjs +1 -1
  26. package/dist/es/render/three/create-visual-element.mjs +42 -0
  27. package/dist/es/render/three/gestures/use-hover.mjs +22 -0
  28. package/dist/es/render/three/gestures/use-tap.mjs +56 -0
  29. package/dist/es/render/three/motion.mjs +30 -0
  30. package/dist/es/render/three/use-render.mjs +11 -0
  31. package/dist/es/render/three/utils/read-value.mjs +43 -0
  32. package/dist/es/render/three/utils/set-value.mjs +59 -0
  33. package/dist/es/render/utils/animation.mjs +1 -4
  34. package/dist/es/render/utils/setters.mjs +2 -39
  35. package/dist/es/three-entry.mjs +2 -0
  36. package/dist/es/utils/array.mjs +2 -13
  37. package/dist/framer-motion-three.cjs.js +3004 -0
  38. package/dist/framer-motion.cjs.js +46 -43
  39. package/dist/framer-motion.dev.js +35 -32
  40. package/dist/framer-motion.js +1 -1
  41. package/dist/projection.dev.js +17 -17
  42. package/dist/size-rollup-dom-animation.js +1 -1
  43. package/dist/size-rollup-dom-max.js +1 -1
  44. package/dist/size-rollup-m.js +1 -1
  45. package/package.json +27 -6
  46. package/types/components/MotionCanvas/index.d.ts +4 -0
  47. package/types/motion/features/types.d.ts +1 -1
  48. package/types/render/three/create-visual-element.d.ts +6 -0
  49. package/types/render/three/gestures/use-hover.d.ts +10 -0
  50. package/types/render/three/gestures/use-tap.d.ts +8 -0
  51. package/types/render/three/motion.d.ts +5 -0
  52. package/types/render/three/types.d.ts +24 -0
  53. package/types/render/three/use-render.d.ts +4 -0
  54. package/types/render/three/utils/read-value.d.ts +2 -0
  55. package/types/render/three/utils/set-value.d.ts +2 -0
  56. package/types/render/utils/lifecycles.d.ts +8 -8
  57. package/types/render/utils/setters.d.ts +1 -0
  58. package/types/three-entry.d.ts +2 -0
@@ -0,0 +1,3004 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var React = require('react');
7
+ var heyListen = require('hey-listen');
8
+ var sync = require('framesync');
9
+ var popmotion = require('popmotion');
10
+ var styleValueTypes = require('style-value-types');
11
+ var three = require('three');
12
+ var fiber = require('@react-three/fiber');
13
+
14
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
+
16
+ function _interopNamespace(e) {
17
+ if (e && e.__esModule) return e;
18
+ var n = Object.create(null);
19
+ if (e) {
20
+ Object.keys(e).forEach(function (k) {
21
+ if (k !== 'default') {
22
+ var d = Object.getOwnPropertyDescriptor(e, k);
23
+ Object.defineProperty(n, k, d.get ? d : {
24
+ enumerable: true,
25
+ get: function () { return e[k]; }
26
+ });
27
+ }
28
+ });
29
+ }
30
+ n["default"] = e;
31
+ return Object.freeze(n);
32
+ }
33
+
34
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
35
+ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
36
+ var sync__default = /*#__PURE__*/_interopDefaultLegacy(sync);
37
+
38
+ var createDefinition = function (propNames) { return ({
39
+ isEnabled: function (props) { return propNames.some(function (name) { return !!props[name]; }); },
40
+ }); };
41
+ var featureDefinitions = {
42
+ measureLayout: createDefinition(["layout", "layoutId", "drag"]),
43
+ animation: createDefinition([
44
+ "animate",
45
+ "exit",
46
+ "variants",
47
+ "whileHover",
48
+ "whileTap",
49
+ "whileFocus",
50
+ "whileDrag",
51
+ "whileInView",
52
+ ]),
53
+ exit: createDefinition(["exit"]),
54
+ drag: createDefinition(["drag", "dragControls"]),
55
+ focus: createDefinition(["whileFocus"]),
56
+ hover: createDefinition(["whileHover", "onHoverStart", "onHoverEnd"]),
57
+ tap: createDefinition(["whileTap", "onTap", "onTapStart", "onTapCancel"]),
58
+ pan: createDefinition([
59
+ "onPan",
60
+ "onPanStart",
61
+ "onPanSessionStart",
62
+ "onPanEnd",
63
+ ]),
64
+ inView: createDefinition([
65
+ "whileInView",
66
+ "onViewportEnter",
67
+ "onViewportLeave",
68
+ ]),
69
+ };
70
+ function loadFeatures(features) {
71
+ for (var key in features) {
72
+ if (features[key] === null)
73
+ continue;
74
+ if (key === "projectionNodeConstructor") {
75
+ featureDefinitions.projectionNodeConstructor = features[key];
76
+ }
77
+ else {
78
+ featureDefinitions[key].Component = features[key];
79
+ }
80
+ }
81
+ }
82
+
83
+ var LazyContext = React.createContext({ strict: false });
84
+
85
+ var featureNames = Object.keys(featureDefinitions);
86
+ var numFeatures = featureNames.length;
87
+ /**
88
+ * Load features via renderless components based on the provided MotionProps.
89
+ */
90
+ function useFeatures(props, visualElement, preloadedFeatures) {
91
+ var features = [];
92
+ var lazyContext = React.useContext(LazyContext);
93
+ if (!visualElement)
94
+ return null;
95
+ /**
96
+ * If we're in development mode, check to make sure we're not rendering a motion component
97
+ * as a child of LazyMotion, as this will break the file-size benefits of using it.
98
+ */
99
+ if (process.env.NODE_ENV !== "production" &&
100
+ preloadedFeatures &&
101
+ lazyContext.strict) {
102
+ heyListen.invariant(false, "You have rendered a `motion` component within a `LazyMotion` component. This will break tree shaking. Import and render a `m` component instead.");
103
+ }
104
+ for (var i = 0; i < numFeatures; i++) {
105
+ var name_1 = featureNames[i];
106
+ var _a = featureDefinitions[name_1], isEnabled = _a.isEnabled, Component = _a.Component;
107
+ /**
108
+ * It might be possible in the future to use this moment to
109
+ * dynamically request functionality. In initial tests this
110
+ * was producing a lot of duplication amongst bundles.
111
+ */
112
+ if (isEnabled(props) && Component) {
113
+ features.push(React__namespace.createElement(Component, tslib.__assign({ key: name_1 }, props, { visualElement: visualElement })));
114
+ }
115
+ }
116
+ return features;
117
+ }
118
+
119
+ /**
120
+ * @public
121
+ */
122
+ var MotionConfigContext = React.createContext({
123
+ transformPagePoint: function (p) { return p; },
124
+ isStatic: false,
125
+ });
126
+
127
+ var MotionContext = React.createContext({});
128
+ function useVisualElementContext() {
129
+ return React.useContext(MotionContext).visualElement;
130
+ }
131
+
132
+ /**
133
+ * @public
134
+ */
135
+ var PresenceContext = React.createContext(null);
136
+
137
+ var isBrowser = typeof window !== "undefined";
138
+
139
+ var useIsomorphicLayoutEffect = isBrowser ? React.useLayoutEffect : React.useEffect;
140
+
141
+ function useVisualElement(Component, visualState, props, createVisualElement) {
142
+ var lazyContext = React.useContext(LazyContext);
143
+ var parent = useVisualElementContext();
144
+ var presenceContext = React.useContext(PresenceContext);
145
+ var visualElementRef = React.useRef(undefined);
146
+ /**
147
+ * If we haven't preloaded a renderer, check to see if we have one lazy-loaded
148
+ */
149
+ if (!createVisualElement)
150
+ createVisualElement = lazyContext.renderer;
151
+ if (!visualElementRef.current && createVisualElement) {
152
+ visualElementRef.current = createVisualElement(Component, {
153
+ visualState: visualState,
154
+ parent: parent,
155
+ props: props,
156
+ presenceId: presenceContext === null || presenceContext === void 0 ? void 0 : presenceContext.id,
157
+ blockInitialAnimation: (presenceContext === null || presenceContext === void 0 ? void 0 : presenceContext.initial) === false,
158
+ });
159
+ }
160
+ var visualElement = visualElementRef.current;
161
+ useIsomorphicLayoutEffect(function () {
162
+ visualElement === null || visualElement === void 0 ? void 0 : visualElement.syncRender();
163
+ });
164
+ React.useEffect(function () {
165
+ var _a;
166
+ (_a = visualElement === null || visualElement === void 0 ? void 0 : visualElement.animationState) === null || _a === void 0 ? void 0 : _a.animateChanges();
167
+ });
168
+ useIsomorphicLayoutEffect(function () { return function () { return visualElement === null || visualElement === void 0 ? void 0 : visualElement.notifyUnmount(); }; }, []);
169
+ return visualElement;
170
+ }
171
+
172
+ function isRefObject(ref) {
173
+ return (typeof ref === "object" &&
174
+ Object.prototype.hasOwnProperty.call(ref, "current"));
175
+ }
176
+
177
+ /**
178
+ * Creates a ref function that, when called, hydrates the provided
179
+ * external ref and VisualElement.
180
+ */
181
+ function useMotionRef(visualState, visualElement, externalRef) {
182
+ return React.useCallback(function (instance) {
183
+ var _a;
184
+ instance && ((_a = visualState.mount) === null || _a === void 0 ? void 0 : _a.call(visualState, instance));
185
+ if (visualElement) {
186
+ instance
187
+ ? visualElement.mount(instance)
188
+ : visualElement.unmount();
189
+ }
190
+ if (externalRef) {
191
+ if (typeof externalRef === "function") {
192
+ externalRef(instance);
193
+ }
194
+ else if (isRefObject(externalRef)) {
195
+ externalRef.current = instance;
196
+ }
197
+ }
198
+ },
199
+ /**
200
+ * Only pass a new ref callback to React if we've received a visual element
201
+ * factory. Otherwise we'll be mounting/remounting every time externalRef
202
+ * or other dependencies change.
203
+ */
204
+ [visualElement]);
205
+ }
206
+
207
+ /**
208
+ * Decides if the supplied variable is an array of variant labels
209
+ */
210
+ function isVariantLabels(v) {
211
+ return Array.isArray(v);
212
+ }
213
+ /**
214
+ * Decides if the supplied variable is variant label
215
+ */
216
+ function isVariantLabel(v) {
217
+ return typeof v === "string" || isVariantLabels(v);
218
+ }
219
+ /**
220
+ * Creates an object containing the latest state of every MotionValue on a VisualElement
221
+ */
222
+ function getCurrent(visualElement) {
223
+ var current = {};
224
+ visualElement.forEachValue(function (value, key) { return (current[key] = value.get()); });
225
+ return current;
226
+ }
227
+ /**
228
+ * Creates an object containing the latest velocity of every MotionValue on a VisualElement
229
+ */
230
+ function getVelocity(visualElement) {
231
+ var velocity = {};
232
+ visualElement.forEachValue(function (value, key) { return (velocity[key] = value.getVelocity()); });
233
+ return velocity;
234
+ }
235
+ function resolveVariantFromProps(props, definition, custom, currentValues, currentVelocity) {
236
+ var _a;
237
+ if (currentValues === void 0) { currentValues = {}; }
238
+ if (currentVelocity === void 0) { currentVelocity = {}; }
239
+ /**
240
+ * If the variant definition is a function, resolve.
241
+ */
242
+ if (typeof definition === "function") {
243
+ definition = definition(custom !== null && custom !== void 0 ? custom : props.custom, currentValues, currentVelocity);
244
+ }
245
+ /**
246
+ * If the variant definition is a variant label, or
247
+ * the function returned a variant label, resolve.
248
+ */
249
+ if (typeof definition === "string") {
250
+ definition = (_a = props.variants) === null || _a === void 0 ? void 0 : _a[definition];
251
+ }
252
+ /**
253
+ * At this point we've resolved both functions and variant labels,
254
+ * but the resolved variant label might itself have been a function.
255
+ * If so, resolve. This can only have returned a valid target object.
256
+ */
257
+ if (typeof definition === "function") {
258
+ definition = definition(custom !== null && custom !== void 0 ? custom : props.custom, currentValues, currentVelocity);
259
+ }
260
+ return definition;
261
+ }
262
+ function resolveVariant(visualElement, definition, custom) {
263
+ var props = visualElement.getProps();
264
+ return resolveVariantFromProps(props, definition, custom !== null && custom !== void 0 ? custom : props.custom, getCurrent(visualElement), getVelocity(visualElement));
265
+ }
266
+ function checkIfControllingVariants(props) {
267
+ var _a;
268
+ return (typeof ((_a = props.animate) === null || _a === void 0 ? void 0 : _a.start) === "function" ||
269
+ isVariantLabel(props.initial) ||
270
+ isVariantLabel(props.animate) ||
271
+ isVariantLabel(props.whileHover) ||
272
+ isVariantLabel(props.whileDrag) ||
273
+ isVariantLabel(props.whileTap) ||
274
+ isVariantLabel(props.whileFocus) ||
275
+ isVariantLabel(props.exit));
276
+ }
277
+ function checkIfVariantNode(props) {
278
+ return Boolean(checkIfControllingVariants(props) || props.variants);
279
+ }
280
+
281
+ function getCurrentTreeVariants(props, context) {
282
+ if (checkIfControllingVariants(props)) {
283
+ var initial = props.initial, animate = props.animate;
284
+ return {
285
+ initial: initial === false || isVariantLabel(initial)
286
+ ? initial
287
+ : undefined,
288
+ animate: isVariantLabel(animate) ? animate : undefined,
289
+ };
290
+ }
291
+ return props.inherit !== false ? context : {};
292
+ }
293
+
294
+ function useCreateMotionContext(props) {
295
+ var _a = getCurrentTreeVariants(props, React.useContext(MotionContext)), initial = _a.initial, animate = _a.animate;
296
+ return React.useMemo(function () { return ({ initial: initial, animate: animate }); }, [variantLabelsAsDependency(initial), variantLabelsAsDependency(animate)]);
297
+ }
298
+ function variantLabelsAsDependency(prop) {
299
+ return Array.isArray(prop) ? prop.join(" ") : prop;
300
+ }
301
+
302
+ /**
303
+ * Creates a constant value over the lifecycle of a component.
304
+ *
305
+ * Even if `useMemo` is provided an empty array as its final argument, it doesn't offer
306
+ * a guarantee that it won't re-run for performance reasons later on. By using `useConstant`
307
+ * you can ensure that initialisers don't execute twice or more.
308
+ */
309
+ function useConstant(init) {
310
+ var ref = React.useRef(null);
311
+ if (ref.current === null) {
312
+ ref.current = init();
313
+ }
314
+ return ref.current;
315
+ }
316
+
317
+ function addUniqueItem(arr, item) {
318
+ arr.indexOf(item) === -1 && arr.push(item);
319
+ }
320
+ function removeItem(arr, item) {
321
+ var index = arr.indexOf(item);
322
+ index > -1 && arr.splice(index, 1);
323
+ }
324
+
325
+ var SubscriptionManager = /** @class */ (function () {
326
+ function SubscriptionManager() {
327
+ this.subscriptions = [];
328
+ }
329
+ SubscriptionManager.prototype.add = function (handler) {
330
+ var _this = this;
331
+ addUniqueItem(this.subscriptions, handler);
332
+ return function () { return removeItem(_this.subscriptions, handler); };
333
+ };
334
+ SubscriptionManager.prototype.notify = function (a, b, c) {
335
+ var numSubscriptions = this.subscriptions.length;
336
+ if (!numSubscriptions)
337
+ return;
338
+ if (numSubscriptions === 1) {
339
+ /**
340
+ * If there's only a single handler we can just call it without invoking a loop.
341
+ */
342
+ this.subscriptions[0](a, b, c);
343
+ }
344
+ else {
345
+ for (var i = 0; i < numSubscriptions; i++) {
346
+ /**
347
+ * Check whether the handler exists before firing as it's possible
348
+ * the subscriptions were modified during this loop running.
349
+ */
350
+ var handler = this.subscriptions[i];
351
+ handler && handler(a, b, c);
352
+ }
353
+ }
354
+ };
355
+ SubscriptionManager.prototype.getSize = function () {
356
+ return this.subscriptions.length;
357
+ };
358
+ SubscriptionManager.prototype.clear = function () {
359
+ this.subscriptions.length = 0;
360
+ };
361
+ return SubscriptionManager;
362
+ }());
363
+
364
+ var isFloat = function (value) {
365
+ return !isNaN(parseFloat(value));
366
+ };
367
+ /**
368
+ * `MotionValue` is used to track the state and velocity of motion values.
369
+ *
370
+ * @public
371
+ */
372
+ var MotionValue = /** @class */ (function () {
373
+ /**
374
+ * @param init - The initiating value
375
+ * @param config - Optional configuration options
376
+ *
377
+ * - `transformer`: A function to transform incoming values with.
378
+ *
379
+ * @internal
380
+ */
381
+ function MotionValue(init) {
382
+ var _this = this;
383
+ /**
384
+ * Duration, in milliseconds, since last updating frame.
385
+ *
386
+ * @internal
387
+ */
388
+ this.timeDelta = 0;
389
+ /**
390
+ * Timestamp of the last time this `MotionValue` was updated.
391
+ *
392
+ * @internal
393
+ */
394
+ this.lastUpdated = 0;
395
+ /**
396
+ * Functions to notify when the `MotionValue` updates.
397
+ *
398
+ * @internal
399
+ */
400
+ this.updateSubscribers = new SubscriptionManager();
401
+ /**
402
+ * Functions to notify when the velocity updates.
403
+ *
404
+ * @internal
405
+ */
406
+ this.velocityUpdateSubscribers = new SubscriptionManager();
407
+ /**
408
+ * Functions to notify when the `MotionValue` updates and `render` is set to `true`.
409
+ *
410
+ * @internal
411
+ */
412
+ this.renderSubscribers = new SubscriptionManager();
413
+ /**
414
+ * Tracks whether this value can output a velocity. Currently this is only true
415
+ * if the value is numerical, but we might be able to widen the scope here and support
416
+ * other value types.
417
+ *
418
+ * @internal
419
+ */
420
+ this.canTrackVelocity = false;
421
+ this.updateAndNotify = function (v, render) {
422
+ if (render === void 0) { render = true; }
423
+ _this.prev = _this.current;
424
+ _this.current = v;
425
+ // Update timestamp
426
+ var _a = sync.getFrameData(), delta = _a.delta, timestamp = _a.timestamp;
427
+ if (_this.lastUpdated !== timestamp) {
428
+ _this.timeDelta = delta;
429
+ _this.lastUpdated = timestamp;
430
+ sync__default["default"].postRender(_this.scheduleVelocityCheck);
431
+ }
432
+ // Update update subscribers
433
+ if (_this.prev !== _this.current) {
434
+ _this.updateSubscribers.notify(_this.current);
435
+ }
436
+ // Update velocity subscribers
437
+ if (_this.velocityUpdateSubscribers.getSize()) {
438
+ _this.velocityUpdateSubscribers.notify(_this.getVelocity());
439
+ }
440
+ // Update render subscribers
441
+ if (render) {
442
+ _this.renderSubscribers.notify(_this.current);
443
+ }
444
+ };
445
+ /**
446
+ * Schedule a velocity check for the next frame.
447
+ *
448
+ * This is an instanced and bound function to prevent generating a new
449
+ * function once per frame.
450
+ *
451
+ * @internal
452
+ */
453
+ this.scheduleVelocityCheck = function () { return sync__default["default"].postRender(_this.velocityCheck); };
454
+ /**
455
+ * Updates `prev` with `current` if the value hasn't been updated this frame.
456
+ * This ensures velocity calculations return `0`.
457
+ *
458
+ * This is an instanced and bound function to prevent generating a new
459
+ * function once per frame.
460
+ *
461
+ * @internal
462
+ */
463
+ this.velocityCheck = function (_a) {
464
+ var timestamp = _a.timestamp;
465
+ if (timestamp !== _this.lastUpdated) {
466
+ _this.prev = _this.current;
467
+ _this.velocityUpdateSubscribers.notify(_this.getVelocity());
468
+ }
469
+ };
470
+ this.hasAnimated = false;
471
+ this.prev = this.current = init;
472
+ this.canTrackVelocity = isFloat(this.current);
473
+ }
474
+ /**
475
+ * Adds a function that will be notified when the `MotionValue` is updated.
476
+ *
477
+ * It returns a function that, when called, will cancel the subscription.
478
+ *
479
+ * When calling `onChange` inside a React component, it should be wrapped with the
480
+ * `useEffect` hook. As it returns an unsubscribe function, this should be returned
481
+ * from the `useEffect` function to ensure you don't add duplicate subscribers..
482
+ *
483
+ * ```jsx
484
+ * export const MyComponent = () => {
485
+ * const x = useMotionValue(0)
486
+ * const y = useMotionValue(0)
487
+ * const opacity = useMotionValue(1)
488
+ *
489
+ * useEffect(() => {
490
+ * function updateOpacity() {
491
+ * const maxXY = Math.max(x.get(), y.get())
492
+ * const newOpacity = transform(maxXY, [0, 100], [1, 0])
493
+ * opacity.set(newOpacity)
494
+ * }
495
+ *
496
+ * const unsubscribeX = x.onChange(updateOpacity)
497
+ * const unsubscribeY = y.onChange(updateOpacity)
498
+ *
499
+ * return () => {
500
+ * unsubscribeX()
501
+ * unsubscribeY()
502
+ * }
503
+ * }, [])
504
+ *
505
+ * return <motion.div style={{ x }} />
506
+ * }
507
+ * ```
508
+ *
509
+ * @internalremarks
510
+ *
511
+ * We could look into a `useOnChange` hook if the above lifecycle management proves confusing.
512
+ *
513
+ * ```jsx
514
+ * useOnChange(x, () => {})
515
+ * ```
516
+ *
517
+ * @param subscriber - A function that receives the latest value.
518
+ * @returns A function that, when called, will cancel this subscription.
519
+ *
520
+ * @public
521
+ */
522
+ MotionValue.prototype.onChange = function (subscription) {
523
+ return this.updateSubscribers.add(subscription);
524
+ };
525
+ MotionValue.prototype.clearListeners = function () {
526
+ this.updateSubscribers.clear();
527
+ };
528
+ /**
529
+ * Adds a function that will be notified when the `MotionValue` requests a render.
530
+ *
531
+ * @param subscriber - A function that's provided the latest value.
532
+ * @returns A function that, when called, will cancel this subscription.
533
+ *
534
+ * @internal
535
+ */
536
+ MotionValue.prototype.onRenderRequest = function (subscription) {
537
+ // Render immediately
538
+ subscription(this.get());
539
+ return this.renderSubscribers.add(subscription);
540
+ };
541
+ /**
542
+ * Attaches a passive effect to the `MotionValue`.
543
+ *
544
+ * @internal
545
+ */
546
+ MotionValue.prototype.attach = function (passiveEffect) {
547
+ this.passiveEffect = passiveEffect;
548
+ };
549
+ /**
550
+ * Sets the state of the `MotionValue`.
551
+ *
552
+ * @remarks
553
+ *
554
+ * ```jsx
555
+ * const x = useMotionValue(0)
556
+ * x.set(10)
557
+ * ```
558
+ *
559
+ * @param latest - Latest value to set.
560
+ * @param render - Whether to notify render subscribers. Defaults to `true`
561
+ *
562
+ * @public
563
+ */
564
+ MotionValue.prototype.set = function (v, render) {
565
+ if (render === void 0) { render = true; }
566
+ if (!render || !this.passiveEffect) {
567
+ this.updateAndNotify(v, render);
568
+ }
569
+ else {
570
+ this.passiveEffect(v, this.updateAndNotify);
571
+ }
572
+ };
573
+ /**
574
+ * Returns the latest state of `MotionValue`
575
+ *
576
+ * @returns - The latest state of `MotionValue`
577
+ *
578
+ * @public
579
+ */
580
+ MotionValue.prototype.get = function () {
581
+ return this.current;
582
+ };
583
+ /**
584
+ * @public
585
+ */
586
+ MotionValue.prototype.getPrevious = function () {
587
+ return this.prev;
588
+ };
589
+ /**
590
+ * Returns the latest velocity of `MotionValue`
591
+ *
592
+ * @returns - The latest velocity of `MotionValue`. Returns `0` if the state is non-numerical.
593
+ *
594
+ * @public
595
+ */
596
+ MotionValue.prototype.getVelocity = function () {
597
+ // This could be isFloat(this.prev) && isFloat(this.current), but that would be wasteful
598
+ return this.canTrackVelocity
599
+ ? // These casts could be avoided if parseFloat would be typed better
600
+ popmotion.velocityPerSecond(parseFloat(this.current) -
601
+ parseFloat(this.prev), this.timeDelta)
602
+ : 0;
603
+ };
604
+ /**
605
+ * Registers a new animation to control this `MotionValue`. Only one
606
+ * animation can drive a `MotionValue` at one time.
607
+ *
608
+ * ```jsx
609
+ * value.start()
610
+ * ```
611
+ *
612
+ * @param animation - A function that starts the provided animation
613
+ *
614
+ * @internal
615
+ */
616
+ MotionValue.prototype.start = function (animation) {
617
+ var _this = this;
618
+ this.stop();
619
+ return new Promise(function (resolve) {
620
+ _this.hasAnimated = true;
621
+ _this.stopAnimation = animation(resolve);
622
+ }).then(function () { return _this.clearAnimation(); });
623
+ };
624
+ /**
625
+ * Stop the currently active animation.
626
+ *
627
+ * @public
628
+ */
629
+ MotionValue.prototype.stop = function () {
630
+ if (this.stopAnimation)
631
+ this.stopAnimation();
632
+ this.clearAnimation();
633
+ };
634
+ /**
635
+ * Returns `true` if this value is currently animating.
636
+ *
637
+ * @public
638
+ */
639
+ MotionValue.prototype.isAnimating = function () {
640
+ return !!this.stopAnimation;
641
+ };
642
+ MotionValue.prototype.clearAnimation = function () {
643
+ this.stopAnimation = null;
644
+ };
645
+ /**
646
+ * Destroy and clean up subscribers to this `MotionValue`.
647
+ *
648
+ * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
649
+ * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
650
+ * created a `MotionValue` via the `motionValue` function.
651
+ *
652
+ * @public
653
+ */
654
+ MotionValue.prototype.destroy = function () {
655
+ this.updateSubscribers.clear();
656
+ this.renderSubscribers.clear();
657
+ this.stop();
658
+ };
659
+ return MotionValue;
660
+ }());
661
+ /**
662
+ * @internal
663
+ */
664
+ function motionValue(init) {
665
+ return new MotionValue(init);
666
+ }
667
+
668
+ var isMotionValue = function (value) {
669
+ return value !== null && typeof value === "object" && value.getVelocity;
670
+ };
671
+
672
+ /**
673
+ * Converts seconds to milliseconds
674
+ *
675
+ * @param seconds - Time in seconds.
676
+ * @return milliseconds - Converted time in milliseconds.
677
+ */
678
+ var secondsToMilliseconds = function (seconds) { return seconds * 1000; };
679
+
680
+ var easingLookup = {
681
+ linear: popmotion.linear,
682
+ easeIn: popmotion.easeIn,
683
+ easeInOut: popmotion.easeInOut,
684
+ easeOut: popmotion.easeOut,
685
+ circIn: popmotion.circIn,
686
+ circInOut: popmotion.circInOut,
687
+ circOut: popmotion.circOut,
688
+ backIn: popmotion.backIn,
689
+ backInOut: popmotion.backInOut,
690
+ backOut: popmotion.backOut,
691
+ anticipate: popmotion.anticipate,
692
+ bounceIn: popmotion.bounceIn,
693
+ bounceInOut: popmotion.bounceInOut,
694
+ bounceOut: popmotion.bounceOut,
695
+ };
696
+ var easingDefinitionToFunction = function (definition) {
697
+ if (Array.isArray(definition)) {
698
+ // If cubic bezier definition, create bezier curve
699
+ heyListen.invariant(definition.length === 4, "Cubic bezier arrays must contain four numerical values.");
700
+ var _a = tslib.__read(definition, 4), x1 = _a[0], y1 = _a[1], x2 = _a[2], y2 = _a[3];
701
+ return popmotion.cubicBezier(x1, y1, x2, y2);
702
+ }
703
+ else if (typeof definition === "string") {
704
+ // Else lookup from table
705
+ heyListen.invariant(easingLookup[definition] !== undefined, "Invalid easing type '".concat(definition, "'"));
706
+ return easingLookup[definition];
707
+ }
708
+ return definition;
709
+ };
710
+ var isEasingArray = function (ease) {
711
+ return Array.isArray(ease) && typeof ease[0] !== "number";
712
+ };
713
+
714
+ /**
715
+ * Check if a value is animatable. Examples:
716
+ *
717
+ * ✅: 100, "100px", "#fff"
718
+ * ❌: "block", "url(2.jpg)"
719
+ * @param value
720
+ *
721
+ * @internal
722
+ */
723
+ var isAnimatable = function (key, value) {
724
+ // If the list of keys tat might be non-animatable grows, replace with Set
725
+ if (key === "zIndex")
726
+ return false;
727
+ // If it's a number or a keyframes array, we can animate it. We might at some point
728
+ // need to do a deep isAnimatable check of keyframes, or let Popmotion handle this,
729
+ // but for now lets leave it like this for performance reasons
730
+ if (typeof value === "number" || Array.isArray(value))
731
+ return true;
732
+ if (typeof value === "string" && // It's animatable if we have a string
733
+ styleValueTypes.complex.test(value) && // And it contains numbers and/or colors
734
+ !value.startsWith("url(") // Unless it starts with "url("
735
+ ) {
736
+ return true;
737
+ }
738
+ return false;
739
+ };
740
+
741
+ var isKeyframesTarget = function (v) {
742
+ return Array.isArray(v);
743
+ };
744
+
745
+ var underDampedSpring = function () { return ({
746
+ type: "spring",
747
+ stiffness: 500,
748
+ damping: 25,
749
+ restDelta: 0.5,
750
+ restSpeed: 10,
751
+ }); };
752
+ var criticallyDampedSpring = function (to) { return ({
753
+ type: "spring",
754
+ stiffness: 550,
755
+ damping: to === 0 ? 2 * Math.sqrt(550) : 30,
756
+ restDelta: 0.01,
757
+ restSpeed: 10,
758
+ }); };
759
+ var linearTween = function () { return ({
760
+ type: "keyframes",
761
+ ease: "linear",
762
+ duration: 0.3,
763
+ }); };
764
+ var keyframes = function (values) { return ({
765
+ type: "keyframes",
766
+ duration: 0.8,
767
+ values: values,
768
+ }); };
769
+ var defaultTransitions = {
770
+ x: underDampedSpring,
771
+ y: underDampedSpring,
772
+ z: underDampedSpring,
773
+ rotate: underDampedSpring,
774
+ rotateX: underDampedSpring,
775
+ rotateY: underDampedSpring,
776
+ rotateZ: underDampedSpring,
777
+ scaleX: criticallyDampedSpring,
778
+ scaleY: criticallyDampedSpring,
779
+ scale: criticallyDampedSpring,
780
+ opacity: linearTween,
781
+ backgroundColor: linearTween,
782
+ color: linearTween,
783
+ default: criticallyDampedSpring,
784
+ };
785
+ var getDefaultTransition = function (valueKey, to) {
786
+ var transitionFactory;
787
+ if (isKeyframesTarget(to)) {
788
+ transitionFactory = keyframes;
789
+ }
790
+ else {
791
+ transitionFactory =
792
+ defaultTransitions[valueKey] || defaultTransitions.default;
793
+ }
794
+ return tslib.__assign({ to: to }, transitionFactory(to));
795
+ };
796
+
797
+ var int = tslib.__assign(tslib.__assign({}, styleValueTypes.number), { transform: Math.round });
798
+
799
+ var numberValueTypes = {
800
+ // Border props
801
+ borderWidth: styleValueTypes.px,
802
+ borderTopWidth: styleValueTypes.px,
803
+ borderRightWidth: styleValueTypes.px,
804
+ borderBottomWidth: styleValueTypes.px,
805
+ borderLeftWidth: styleValueTypes.px,
806
+ borderRadius: styleValueTypes.px,
807
+ radius: styleValueTypes.px,
808
+ borderTopLeftRadius: styleValueTypes.px,
809
+ borderTopRightRadius: styleValueTypes.px,
810
+ borderBottomRightRadius: styleValueTypes.px,
811
+ borderBottomLeftRadius: styleValueTypes.px,
812
+ // Positioning props
813
+ width: styleValueTypes.px,
814
+ maxWidth: styleValueTypes.px,
815
+ height: styleValueTypes.px,
816
+ maxHeight: styleValueTypes.px,
817
+ size: styleValueTypes.px,
818
+ top: styleValueTypes.px,
819
+ right: styleValueTypes.px,
820
+ bottom: styleValueTypes.px,
821
+ left: styleValueTypes.px,
822
+ // Spacing props
823
+ padding: styleValueTypes.px,
824
+ paddingTop: styleValueTypes.px,
825
+ paddingRight: styleValueTypes.px,
826
+ paddingBottom: styleValueTypes.px,
827
+ paddingLeft: styleValueTypes.px,
828
+ margin: styleValueTypes.px,
829
+ marginTop: styleValueTypes.px,
830
+ marginRight: styleValueTypes.px,
831
+ marginBottom: styleValueTypes.px,
832
+ marginLeft: styleValueTypes.px,
833
+ // Transform props
834
+ rotate: styleValueTypes.degrees,
835
+ rotateX: styleValueTypes.degrees,
836
+ rotateY: styleValueTypes.degrees,
837
+ rotateZ: styleValueTypes.degrees,
838
+ scale: styleValueTypes.scale,
839
+ scaleX: styleValueTypes.scale,
840
+ scaleY: styleValueTypes.scale,
841
+ scaleZ: styleValueTypes.scale,
842
+ skew: styleValueTypes.degrees,
843
+ skewX: styleValueTypes.degrees,
844
+ skewY: styleValueTypes.degrees,
845
+ distance: styleValueTypes.px,
846
+ translateX: styleValueTypes.px,
847
+ translateY: styleValueTypes.px,
848
+ translateZ: styleValueTypes.px,
849
+ x: styleValueTypes.px,
850
+ y: styleValueTypes.px,
851
+ z: styleValueTypes.px,
852
+ perspective: styleValueTypes.px,
853
+ transformPerspective: styleValueTypes.px,
854
+ opacity: styleValueTypes.alpha,
855
+ originX: styleValueTypes.progressPercentage,
856
+ originY: styleValueTypes.progressPercentage,
857
+ originZ: styleValueTypes.px,
858
+ // Misc
859
+ zIndex: int,
860
+ // SVG
861
+ fillOpacity: styleValueTypes.alpha,
862
+ strokeOpacity: styleValueTypes.alpha,
863
+ numOctaves: int,
864
+ };
865
+
866
+ /**
867
+ * A map of default value types for common values
868
+ */
869
+ var defaultValueTypes = tslib.__assign(tslib.__assign({}, numberValueTypes), {
870
+ // Color props
871
+ color: styleValueTypes.color, backgroundColor: styleValueTypes.color, outlineColor: styleValueTypes.color, fill: styleValueTypes.color, stroke: styleValueTypes.color,
872
+ // Border props
873
+ borderColor: styleValueTypes.color, borderTopColor: styleValueTypes.color, borderRightColor: styleValueTypes.color, borderBottomColor: styleValueTypes.color, borderLeftColor: styleValueTypes.color, filter: styleValueTypes.filter, WebkitFilter: styleValueTypes.filter });
874
+ /**
875
+ * Gets the default ValueType for the provided value key
876
+ */
877
+ var getDefaultValueType = function (key) { return defaultValueTypes[key]; };
878
+
879
+ function getAnimatableNone(key, value) {
880
+ var _a;
881
+ var defaultValueType = getDefaultValueType(key);
882
+ if (defaultValueType !== styleValueTypes.filter)
883
+ defaultValueType = styleValueTypes.complex;
884
+ // If value is not recognised as animatable, ie "none", create an animatable version origin based on the target
885
+ return (_a = defaultValueType.getAnimatableNone) === null || _a === void 0 ? void 0 : _a.call(defaultValueType, value);
886
+ }
887
+
888
+ var instantAnimationState = {
889
+ current: false,
890
+ };
891
+
892
+ var isCustomValue = function (v) {
893
+ return Boolean(v && typeof v === "object" && v.mix && v.toValue);
894
+ };
895
+ var resolveFinalValueInKeyframes = function (v) {
896
+ // TODO maybe throw if v.length - 1 is placeholder token?
897
+ return isKeyframesTarget(v) ? v[v.length - 1] || 0 : v;
898
+ };
899
+
900
+ /**
901
+ * Decide whether a transition is defined on a given Transition.
902
+ * This filters out orchestration options and returns true
903
+ * if any options are left.
904
+ */
905
+ function isTransitionDefined(_a) {
906
+ _a.when; _a.delay; _a.delayChildren; _a.staggerChildren; _a.staggerDirection; _a.repeat; _a.repeatType; _a.repeatDelay; _a.from; var transition = tslib.__rest(_a, ["when", "delay", "delayChildren", "staggerChildren", "staggerDirection", "repeat", "repeatType", "repeatDelay", "from"]);
907
+ return !!Object.keys(transition).length;
908
+ }
909
+ var legacyRepeatWarning = false;
910
+ /**
911
+ * Convert Framer Motion's Transition type into Popmotion-compatible options.
912
+ */
913
+ function convertTransitionToAnimationOptions(_a) {
914
+ var ease = _a.ease, times = _a.times, yoyo = _a.yoyo, flip = _a.flip, loop = _a.loop, transition = tslib.__rest(_a, ["ease", "times", "yoyo", "flip", "loop"]);
915
+ var options = tslib.__assign({}, transition);
916
+ if (times)
917
+ options["offset"] = times;
918
+ /**
919
+ * Convert any existing durations from seconds to milliseconds
920
+ */
921
+ if (transition.duration)
922
+ options["duration"] = secondsToMilliseconds(transition.duration);
923
+ if (transition.repeatDelay)
924
+ options.repeatDelay = secondsToMilliseconds(transition.repeatDelay);
925
+ /**
926
+ * Map easing names to Popmotion's easing functions
927
+ */
928
+ if (ease) {
929
+ options["ease"] = isEasingArray(ease)
930
+ ? ease.map(easingDefinitionToFunction)
931
+ : easingDefinitionToFunction(ease);
932
+ }
933
+ /**
934
+ * Support legacy transition API
935
+ */
936
+ if (transition.type === "tween")
937
+ options.type = "keyframes";
938
+ /**
939
+ * TODO: These options are officially removed from the API.
940
+ */
941
+ if (yoyo || loop || flip) {
942
+ heyListen.warning(!legacyRepeatWarning, "yoyo, loop and flip have been removed from the API. Replace with repeat and repeatType options.");
943
+ legacyRepeatWarning = true;
944
+ if (yoyo) {
945
+ options.repeatType = "reverse";
946
+ }
947
+ else if (loop) {
948
+ options.repeatType = "loop";
949
+ }
950
+ else if (flip) {
951
+ options.repeatType = "mirror";
952
+ }
953
+ options.repeat = loop || yoyo || flip || transition.repeat;
954
+ }
955
+ /**
956
+ * TODO: Popmotion 9 has the ability to automatically detect whether to use
957
+ * a keyframes or spring animation, but does so by detecting velocity and other spring options.
958
+ * It'd be good to introduce a similar thing here.
959
+ */
960
+ if (transition.type !== "spring")
961
+ options.type = "keyframes";
962
+ return options;
963
+ }
964
+ /**
965
+ * Get the delay for a value by checking Transition with decreasing specificity.
966
+ */
967
+ function getDelayFromTransition(transition, key) {
968
+ var _a, _b;
969
+ var valueTransition = getValueTransition(transition, key) || {};
970
+ return (_b = (_a = valueTransition.delay) !== null && _a !== void 0 ? _a : transition.delay) !== null && _b !== void 0 ? _b : 0;
971
+ }
972
+ function hydrateKeyframes(options) {
973
+ if (Array.isArray(options.to) && options.to[0] === null) {
974
+ options.to = tslib.__spreadArray([], tslib.__read(options.to), false);
975
+ options.to[0] = options.from;
976
+ }
977
+ return options;
978
+ }
979
+ function getPopmotionAnimationOptions(transition, options, key) {
980
+ var _a;
981
+ if (Array.isArray(options.to)) {
982
+ (_a = transition.duration) !== null && _a !== void 0 ? _a : (transition.duration = 0.8);
983
+ }
984
+ hydrateKeyframes(options);
985
+ /**
986
+ * Get a default transition if none is determined to be defined.
987
+ */
988
+ if (!isTransitionDefined(transition)) {
989
+ transition = tslib.__assign(tslib.__assign({}, transition), getDefaultTransition(key, options.to));
990
+ }
991
+ return tslib.__assign(tslib.__assign({}, options), convertTransitionToAnimationOptions(transition));
992
+ }
993
+ /**
994
+ *
995
+ */
996
+ function getAnimation(key, value, target, transition, onComplete) {
997
+ var _a;
998
+ var valueTransition = getValueTransition(transition, key);
999
+ var origin = (_a = valueTransition.from) !== null && _a !== void 0 ? _a : value.get();
1000
+ var isTargetAnimatable = isAnimatable(key, target);
1001
+ if (origin === "none" && isTargetAnimatable && typeof target === "string") {
1002
+ /**
1003
+ * If we're trying to animate from "none", try and get an animatable version
1004
+ * of the target. This could be improved to work both ways.
1005
+ */
1006
+ origin = getAnimatableNone(key, target);
1007
+ }
1008
+ else if (isZero(origin) && typeof target === "string") {
1009
+ origin = getZeroUnit(target);
1010
+ }
1011
+ else if (!Array.isArray(target) &&
1012
+ isZero(target) &&
1013
+ typeof origin === "string") {
1014
+ target = getZeroUnit(origin);
1015
+ }
1016
+ var isOriginAnimatable = isAnimatable(key, origin);
1017
+ heyListen.warning(isOriginAnimatable === isTargetAnimatable, "You are trying to animate ".concat(key, " from \"").concat(origin, "\" to \"").concat(target, "\". ").concat(origin, " is not an animatable value - to enable this animation set ").concat(origin, " to a value animatable to ").concat(target, " via the `style` property."));
1018
+ function start() {
1019
+ var options = {
1020
+ from: origin,
1021
+ to: target,
1022
+ velocity: value.getVelocity(),
1023
+ onComplete: onComplete,
1024
+ onUpdate: function (v) { return value.set(v); },
1025
+ };
1026
+ return valueTransition.type === "inertia" ||
1027
+ valueTransition.type === "decay"
1028
+ ? popmotion.inertia(tslib.__assign(tslib.__assign({}, options), valueTransition))
1029
+ : popmotion.animate(tslib.__assign(tslib.__assign({}, getPopmotionAnimationOptions(valueTransition, options, key)), { onUpdate: function (v) {
1030
+ var _a;
1031
+ options.onUpdate(v);
1032
+ (_a = valueTransition.onUpdate) === null || _a === void 0 ? void 0 : _a.call(valueTransition, v);
1033
+ }, onComplete: function () {
1034
+ var _a;
1035
+ options.onComplete();
1036
+ (_a = valueTransition.onComplete) === null || _a === void 0 ? void 0 : _a.call(valueTransition);
1037
+ } }));
1038
+ }
1039
+ function set() {
1040
+ var _a, _b;
1041
+ var finalTarget = resolveFinalValueInKeyframes(target);
1042
+ value.set(finalTarget);
1043
+ onComplete();
1044
+ (_a = valueTransition === null || valueTransition === void 0 ? void 0 : valueTransition.onUpdate) === null || _a === void 0 ? void 0 : _a.call(valueTransition, finalTarget);
1045
+ (_b = valueTransition === null || valueTransition === void 0 ? void 0 : valueTransition.onComplete) === null || _b === void 0 ? void 0 : _b.call(valueTransition);
1046
+ return { stop: function () { } };
1047
+ }
1048
+ return !isOriginAnimatable ||
1049
+ !isTargetAnimatable ||
1050
+ valueTransition.type === false
1051
+ ? set
1052
+ : start;
1053
+ }
1054
+ function isZero(value) {
1055
+ return (value === 0 ||
1056
+ (typeof value === "string" &&
1057
+ parseFloat(value) === 0 &&
1058
+ value.indexOf(" ") === -1));
1059
+ }
1060
+ function getZeroUnit(potentialUnitType) {
1061
+ return typeof potentialUnitType === "number"
1062
+ ? 0
1063
+ : getAnimatableNone("", potentialUnitType);
1064
+ }
1065
+ function getValueTransition(transition, key) {
1066
+ return transition[key] || transition["default"] || transition;
1067
+ }
1068
+ /**
1069
+ * Start animation on a MotionValue. This function is an interface between
1070
+ * Framer Motion and Popmotion
1071
+ *
1072
+ * @internal
1073
+ */
1074
+ function startAnimation(key, value, target, transition) {
1075
+ if (transition === void 0) { transition = {}; }
1076
+ if (instantAnimationState.current) {
1077
+ transition = { type: false };
1078
+ }
1079
+ return value.start(function (onComplete) {
1080
+ var delayTimer;
1081
+ var controls;
1082
+ var animation = getAnimation(key, value, target, transition, onComplete);
1083
+ var delay = getDelayFromTransition(transition, key);
1084
+ var start = function () { return (controls = animation()); };
1085
+ if (delay) {
1086
+ delayTimer = setTimeout(start, secondsToMilliseconds(delay));
1087
+ }
1088
+ else {
1089
+ start();
1090
+ }
1091
+ return function () {
1092
+ clearTimeout(delayTimer);
1093
+ controls === null || controls === void 0 ? void 0 : controls.stop();
1094
+ };
1095
+ });
1096
+ }
1097
+
1098
+ var createAxis = function () { return ({ min: 0, max: 0 }); };
1099
+ var createBox = function () { return ({
1100
+ x: createAxis(),
1101
+ y: createAxis(),
1102
+ }); };
1103
+
1104
+ /**
1105
+ * If the provided value is a MotionValue, this returns the actual value, otherwise just the value itself
1106
+ *
1107
+ * TODO: Remove and move to library
1108
+ *
1109
+ * @internal
1110
+ */
1111
+ function resolveMotionValue(value) {
1112
+ var unwrappedValue = isMotionValue(value) ? value.get() : value;
1113
+ return isCustomValue(unwrappedValue)
1114
+ ? unwrappedValue.toValue()
1115
+ : unwrappedValue;
1116
+ }
1117
+
1118
+ /**
1119
+ * This should only ever be modified on the client otherwise it'll
1120
+ * persist through server requests. If we need instanced states we
1121
+ * could lazy-init via root.
1122
+ */
1123
+ var globalProjectionState = {
1124
+ /**
1125
+ * Global flag as to whether the tree has animated since the last time
1126
+ * we resized the window
1127
+ */
1128
+ hasAnimatedSinceResize: true,
1129
+ /**
1130
+ * We set this to true once, on the first update. Any nodes added to the tree beyond that
1131
+ * update will be given a `data-projection-id` attribute.
1132
+ */
1133
+ hasEverUpdated: false,
1134
+ };
1135
+
1136
+ var id = 1;
1137
+ function useProjectionId() {
1138
+ return useConstant(function () {
1139
+ if (globalProjectionState.hasEverUpdated) {
1140
+ return id++;
1141
+ }
1142
+ });
1143
+ }
1144
+
1145
+ /**
1146
+ * @internal
1147
+ */
1148
+ var LayoutGroupContext = React.createContext({});
1149
+
1150
+ /**
1151
+ * @internal
1152
+ */
1153
+ var SwitchLayoutGroupContext = React.createContext({});
1154
+
1155
+ function useProjection(projectionId, _a, visualElement, ProjectionNodeConstructor) {
1156
+ var _b;
1157
+ var layoutId = _a.layoutId, layout = _a.layout, drag = _a.drag, dragConstraints = _a.dragConstraints, layoutScroll = _a.layoutScroll;
1158
+ var initialPromotionConfig = React.useContext(SwitchLayoutGroupContext);
1159
+ if (!ProjectionNodeConstructor ||
1160
+ !visualElement ||
1161
+ (visualElement === null || visualElement === void 0 ? void 0 : visualElement.projection)) {
1162
+ return;
1163
+ }
1164
+ visualElement.projection = new ProjectionNodeConstructor(projectionId, visualElement.getLatestValues(), (_b = visualElement.parent) === null || _b === void 0 ? void 0 : _b.projection);
1165
+ visualElement.projection.setOptions({
1166
+ layoutId: layoutId,
1167
+ layout: layout,
1168
+ alwaysMeasureLayout: Boolean(drag) || (dragConstraints && isRefObject(dragConstraints)),
1169
+ visualElement: visualElement,
1170
+ scheduleRender: function () { return visualElement.scheduleRender(); },
1171
+ /**
1172
+ * TODO: Update options in an effect. This could be tricky as it'll be too late
1173
+ * to update by the time layout animations run.
1174
+ * We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
1175
+ * ensuring it gets called if there's no potential layout animations.
1176
+ *
1177
+ */
1178
+ animationType: typeof layout === "string" ? layout : "both",
1179
+ initialPromotionConfig: initialPromotionConfig,
1180
+ layoutScroll: layoutScroll,
1181
+ });
1182
+ }
1183
+
1184
+ var VisualElementHandler = /** @class */ (function (_super) {
1185
+ tslib.__extends(VisualElementHandler, _super);
1186
+ function VisualElementHandler() {
1187
+ return _super !== null && _super.apply(this, arguments) || this;
1188
+ }
1189
+ /**
1190
+ * Update visual element props as soon as we know this update is going to be commited.
1191
+ */
1192
+ VisualElementHandler.prototype.getSnapshotBeforeUpdate = function () {
1193
+ this.updateProps();
1194
+ return null;
1195
+ };
1196
+ VisualElementHandler.prototype.componentDidUpdate = function () { };
1197
+ VisualElementHandler.prototype.updateProps = function () {
1198
+ var _a = this.props, visualElement = _a.visualElement, props = _a.props;
1199
+ if (visualElement)
1200
+ visualElement.setProps(props);
1201
+ };
1202
+ VisualElementHandler.prototype.render = function () {
1203
+ return this.props.children;
1204
+ };
1205
+ return VisualElementHandler;
1206
+ }(React__default["default"].Component));
1207
+
1208
+ /**
1209
+ * Create a `motion` component.
1210
+ *
1211
+ * This function accepts a Component argument, which can be either a string (ie "div"
1212
+ * for `motion.div`), or an actual React component.
1213
+ *
1214
+ * Alongside this is a config option which provides a way of rendering the provided
1215
+ * component "offline", or outside the React render cycle.
1216
+ *
1217
+ * @internal
1218
+ */
1219
+ function createMotionComponent(_a) {
1220
+ var preloadedFeatures = _a.preloadedFeatures, createVisualElement = _a.createVisualElement, projectionNodeConstructor = _a.projectionNodeConstructor, useRender = _a.useRender, useVisualState = _a.useVisualState, Component = _a.Component;
1221
+ preloadedFeatures && loadFeatures(preloadedFeatures);
1222
+ function MotionComponent(props, externalRef) {
1223
+ var layoutId = useLayoutId(props);
1224
+ props = tslib.__assign(tslib.__assign({}, props), { layoutId: layoutId });
1225
+ /**
1226
+ * If we're rendering in a static environment, we only visually update the component
1227
+ * as a result of a React-rerender rather than interactions or animations. This
1228
+ * means we don't need to load additional memory structures like VisualElement,
1229
+ * or any gesture/animation features.
1230
+ */
1231
+ var config = React.useContext(MotionConfigContext);
1232
+ var features = null;
1233
+ var context = useCreateMotionContext(props);
1234
+ /**
1235
+ * Create a unique projection ID for this component. If a new component is added
1236
+ * during a layout animation we'll use this to query the DOM and hydrate its ref early, allowing
1237
+ * us to measure it as soon as any layout effect flushes pending layout animations.
1238
+ *
1239
+ * Performance note: It'd be better not to have to search the DOM for these elements.
1240
+ * For newly-entering components it could be enough to only correct treeScale, in which
1241
+ * case we could mount in a scale-correction mode. This wouldn't be enough for
1242
+ * shared element transitions however. Perhaps for those we could revert to a root node
1243
+ * that gets forceRendered and layout animations are triggered on its layout effect.
1244
+ */
1245
+ var projectionId = useProjectionId();
1246
+ /**
1247
+ *
1248
+ */
1249
+ var visualState = useVisualState(props, config.isStatic);
1250
+ if (!config.isStatic && isBrowser) {
1251
+ /**
1252
+ * Create a VisualElement for this component. A VisualElement provides a common
1253
+ * interface to renderer-specific APIs (ie DOM/Three.js etc) as well as
1254
+ * providing a way of rendering to these APIs outside of the React render loop
1255
+ * for more performant animations and interactions
1256
+ */
1257
+ context.visualElement = useVisualElement(Component, visualState, tslib.__assign(tslib.__assign({}, config), props), createVisualElement);
1258
+ useProjection(projectionId, props, context.visualElement, projectionNodeConstructor ||
1259
+ featureDefinitions.projectionNodeConstructor);
1260
+ /**
1261
+ * Load Motion gesture and animation features. These are rendered as renderless
1262
+ * components so each feature can optionally make use of React lifecycle methods.
1263
+ */
1264
+ features = useFeatures(props, context.visualElement, preloadedFeatures);
1265
+ }
1266
+ /**
1267
+ * The mount order and hierarchy is specific to ensure our element ref
1268
+ * is hydrated by the time features fire their effects.
1269
+ */
1270
+ return (React__namespace.createElement(VisualElementHandler, { visualElement: context.visualElement, props: tslib.__assign(tslib.__assign({}, config), props) },
1271
+ features,
1272
+ React__namespace.createElement(MotionContext.Provider, { value: context }, useRender(Component, props, projectionId, useMotionRef(visualState, context.visualElement, externalRef), visualState, config.isStatic, context.visualElement))));
1273
+ }
1274
+ return React.forwardRef(MotionComponent);
1275
+ }
1276
+ function useLayoutId(_a) {
1277
+ var _b;
1278
+ var layoutId = _a.layoutId;
1279
+ var layoutGroupId = (_b = React.useContext(LayoutGroupContext)) === null || _b === void 0 ? void 0 : _b.id;
1280
+ return layoutGroupId && layoutId !== undefined
1281
+ ? layoutGroupId + "-" + layoutId
1282
+ : layoutId;
1283
+ }
1284
+
1285
+ function isAnimationControls(v) {
1286
+ return typeof v === "object" && typeof v.start === "function";
1287
+ }
1288
+
1289
+ /**
1290
+ * When a component is the child of `AnimatePresence`, it can use `usePresence`
1291
+ * to access information about whether it's still present in the React tree.
1292
+ *
1293
+ * ```jsx
1294
+ * import { usePresence } from "framer-motion"
1295
+ *
1296
+ * export const Component = () => {
1297
+ * const [isPresent, safeToRemove] = usePresence()
1298
+ *
1299
+ * useEffect(() => {
1300
+ * !isPresent && setTimeout(safeToRemove, 1000)
1301
+ * }, [isPresent])
1302
+ *
1303
+ * return <div />
1304
+ * }
1305
+ * ```
1306
+ *
1307
+ * If `isPresent` is `false`, it means that a component has been removed the tree, but
1308
+ * `AnimatePresence` won't really remove it until `safeToRemove` has been called.
1309
+ *
1310
+ * @public
1311
+ */
1312
+ function usePresence() {
1313
+ var context = React.useContext(PresenceContext);
1314
+ if (context === null)
1315
+ return [true, null];
1316
+ var isPresent = context.isPresent, onExitComplete = context.onExitComplete, register = context.register;
1317
+ // It's safe to call the following hooks conditionally (after an early return) because the context will always
1318
+ // either be null or non-null for the lifespan of the component.
1319
+ // Replace with useOpaqueId when released in React
1320
+ var id = useUniqueId();
1321
+ React.useEffect(function () { return register(id); }, []);
1322
+ var safeToRemove = function () { return onExitComplete === null || onExitComplete === void 0 ? void 0 : onExitComplete(id); };
1323
+ return !isPresent && onExitComplete ? [false, safeToRemove] : [true];
1324
+ }
1325
+ var counter = 0;
1326
+ var incrementId = function () { return counter++; };
1327
+ var useUniqueId = function () { return useConstant(incrementId); };
1328
+
1329
+ function shallowCompare(next, prev) {
1330
+ if (!Array.isArray(prev))
1331
+ return false;
1332
+ var prevLength = prev.length;
1333
+ if (prevLength !== next.length)
1334
+ return false;
1335
+ for (var i = 0; i < prevLength; i++) {
1336
+ if (prev[i] !== next[i])
1337
+ return false;
1338
+ }
1339
+ return true;
1340
+ }
1341
+
1342
+ /**
1343
+ * Check if value is a numerical string, ie a string that is purely a number eg "100" or "-100.1"
1344
+ */
1345
+ var isNumericalString = function (v) { return /^\-?\d*\.?\d+$/.test(v); };
1346
+
1347
+ /**
1348
+ * Check if the value is a zero value string like "0px" or "0%"
1349
+ */
1350
+ var isZeroValueString = function (v) { return /^0[^.\s]+$/.test(v); };
1351
+
1352
+ /**
1353
+ * Tests a provided value against a ValueType
1354
+ */
1355
+ var testValueType = function (v) { return function (type) { return type.test(v); }; };
1356
+
1357
+ /**
1358
+ * ValueType for "auto"
1359
+ */
1360
+ var auto = {
1361
+ test: function (v) { return v === "auto"; },
1362
+ parse: function (v) { return v; },
1363
+ };
1364
+
1365
+ /**
1366
+ * A list of value types commonly used for dimensions
1367
+ */
1368
+ var dimensionValueTypes = [styleValueTypes.number, styleValueTypes.px, styleValueTypes.percent, styleValueTypes.degrees, styleValueTypes.vw, styleValueTypes.vh, auto];
1369
+
1370
+ /**
1371
+ * A list of all ValueTypes
1372
+ */
1373
+ var valueTypes = tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(dimensionValueTypes), false), [styleValueTypes.color, styleValueTypes.complex], false);
1374
+ /**
1375
+ * Tests a value against the list of ValueTypes
1376
+ */
1377
+ var findValueType = function (v) { return valueTypes.find(testValueType(v)); };
1378
+
1379
+ /**
1380
+ * Set VisualElement's MotionValue, creating a new MotionValue for it if
1381
+ * it doesn't exist.
1382
+ */
1383
+ function setMotionValue(visualElement, key, value) {
1384
+ if (visualElement.hasValue(key)) {
1385
+ visualElement.getValue(key).set(value);
1386
+ }
1387
+ else {
1388
+ visualElement.addValue(key, motionValue(value));
1389
+ }
1390
+ }
1391
+ function setTarget(visualElement, definition) {
1392
+ var resolved = resolveVariant(visualElement, definition);
1393
+ var _a = resolved ? visualElement.makeTargetAnimatable(resolved, false) : {}, _b = _a.transitionEnd, transitionEnd = _b === void 0 ? {} : _b; _a.transition; var target = tslib.__rest(_a, ["transitionEnd", "transition"]);
1394
+ target = tslib.__assign(tslib.__assign({}, target), transitionEnd);
1395
+ for (var key in target) {
1396
+ var value = resolveFinalValueInKeyframes(target[key]);
1397
+ setMotionValue(visualElement, key, value);
1398
+ }
1399
+ }
1400
+ function checkTargetForNewValues(visualElement, target, origin) {
1401
+ var _a, _b, _c;
1402
+ var _d;
1403
+ var newValueKeys = Object.keys(target).filter(function (key) { return !visualElement.hasValue(key); });
1404
+ var numNewValues = newValueKeys.length;
1405
+ if (!numNewValues)
1406
+ return;
1407
+ for (var i = 0; i < numNewValues; i++) {
1408
+ var key = newValueKeys[i];
1409
+ var targetValue = target[key];
1410
+ var value = null;
1411
+ /**
1412
+ * If the target is a series of keyframes, we can use the first value
1413
+ * in the array. If this first value is null, we'll still need to read from the DOM.
1414
+ */
1415
+ if (Array.isArray(targetValue)) {
1416
+ value = targetValue[0];
1417
+ }
1418
+ /**
1419
+ * If the target isn't keyframes, or the first keyframe was null, we need to
1420
+ * first check if an origin value was explicitly defined in the transition as "from",
1421
+ * if not read the value from the DOM. As an absolute fallback, take the defined target value.
1422
+ */
1423
+ if (value === null) {
1424
+ value = (_b = (_a = origin[key]) !== null && _a !== void 0 ? _a : visualElement.readValue(key)) !== null && _b !== void 0 ? _b : target[key];
1425
+ }
1426
+ /**
1427
+ * If value is still undefined or null, ignore it. Preferably this would throw,
1428
+ * but this was causing issues in Framer.
1429
+ */
1430
+ if (value === undefined || value === null)
1431
+ continue;
1432
+ if (typeof value === "string" &&
1433
+ (isNumericalString(value) || isZeroValueString(value))) {
1434
+ // If this is a number read as a string, ie "0" or "200", convert it to a number
1435
+ value = parseFloat(value);
1436
+ }
1437
+ else if (!findValueType(value) && styleValueTypes.complex.test(targetValue)) {
1438
+ value = getAnimatableNone(key, targetValue);
1439
+ }
1440
+ visualElement.addValue(key, motionValue(value));
1441
+ (_c = (_d = origin)[key]) !== null && _c !== void 0 ? _c : (_d[key] = value);
1442
+ visualElement.setBaseTarget(key, value);
1443
+ }
1444
+ }
1445
+
1446
+ /**
1447
+ * @internal
1448
+ */
1449
+ function animateVisualElement(visualElement, definition, options) {
1450
+ if (options === void 0) { options = {}; }
1451
+ visualElement.notifyAnimationStart(definition);
1452
+ var animation;
1453
+ if (Array.isArray(definition)) {
1454
+ var animations = definition.map(function (variant) {
1455
+ return animateVariant(visualElement, variant, options);
1456
+ });
1457
+ animation = Promise.all(animations);
1458
+ }
1459
+ else if (typeof definition === "string") {
1460
+ animation = animateVariant(visualElement, definition, options);
1461
+ }
1462
+ else {
1463
+ var resolvedDefinition = typeof definition === "function"
1464
+ ? resolveVariant(visualElement, definition, options.custom)
1465
+ : definition;
1466
+ animation = animateTarget(visualElement, resolvedDefinition, options);
1467
+ }
1468
+ return animation.then(function () {
1469
+ return visualElement.notifyAnimationComplete(definition);
1470
+ });
1471
+ }
1472
+ function animateVariant(visualElement, variant, options) {
1473
+ var _a;
1474
+ if (options === void 0) { options = {}; }
1475
+ var resolved = resolveVariant(visualElement, variant, options.custom);
1476
+ var _b = (resolved || {}).transition, transition = _b === void 0 ? visualElement.getDefaultTransition() || {} : _b;
1477
+ if (options.transitionOverride) {
1478
+ transition = options.transitionOverride;
1479
+ }
1480
+ /**
1481
+ * If we have a variant, create a callback that runs it as an animation.
1482
+ * Otherwise, we resolve a Promise immediately for a composable no-op.
1483
+ */
1484
+ var getAnimation = resolved
1485
+ ? function () { return animateTarget(visualElement, resolved, options); }
1486
+ : function () { return Promise.resolve(); };
1487
+ /**
1488
+ * If we have children, create a callback that runs all their animations.
1489
+ * Otherwise, we resolve a Promise immediately for a composable no-op.
1490
+ */
1491
+ var getChildAnimations = ((_a = visualElement.variantChildren) === null || _a === void 0 ? void 0 : _a.size)
1492
+ ? function (forwardDelay) {
1493
+ if (forwardDelay === void 0) { forwardDelay = 0; }
1494
+ var _a = transition.delayChildren, delayChildren = _a === void 0 ? 0 : _a, staggerChildren = transition.staggerChildren, staggerDirection = transition.staggerDirection;
1495
+ return animateChildren(visualElement, variant, delayChildren + forwardDelay, staggerChildren, staggerDirection, options);
1496
+ }
1497
+ : function () { return Promise.resolve(); };
1498
+ /**
1499
+ * If the transition explicitly defines a "when" option, we need to resolve either
1500
+ * this animation or all children animations before playing the other.
1501
+ */
1502
+ var when = transition.when;
1503
+ if (when) {
1504
+ var _c = tslib.__read(when === "beforeChildren"
1505
+ ? [getAnimation, getChildAnimations]
1506
+ : [getChildAnimations, getAnimation], 2), first = _c[0], last = _c[1];
1507
+ return first().then(last);
1508
+ }
1509
+ else {
1510
+ return Promise.all([getAnimation(), getChildAnimations(options.delay)]);
1511
+ }
1512
+ }
1513
+ /**
1514
+ * @internal
1515
+ */
1516
+ function animateTarget(visualElement, definition, _a) {
1517
+ var _b;
1518
+ var _c = _a === void 0 ? {} : _a, _d = _c.delay, delay = _d === void 0 ? 0 : _d, transitionOverride = _c.transitionOverride, type = _c.type;
1519
+ var _e = visualElement.makeTargetAnimatable(definition), _f = _e.transition, transition = _f === void 0 ? visualElement.getDefaultTransition() : _f, transitionEnd = _e.transitionEnd, target = tslib.__rest(_e, ["transition", "transitionEnd"]);
1520
+ if (transitionOverride)
1521
+ transition = transitionOverride;
1522
+ var animations = [];
1523
+ var animationTypeState = type && ((_b = visualElement.animationState) === null || _b === void 0 ? void 0 : _b.getState()[type]);
1524
+ for (var key in target) {
1525
+ var value = visualElement.getValue(key);
1526
+ var valueTarget = target[key];
1527
+ if (!value ||
1528
+ valueTarget === undefined ||
1529
+ (animationTypeState &&
1530
+ shouldBlockAnimation(animationTypeState, key))) {
1531
+ continue;
1532
+ }
1533
+ var animation = startAnimation(key, value, valueTarget, tslib.__assign({ delay: delay }, transition));
1534
+ animations.push(animation);
1535
+ }
1536
+ return Promise.all(animations).then(function () {
1537
+ transitionEnd && setTarget(visualElement, transitionEnd);
1538
+ });
1539
+ }
1540
+ function animateChildren(visualElement, variant, delayChildren, staggerChildren, staggerDirection, options) {
1541
+ if (delayChildren === void 0) { delayChildren = 0; }
1542
+ if (staggerChildren === void 0) { staggerChildren = 0; }
1543
+ if (staggerDirection === void 0) { staggerDirection = 1; }
1544
+ var animations = [];
1545
+ var maxStaggerDuration = (visualElement.variantChildren.size - 1) * staggerChildren;
1546
+ var generateStaggerDuration = staggerDirection === 1
1547
+ ? function (i) {
1548
+ if (i === void 0) { i = 0; }
1549
+ return i * staggerChildren;
1550
+ }
1551
+ : function (i) {
1552
+ if (i === void 0) { i = 0; }
1553
+ return maxStaggerDuration - i * staggerChildren;
1554
+ };
1555
+ Array.from(visualElement.variantChildren)
1556
+ .sort(sortByTreeOrder)
1557
+ .forEach(function (child, i) {
1558
+ animations.push(animateVariant(child, variant, tslib.__assign(tslib.__assign({}, options), { delay: delayChildren + generateStaggerDuration(i) })).then(function () { return child.notifyAnimationComplete(variant); }));
1559
+ });
1560
+ return Promise.all(animations);
1561
+ }
1562
+ function sortByTreeOrder(a, b) {
1563
+ return a.sortNodePosition(b);
1564
+ }
1565
+ /**
1566
+ * Decide whether we should block this animation. Previously, we achieved this
1567
+ * just by checking whether the key was listed in protectedKeys, but this
1568
+ * posed problems if an animation was triggered by afterChildren and protectedKeys
1569
+ * had been set to true in the meantime.
1570
+ */
1571
+ function shouldBlockAnimation(_a, key) {
1572
+ var protectedKeys = _a.protectedKeys, needsAnimating = _a.needsAnimating;
1573
+ var shouldBlock = protectedKeys.hasOwnProperty(key) && needsAnimating[key] !== true;
1574
+ needsAnimating[key] = false;
1575
+ return shouldBlock;
1576
+ }
1577
+
1578
+ var AnimationType;
1579
+ (function (AnimationType) {
1580
+ AnimationType["Animate"] = "animate";
1581
+ AnimationType["Hover"] = "whileHover";
1582
+ AnimationType["Tap"] = "whileTap";
1583
+ AnimationType["Drag"] = "whileDrag";
1584
+ AnimationType["Focus"] = "whileFocus";
1585
+ AnimationType["InView"] = "whileInView";
1586
+ AnimationType["Exit"] = "exit";
1587
+ })(AnimationType || (AnimationType = {}));
1588
+
1589
+ var variantPriorityOrder = [
1590
+ AnimationType.Animate,
1591
+ AnimationType.InView,
1592
+ AnimationType.Focus,
1593
+ AnimationType.Hover,
1594
+ AnimationType.Tap,
1595
+ AnimationType.Drag,
1596
+ AnimationType.Exit,
1597
+ ];
1598
+ var reversePriorityOrder = tslib.__spreadArray([], tslib.__read(variantPriorityOrder), false).reverse();
1599
+ var numAnimationTypes = variantPriorityOrder.length;
1600
+ function animateList(visualElement) {
1601
+ return function (animations) {
1602
+ return Promise.all(animations.map(function (_a) {
1603
+ var animation = _a.animation, options = _a.options;
1604
+ return animateVisualElement(visualElement, animation, options);
1605
+ }));
1606
+ };
1607
+ }
1608
+ function createAnimationState(visualElement) {
1609
+ var animate = animateList(visualElement);
1610
+ var state = createState();
1611
+ var allAnimatedKeys = {};
1612
+ var isInitialRender = true;
1613
+ /**
1614
+ * This function will be used to reduce the animation definitions for
1615
+ * each active animation type into an object of resolved values for it.
1616
+ */
1617
+ var buildResolvedTypeValues = function (acc, definition) {
1618
+ var resolved = resolveVariant(visualElement, definition);
1619
+ if (resolved) {
1620
+ resolved.transition; var transitionEnd = resolved.transitionEnd, target = tslib.__rest(resolved, ["transition", "transitionEnd"]);
1621
+ acc = tslib.__assign(tslib.__assign(tslib.__assign({}, acc), target), transitionEnd);
1622
+ }
1623
+ return acc;
1624
+ };
1625
+ function isAnimated(key) {
1626
+ return allAnimatedKeys[key] !== undefined;
1627
+ }
1628
+ /**
1629
+ * This just allows us to inject mocked animation functions
1630
+ * @internal
1631
+ */
1632
+ function setAnimateFunction(makeAnimator) {
1633
+ animate = makeAnimator(visualElement);
1634
+ }
1635
+ /**
1636
+ * When we receive new props, we need to:
1637
+ * 1. Create a list of protected keys for each type. This is a directory of
1638
+ * value keys that are currently being "handled" by types of a higher priority
1639
+ * so that whenever an animation is played of a given type, these values are
1640
+ * protected from being animated.
1641
+ * 2. Determine if an animation type needs animating.
1642
+ * 3. Determine if any values have been removed from a type and figure out
1643
+ * what to animate those to.
1644
+ */
1645
+ function animateChanges(options, changedActiveType) {
1646
+ var _a;
1647
+ var props = visualElement.getProps();
1648
+ var context = visualElement.getVariantContext(true) || {};
1649
+ /**
1650
+ * A list of animations that we'll build into as we iterate through the animation
1651
+ * types. This will get executed at the end of the function.
1652
+ */
1653
+ var animations = [];
1654
+ /**
1655
+ * Keep track of which values have been removed. Then, as we hit lower priority
1656
+ * animation types, we can check if they contain removed values and animate to that.
1657
+ */
1658
+ var removedKeys = new Set();
1659
+ /**
1660
+ * A dictionary of all encountered keys. This is an object to let us build into and
1661
+ * copy it without iteration. Each time we hit an animation type we set its protected
1662
+ * keys - the keys its not allowed to animate - to the latest version of this object.
1663
+ */
1664
+ var encounteredKeys = {};
1665
+ /**
1666
+ * If a variant has been removed at a given index, and this component is controlling
1667
+ * variant animations, we want to ensure lower-priority variants are forced to animate.
1668
+ */
1669
+ var removedVariantIndex = Infinity;
1670
+ var _loop_1 = function (i) {
1671
+ var type = reversePriorityOrder[i];
1672
+ var typeState = state[type];
1673
+ var prop = (_a = props[type]) !== null && _a !== void 0 ? _a : context[type];
1674
+ var propIsVariant = isVariantLabel(prop);
1675
+ /**
1676
+ * If this type has *just* changed isActive status, set activeDelta
1677
+ * to that status. Otherwise set to null.
1678
+ */
1679
+ var activeDelta = type === changedActiveType ? typeState.isActive : null;
1680
+ if (activeDelta === false)
1681
+ removedVariantIndex = i;
1682
+ /**
1683
+ * If this prop is an inherited variant, rather than been set directly on the
1684
+ * component itself, we want to make sure we allow the parent to trigger animations.
1685
+ *
1686
+ * TODO: Can probably change this to a !isControllingVariants check
1687
+ */
1688
+ var isInherited = prop === context[type] && prop !== props[type] && propIsVariant;
1689
+ /**
1690
+ *
1691
+ */
1692
+ if (isInherited &&
1693
+ isInitialRender &&
1694
+ visualElement.manuallyAnimateOnMount) {
1695
+ isInherited = false;
1696
+ }
1697
+ /**
1698
+ * Set all encountered keys so far as the protected keys for this type. This will
1699
+ * be any key that has been animated or otherwise handled by active, higher-priortiy types.
1700
+ */
1701
+ typeState.protectedKeys = tslib.__assign({}, encounteredKeys);
1702
+ // Check if we can skip analysing this prop early
1703
+ if (
1704
+ // If it isn't active and hasn't *just* been set as inactive
1705
+ (!typeState.isActive && activeDelta === null) ||
1706
+ // If we didn't and don't have any defined prop for this animation type
1707
+ (!prop && !typeState.prevProp) ||
1708
+ // Or if the prop doesn't define an animation
1709
+ isAnimationControls(prop) ||
1710
+ typeof prop === "boolean") {
1711
+ return "continue";
1712
+ }
1713
+ /**
1714
+ * As we go look through the values defined on this type, if we detect
1715
+ * a changed value or a value that was removed in a higher priority, we set
1716
+ * this to true and add this prop to the animation list.
1717
+ */
1718
+ var variantDidChange = checkVariantsDidChange(typeState.prevProp, prop);
1719
+ var shouldAnimateType = variantDidChange ||
1720
+ // If we're making this variant active, we want to always make it active
1721
+ (type === changedActiveType &&
1722
+ typeState.isActive &&
1723
+ !isInherited &&
1724
+ propIsVariant) ||
1725
+ // If we removed a higher-priority variant (i is in reverse order)
1726
+ (i > removedVariantIndex && propIsVariant);
1727
+ /**
1728
+ * As animations can be set as variant lists, variants or target objects, we
1729
+ * coerce everything to an array if it isn't one already
1730
+ */
1731
+ var definitionList = Array.isArray(prop) ? prop : [prop];
1732
+ /**
1733
+ * Build an object of all the resolved values. We'll use this in the subsequent
1734
+ * animateChanges calls to determine whether a value has changed.
1735
+ */
1736
+ var resolvedValues = definitionList.reduce(buildResolvedTypeValues, {});
1737
+ if (activeDelta === false)
1738
+ resolvedValues = {};
1739
+ /**
1740
+ * Now we need to loop through all the keys in the prev prop and this prop,
1741
+ * and decide:
1742
+ * 1. If the value has changed, and needs animating
1743
+ * 2. If it has been removed, and needs adding to the removedKeys set
1744
+ * 3. If it has been removed in a higher priority type and needs animating
1745
+ * 4. If it hasn't been removed in a higher priority but hasn't changed, and
1746
+ * needs adding to the type's protectedKeys list.
1747
+ */
1748
+ var _b = typeState.prevResolvedValues, prevResolvedValues = _b === void 0 ? {} : _b;
1749
+ var allKeys = tslib.__assign(tslib.__assign({}, prevResolvedValues), resolvedValues);
1750
+ var markToAnimate = function (key) {
1751
+ shouldAnimateType = true;
1752
+ removedKeys.delete(key);
1753
+ typeState.needsAnimating[key] = true;
1754
+ };
1755
+ for (var key in allKeys) {
1756
+ var next = resolvedValues[key];
1757
+ var prev = prevResolvedValues[key];
1758
+ // If we've already handled this we can just skip ahead
1759
+ if (encounteredKeys.hasOwnProperty(key))
1760
+ continue;
1761
+ /**
1762
+ * If the value has changed, we probably want to animate it.
1763
+ */
1764
+ if (next !== prev) {
1765
+ /**
1766
+ * If both values are keyframes, we need to shallow compare them to
1767
+ * detect whether any value has changed. If it has, we animate it.
1768
+ */
1769
+ if (isKeyframesTarget(next) && isKeyframesTarget(prev)) {
1770
+ if (!shallowCompare(next, prev) || variantDidChange) {
1771
+ markToAnimate(key);
1772
+ }
1773
+ else {
1774
+ /**
1775
+ * If it hasn't changed, we want to ensure it doesn't animate by
1776
+ * adding it to the list of protected keys.
1777
+ */
1778
+ typeState.protectedKeys[key] = true;
1779
+ }
1780
+ }
1781
+ else if (next !== undefined) {
1782
+ // If next is defined and doesn't equal prev, it needs animating
1783
+ markToAnimate(key);
1784
+ }
1785
+ else {
1786
+ // If it's undefined, it's been removed.
1787
+ removedKeys.add(key);
1788
+ }
1789
+ }
1790
+ else if (next !== undefined && removedKeys.has(key)) {
1791
+ /**
1792
+ * If next hasn't changed and it isn't undefined, we want to check if it's
1793
+ * been removed by a higher priority
1794
+ */
1795
+ markToAnimate(key);
1796
+ }
1797
+ else {
1798
+ /**
1799
+ * If it hasn't changed, we add it to the list of protected values
1800
+ * to ensure it doesn't get animated.
1801
+ */
1802
+ typeState.protectedKeys[key] = true;
1803
+ }
1804
+ }
1805
+ /**
1806
+ * Update the typeState so next time animateChanges is called we can compare the
1807
+ * latest prop and resolvedValues to these.
1808
+ */
1809
+ typeState.prevProp = prop;
1810
+ typeState.prevResolvedValues = resolvedValues;
1811
+ /**
1812
+ *
1813
+ */
1814
+ if (typeState.isActive) {
1815
+ encounteredKeys = tslib.__assign(tslib.__assign({}, encounteredKeys), resolvedValues);
1816
+ }
1817
+ if (isInitialRender && visualElement.blockInitialAnimation) {
1818
+ shouldAnimateType = false;
1819
+ }
1820
+ /**
1821
+ * If this is an inherited prop we want to hard-block animations
1822
+ * TODO: Test as this should probably still handle animations triggered
1823
+ * by removed values?
1824
+ */
1825
+ if (shouldAnimateType && !isInherited) {
1826
+ animations.push.apply(animations, tslib.__spreadArray([], tslib.__read(definitionList.map(function (animation) { return ({
1827
+ animation: animation,
1828
+ options: tslib.__assign({ type: type }, options),
1829
+ }); })), false));
1830
+ }
1831
+ };
1832
+ /**
1833
+ * Iterate through all animation types in reverse priority order. For each, we want to
1834
+ * detect which values it's handling and whether or not they've changed (and therefore
1835
+ * need to be animated). If any values have been removed, we want to detect those in
1836
+ * lower priority props and flag for animation.
1837
+ */
1838
+ for (var i = 0; i < numAnimationTypes; i++) {
1839
+ _loop_1(i);
1840
+ }
1841
+ allAnimatedKeys = tslib.__assign({}, encounteredKeys);
1842
+ /**
1843
+ * If there are some removed value that haven't been dealt with,
1844
+ * we need to create a new animation that falls back either to the value
1845
+ * defined in the style prop, or the last read value.
1846
+ */
1847
+ if (removedKeys.size) {
1848
+ var fallbackAnimation_1 = {};
1849
+ removedKeys.forEach(function (key) {
1850
+ var fallbackTarget = visualElement.getBaseTarget(key);
1851
+ if (fallbackTarget !== undefined) {
1852
+ fallbackAnimation_1[key] = fallbackTarget;
1853
+ }
1854
+ });
1855
+ animations.push({ animation: fallbackAnimation_1 });
1856
+ }
1857
+ var shouldAnimate = Boolean(animations.length);
1858
+ if (isInitialRender &&
1859
+ props.initial === false &&
1860
+ !visualElement.manuallyAnimateOnMount) {
1861
+ shouldAnimate = false;
1862
+ }
1863
+ isInitialRender = false;
1864
+ return shouldAnimate ? animate(animations) : Promise.resolve();
1865
+ }
1866
+ /**
1867
+ * Change whether a certain animation type is active.
1868
+ */
1869
+ function setActive(type, isActive, options) {
1870
+ var _a;
1871
+ // If the active state hasn't changed, we can safely do nothing here
1872
+ if (state[type].isActive === isActive)
1873
+ return Promise.resolve();
1874
+ // Propagate active change to children
1875
+ (_a = visualElement.variantChildren) === null || _a === void 0 ? void 0 : _a.forEach(function (child) { var _a; return (_a = child.animationState) === null || _a === void 0 ? void 0 : _a.setActive(type, isActive); });
1876
+ state[type].isActive = isActive;
1877
+ return animateChanges(options, type);
1878
+ }
1879
+ return {
1880
+ isAnimated: isAnimated,
1881
+ animateChanges: animateChanges,
1882
+ setActive: setActive,
1883
+ setAnimateFunction: setAnimateFunction,
1884
+ getState: function () { return state; },
1885
+ };
1886
+ }
1887
+ function checkVariantsDidChange(prev, next) {
1888
+ if (typeof next === "string") {
1889
+ return next !== prev;
1890
+ }
1891
+ else if (isVariantLabels(next)) {
1892
+ return !shallowCompare(next, prev);
1893
+ }
1894
+ return false;
1895
+ }
1896
+ function createTypeState(isActive) {
1897
+ if (isActive === void 0) { isActive = false; }
1898
+ return {
1899
+ isActive: isActive,
1900
+ protectedKeys: {},
1901
+ needsAnimating: {},
1902
+ prevResolvedValues: {},
1903
+ };
1904
+ }
1905
+ function createState() {
1906
+ var _a;
1907
+ return _a = {},
1908
+ _a[AnimationType.Animate] = createTypeState(true),
1909
+ _a[AnimationType.InView] = createTypeState(),
1910
+ _a[AnimationType.Hover] = createTypeState(),
1911
+ _a[AnimationType.Tap] = createTypeState(),
1912
+ _a[AnimationType.Drag] = createTypeState(),
1913
+ _a[AnimationType.Focus] = createTypeState(),
1914
+ _a[AnimationType.Exit] = createTypeState(),
1915
+ _a;
1916
+ }
1917
+
1918
+ var makeRenderlessComponent = function (hook) { return function (props) {
1919
+ hook(props);
1920
+ return null;
1921
+ }; };
1922
+
1923
+ var animations = {
1924
+ animation: makeRenderlessComponent(function (_a) {
1925
+ var visualElement = _a.visualElement, animate = _a.animate;
1926
+ /**
1927
+ * We dynamically generate the AnimationState manager as it contains a reference
1928
+ * to the underlying animation library. We only want to load that if we load this,
1929
+ * so people can optionally code split it out using the `m` component.
1930
+ */
1931
+ visualElement.animationState || (visualElement.animationState = createAnimationState(visualElement));
1932
+ /**
1933
+ * Subscribe any provided AnimationControls to the component's VisualElement
1934
+ */
1935
+ if (isAnimationControls(animate)) {
1936
+ React.useEffect(function () { return animate.subscribe(visualElement); }, [animate]);
1937
+ }
1938
+ }),
1939
+ exit: makeRenderlessComponent(function (props) {
1940
+ var custom = props.custom, visualElement = props.visualElement;
1941
+ var _a = tslib.__read(usePresence(), 2), isPresent = _a[0], safeToRemove = _a[1];
1942
+ var presenceContext = React.useContext(PresenceContext);
1943
+ React.useEffect(function () {
1944
+ var _a, _b;
1945
+ visualElement.isPresent = isPresent;
1946
+ var animation = (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(AnimationType.Exit, !isPresent, { custom: (_b = presenceContext === null || presenceContext === void 0 ? void 0 : presenceContext.custom) !== null && _b !== void 0 ? _b : custom });
1947
+ !isPresent && (animation === null || animation === void 0 ? void 0 : animation.then(safeToRemove));
1948
+ }, [isPresent]);
1949
+ }),
1950
+ };
1951
+
1952
+ function makeState(_a, props, context, presenceContext) {
1953
+ var scrapeMotionValuesFromProps = _a.scrapeMotionValuesFromProps, createRenderState = _a.createRenderState, onMount = _a.onMount;
1954
+ var state = {
1955
+ latestValues: makeLatestValues(props, context, presenceContext, scrapeMotionValuesFromProps),
1956
+ renderState: createRenderState(),
1957
+ };
1958
+ if (onMount) {
1959
+ state.mount = function (instance) { return onMount(props, instance, state); };
1960
+ }
1961
+ return state;
1962
+ }
1963
+ var makeUseVisualState = function (config) {
1964
+ return function (props, isStatic) {
1965
+ var context = React.useContext(MotionContext);
1966
+ var presenceContext = React.useContext(PresenceContext);
1967
+ return isStatic
1968
+ ? makeState(config, props, context, presenceContext)
1969
+ : useConstant(function () {
1970
+ return makeState(config, props, context, presenceContext);
1971
+ });
1972
+ };
1973
+ };
1974
+ function makeLatestValues(props, context, presenceContext, scrapeMotionValues) {
1975
+ var values = {};
1976
+ var blockInitialAnimation = (presenceContext === null || presenceContext === void 0 ? void 0 : presenceContext.initial) === false;
1977
+ var motionValues = scrapeMotionValues(props);
1978
+ for (var key in motionValues) {
1979
+ values[key] = resolveMotionValue(motionValues[key]);
1980
+ }
1981
+ var initial = props.initial, animate = props.animate;
1982
+ var isControllingVariants = checkIfControllingVariants(props);
1983
+ var isVariantNode = checkIfVariantNode(props);
1984
+ if (context &&
1985
+ isVariantNode &&
1986
+ !isControllingVariants &&
1987
+ props.inherit !== false) {
1988
+ initial !== null && initial !== void 0 ? initial : (initial = context.initial);
1989
+ animate !== null && animate !== void 0 ? animate : (animate = context.animate);
1990
+ }
1991
+ var initialAnimationIsBlocked = blockInitialAnimation || initial === false;
1992
+ var variantToSet = initialAnimationIsBlocked ? animate : initial;
1993
+ if (variantToSet &&
1994
+ typeof variantToSet !== "boolean" &&
1995
+ !isAnimationControls(variantToSet)) {
1996
+ var list = Array.isArray(variantToSet) ? variantToSet : [variantToSet];
1997
+ list.forEach(function (definition) {
1998
+ var resolved = resolveVariantFromProps(props, definition);
1999
+ if (!resolved)
2000
+ return;
2001
+ var transitionEnd = resolved.transitionEnd; resolved.transition; var target = tslib.__rest(resolved, ["transitionEnd", "transition"]);
2002
+ for (var key in target) {
2003
+ var valueTarget = target[key];
2004
+ if (Array.isArray(valueTarget)) {
2005
+ /**
2006
+ * Take final keyframe if the initial animation is blocked because
2007
+ * we want to initialise at the end of that blocked animation.
2008
+ */
2009
+ var index = initialAnimationIsBlocked
2010
+ ? valueTarget.length - 1
2011
+ : 0;
2012
+ valueTarget = valueTarget[index];
2013
+ }
2014
+ if (valueTarget !== null) {
2015
+ values[key] = valueTarget;
2016
+ }
2017
+ }
2018
+ for (var key in transitionEnd)
2019
+ values[key] = transitionEnd[key];
2020
+ });
2021
+ }
2022
+ return values;
2023
+ }
2024
+
2025
+ /**
2026
+ * A list of all valid MotionProps.
2027
+ *
2028
+ * @internalremarks
2029
+ * This doesn't throw if a `MotionProp` name is missing - it should.
2030
+ */
2031
+ var validMotionProps = new Set([
2032
+ "initial",
2033
+ "animate",
2034
+ "exit",
2035
+ "style",
2036
+ "variants",
2037
+ "transition",
2038
+ "transformTemplate",
2039
+ "transformValues",
2040
+ "custom",
2041
+ "inherit",
2042
+ "layout",
2043
+ "layoutId",
2044
+ "layoutDependency",
2045
+ "onLayoutAnimationComplete",
2046
+ "onLayoutMeasure",
2047
+ "onBeforeLayoutMeasure",
2048
+ "onAnimationStart",
2049
+ "onAnimationComplete",
2050
+ "onUpdate",
2051
+ "onDragStart",
2052
+ "onDrag",
2053
+ "onDragEnd",
2054
+ "onMeasureDragConstraints",
2055
+ "onDirectionLock",
2056
+ "onDragTransitionEnd",
2057
+ "drag",
2058
+ "dragControls",
2059
+ "dragListener",
2060
+ "dragConstraints",
2061
+ "dragDirectionLock",
2062
+ "dragSnapToOrigin",
2063
+ "_dragX",
2064
+ "_dragY",
2065
+ "dragElastic",
2066
+ "dragMomentum",
2067
+ "dragPropagation",
2068
+ "dragTransition",
2069
+ "whileDrag",
2070
+ "onPan",
2071
+ "onPanStart",
2072
+ "onPanEnd",
2073
+ "onPanSessionStart",
2074
+ "onTap",
2075
+ "onTapStart",
2076
+ "onTapCancel",
2077
+ "onHoverStart",
2078
+ "onHoverEnd",
2079
+ "whileFocus",
2080
+ "whileTap",
2081
+ "whileHover",
2082
+ "whileInView",
2083
+ "onViewportEnter",
2084
+ "onViewportLeave",
2085
+ "viewport",
2086
+ "layoutScroll",
2087
+ ]);
2088
+ /**
2089
+ * Check whether a prop name is a valid `MotionProp` key.
2090
+ *
2091
+ * @param key - Name of the property to check
2092
+ * @returns `true` is key is a valid `MotionProp`.
2093
+ *
2094
+ * @public
2095
+ */
2096
+ function isValidMotionProp(key) {
2097
+ return validMotionProps.has(key);
2098
+ }
2099
+
2100
+ var shouldForward = function (key) { return !isValidMotionProp(key); };
2101
+ /**
2102
+ * Emotion and Styled Components both allow users to pass through arbitrary props to their components
2103
+ * to dynamically generate CSS. They both use the `@emotion/is-prop-valid` package to determine which
2104
+ * of these should be passed to the underlying DOM node.
2105
+ *
2106
+ * However, when styling a Motion component `styled(motion.div)`, both packages pass through *all* props
2107
+ * as it's seen as an arbitrary component rather than a DOM node. Motion only allows arbitrary props
2108
+ * passed through the `custom` prop so it doesn't *need* the payload or computational overhead of
2109
+ * `@emotion/is-prop-valid`, however to fix this problem we need to use it.
2110
+ *
2111
+ * By making it an optionalDependency we can offer this functionality only in the situations where it's
2112
+ * actually required.
2113
+ */
2114
+ try {
2115
+ var emotionIsPropValid_1 = require("@emotion/is-prop-valid").default;
2116
+ shouldForward = function (key) {
2117
+ // Handle events explicitly as Emotion validates them all as true
2118
+ if (key.startsWith("on")) {
2119
+ return !isValidMotionProp(key);
2120
+ }
2121
+ else {
2122
+ return emotionIsPropValid_1(key);
2123
+ }
2124
+ };
2125
+ }
2126
+ catch (_a) {
2127
+ // We don't need to actually do anything here - the fallback is the existing `isPropValid`.
2128
+ }
2129
+ function filterProps(props, isDom, forwardMotionProps) {
2130
+ var filteredProps = {};
2131
+ for (var key in props) {
2132
+ if (shouldForward(key) ||
2133
+ (forwardMotionProps === true && isValidMotionProp(key)) ||
2134
+ (!isDom && !isValidMotionProp(key)) ||
2135
+ // If trying to use native HTML drag events, forward drag listeners
2136
+ (props["draggable"] && key.startsWith("onDrag"))) {
2137
+ filteredProps[key] = props[key];
2138
+ }
2139
+ }
2140
+ return filteredProps;
2141
+ }
2142
+
2143
+ function useHover(isStatic, _a, visualElement) {
2144
+ var whileHover = _a.whileHover, onHoverStart = _a.onHoverStart, onHoverEnd = _a.onHoverEnd, onPointerOver = _a.onPointerOver, onPointerOut = _a.onPointerOut;
2145
+ var isHoverEnabled = whileHover || onHoverStart || onHoverEnd;
2146
+ if (isStatic || !visualElement || !isHoverEnabled)
2147
+ return {};
2148
+ return {
2149
+ onPointerOver: function (event) {
2150
+ var _a;
2151
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(AnimationType.Hover, true);
2152
+ onPointerOver === null || onPointerOver === void 0 ? void 0 : onPointerOver(event);
2153
+ },
2154
+ onPointerOut: function (event) {
2155
+ var _a;
2156
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(AnimationType.Hover, false);
2157
+ onPointerOut === null || onPointerOut === void 0 ? void 0 : onPointerOut(event);
2158
+ },
2159
+ };
2160
+ }
2161
+
2162
+ function isTouchEvent(event) {
2163
+ var hasTouches = !!event.touches;
2164
+ return hasTouches;
2165
+ }
2166
+
2167
+ /**
2168
+ * Filters out events not attached to the primary pointer (currently left mouse button)
2169
+ * @param eventHandler
2170
+ */
2171
+ function filterPrimaryPointer(eventHandler) {
2172
+ return function (event) {
2173
+ var isMouseEvent = event instanceof MouseEvent;
2174
+ var isPrimaryPointer = !isMouseEvent ||
2175
+ (isMouseEvent && event.button === 0);
2176
+ if (isPrimaryPointer) {
2177
+ eventHandler(event);
2178
+ }
2179
+ };
2180
+ }
2181
+ var defaultPagePoint = { pageX: 0, pageY: 0 };
2182
+ function pointFromTouch(e, pointType) {
2183
+ if (pointType === void 0) { pointType = "page"; }
2184
+ var primaryTouch = e.touches[0] || e.changedTouches[0];
2185
+ var point = primaryTouch || defaultPagePoint;
2186
+ return {
2187
+ x: point[pointType + "X"],
2188
+ y: point[pointType + "Y"],
2189
+ };
2190
+ }
2191
+ function pointFromMouse(point, pointType) {
2192
+ if (pointType === void 0) { pointType = "page"; }
2193
+ return {
2194
+ x: point[pointType + "X"],
2195
+ y: point[pointType + "Y"],
2196
+ };
2197
+ }
2198
+ function extractEventInfo(event, pointType) {
2199
+ if (pointType === void 0) { pointType = "page"; }
2200
+ return {
2201
+ point: isTouchEvent(event)
2202
+ ? pointFromTouch(event, pointType)
2203
+ : pointFromMouse(event, pointType),
2204
+ };
2205
+ }
2206
+ var wrapHandler = function (handler, shouldFilterPrimaryPointer) {
2207
+ if (shouldFilterPrimaryPointer === void 0) { shouldFilterPrimaryPointer = false; }
2208
+ var listener = function (event) {
2209
+ return handler(event, extractEventInfo(event));
2210
+ };
2211
+ return shouldFilterPrimaryPointer
2212
+ ? filterPrimaryPointer(listener)
2213
+ : listener;
2214
+ };
2215
+
2216
+ function addDomEvent(target, eventName, handler, options) {
2217
+ target.addEventListener(eventName, handler, options);
2218
+ return function () { return target.removeEventListener(eventName, handler, options); };
2219
+ }
2220
+
2221
+ // We check for event support via functions in case they've been mocked by a testing suite.
2222
+ var supportsPointerEvents = function () {
2223
+ return isBrowser && window.onpointerdown === null;
2224
+ };
2225
+ var supportsTouchEvents = function () {
2226
+ return isBrowser && window.ontouchstart === null;
2227
+ };
2228
+ var supportsMouseEvents = function () {
2229
+ return isBrowser && window.onmousedown === null;
2230
+ };
2231
+
2232
+ var mouseEventNames = {
2233
+ pointerdown: "mousedown",
2234
+ pointermove: "mousemove",
2235
+ pointerup: "mouseup",
2236
+ pointercancel: "mousecancel",
2237
+ pointerover: "mouseover",
2238
+ pointerout: "mouseout",
2239
+ pointerenter: "mouseenter",
2240
+ pointerleave: "mouseleave",
2241
+ };
2242
+ var touchEventNames = {
2243
+ pointerdown: "touchstart",
2244
+ pointermove: "touchmove",
2245
+ pointerup: "touchend",
2246
+ pointercancel: "touchcancel",
2247
+ };
2248
+ function getPointerEventName(name) {
2249
+ if (supportsPointerEvents()) {
2250
+ return name;
2251
+ }
2252
+ else if (supportsTouchEvents()) {
2253
+ return touchEventNames[name];
2254
+ }
2255
+ else if (supportsMouseEvents()) {
2256
+ return mouseEventNames[name];
2257
+ }
2258
+ return name;
2259
+ }
2260
+ function addPointerEvent(target, eventName, handler, options) {
2261
+ return addDomEvent(target, getPointerEventName(eventName), wrapHandler(handler, eventName === "pointerdown"), options);
2262
+ }
2263
+
2264
+ function createLock(name) {
2265
+ var lock = null;
2266
+ return function () {
2267
+ var openLock = function () {
2268
+ lock = null;
2269
+ };
2270
+ if (lock === null) {
2271
+ lock = name;
2272
+ return openLock;
2273
+ }
2274
+ return false;
2275
+ };
2276
+ }
2277
+ var globalHorizontalLock = createLock("dragHorizontal");
2278
+ var globalVerticalLock = createLock("dragVertical");
2279
+ function getGlobalLock(drag) {
2280
+ var lock = false;
2281
+ if (drag === "y") {
2282
+ lock = globalVerticalLock();
2283
+ }
2284
+ else if (drag === "x") {
2285
+ lock = globalHorizontalLock();
2286
+ }
2287
+ else {
2288
+ var openHorizontal_1 = globalHorizontalLock();
2289
+ var openVertical_1 = globalVerticalLock();
2290
+ if (openHorizontal_1 && openVertical_1) {
2291
+ lock = function () {
2292
+ openHorizontal_1();
2293
+ openVertical_1();
2294
+ };
2295
+ }
2296
+ else {
2297
+ // Release the locks because we don't use them
2298
+ if (openHorizontal_1)
2299
+ openHorizontal_1();
2300
+ if (openVertical_1)
2301
+ openVertical_1();
2302
+ }
2303
+ }
2304
+ return lock;
2305
+ }
2306
+ function isDragActive() {
2307
+ // Check the gesture lock - if we get it, it means no drag gesture is active
2308
+ // and we can safely fire the tap gesture.
2309
+ var openGestureLock = getGlobalLock(true);
2310
+ if (!openGestureLock)
2311
+ return true;
2312
+ openGestureLock();
2313
+ return false;
2314
+ }
2315
+
2316
+ function useTap(isStatic, _a, visualElement) {
2317
+ var whileTap = _a.whileTap, onTapStart = _a.onTapStart, onTap = _a.onTap, onTapCancel = _a.onTapCancel, onPointerDown = _a.onPointerDown;
2318
+ var isTapEnabled = onTap || onTapStart || onTapCancel || whileTap;
2319
+ var isPressing = React.useRef(false);
2320
+ var cancelPointerEndListeners = React.useRef(null);
2321
+ if (isStatic || !visualElement || !isTapEnabled)
2322
+ return {};
2323
+ function removePointerEndListener() {
2324
+ var _a;
2325
+ (_a = cancelPointerEndListeners.current) === null || _a === void 0 ? void 0 : _a.call(cancelPointerEndListeners);
2326
+ cancelPointerEndListeners.current = null;
2327
+ }
2328
+ function checkPointerEnd() {
2329
+ var _a;
2330
+ removePointerEndListener();
2331
+ isPressing.current = false;
2332
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(AnimationType.Tap, false);
2333
+ return !isDragActive();
2334
+ }
2335
+ function onPointerUp(event, info) {
2336
+ if (!checkPointerEnd())
2337
+ return;
2338
+ /**
2339
+ * We only count this as a tap gesture if the event.target is the same
2340
+ * as, or a child of, this component's element
2341
+ */
2342
+ onTap === null || onTap === void 0 ? void 0 : onTap(event, info);
2343
+ }
2344
+ function onPointerCancel(event, info) {
2345
+ if (!checkPointerEnd())
2346
+ return;
2347
+ onTapCancel === null || onTapCancel === void 0 ? void 0 : onTapCancel(event, info);
2348
+ }
2349
+ return {
2350
+ onPointerDown: wrapHandler(function (event, info) {
2351
+ var _a;
2352
+ removePointerEndListener();
2353
+ if (isPressing.current)
2354
+ return;
2355
+ isPressing.current = true;
2356
+ cancelPointerEndListeners.current = popmotion.pipe(addPointerEvent(window, "pointerup", onPointerUp), addPointerEvent(window, "pointercancel", onPointerCancel));
2357
+ (_a = visualElement.animationState) === null || _a === void 0 ? void 0 : _a.setActive(AnimationType.Tap, true);
2358
+ onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown(event);
2359
+ onTapStart === null || onTapStart === void 0 ? void 0 : onTapStart(event, info);
2360
+ }, true),
2361
+ };
2362
+ }
2363
+
2364
+ var useRender = function (Component, props, _projectionId, ref, _state, isStatic, visualElement) {
2365
+ return React.createElement(Component, tslib.__assign(tslib.__assign(tslib.__assign(tslib.__assign({ ref: ref }, filterProps(props, false, false)), { onUpdate: props.onInstanceUpdate }), useHover(isStatic, props, visualElement)), useTap(isStatic, props, visualElement)));
2366
+ };
2367
+
2368
+ var names = [
2369
+ "LayoutMeasure",
2370
+ "BeforeLayoutMeasure",
2371
+ "LayoutUpdate",
2372
+ "ViewportBoxUpdate",
2373
+ "Update",
2374
+ "Render",
2375
+ "AnimationComplete",
2376
+ "LayoutAnimationComplete",
2377
+ "AnimationStart",
2378
+ "SetAxisTarget",
2379
+ "Unmount",
2380
+ ];
2381
+ function createLifecycles() {
2382
+ var managers = names.map(function () { return new SubscriptionManager(); });
2383
+ var propSubscriptions = {};
2384
+ var lifecycles = {
2385
+ clearAllListeners: function () { return managers.forEach(function (manager) { return manager.clear(); }); },
2386
+ updatePropListeners: function (props) {
2387
+ names.forEach(function (name) {
2388
+ var _a;
2389
+ var on = "on" + name;
2390
+ var propListener = props[on];
2391
+ // Unsubscribe existing subscription
2392
+ (_a = propSubscriptions[name]) === null || _a === void 0 ? void 0 : _a.call(propSubscriptions);
2393
+ // Add new subscription
2394
+ if (propListener) {
2395
+ propSubscriptions[name] = lifecycles[on](propListener);
2396
+ }
2397
+ });
2398
+ },
2399
+ };
2400
+ managers.forEach(function (manager, i) {
2401
+ lifecycles["on" + names[i]] = function (handler) { return manager.add(handler); };
2402
+ lifecycles["notify" + names[i]] = function () {
2403
+ var args = [];
2404
+ for (var _i = 0; _i < arguments.length; _i++) {
2405
+ args[_i] = arguments[_i];
2406
+ }
2407
+ manager.notify.apply(manager, tslib.__spreadArray([], tslib.__read(args), false));
2408
+ };
2409
+ });
2410
+ return lifecycles;
2411
+ }
2412
+
2413
+ function updateMotionValuesFromProps(element, next, prev) {
2414
+ var _a;
2415
+ for (var key in next) {
2416
+ var nextValue = next[key];
2417
+ var prevValue = prev[key];
2418
+ if (isMotionValue(nextValue)) {
2419
+ /**
2420
+ * If this is a motion value found in props or style, we want to add it
2421
+ * to our visual element's motion value map.
2422
+ */
2423
+ element.addValue(key, nextValue);
2424
+ }
2425
+ else if (isMotionValue(prevValue)) {
2426
+ /**
2427
+ * If we're swapping to a new motion value, create a new motion value
2428
+ * from that
2429
+ */
2430
+ element.addValue(key, motionValue(nextValue));
2431
+ }
2432
+ else if (prevValue !== nextValue) {
2433
+ /**
2434
+ * If this is a flat value that has changed, update the motion value
2435
+ * or create one if it doesn't exist. We only want to do this if we're
2436
+ * not handling the value with our animation state.
2437
+ */
2438
+ if (element.hasValue(key)) {
2439
+ var existingValue = element.getValue(key);
2440
+ // TODO: Only update values that aren't being animated or even looked at
2441
+ !existingValue.hasAnimated && existingValue.set(nextValue);
2442
+ }
2443
+ else {
2444
+ element.addValue(key, motionValue((_a = element.getStaticValue(key)) !== null && _a !== void 0 ? _a : nextValue));
2445
+ }
2446
+ }
2447
+ }
2448
+ // Handle removed values
2449
+ for (var key in prev) {
2450
+ if (next[key] === undefined)
2451
+ element.removeValue(key);
2452
+ }
2453
+ return next;
2454
+ }
2455
+
2456
+ var visualElement = function (_a) {
2457
+ var _b = _a.treeType, treeType = _b === void 0 ? "" : _b, build = _a.build, getBaseTarget = _a.getBaseTarget, makeTargetAnimatable = _a.makeTargetAnimatable, measureViewportBox = _a.measureViewportBox, renderInstance = _a.render, readValueFromInstance = _a.readValueFromInstance, removeValueFromRenderState = _a.removeValueFromRenderState, sortNodePosition = _a.sortNodePosition, scrapeMotionValuesFromProps = _a.scrapeMotionValuesFromProps;
2458
+ return function (_a, options) {
2459
+ var parent = _a.parent, props = _a.props, presenceId = _a.presenceId, blockInitialAnimation = _a.blockInitialAnimation, visualState = _a.visualState;
2460
+ if (options === void 0) { options = {}; }
2461
+ var isMounted = false;
2462
+ var latestValues = visualState.latestValues, renderState = visualState.renderState;
2463
+ /**
2464
+ * The instance of the render-specific node that will be hydrated by the
2465
+ * exposed React ref. So for example, this visual element can host a
2466
+ * HTMLElement, plain object, or Three.js object. The functions provided
2467
+ * in VisualElementConfig allow us to interface with this instance.
2468
+ */
2469
+ var instance;
2470
+ /**
2471
+ * Manages the subscriptions for a visual element's lifecycle, for instance
2472
+ * onRender
2473
+ */
2474
+ var lifecycles = createLifecycles();
2475
+ /**
2476
+ * A map of all motion values attached to this visual element. Motion
2477
+ * values are source of truth for any given animated value. A motion
2478
+ * value might be provided externally by the component via props.
2479
+ */
2480
+ var values = new Map();
2481
+ /**
2482
+ * A map of every subscription that binds the provided or generated
2483
+ * motion values onChange listeners to this visual element.
2484
+ */
2485
+ var valueSubscriptions = new Map();
2486
+ /**
2487
+ * A reference to the previously-provided motion values as returned
2488
+ * from scrapeMotionValuesFromProps. We use the keys in here to determine
2489
+ * if any motion values need to be removed after props are updated.
2490
+ */
2491
+ var prevMotionValues = {};
2492
+ /**
2493
+ * When values are removed from all animation props we need to search
2494
+ * for a fallback value to animate to. These values are tracked in baseTarget.
2495
+ */
2496
+ var baseTarget = tslib.__assign({}, latestValues);
2497
+ // Internal methods ========================
2498
+ /**
2499
+ * On mount, this will be hydrated with a callback to disconnect
2500
+ * this visual element from its parent on unmount.
2501
+ */
2502
+ var removeFromVariantTree;
2503
+ /**
2504
+ * Render the element with the latest styles outside of the React
2505
+ * render lifecycle
2506
+ */
2507
+ function render() {
2508
+ if (!instance || !isMounted)
2509
+ return;
2510
+ triggerBuild();
2511
+ renderInstance(instance, renderState, props.style, element.projection);
2512
+ }
2513
+ function triggerBuild() {
2514
+ build(element, renderState, latestValues, options, props);
2515
+ }
2516
+ function update() {
2517
+ lifecycles.notifyUpdate(latestValues);
2518
+ }
2519
+ /**
2520
+ *
2521
+ */
2522
+ function bindToMotionValue(key, value) {
2523
+ var removeOnChange = value.onChange(function (latestValue) {
2524
+ latestValues[key] = latestValue;
2525
+ props.onUpdate && sync__default["default"].update(update, false, true);
2526
+ });
2527
+ var removeOnRenderRequest = value.onRenderRequest(element.scheduleRender);
2528
+ valueSubscriptions.set(key, function () {
2529
+ removeOnChange();
2530
+ removeOnRenderRequest();
2531
+ });
2532
+ }
2533
+ /**
2534
+ * Any motion values that are provided to the element when created
2535
+ * aren't yet bound to the element, as this would technically be impure.
2536
+ * However, we iterate through the motion values and set them to the
2537
+ * initial values for this component.
2538
+ *
2539
+ * TODO: This is impure and we should look at changing this to run on mount.
2540
+ * Doing so will break some tests but this isn't neccessarily a breaking change,
2541
+ * more a reflection of the test.
2542
+ */
2543
+ var initialMotionValues = scrapeMotionValuesFromProps(props);
2544
+ for (var key in initialMotionValues) {
2545
+ var value = initialMotionValues[key];
2546
+ if (latestValues[key] !== undefined && isMotionValue(value)) {
2547
+ value.set(latestValues[key], false);
2548
+ }
2549
+ }
2550
+ /**
2551
+ * Determine what role this visual element should take in the variant tree.
2552
+ */
2553
+ var isControllingVariants = checkIfControllingVariants(props);
2554
+ var isVariantNode = checkIfVariantNode(props);
2555
+ var element = tslib.__assign(tslib.__assign({ treeType: treeType,
2556
+ /**
2557
+ * This is a mirror of the internal instance prop, which keeps
2558
+ * VisualElement type-compatible with React's RefObject.
2559
+ */
2560
+ current: null,
2561
+ /**
2562
+ * The depth of this visual element within the visual element tree.
2563
+ */
2564
+ depth: parent ? parent.depth + 1 : 0, parent: parent, children: new Set(),
2565
+ /**
2566
+ *
2567
+ */
2568
+ presenceId: presenceId,
2569
+ /**
2570
+ * If this component is part of the variant tree, it should track
2571
+ * any children that are also part of the tree. This is essentially
2572
+ * a shadow tree to simplify logic around how to stagger over children.
2573
+ */
2574
+ variantChildren: isVariantNode ? new Set() : undefined,
2575
+ /**
2576
+ * Whether this instance is visible. This can be changed imperatively
2577
+ * by the projection tree, is analogous to CSS's visibility in that
2578
+ * hidden elements should take up layout, and needs enacting by the configured
2579
+ * render function.
2580
+ */
2581
+ isVisible: undefined,
2582
+ /**
2583
+ * Normally, if a component is controlled by a parent's variants, it can
2584
+ * rely on that ancestor to trigger animations further down the tree.
2585
+ * However, if a component is created after its parent is mounted, the parent
2586
+ * won't trigger that mount animation so the child needs to.
2587
+ *
2588
+ * TODO: This might be better replaced with a method isParentMounted
2589
+ */
2590
+ manuallyAnimateOnMount: Boolean(parent === null || parent === void 0 ? void 0 : parent.isMounted()),
2591
+ /**
2592
+ * This can be set by AnimatePresence to force components that mount
2593
+ * at the same time as it to mount as if they have initial={false} set.
2594
+ */
2595
+ blockInitialAnimation: blockInitialAnimation,
2596
+ /**
2597
+ * Determine whether this component has mounted yet. This is mostly used
2598
+ * by variant children to determine whether they need to trigger their
2599
+ * own animations on mount.
2600
+ */
2601
+ isMounted: function () { return Boolean(instance); }, mount: function (newInstance) {
2602
+ isMounted = true;
2603
+ instance = element.current = newInstance;
2604
+ if (element.projection) {
2605
+ element.projection.mount(newInstance);
2606
+ }
2607
+ if (isVariantNode && parent && !isControllingVariants) {
2608
+ removeFromVariantTree = parent === null || parent === void 0 ? void 0 : parent.addVariantChild(element);
2609
+ }
2610
+ parent === null || parent === void 0 ? void 0 : parent.children.add(element);
2611
+ element.setProps(props);
2612
+ },
2613
+ /**
2614
+ *
2615
+ */
2616
+ unmount: function () {
2617
+ var _a;
2618
+ (_a = element.projection) === null || _a === void 0 ? void 0 : _a.unmount();
2619
+ sync.cancelSync.update(update);
2620
+ sync.cancelSync.render(render);
2621
+ valueSubscriptions.forEach(function (remove) { return remove(); });
2622
+ removeFromVariantTree === null || removeFromVariantTree === void 0 ? void 0 : removeFromVariantTree();
2623
+ parent === null || parent === void 0 ? void 0 : parent.children.delete(element);
2624
+ lifecycles.clearAllListeners();
2625
+ instance = undefined;
2626
+ isMounted = false;
2627
+ },
2628
+ /**
2629
+ * Add a child visual element to our set of children.
2630
+ */
2631
+ addVariantChild: function (child) {
2632
+ var _a;
2633
+ var closestVariantNode = element.getClosestVariantNode();
2634
+ if (closestVariantNode) {
2635
+ (_a = closestVariantNode.variantChildren) === null || _a === void 0 ? void 0 : _a.add(child);
2636
+ return function () {
2637
+ return closestVariantNode.variantChildren.delete(child);
2638
+ };
2639
+ }
2640
+ }, sortNodePosition: function (other) {
2641
+ /**
2642
+ * If these nodes aren't even of the same type we can't compare their depth.
2643
+ */
2644
+ if (!sortNodePosition || treeType !== other.treeType)
2645
+ return 0;
2646
+ return sortNodePosition(element.getInstance(), other.getInstance());
2647
+ },
2648
+ /**
2649
+ * Returns the closest variant node in the tree starting from
2650
+ * this visual element.
2651
+ */
2652
+ getClosestVariantNode: function () {
2653
+ return isVariantNode ? element : parent === null || parent === void 0 ? void 0 : parent.getClosestVariantNode();
2654
+ },
2655
+ /**
2656
+ * Expose the latest layoutId prop.
2657
+ */
2658
+ getLayoutId: function () { return props.layoutId; },
2659
+ /**
2660
+ * Returns the current instance.
2661
+ */
2662
+ getInstance: function () { return instance; },
2663
+ /**
2664
+ * Get/set the latest static values.
2665
+ */
2666
+ getStaticValue: function (key) { return latestValues[key]; }, setStaticValue: function (key, value) { return (latestValues[key] = value); },
2667
+ /**
2668
+ * Returns the latest motion value state. Currently only used to take
2669
+ * a snapshot of the visual element - perhaps this can return the whole
2670
+ * visual state
2671
+ */
2672
+ getLatestValues: function () { return latestValues; },
2673
+ /**
2674
+ * Set the visiblity of the visual element. If it's changed, schedule
2675
+ * a render to reflect these changes.
2676
+ */
2677
+ setVisibility: function (visibility) {
2678
+ if (element.isVisible === visibility)
2679
+ return;
2680
+ element.isVisible = visibility;
2681
+ element.scheduleRender();
2682
+ },
2683
+ /**
2684
+ * Make a target animatable by Popmotion. For instance, if we're
2685
+ * trying to animate width from 100px to 100vw we need to measure 100vw
2686
+ * in pixels to determine what we really need to animate to. This is also
2687
+ * pluggable to support Framer's custom value types like Color,
2688
+ * and CSS variables.
2689
+ */
2690
+ makeTargetAnimatable: function (target, canMutate) {
2691
+ if (canMutate === void 0) { canMutate = true; }
2692
+ return makeTargetAnimatable(element, target, props, canMutate);
2693
+ },
2694
+ /**
2695
+ * Measure the current viewport box with or without transforms.
2696
+ * Only measures axis-aligned boxes, rotate and skew must be manually
2697
+ * removed with a re-render to work.
2698
+ */
2699
+ measureViewportBox: function () {
2700
+ return measureViewportBox(instance, props);
2701
+ },
2702
+ // Motion values ========================
2703
+ /**
2704
+ * Add a motion value and bind it to this visual element.
2705
+ */
2706
+ addValue: function (key, value) {
2707
+ // Remove existing value if it exists
2708
+ if (element.hasValue(key))
2709
+ element.removeValue(key);
2710
+ values.set(key, value);
2711
+ latestValues[key] = value.get();
2712
+ bindToMotionValue(key, value);
2713
+ },
2714
+ /**
2715
+ * Remove a motion value and unbind any active subscriptions.
2716
+ */
2717
+ removeValue: function (key) {
2718
+ var _a;
2719
+ values.delete(key);
2720
+ (_a = valueSubscriptions.get(key)) === null || _a === void 0 ? void 0 : _a();
2721
+ valueSubscriptions.delete(key);
2722
+ delete latestValues[key];
2723
+ removeValueFromRenderState(key, renderState);
2724
+ },
2725
+ /**
2726
+ * Check whether we have a motion value for this key
2727
+ */
2728
+ hasValue: function (key) { return values.has(key); },
2729
+ /**
2730
+ * Get a motion value for this key. If called with a default
2731
+ * value, we'll create one if none exists.
2732
+ */
2733
+ getValue: function (key, defaultValue) {
2734
+ var value = values.get(key);
2735
+ if (value === undefined && defaultValue !== undefined) {
2736
+ value = motionValue(defaultValue);
2737
+ element.addValue(key, value);
2738
+ }
2739
+ return value;
2740
+ },
2741
+ /**
2742
+ * Iterate over our motion values.
2743
+ */
2744
+ forEachValue: function (callback) { return values.forEach(callback); },
2745
+ /**
2746
+ * If we're trying to animate to a previously unencountered value,
2747
+ * we need to check for it in our state and as a last resort read it
2748
+ * directly from the instance (which might have performance implications).
2749
+ */
2750
+ readValue: function (key) {
2751
+ var _a;
2752
+ return (_a = latestValues[key]) !== null && _a !== void 0 ? _a : readValueFromInstance(instance, key, options);
2753
+ },
2754
+ /**
2755
+ * Set the base target to later animate back to. This is currently
2756
+ * only hydrated on creation and when we first read a value.
2757
+ */
2758
+ setBaseTarget: function (key, value) {
2759
+ baseTarget[key] = value;
2760
+ },
2761
+ /**
2762
+ * Find the base target for a value thats been removed from all animation
2763
+ * props.
2764
+ */
2765
+ getBaseTarget: function (key) {
2766
+ if (getBaseTarget) {
2767
+ var target = getBaseTarget(props, key);
2768
+ if (target !== undefined && !isMotionValue(target))
2769
+ return target;
2770
+ }
2771
+ return baseTarget[key];
2772
+ } }, lifecycles), {
2773
+ /**
2774
+ * Build the renderer state based on the latest visual state.
2775
+ */
2776
+ build: function () {
2777
+ triggerBuild();
2778
+ return renderState;
2779
+ },
2780
+ /**
2781
+ * Schedule a render on the next animation frame.
2782
+ */
2783
+ scheduleRender: function () {
2784
+ sync__default["default"].render(render, false, true);
2785
+ },
2786
+ /**
2787
+ * Synchronously fire render. It's prefered that we batch renders but
2788
+ * in many circumstances, like layout measurement, we need to run this
2789
+ * synchronously. However in those instances other measures should be taken
2790
+ * to batch reads/writes.
2791
+ */
2792
+ syncRender: render,
2793
+ /**
2794
+ * Update the provided props. Ensure any newly-added motion values are
2795
+ * added to our map, old ones removed, and listeners updated.
2796
+ */
2797
+ setProps: function (newProps) {
2798
+ props = newProps;
2799
+ lifecycles.updatePropListeners(newProps);
2800
+ prevMotionValues = updateMotionValuesFromProps(element, scrapeMotionValuesFromProps(props), prevMotionValues);
2801
+ }, getProps: function () { return props; },
2802
+ // Variants ==============================
2803
+ /**
2804
+ * Returns the variant definition with a given name.
2805
+ */
2806
+ getVariant: function (name) { var _a; return (_a = props.variants) === null || _a === void 0 ? void 0 : _a[name]; },
2807
+ /**
2808
+ * Returns the defined default transition on this component.
2809
+ */
2810
+ getDefaultTransition: function () { return props.transition; }, getTransformPagePoint: function () {
2811
+ return props.transformPagePoint;
2812
+ },
2813
+ /**
2814
+ * Used by child variant nodes to get the closest ancestor variant props.
2815
+ */
2816
+ getVariantContext: function (startAtParent) {
2817
+ if (startAtParent === void 0) { startAtParent = false; }
2818
+ if (startAtParent)
2819
+ return parent === null || parent === void 0 ? void 0 : parent.getVariantContext();
2820
+ if (!isControllingVariants) {
2821
+ var context_1 = (parent === null || parent === void 0 ? void 0 : parent.getVariantContext()) || {};
2822
+ if (props.initial !== undefined) {
2823
+ context_1.initial = props.initial;
2824
+ }
2825
+ return context_1;
2826
+ }
2827
+ var context = {};
2828
+ for (var i = 0; i < numVariantProps; i++) {
2829
+ var name_1 = variantProps[i];
2830
+ var prop = props[name_1];
2831
+ if (isVariantLabel(prop) || prop === false) {
2832
+ context[name_1] = prop;
2833
+ }
2834
+ }
2835
+ return context;
2836
+ } });
2837
+ return element;
2838
+ };
2839
+ };
2840
+ var variantProps = tslib.__spreadArray(["initial"], tslib.__read(variantPriorityOrder), false);
2841
+ var numVariantProps = variantProps.length;
2842
+
2843
+ var setVector = function (name, defaultValue) {
2844
+ return function (i) {
2845
+ return function (instance, value) {
2846
+ var _a;
2847
+ (_a = instance[name]) !== null && _a !== void 0 ? _a : (instance[name] = new three.Vector3(defaultValue));
2848
+ var vector = instance[name];
2849
+ vector.setComponent(i, value);
2850
+ };
2851
+ };
2852
+ };
2853
+ var setEuler = function (name, defaultValue) {
2854
+ return function (axis) {
2855
+ return function (instance, value) {
2856
+ var _a;
2857
+ (_a = instance[name]) !== null && _a !== void 0 ? _a : (instance[name] = new three.Euler(defaultValue));
2858
+ var euler = instance[name];
2859
+ euler[axis] = value;
2860
+ };
2861
+ };
2862
+ };
2863
+ var setColor = function (name) { return function (instance, value) {
2864
+ var _a;
2865
+ (_a = instance[name]) !== null && _a !== void 0 ? _a : (instance[name] = new three.Color(value));
2866
+ instance[name].set(value);
2867
+ }; };
2868
+ var setScale = setVector("scale", 1);
2869
+ var setPosition = setVector("position", 0);
2870
+ var setRotation = setEuler("rotation", 0);
2871
+ var setters = {
2872
+ x: setPosition(0),
2873
+ y: setPosition(1),
2874
+ z: setPosition(2),
2875
+ scale: function (instance, value) {
2876
+ var _a;
2877
+ (_a = instance.scale) !== null && _a !== void 0 ? _a : (instance.scale = new three.Vector3(1));
2878
+ var scale = instance.scale;
2879
+ scale.set(value, value, value);
2880
+ },
2881
+ scaleX: setScale(0),
2882
+ scaleY: setScale(1),
2883
+ scaleZ: setScale(2),
2884
+ rotateX: setRotation("x"),
2885
+ rotateY: setRotation("y"),
2886
+ rotateZ: setRotation("z"),
2887
+ color: setColor("color"),
2888
+ specular: setColor("specular"),
2889
+ };
2890
+ function setThreeValue(instance, key, values) {
2891
+ if (setters[key]) {
2892
+ setters[key](instance, values[key]);
2893
+ }
2894
+ else {
2895
+ instance[key] = values[key];
2896
+ }
2897
+ }
2898
+
2899
+ var readVector = function (name, defaultValue) {
2900
+ return function (axis) {
2901
+ return function (instance) {
2902
+ var value = instance[name];
2903
+ return value ? value[axis] : defaultValue;
2904
+ };
2905
+ };
2906
+ };
2907
+ var readPosition = readVector("position", 0);
2908
+ var readScale = readVector("scale", 1);
2909
+ var readRotation = readVector("rotation", 0);
2910
+ var readers = {
2911
+ x: readPosition("x"),
2912
+ y: readPosition("y"),
2913
+ z: readPosition("z"),
2914
+ scale: readScale("x"),
2915
+ scaleX: readScale("x"),
2916
+ scaleY: readScale("y"),
2917
+ scaleZ: readScale("z"),
2918
+ rotateX: readRotation("x"),
2919
+ rotateY: readRotation("y"),
2920
+ rotateZ: readRotation("z"),
2921
+ };
2922
+ function readAnimatableValue(value) {
2923
+ if (!value)
2924
+ return;
2925
+ if (value instanceof three.Color) {
2926
+ return value.getStyle();
2927
+ }
2928
+ else {
2929
+ return value;
2930
+ }
2931
+ }
2932
+ function readThreeValue(instance, name) {
2933
+ var _a;
2934
+ return readers[name]
2935
+ ? readers[name](instance)
2936
+ : (_a = readAnimatableValue(instance[name])) !== null && _a !== void 0 ? _a : 0;
2937
+ }
2938
+
2939
+ var scrapeMotionValuesFromProps = function () { return ({}); };
2940
+ var createRenderState = function () { return ({}); };
2941
+ var threeVisualElement = visualElement({
2942
+ treeType: "three",
2943
+ readValueFromInstance: readThreeValue,
2944
+ getBaseTarget: function (_props, _key) {
2945
+ return 0;
2946
+ },
2947
+ sortNodePosition: function (_a, _b) {
2948
+ return 0;
2949
+ },
2950
+ makeTargetAnimatable: function (element, target) {
2951
+ checkTargetForNewValues(element, target, {});
2952
+ return target;
2953
+ },
2954
+ restoreTransform: function () { },
2955
+ resetTransform: function () { },
2956
+ removeValueFromRenderState: function (_key, _renderState) { },
2957
+ measureViewportBox: createBox,
2958
+ scrapeMotionValuesFromProps: scrapeMotionValuesFromProps,
2959
+ build: function (_element, state, latestValues) {
2960
+ for (var key in latestValues) {
2961
+ state[key] = latestValues[key];
2962
+ }
2963
+ },
2964
+ render: function (instance, renderState) {
2965
+ for (var key in renderState) {
2966
+ setThreeValue(instance, key, renderState);
2967
+ }
2968
+ },
2969
+ });
2970
+ var createVisualElement = function (_, options) {
2971
+ return threeVisualElement(options);
2972
+ };
2973
+
2974
+ var useVisualState = makeUseVisualState({
2975
+ scrapeMotionValuesFromProps: scrapeMotionValuesFromProps,
2976
+ createRenderState: createRenderState,
2977
+ });
2978
+ var preloadedFeatures = tslib.__assign({}, animations);
2979
+ function custom(Component) {
2980
+ return createMotionComponent({
2981
+ Component: Component,
2982
+ preloadedFeatures: preloadedFeatures,
2983
+ useRender: useRender,
2984
+ useVisualState: useVisualState,
2985
+ createVisualElement: createVisualElement,
2986
+ });
2987
+ }
2988
+ var componentCache = new Map();
2989
+ var motion = new Proxy(custom, {
2990
+ get: function (_, key) {
2991
+ !componentCache.has(key) && componentCache.set(key, custom(key));
2992
+ return componentCache.get(key);
2993
+ },
2994
+ });
2995
+
2996
+ function MotionCanvas(_a) {
2997
+ var children = _a.children, props = tslib.__rest(_a, ["children"]);
2998
+ var motionContext = React.useContext(MotionContext);
2999
+ return (React__namespace.createElement(fiber.Canvas, tslib.__assign({}, props),
3000
+ React__namespace.createElement(MotionContext.Provider, { value: motionContext }, children)));
3001
+ }
3002
+
3003
+ exports.MotionCanvas = MotionCanvas;
3004
+ exports.motion = motion;