framer-motion 5.3.3 → 5.4.0-beta

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