framer-motion 7.6.4 → 7.6.6

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/cjs/index.js +789 -850
  2. package/dist/es/animation/use-animated-state.mjs +29 -17
  3. package/dist/es/gestures/drag/VisualElementDragControls.mjs +14 -10
  4. package/dist/es/gestures/use-focus-gesture.mjs +1 -1
  5. package/dist/es/gestures/use-tap-gesture.mjs +1 -1
  6. package/dist/es/index.mjs +1 -1
  7. package/dist/es/motion/features/viewport/use-viewport.mjs +2 -2
  8. package/dist/es/motion/utils/use-visual-element.mjs +3 -3
  9. package/dist/es/projection/node/create-projection-node.mjs +74 -60
  10. package/dist/es/render/VisualElement.mjs +480 -0
  11. package/dist/es/render/dom/DOMVisualElement.mjs +49 -0
  12. package/dist/es/render/dom/create-visual-element.mjs +4 -4
  13. package/dist/es/render/dom/utils/css-variables-conversion.mjs +2 -2
  14. package/dist/es/render/dom/utils/unit-conversion.mjs +4 -4
  15. package/dist/es/render/html/HTMLVisualElement.mjs +41 -0
  16. package/dist/es/render/svg/{visual-element.mjs → SVGVisualElement.mjs} +21 -15
  17. package/dist/es/render/utils/animation.mjs +4 -4
  18. package/dist/es/render/utils/motion-values.mjs +1 -1
  19. package/dist/es/render/utils/resolve-dynamic-variants.mjs +2 -2
  20. package/dist/es/render/utils/setters.mjs +2 -1
  21. package/dist/es/value/index.mjs +1 -1
  22. package/dist/framer-motion.dev.js +789 -850
  23. package/dist/framer-motion.js +1 -1
  24. package/dist/index.d.ts +2101 -1931
  25. package/dist/projection.dev.js +1053 -1136
  26. package/dist/size-rollup-dom-animation-m.js +1 -1
  27. package/dist/size-rollup-dom-animation.js +1 -1
  28. package/dist/size-rollup-dom-max-assets.js +1 -1
  29. package/dist/size-rollup-dom-max.js +1 -1
  30. package/dist/size-rollup-m.js +1 -1
  31. package/dist/size-rollup-motion.js +1 -1
  32. package/dist/size-webpack-dom-animation.js +1 -1
  33. package/dist/size-webpack-dom-max.js +1 -1
  34. package/dist/size-webpack-m.js +1 -1
  35. package/dist/three-entry.d.ts +1956 -1745
  36. package/package.json +8 -8
  37. package/dist/es/render/html/visual-element.mjs +0 -109
  38. package/dist/es/render/index.mjs +0 -515
  39. package/dist/es/render/utils/lifecycles.mjs +0 -43
@@ -57,7 +57,7 @@
57
57
  const lazyContext = React.useContext(LazyContext);
58
58
  const presenceContext = React.useContext(PresenceContext);
59
59
  const reducedMotionConfig = React.useContext(MotionConfigContext).reducedMotion;
60
- const visualElementRef = React.useRef(undefined);
60
+ const visualElementRef = React.useRef();
61
61
  /**
62
62
  * If we haven't preloaded a renderer, check to see if we have one lazy-loaded
63
63
  */
@@ -76,14 +76,14 @@
76
76
  }
77
77
  const visualElement = visualElementRef.current;
78
78
  useIsomorphicLayoutEffect(() => {
79
- visualElement && visualElement.syncRender();
79
+ visualElement && visualElement.render();
80
80
  });
81
81
  React.useEffect(() => {
82
82
  if (visualElement && visualElement.animationState) {
83
83
  visualElement.animationState.animateChanges();
84
84
  }
85
85
  });
86
- useIsomorphicLayoutEffect(() => () => visualElement && visualElement.notifyUnmount(), []);
86
+ useIsomorphicLayoutEffect(() => () => visualElement && visualElement.notify("Unmount"), []);
87
87
  return visualElement;
88
88
  }
89
89
 
@@ -1489,7 +1489,7 @@
1489
1489
  * @param ref
1490
1490
  * @internal
1491
1491
  */
1492
- function useFocusGesture({ whileFocus, visualElement }) {
1492
+ function useFocusGesture({ whileFocus, visualElement, }) {
1493
1493
  const { animationState } = visualElement;
1494
1494
  const onFocus = () => {
1495
1495
  animationState && animationState.setActive(exports.AnimationType.Focus, true);
@@ -2698,7 +2698,7 @@
2698
2698
  * We only count this as a tap gesture if the event.target is the same
2699
2699
  * as, or a child of, this component's element
2700
2700
  */
2701
- !isNodeOrChild(visualElement.getInstance(), event.target)
2701
+ !isNodeOrChild(visualElement.current, event.target)
2702
2702
  ? onTapCancel && onTapCancel(event, info)
2703
2703
  : onTap && onTap(event, info);
2704
2704
  }
@@ -2809,7 +2809,7 @@
2809
2809
  };
2810
2810
  function useIntersectionObserver(shouldObserve, state, visualElement, { root, margin: rootMargin, amount = "some", once }) {
2811
2811
  React.useEffect(() => {
2812
- if (!shouldObserve)
2812
+ if (!shouldObserve || !visualElement.current)
2813
2813
  return;
2814
2814
  const options = {
2815
2815
  root: root === null || root === void 0 ? void 0 : root.current,
@@ -2847,7 +2847,7 @@
2847
2847
  : props.onViewportLeave;
2848
2848
  callback && callback(entry);
2849
2849
  };
2850
- return observeIntersection(visualElement.getInstance(), options, intersectionCallback);
2850
+ return observeIntersection(visualElement.current, options, intersectionCallback);
2851
2851
  }, [shouldObserve, root, rootMargin, amount]);
2852
2852
  }
2853
2853
  /**
@@ -3426,7 +3426,7 @@
3426
3426
  * This will be replaced by the build step with the latest version number.
3427
3427
  * When MotionValues are provided to motion components, warn if versions are mixed.
3428
3428
  */
3429
- this.version = "7.6.4";
3429
+ this.version = "7.6.6";
3430
3430
  /**
3431
3431
  * Duration, in milliseconds, since last updating frame.
3432
3432
  *
@@ -3740,7 +3740,7 @@
3740
3740
  */
3741
3741
  function getCurrent(visualElement) {
3742
3742
  const current = {};
3743
- visualElement.forEachValue((value, key) => (current[key] = value.get()));
3743
+ visualElement.values.forEach((value, key) => (current[key] = value.get()));
3744
3744
  return current;
3745
3745
  }
3746
3746
  /**
@@ -3748,7 +3748,7 @@
3748
3748
  */
3749
3749
  function getVelocity$1(visualElement) {
3750
3750
  const velocity = {};
3751
- visualElement.forEachValue((value, key) => (velocity[key] = value.getVelocity()));
3751
+ visualElement.values.forEach((value, key) => (velocity[key] = value.getVelocity()));
3752
3752
  return velocity;
3753
3753
  }
3754
3754
  function resolveVariant(visualElement, definition, custom) {
@@ -3842,7 +3842,8 @@
3842
3842
  if (origin[key] === undefined) {
3843
3843
  origin[key] = value;
3844
3844
  }
3845
- visualElement.setBaseTarget(key, value);
3845
+ if (value !== null)
3846
+ visualElement.setBaseTarget(key, value);
3846
3847
  }
3847
3848
  }
3848
3849
  function getOriginFromTransition(key, transition) {
@@ -3869,7 +3870,7 @@
3869
3870
  }
3870
3871
 
3871
3872
  function animateVisualElement(visualElement, definition, options = {}) {
3872
- visualElement.notifyAnimationStart(definition);
3873
+ visualElement.notify("AnimationStart", definition);
3873
3874
  let animation;
3874
3875
  if (Array.isArray(definition)) {
3875
3876
  const animations = definition.map((variant) => animateVariant(visualElement, variant, options));
@@ -3884,7 +3885,7 @@
3884
3885
  : definition;
3885
3886
  animation = animateTarget(visualElement, resolvedDefinition, options);
3886
3887
  }
3887
- return animation.then(() => visualElement.notifyAnimationComplete(definition));
3888
+ return animation.then(() => visualElement.notify("AnimationComplete", definition));
3888
3889
  }
3889
3890
  function animateVariant(visualElement, variant, options = {}) {
3890
3891
  var _a;
@@ -3979,12 +3980,12 @@
3979
3980
  animations.push(animateVariant(child, variant, {
3980
3981
  ...options,
3981
3982
  delay: delayChildren + generateStaggerDuration(i),
3982
- }).then(() => child.notifyAnimationComplete(variant)));
3983
+ }).then(() => child.notify("AnimationComplete", variant)));
3983
3984
  });
3984
3985
  return Promise.all(animations);
3985
3986
  }
3986
3987
  function stopAnimation(visualElement) {
3987
- visualElement.forEachValue((value) => value.stop());
3988
+ visualElement.values.forEach((value) => value.stop());
3988
3989
  }
3989
3990
  function sortByTreeOrder(a, b) {
3990
3991
  return a.sortNodePosition(b);
@@ -4905,7 +4906,7 @@
4905
4906
  * If the MotionValue is a percentage value convert to px
4906
4907
  */
4907
4908
  if (percent.test(current)) {
4908
- const measuredAxis = (_b = (_a = this.visualElement.projection) === null || _a === void 0 ? void 0 : _a.layout) === null || _b === void 0 ? void 0 : _b.actual[axis];
4909
+ const measuredAxis = (_b = (_a = this.visualElement.projection) === null || _a === void 0 ? void 0 : _a.layout) === null || _b === void 0 ? void 0 : _b.layoutBox[axis];
4909
4910
  if (measuredAxis) {
4910
4911
  const length = calcLength(measuredAxis);
4911
4912
  current = length * (parseFloat(current) / 100);
@@ -4942,9 +4943,9 @@
4942
4943
  * of a re-render we want to ensure the browser can read the latest
4943
4944
  * bounding box to ensure the pointer and element don't fall out of sync.
4944
4945
  */
4945
- this.visualElement.syncRender();
4946
+ this.visualElement.render();
4946
4947
  /**
4947
- * This must fire after the syncRender call as it might trigger a state
4948
+ * This must fire after the render call as it might trigger a state
4948
4949
  * change which itself might trigger a layout update.
4949
4950
  */
4950
4951
  onDrag === null || onDrag === void 0 ? void 0 : onDrag(event, info);
@@ -5006,7 +5007,7 @@
5006
5007
  }
5007
5008
  else {
5008
5009
  if (dragConstraints && layout) {
5009
- this.constraints = calcRelativeConstraints(layout.actual, dragConstraints);
5010
+ this.constraints = calcRelativeConstraints(layout.layoutBox, dragConstraints);
5010
5011
  }
5011
5012
  else {
5012
5013
  this.constraints = false;
@@ -5023,7 +5024,7 @@
5023
5024
  !this.hasMutatedConstraints) {
5024
5025
  eachAxis((axis) => {
5025
5026
  if (this.getAxisMotionValue(axis)) {
5026
- this.constraints[axis] = rebaseAxisConstraints(layout.actual[axis], this.constraints[axis]);
5027
+ this.constraints[axis] = rebaseAxisConstraints(layout.layoutBox[axis], this.constraints[axis]);
5027
5028
  }
5028
5029
  });
5029
5030
  }
@@ -5039,7 +5040,7 @@
5039
5040
  if (!projection || !projection.layout)
5040
5041
  return false;
5041
5042
  const constraintsBox = measurePageBox(constraintsElement, projection.root, this.visualElement.getTransformPagePoint());
5042
- let measuredConstraints = calcViewportConstraints(projection.layout.actual, constraintsBox);
5043
+ let measuredConstraints = calcViewportConstraints(projection.layout.layoutBox, constraintsBox);
5043
5044
  /**
5044
5045
  * If there's an onMeasureDragConstraints listener we call it and
5045
5046
  * if different constraints are returned, set constraints to that
@@ -5121,7 +5122,7 @@
5121
5122
  const { projection } = this.visualElement;
5122
5123
  const axisValue = this.getAxisMotionValue(axis);
5123
5124
  if (projection && projection.layout) {
5124
- const { min, max } = projection.layout.actual[axis];
5125
+ const { min, max } = projection.layout.layoutBox[axis];
5125
5126
  axisValue.set(point[axis] - mix$1(min, max, 0.5));
5126
5127
  }
5127
5128
  });
@@ -5133,6 +5134,8 @@
5133
5134
  */
5134
5135
  scalePositionWithinConstraints() {
5135
5136
  var _a;
5137
+ if (!this.visualElement.current)
5138
+ return;
5136
5139
  const { drag, dragConstraints } = this.getProps();
5137
5140
  const { projection } = this.visualElement;
5138
5141
  if (!isRefObject(dragConstraints) || !projection || !this.constraints)
@@ -5158,7 +5161,7 @@
5158
5161
  * Update the layout of this element and resolve the latest drag constraints
5159
5162
  */
5160
5163
  const { transformTemplate } = this.visualElement.getProps();
5161
- this.visualElement.getInstance().style.transform = transformTemplate
5164
+ this.visualElement.current.style.transform = transformTemplate
5162
5165
  ? transformTemplate({}, "")
5163
5166
  : "none";
5164
5167
  (_a = projection.root) === null || _a === void 0 ? void 0 : _a.updateScroll();
@@ -5181,8 +5184,10 @@
5181
5184
  }
5182
5185
  addListeners() {
5183
5186
  var _a;
5187
+ if (!this.visualElement.current)
5188
+ return;
5184
5189
  elementDragControls.set(this.visualElement, this);
5185
- const element = this.visualElement.getInstance();
5190
+ const element = this.visualElement.current;
5186
5191
  /**
5187
5192
  * Attach a pointerdown event listener on this DOM element to initiate drag tracking.
5188
5193
  */
@@ -5221,7 +5226,7 @@
5221
5226
  this.originPoint[axis] += delta[axis].translate;
5222
5227
  motionValue.set(motionValue.get() + delta[axis].translate);
5223
5228
  });
5224
- this.visualElement.syncRender();
5229
+ this.visualElement.render();
5225
5230
  }
5226
5231
  }));
5227
5232
  return () => {
@@ -5326,618 +5331,6 @@
5326
5331
  drag: makeRenderlessComponent(useDrag),
5327
5332
  };
5328
5333
 
5329
- // Does this device prefer reduced motion? Returns `null` server-side.
5330
- const prefersReducedMotion = { current: null };
5331
- const hasReducedMotionListener = { current: false };
5332
-
5333
- function initPrefersReducedMotion() {
5334
- hasReducedMotionListener.current = true;
5335
- if (!isBrowser)
5336
- return;
5337
- if (window.matchMedia) {
5338
- const motionMediaQuery = window.matchMedia("(prefers-reduced-motion)");
5339
- const setReducedMotionPreferences = () => (prefersReducedMotion.current = motionMediaQuery.matches);
5340
- motionMediaQuery.addListener(setReducedMotionPreferences);
5341
- setReducedMotionPreferences();
5342
- }
5343
- else {
5344
- prefersReducedMotion.current = false;
5345
- }
5346
- }
5347
-
5348
- const names = [
5349
- "LayoutMeasure",
5350
- "BeforeLayoutMeasure",
5351
- "LayoutUpdate",
5352
- "ViewportBoxUpdate",
5353
- "Update",
5354
- "Render",
5355
- "AnimationComplete",
5356
- "LayoutAnimationComplete",
5357
- "AnimationStart",
5358
- "LayoutAnimationStart",
5359
- "SetAxisTarget",
5360
- "Unmount",
5361
- ];
5362
- function createLifecycles() {
5363
- const managers = names.map(() => new SubscriptionManager());
5364
- const propSubscriptions = {};
5365
- const lifecycles = {
5366
- clearAllListeners: () => managers.forEach((manager) => manager.clear()),
5367
- updatePropListeners: (props) => {
5368
- names.forEach((name) => {
5369
- var _a;
5370
- const on = "on" + name;
5371
- const propListener = props[on];
5372
- // Unsubscribe existing subscription
5373
- (_a = propSubscriptions[name]) === null || _a === void 0 ? void 0 : _a.call(propSubscriptions);
5374
- // Add new subscription
5375
- if (propListener) {
5376
- propSubscriptions[name] = lifecycles[on](propListener);
5377
- }
5378
- });
5379
- },
5380
- };
5381
- managers.forEach((manager, i) => {
5382
- lifecycles["on" + names[i]] = (handler) => manager.add(handler);
5383
- lifecycles["notify" + names[i]] = (...args) => manager.notify(...args);
5384
- });
5385
- return lifecycles;
5386
- }
5387
-
5388
- function updateMotionValuesFromProps(element, next, prev) {
5389
- const { willChange } = next;
5390
- for (const key in next) {
5391
- const nextValue = next[key];
5392
- const prevValue = prev[key];
5393
- if (isMotionValue(nextValue)) {
5394
- /**
5395
- * If this is a motion value found in props or style, we want to add it
5396
- * to our visual element's motion value map.
5397
- */
5398
- element.addValue(key, nextValue);
5399
- if (isWillChangeMotionValue(willChange)) {
5400
- willChange.add(key);
5401
- }
5402
- /**
5403
- * Check the version of the incoming motion value with this version
5404
- * and warn against mismatches.
5405
- */
5406
- {
5407
- warnOnce(nextValue.version === "7.6.4", `Attempting to mix Framer Motion versions ${nextValue.version} with 7.6.4 may not work as expected.`);
5408
- }
5409
- }
5410
- else if (isMotionValue(prevValue)) {
5411
- /**
5412
- * If we're swapping from a motion value to a static value,
5413
- * create a new motion value from that
5414
- */
5415
- element.addValue(key, motionValue(nextValue));
5416
- if (isWillChangeMotionValue(willChange)) {
5417
- willChange.remove(key);
5418
- }
5419
- }
5420
- else if (prevValue !== nextValue) {
5421
- /**
5422
- * If this is a flat value that has changed, update the motion value
5423
- * or create one if it doesn't exist. We only want to do this if we're
5424
- * not handling the value with our animation state.
5425
- */
5426
- if (element.hasValue(key)) {
5427
- const existingValue = element.getValue(key);
5428
- // TODO: Only update values that aren't being animated or even looked at
5429
- !existingValue.hasAnimated && existingValue.set(nextValue);
5430
- }
5431
- else {
5432
- const latestValue = element.getStaticValue(key);
5433
- element.addValue(key, motionValue(latestValue !== undefined ? latestValue : nextValue));
5434
- }
5435
- }
5436
- }
5437
- // Handle removed values
5438
- for (const key in prev) {
5439
- if (next[key] === undefined)
5440
- element.removeValue(key);
5441
- }
5442
- return next;
5443
- }
5444
-
5445
- const featureNames = Object.keys(featureDefinitions);
5446
- const numFeatures = featureNames.length;
5447
- const visualElement = ({ treeType = "", build, getBaseTarget, makeTargetAnimatable, measureViewportBox, render: renderInstance, readValueFromInstance, removeValueFromRenderState, sortNodePosition, scrapeMotionValuesFromProps, }) => ({ parent, props, presenceId, blockInitialAnimation, visualState, reducedMotionConfig, }, options = {}) => {
5448
- let isMounted = false;
5449
- const { latestValues, renderState } = visualState;
5450
- /**
5451
- * The instance of the render-specific node that will be hydrated by the
5452
- * exposed React ref. So for example, this visual element can host a
5453
- * HTMLElement, plain object, or Three.js object. The functions provided
5454
- * in VisualElementConfig allow us to interface with this instance.
5455
- */
5456
- let instance;
5457
- /**
5458
- * Manages the subscriptions for a visual element's lifecycle, for instance
5459
- * onRender
5460
- */
5461
- const lifecycles = createLifecycles();
5462
- /**
5463
- * A map of all motion values attached to this visual element. Motion
5464
- * values are source of truth for any given animated value. A motion
5465
- * value might be provided externally by the component via props.
5466
- */
5467
- const values = new Map();
5468
- /**
5469
- * A map of every subscription that binds the provided or generated
5470
- * motion values onChange listeners to this visual element.
5471
- */
5472
- const valueSubscriptions = new Map();
5473
- /**
5474
- * A reference to the previously-provided motion values as returned
5475
- * from scrapeMotionValuesFromProps. We use the keys in here to determine
5476
- * if any motion values need to be removed after props are updated.
5477
- */
5478
- let prevMotionValues = {};
5479
- /**
5480
- * When values are removed from all animation props we need to search
5481
- * for a fallback value to animate to. These values are tracked in baseTarget.
5482
- */
5483
- const baseTarget = {
5484
- ...latestValues,
5485
- };
5486
- /**
5487
- * Create an object of the values we initially animated from (if initial prop present).
5488
- */
5489
- const initialValues = props.initial ? { ...latestValues } : {};
5490
- // Internal methods ========================
5491
- /**
5492
- * On mount, this will be hydrated with a callback to disconnect
5493
- * this visual element from its parent on unmount.
5494
- */
5495
- let removeFromVariantTree;
5496
- /**
5497
- * Render the element with the latest styles outside of the React
5498
- * render lifecycle
5499
- */
5500
- function render() {
5501
- if (!instance || !isMounted)
5502
- return;
5503
- triggerBuild();
5504
- renderInstance(instance, renderState, props.style, element.projection);
5505
- }
5506
- function triggerBuild() {
5507
- build(element, renderState, latestValues, options, props);
5508
- }
5509
- function update() {
5510
- lifecycles.notifyUpdate(latestValues);
5511
- }
5512
- /**
5513
- *
5514
- */
5515
- function bindToMotionValue(key, value) {
5516
- const removeOnChange = value.onChange((latestValue) => {
5517
- latestValues[key] = latestValue;
5518
- props.onUpdate && sync.update(update, false, true);
5519
- });
5520
- const removeOnRenderRequest = value.onRenderRequest(element.scheduleRender);
5521
- valueSubscriptions.set(key, () => {
5522
- removeOnChange();
5523
- removeOnRenderRequest();
5524
- });
5525
- }
5526
- /**
5527
- * Any motion values that are provided to the element when created
5528
- * aren't yet bound to the element, as this would technically be impure.
5529
- * However, we iterate through the motion values and set them to the
5530
- * initial values for this component.
5531
- *
5532
- * TODO: This is impure and we should look at changing this to run on mount.
5533
- * Doing so will break some tests but this isn't neccessarily a breaking change,
5534
- * more a reflection of the test.
5535
- */
5536
- const { willChange, ...initialMotionValues } = scrapeMotionValuesFromProps(props);
5537
- for (const key in initialMotionValues) {
5538
- const value = initialMotionValues[key];
5539
- if (latestValues[key] !== undefined && isMotionValue(value)) {
5540
- value.set(latestValues[key], false);
5541
- if (isWillChangeMotionValue(willChange)) {
5542
- willChange.add(key);
5543
- }
5544
- }
5545
- }
5546
- /**
5547
- * Update external values with initial values
5548
- */
5549
- if (props.values) {
5550
- for (const key in props.values) {
5551
- const value = props.values[key];
5552
- if (latestValues[key] !== undefined && isMotionValue(value)) {
5553
- value.set(latestValues[key]);
5554
- }
5555
- }
5556
- }
5557
- /**
5558
- * Determine what role this visual element should take in the variant tree.
5559
- */
5560
- const isControllingVariants$1 = isControllingVariants(props);
5561
- const isVariantNode$1 = isVariantNode(props);
5562
- const element = {
5563
- treeType,
5564
- /**
5565
- * This is a mirror of the internal instance prop, which keeps
5566
- * VisualElement type-compatible with React's RefObject.
5567
- */
5568
- current: null,
5569
- /**
5570
- * The depth of this visual element within the visual element tree.
5571
- */
5572
- depth: parent ? parent.depth + 1 : 0,
5573
- parent,
5574
- children: new Set(),
5575
- /**
5576
- *
5577
- */
5578
- presenceId,
5579
- shouldReduceMotion: null,
5580
- /**
5581
- * If this component is part of the variant tree, it should track
5582
- * any children that are also part of the tree. This is essentially
5583
- * a shadow tree to simplify logic around how to stagger over children.
5584
- */
5585
- variantChildren: isVariantNode$1 ? new Set() : undefined,
5586
- /**
5587
- * Whether this instance is visible. This can be changed imperatively
5588
- * by the projection tree, is analogous to CSS's visibility in that
5589
- * hidden elements should take up layout, and needs enacting by the configured
5590
- * render function.
5591
- */
5592
- isVisible: undefined,
5593
- /**
5594
- * Normally, if a component is controlled by a parent's variants, it can
5595
- * rely on that ancestor to trigger animations further down the tree.
5596
- * However, if a component is created after its parent is mounted, the parent
5597
- * won't trigger that mount animation so the child needs to.
5598
- *
5599
- * TODO: This might be better replaced with a method isParentMounted
5600
- */
5601
- manuallyAnimateOnMount: Boolean(parent === null || parent === void 0 ? void 0 : parent.isMounted()),
5602
- /**
5603
- * This can be set by AnimatePresence to force components that mount
5604
- * at the same time as it to mount as if they have initial={false} set.
5605
- */
5606
- blockInitialAnimation,
5607
- /**
5608
- * Determine whether this component has mounted yet. This is mostly used
5609
- * by variant children to determine whether they need to trigger their
5610
- * own animations on mount.
5611
- */
5612
- isMounted: () => Boolean(instance),
5613
- mount(newInstance) {
5614
- isMounted = true;
5615
- instance = element.current = newInstance;
5616
- if (element.projection) {
5617
- element.projection.mount(newInstance);
5618
- }
5619
- if (isVariantNode$1 && parent && !isControllingVariants$1) {
5620
- removeFromVariantTree = parent === null || parent === void 0 ? void 0 : parent.addVariantChild(element);
5621
- }
5622
- values.forEach((value, key) => bindToMotionValue(key, value));
5623
- if (!hasReducedMotionListener.current) {
5624
- initPrefersReducedMotion();
5625
- }
5626
- element.shouldReduceMotion =
5627
- reducedMotionConfig === "never"
5628
- ? false
5629
- : reducedMotionConfig === "always"
5630
- ? true
5631
- : prefersReducedMotion.current;
5632
- parent === null || parent === void 0 ? void 0 : parent.children.add(element);
5633
- element.setProps(props);
5634
- },
5635
- /**
5636
- *
5637
- */
5638
- unmount() {
5639
- var _a;
5640
- (_a = element.projection) === null || _a === void 0 ? void 0 : _a.unmount();
5641
- cancelSync.update(update);
5642
- cancelSync.render(render);
5643
- valueSubscriptions.forEach((remove) => remove());
5644
- removeFromVariantTree === null || removeFromVariantTree === void 0 ? void 0 : removeFromVariantTree();
5645
- parent === null || parent === void 0 ? void 0 : parent.children.delete(element);
5646
- lifecycles.clearAllListeners();
5647
- instance = undefined;
5648
- isMounted = false;
5649
- },
5650
- loadFeatures(renderedProps, isStrict, preloadedFeatures, projectionId, ProjectionNodeConstructor, initialLayoutGroupConfig) {
5651
- const features = [];
5652
- /**
5653
- * If we're in development mode, check to make sure we're not rendering a motion component
5654
- * as a child of LazyMotion, as this will break the file-size benefits of using it.
5655
- */
5656
- if (env !== "production" && preloadedFeatures && isStrict) {
5657
- invariant(false, "You have rendered a `motion` component within a `LazyMotion` component. This will break tree shaking. Import and render a `m` component instead.");
5658
- }
5659
- for (let i = 0; i < numFeatures; i++) {
5660
- const name = featureNames[i];
5661
- const { isEnabled, Component } = featureDefinitions[name];
5662
- /**
5663
- * It might be possible in the future to use this moment to
5664
- * dynamically request functionality. In initial tests this
5665
- * was producing a lot of duplication amongst bundles.
5666
- */
5667
- if (isEnabled(renderedProps) && Component) {
5668
- features.push(React.createElement(Component, {
5669
- key: name,
5670
- ...renderedProps,
5671
- visualElement: element,
5672
- }));
5673
- }
5674
- }
5675
- if (!element.projection && ProjectionNodeConstructor) {
5676
- element.projection = new ProjectionNodeConstructor(projectionId, element.getLatestValues(), parent && parent.projection);
5677
- const { layoutId, layout, drag, dragConstraints, layoutScroll, } = renderedProps;
5678
- element.projection.setOptions({
5679
- layoutId,
5680
- layout,
5681
- alwaysMeasureLayout: Boolean(drag) ||
5682
- (dragConstraints && isRefObject(dragConstraints)),
5683
- visualElement: element,
5684
- scheduleRender: () => element.scheduleRender(),
5685
- /**
5686
- * TODO: Update options in an effect. This could be tricky as it'll be too late
5687
- * to update by the time layout animations run.
5688
- * We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
5689
- * ensuring it gets called if there's no potential layout animations.
5690
- *
5691
- */
5692
- animationType: typeof layout === "string" ? layout : "both",
5693
- initialPromotionConfig: initialLayoutGroupConfig,
5694
- layoutScroll,
5695
- });
5696
- }
5697
- return features;
5698
- },
5699
- /**
5700
- * Add a child visual element to our set of children.
5701
- */
5702
- addVariantChild(child) {
5703
- var _a;
5704
- const closestVariantNode = element.getClosestVariantNode();
5705
- if (closestVariantNode) {
5706
- (_a = closestVariantNode.variantChildren) === null || _a === void 0 ? void 0 : _a.add(child);
5707
- return () => closestVariantNode.variantChildren.delete(child);
5708
- }
5709
- },
5710
- sortNodePosition(other) {
5711
- /**
5712
- * If these nodes aren't even of the same type we can't compare their depth.
5713
- */
5714
- if (!sortNodePosition || treeType !== other.treeType)
5715
- return 0;
5716
- return sortNodePosition(element.getInstance(), other.getInstance());
5717
- },
5718
- /**
5719
- * Returns the closest variant node in the tree starting from
5720
- * this visual element.
5721
- */
5722
- getClosestVariantNode: () => isVariantNode$1 ? element : parent === null || parent === void 0 ? void 0 : parent.getClosestVariantNode(),
5723
- /**
5724
- * Expose the latest layoutId prop.
5725
- */
5726
- getLayoutId: () => props.layoutId,
5727
- /**
5728
- * Returns the current instance.
5729
- */
5730
- getInstance: () => instance,
5731
- /**
5732
- * Get/set the latest static values.
5733
- */
5734
- getStaticValue: (key) => latestValues[key],
5735
- setStaticValue: (key, value) => (latestValues[key] = value),
5736
- /**
5737
- * Returns the latest motion value state. Currently only used to take
5738
- * a snapshot of the visual element - perhaps this can return the whole
5739
- * visual state
5740
- */
5741
- getLatestValues: () => latestValues,
5742
- /**
5743
- * Set the visiblity of the visual element. If it's changed, schedule
5744
- * a render to reflect these changes.
5745
- */
5746
- setVisibility(visibility) {
5747
- if (element.isVisible === visibility)
5748
- return;
5749
- element.isVisible = visibility;
5750
- element.scheduleRender();
5751
- },
5752
- /**
5753
- * Make a target animatable by Popmotion. For instance, if we're
5754
- * trying to animate width from 100px to 100vw we need to measure 100vw
5755
- * in pixels to determine what we really need to animate to. This is also
5756
- * pluggable to support Framer's custom value types like Color,
5757
- * and CSS variables.
5758
- */
5759
- makeTargetAnimatable(target, canMutate = true) {
5760
- return makeTargetAnimatable(element, target, props, canMutate);
5761
- },
5762
- /**
5763
- * Measure the current viewport box with or without transforms.
5764
- * Only measures axis-aligned boxes, rotate and skew must be manually
5765
- * removed with a re-render to work.
5766
- */
5767
- measureViewportBox() {
5768
- return measureViewportBox(instance, props);
5769
- },
5770
- // Motion values ========================
5771
- /**
5772
- * Add a motion value and bind it to this visual element.
5773
- */
5774
- addValue(key, value) {
5775
- // Remove existing value if it exists
5776
- if (element.hasValue(key))
5777
- element.removeValue(key);
5778
- values.set(key, value);
5779
- latestValues[key] = value.get();
5780
- bindToMotionValue(key, value);
5781
- },
5782
- /**
5783
- * Remove a motion value and unbind any active subscriptions.
5784
- */
5785
- removeValue(key) {
5786
- var _a;
5787
- values.delete(key);
5788
- (_a = valueSubscriptions.get(key)) === null || _a === void 0 ? void 0 : _a();
5789
- valueSubscriptions.delete(key);
5790
- delete latestValues[key];
5791
- removeValueFromRenderState(key, renderState);
5792
- },
5793
- /**
5794
- * Check whether we have a motion value for this key
5795
- */
5796
- hasValue: (key) => values.has(key),
5797
- /**
5798
- * Get a motion value for this key. If called with a default
5799
- * value, we'll create one if none exists.
5800
- */
5801
- getValue(key, defaultValue) {
5802
- if (props.values && props.values[key]) {
5803
- return props.values[key];
5804
- }
5805
- let value = values.get(key);
5806
- if (value === undefined && defaultValue !== undefined) {
5807
- value = motionValue(defaultValue);
5808
- element.addValue(key, value);
5809
- }
5810
- return value;
5811
- },
5812
- /**
5813
- * Iterate over our motion values.
5814
- */
5815
- forEachValue: (callback) => values.forEach(callback),
5816
- /**
5817
- * If we're trying to animate to a previously unencountered value,
5818
- * we need to check for it in our state and as a last resort read it
5819
- * directly from the instance (which might have performance implications).
5820
- */
5821
- readValue: (key) => latestValues[key] !== undefined
5822
- ? latestValues[key]
5823
- : readValueFromInstance(instance, key, options),
5824
- /**
5825
- * Set the base target to later animate back to. This is currently
5826
- * only hydrated on creation and when we first read a value.
5827
- */
5828
- setBaseTarget(key, value) {
5829
- baseTarget[key] = value;
5830
- },
5831
- /**
5832
- * Find the base target for a value thats been removed from all animation
5833
- * props.
5834
- */
5835
- getBaseTarget(key) {
5836
- var _a;
5837
- const { initial } = props;
5838
- const valueFromInitial = typeof initial === "string" || typeof initial === "object"
5839
- ? (_a = resolveVariantFromProps(props, initial)) === null || _a === void 0 ? void 0 : _a[key]
5840
- : undefined;
5841
- /**
5842
- * If this value still exists in the current initial variant, read that.
5843
- */
5844
- if (initial && valueFromInitial !== undefined) {
5845
- return valueFromInitial;
5846
- }
5847
- /**
5848
- * Alternatively, if this VisualElement config has defined a getBaseTarget
5849
- * so we can read the value from an alternative source, try that.
5850
- */
5851
- if (getBaseTarget) {
5852
- const target = getBaseTarget(props, key);
5853
- if (target !== undefined && !isMotionValue(target))
5854
- return target;
5855
- }
5856
- /**
5857
- * If the value was initially defined on initial, but it doesn't any more,
5858
- * return undefined. Otherwise return the value as initially read from the DOM.
5859
- */
5860
- return initialValues[key] !== undefined &&
5861
- valueFromInitial === undefined
5862
- ? undefined
5863
- : baseTarget[key];
5864
- },
5865
- // Lifecyles ========================
5866
- ...lifecycles,
5867
- /**
5868
- * Build the renderer state based on the latest visual state.
5869
- */
5870
- build() {
5871
- triggerBuild();
5872
- return renderState;
5873
- },
5874
- /**
5875
- * Schedule a render on the next animation frame.
5876
- */
5877
- scheduleRender() {
5878
- sync.render(render, false, true);
5879
- },
5880
- /**
5881
- * Synchronously fire render. It's prefered that we batch renders but
5882
- * in many circumstances, like layout measurement, we need to run this
5883
- * synchronously. However in those instances other measures should be taken
5884
- * to batch reads/writes.
5885
- */
5886
- syncRender: render,
5887
- /**
5888
- * Update the provided props. Ensure any newly-added motion values are
5889
- * added to our map, old ones removed, and listeners updated.
5890
- */
5891
- setProps(newProps) {
5892
- if (newProps.transformTemplate || props.transformTemplate) {
5893
- element.scheduleRender();
5894
- }
5895
- props = newProps;
5896
- lifecycles.updatePropListeners(newProps);
5897
- prevMotionValues = updateMotionValuesFromProps(element, scrapeMotionValuesFromProps(props), prevMotionValues);
5898
- },
5899
- getProps: () => props,
5900
- // Variants ==============================
5901
- /**
5902
- * Returns the variant definition with a given name.
5903
- */
5904
- getVariant: (name) => { var _a; return (_a = props.variants) === null || _a === void 0 ? void 0 : _a[name]; },
5905
- /**
5906
- * Returns the defined default transition on this component.
5907
- */
5908
- getDefaultTransition: () => props.transition,
5909
- getTransformPagePoint: () => {
5910
- return props.transformPagePoint;
5911
- },
5912
- /**
5913
- * Used by child variant nodes to get the closest ancestor variant props.
5914
- */
5915
- getVariantContext(startAtParent = false) {
5916
- if (startAtParent)
5917
- return parent === null || parent === void 0 ? void 0 : parent.getVariantContext();
5918
- if (!isControllingVariants$1) {
5919
- const context = (parent === null || parent === void 0 ? void 0 : parent.getVariantContext()) || {};
5920
- if (props.initial !== undefined) {
5921
- context.initial = props.initial;
5922
- }
5923
- return context;
5924
- }
5925
- const context = {};
5926
- for (let i = 0; i < numVariantProps; i++) {
5927
- const name = variantProps[i];
5928
- const prop = props[name];
5929
- if (isVariantLabel(prop) || prop === false) {
5930
- context[name] = prop;
5931
- }
5932
- }
5933
- return context;
5934
- },
5935
- };
5936
- return element;
5937
- };
5938
- const variantProps = ["initial", ...variantPriorityOrder];
5939
- const numVariantProps = variantProps.length;
5940
-
5941
5334
  function isCSSVariable(value) {
5942
5335
  return typeof value === "string" && value.startsWith("var(--");
5943
5336
  }
@@ -5984,7 +5377,7 @@
5984
5377
  * @internal
5985
5378
  */
5986
5379
  function resolveCSSVariables(visualElement, { ...target }, transitionEnd) {
5987
- const element = visualElement.getInstance();
5380
+ const element = visualElement.current;
5988
5381
  if (!(element instanceof Element))
5989
5382
  return { target, transitionEnd };
5990
5383
  // If `transitionEnd` isn't `undefined`, clone it. We could clone `target` and `transitionEnd`
@@ -5993,7 +5386,7 @@
5993
5386
  transitionEnd = { ...transitionEnd };
5994
5387
  }
5995
5388
  // Go through existing `MotionValue`s and ensure any existing CSS variables are resolved
5996
- visualElement.forEachValue((value) => {
5389
+ visualElement.values.forEach((value) => {
5997
5390
  const current = value.get();
5998
5391
  if (!isCSSVariable(current))
5999
5392
  return;
@@ -6083,7 +5476,7 @@
6083
5476
  });
6084
5477
  // Apply changes to element before measurement
6085
5478
  if (removedTransforms.length)
6086
- visualElement.syncRender();
5479
+ visualElement.render();
6087
5480
  return removedTransforms;
6088
5481
  }
6089
5482
  const positionalValues = {
@@ -6100,7 +5493,7 @@
6100
5493
  };
6101
5494
  const convertChangedValueTypes = (target, visualElement, changedKeys) => {
6102
5495
  const originBbox = visualElement.measureViewportBox();
6103
- const element = visualElement.getInstance();
5496
+ const element = visualElement.current;
6104
5497
  const elementComputedStyle = getComputedStyle(element);
6105
5498
  const { display } = elementComputedStyle;
6106
5499
  const origin = {};
@@ -6116,7 +5509,7 @@
6116
5509
  origin[key] = positionalValues[key](originBbox, elementComputedStyle);
6117
5510
  });
6118
5511
  // Apply the latest values (as set in checkAndConvertChangedValueTypes)
6119
- visualElement.syncRender();
5512
+ visualElement.render();
6120
5513
  const targetBbox = visualElement.measureViewportBox();
6121
5514
  changedKeys.forEach((key) => {
6122
5515
  // Restore styles to their **calculated computed style**, not their actual
@@ -6194,131 +5587,625 @@
6194
5587
  target[key] = fromType.transform(to);
6195
5588
  }
6196
5589
  }
6197
- else {
6198
- // If we're going to do value conversion via DOM measurements, we first
6199
- // need to remove non-positional transform values that could affect the bbox measurements.
6200
- if (!hasAttemptedToRemoveTransformValues) {
6201
- removedTransformValues =
6202
- removeNonTranslationalTransform(visualElement);
6203
- hasAttemptedToRemoveTransformValues = true;
5590
+ else {
5591
+ // If we're going to do value conversion via DOM measurements, we first
5592
+ // need to remove non-positional transform values that could affect the bbox measurements.
5593
+ if (!hasAttemptedToRemoveTransformValues) {
5594
+ removedTransformValues =
5595
+ removeNonTranslationalTransform(visualElement);
5596
+ hasAttemptedToRemoveTransformValues = true;
5597
+ }
5598
+ changedValueTypeKeys.push(key);
5599
+ transitionEnd[key] =
5600
+ transitionEnd[key] !== undefined
5601
+ ? transitionEnd[key]
5602
+ : target[key];
5603
+ setAndResetVelocity(value, to);
5604
+ }
5605
+ }
5606
+ });
5607
+ if (changedValueTypeKeys.length) {
5608
+ const scrollY = changedValueTypeKeys.indexOf("height") >= 0
5609
+ ? window.pageYOffset
5610
+ : null;
5611
+ const convertedTarget = convertChangedValueTypes(target, visualElement, changedValueTypeKeys);
5612
+ // If we removed transform values, reapply them before the next render
5613
+ if (removedTransformValues.length) {
5614
+ removedTransformValues.forEach(([key, value]) => {
5615
+ visualElement.getValue(key).set(value);
5616
+ });
5617
+ }
5618
+ // Reapply original values
5619
+ visualElement.render();
5620
+ // Restore scroll position
5621
+ if (isBrowser && scrollY !== null) {
5622
+ window.scrollTo({ top: scrollY });
5623
+ }
5624
+ return { target: convertedTarget, transitionEnd };
5625
+ }
5626
+ else {
5627
+ return { target, transitionEnd };
5628
+ }
5629
+ };
5630
+ /**
5631
+ * Convert value types for x/y/width/height/top/left/bottom/right
5632
+ *
5633
+ * Allows animation between `'auto'` -> `'100%'` or `0` -> `'calc(50% - 10vw)'`
5634
+ *
5635
+ * @internal
5636
+ */
5637
+ function unitConversion(visualElement, target, origin, transitionEnd) {
5638
+ return hasPositionalKey(target)
5639
+ ? checkAndConvertChangedValueTypes(visualElement, target, origin, transitionEnd)
5640
+ : { target, transitionEnd };
5641
+ }
5642
+
5643
+ /**
5644
+ * Parse a DOM variant to make it animatable. This involves resolving CSS variables
5645
+ * and ensuring animations like "20%" => "calc(50vw)" are performed in pixels.
5646
+ */
5647
+ const parseDomVariant = (visualElement, target, origin, transitionEnd) => {
5648
+ const resolved = resolveCSSVariables(visualElement, target, transitionEnd);
5649
+ target = resolved.target;
5650
+ transitionEnd = resolved.transitionEnd;
5651
+ return unitConversion(visualElement, target, origin, transitionEnd);
5652
+ };
5653
+
5654
+ // Does this device prefer reduced motion? Returns `null` server-side.
5655
+ const prefersReducedMotion = { current: null };
5656
+ const hasReducedMotionListener = { current: false };
5657
+
5658
+ function initPrefersReducedMotion() {
5659
+ hasReducedMotionListener.current = true;
5660
+ if (!isBrowser)
5661
+ return;
5662
+ if (window.matchMedia) {
5663
+ const motionMediaQuery = window.matchMedia("(prefers-reduced-motion)");
5664
+ const setReducedMotionPreferences = () => (prefersReducedMotion.current = motionMediaQuery.matches);
5665
+ motionMediaQuery.addListener(setReducedMotionPreferences);
5666
+ setReducedMotionPreferences();
5667
+ }
5668
+ else {
5669
+ prefersReducedMotion.current = false;
5670
+ }
5671
+ }
5672
+
5673
+ function updateMotionValuesFromProps(element, next, prev) {
5674
+ const { willChange } = next;
5675
+ for (const key in next) {
5676
+ const nextValue = next[key];
5677
+ const prevValue = prev[key];
5678
+ if (isMotionValue(nextValue)) {
5679
+ /**
5680
+ * If this is a motion value found in props or style, we want to add it
5681
+ * to our visual element's motion value map.
5682
+ */
5683
+ element.addValue(key, nextValue);
5684
+ if (isWillChangeMotionValue(willChange)) {
5685
+ willChange.add(key);
5686
+ }
5687
+ /**
5688
+ * Check the version of the incoming motion value with this version
5689
+ * and warn against mismatches.
5690
+ */
5691
+ {
5692
+ warnOnce(nextValue.version === "7.6.6", `Attempting to mix Framer Motion versions ${nextValue.version} with 7.6.6 may not work as expected.`);
5693
+ }
5694
+ }
5695
+ else if (isMotionValue(prevValue)) {
5696
+ /**
5697
+ * If we're swapping from a motion value to a static value,
5698
+ * create a new motion value from that
5699
+ */
5700
+ element.addValue(key, motionValue(nextValue));
5701
+ if (isWillChangeMotionValue(willChange)) {
5702
+ willChange.remove(key);
5703
+ }
5704
+ }
5705
+ else if (prevValue !== nextValue) {
5706
+ /**
5707
+ * If this is a flat value that has changed, update the motion value
5708
+ * or create one if it doesn't exist. We only want to do this if we're
5709
+ * not handling the value with our animation state.
5710
+ */
5711
+ if (element.hasValue(key)) {
5712
+ const existingValue = element.getValue(key);
5713
+ // TODO: Only update values that aren't being animated or even looked at
5714
+ !existingValue.hasAnimated && existingValue.set(nextValue);
5715
+ }
5716
+ else {
5717
+ const latestValue = element.getStaticValue(key);
5718
+ element.addValue(key, motionValue(latestValue !== undefined ? latestValue : nextValue));
5719
+ }
5720
+ }
5721
+ }
5722
+ // Handle removed values
5723
+ for (const key in prev) {
5724
+ if (next[key] === undefined)
5725
+ element.removeValue(key);
5726
+ }
5727
+ return next;
5728
+ }
5729
+
5730
+ const featureNames = Object.keys(featureDefinitions);
5731
+ const numFeatures = featureNames.length;
5732
+ const propEventHandlers = [
5733
+ "AnimationStart",
5734
+ "AnimationComplete",
5735
+ "Update",
5736
+ "Unmount",
5737
+ "BeforeLayoutMeasure",
5738
+ "LayoutMeasure",
5739
+ "LayoutAnimationStart",
5740
+ "LayoutAnimationComplete",
5741
+ ];
5742
+ /**
5743
+ * A VisualElement is an imperative abstraction around UI elements such as
5744
+ * HTMLElement, SVGElement, Three.Object3D etc.
5745
+ */
5746
+ class VisualElement {
5747
+ constructor({ parent, props, reducedMotionConfig, visualState, }, options = {}) {
5748
+ /**
5749
+ * A reference to the current underlying Instance, e.g. a HTMLElement
5750
+ * or Three.Mesh etc.
5751
+ */
5752
+ this.current = null;
5753
+ /**
5754
+ * A set containing references to this VisualElement's children.
5755
+ */
5756
+ this.children = new Set();
5757
+ /**
5758
+ * Determine what role this visual element should take in the variant tree.
5759
+ */
5760
+ this.isVariantNode = false;
5761
+ this.isControllingVariants = false;
5762
+ /**
5763
+ * Decides whether this VisualElement should animate in reduced motion
5764
+ * mode.
5765
+ *
5766
+ * TODO: This is currently set on every individual VisualElement but feels
5767
+ * like it could be set globally.
5768
+ */
5769
+ this.shouldReduceMotion = null;
5770
+ /**
5771
+ * A map of all motion values attached to this visual element. Motion
5772
+ * values are source of truth for any given animated value. A motion
5773
+ * value might be provided externally by the component via props.
5774
+ */
5775
+ this.values = new Map();
5776
+ /**
5777
+ * Tracks whether this VisualElement's React component is currently present
5778
+ * within the defined React tree.
5779
+ */
5780
+ this.isPresent = true;
5781
+ /**
5782
+ * A map of every subscription that binds the provided or generated
5783
+ * motion values onChange listeners to this visual element.
5784
+ */
5785
+ this.valueSubscriptions = new Map();
5786
+ /**
5787
+ * A reference to the previously-provided motion values as returned
5788
+ * from scrapeMotionValuesFromProps. We use the keys in here to determine
5789
+ * if any motion values need to be removed after props are updated.
5790
+ */
5791
+ this.prevMotionValues = {};
5792
+ /**
5793
+ * An object containing a SubscriptionManager for each active event.
5794
+ */
5795
+ this.events = {};
5796
+ /**
5797
+ * An object containing an unsubscribe function for each prop event subscription.
5798
+ * For example, every "Update" event can have multiple subscribers via
5799
+ * VisualElement.on(), but only one of those can be defined via the onUpdate prop.
5800
+ */
5801
+ this.propEventSubscriptions = {};
5802
+ this.notifyUpdate = () => this.notify("Update", this.latestValues);
5803
+ this.render = () => {
5804
+ if (!this.current)
5805
+ return;
5806
+ this.triggerBuild();
5807
+ this.renderInstance(this.current, this.renderState, this.props.style, this.projection);
5808
+ };
5809
+ this.scheduleRender = () => sync.render(this.render, false, true);
5810
+ const { latestValues, renderState } = visualState;
5811
+ this.latestValues = latestValues;
5812
+ this.baseTarget = { ...latestValues };
5813
+ this.initialValues = props.initial ? { ...latestValues } : {};
5814
+ this.renderState = renderState;
5815
+ this.parent = parent;
5816
+ this.props = props;
5817
+ this.depth = parent ? parent.depth + 1 : 0;
5818
+ this.reducedMotionConfig = reducedMotionConfig;
5819
+ this.options = options;
5820
+ this.isControllingVariants = isControllingVariants(props);
5821
+ this.isVariantNode = isVariantNode(props);
5822
+ if (this.isVariantNode) {
5823
+ this.variantChildren = new Set();
5824
+ }
5825
+ this.manuallyAnimateOnMount = Boolean(parent && parent.current);
5826
+ /**
5827
+ * Any motion values that are provided to the element when created
5828
+ * aren't yet bound to the element, as this would technically be impure.
5829
+ * However, we iterate through the motion values and set them to the
5830
+ * initial values for this component.
5831
+ *
5832
+ * TODO: This is impure and we should look at changing this to run on mount.
5833
+ * Doing so will break some tests but this isn't neccessarily a breaking change,
5834
+ * more a reflection of the test.
5835
+ */
5836
+ const { willChange, ...initialMotionValues } = this.scrapeMotionValuesFromProps(props);
5837
+ for (const key in initialMotionValues) {
5838
+ const value = initialMotionValues[key];
5839
+ if (latestValues[key] !== undefined && isMotionValue(value)) {
5840
+ value.set(latestValues[key], false);
5841
+ if (isWillChangeMotionValue(willChange)) {
5842
+ willChange.add(key);
5843
+ }
5844
+ }
5845
+ }
5846
+ /**
5847
+ * Update external values with initial values
5848
+ */
5849
+ if (props.values) {
5850
+ for (const key in props.values) {
5851
+ const value = props.values[key];
5852
+ if (latestValues[key] !== undefined && isMotionValue(value)) {
5853
+ value.set(latestValues[key]);
6204
5854
  }
6205
- changedValueTypeKeys.push(key);
6206
- transitionEnd[key] =
6207
- transitionEnd[key] !== undefined
6208
- ? transitionEnd[key]
6209
- : target[key];
6210
- setAndResetVelocity(value, to);
6211
5855
  }
6212
5856
  }
6213
- });
6214
- if (changedValueTypeKeys.length) {
6215
- const scrollY = changedValueTypeKeys.indexOf("height") >= 0
6216
- ? window.pageYOffset
6217
- : null;
6218
- const convertedTarget = convertChangedValueTypes(target, visualElement, changedValueTypeKeys);
6219
- // If we removed transform values, reapply them before the next render
6220
- if (removedTransformValues.length) {
6221
- removedTransformValues.forEach(([key, value]) => {
6222
- visualElement.getValue(key).set(value);
5857
+ }
5858
+ /**
5859
+ * This method takes React props and returns found MotionValues. For example, HTML
5860
+ * MotionValues will be found within the style prop, whereas for Three.js within attribute arrays.
5861
+ *
5862
+ * This isn't an abstract method as it needs calling in the constructor, but it is
5863
+ * intended to be one.
5864
+ */
5865
+ scrapeMotionValuesFromProps(_props) {
5866
+ return {};
5867
+ }
5868
+ mount(instance) {
5869
+ var _a;
5870
+ this.current = instance;
5871
+ if (this.projection) {
5872
+ this.projection.mount(instance);
5873
+ }
5874
+ if (this.parent && this.isVariantNode && !this.isControllingVariants) {
5875
+ this.removeFromVariantTree = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.addVariantChild(this);
5876
+ }
5877
+ this.values.forEach((value, key) => this.bindToMotionValue(key, value));
5878
+ if (!hasReducedMotionListener.current) {
5879
+ initPrefersReducedMotion();
5880
+ }
5881
+ this.shouldReduceMotion =
5882
+ this.reducedMotionConfig === "never"
5883
+ ? false
5884
+ : this.reducedMotionConfig === "always"
5885
+ ? true
5886
+ : prefersReducedMotion.current;
5887
+ if (this.parent)
5888
+ this.parent.children.add(this);
5889
+ this.setProps(this.props);
5890
+ }
5891
+ unmount() {
5892
+ var _a, _b, _c;
5893
+ (_a = this.projection) === null || _a === void 0 ? void 0 : _a.unmount();
5894
+ cancelSync.update(this.notifyUpdate);
5895
+ cancelSync.render(this.render);
5896
+ this.valueSubscriptions.forEach((remove) => remove());
5897
+ (_b = this.removeFromVariantTree) === null || _b === void 0 ? void 0 : _b.call(this);
5898
+ (_c = this.parent) === null || _c === void 0 ? void 0 : _c.children.delete(this);
5899
+ for (const key in this.events) {
5900
+ this.events[key].clear();
5901
+ }
5902
+ this.current = null;
5903
+ }
5904
+ bindToMotionValue(key, value) {
5905
+ const removeOnChange = value.onChange((latestValue) => {
5906
+ this.latestValues[key] = latestValue;
5907
+ this.props.onUpdate &&
5908
+ sync.update(this.notifyUpdate, false, true);
5909
+ });
5910
+ const removeOnRenderRequest = value.onRenderRequest(this.scheduleRender);
5911
+ this.valueSubscriptions.set(key, () => {
5912
+ removeOnChange();
5913
+ removeOnRenderRequest();
5914
+ });
5915
+ }
5916
+ sortNodePosition(other) {
5917
+ /**
5918
+ * If these nodes aren't even of the same type we can't compare their depth.
5919
+ */
5920
+ if (!this.current ||
5921
+ !this.sortInstanceNodePosition ||
5922
+ this.type !== other.type)
5923
+ return 0;
5924
+ return this.sortInstanceNodePosition(this.current, other.current);
5925
+ }
5926
+ loadFeatures(renderedProps, isStrict, preloadedFeatures, projectionId, ProjectionNodeConstructor, initialLayoutGroupConfig) {
5927
+ const features = [];
5928
+ /**
5929
+ * If we're in development mode, check to make sure we're not rendering a motion component
5930
+ * as a child of LazyMotion, as this will break the file-size benefits of using it.
5931
+ */
5932
+ if (env !== "production" && preloadedFeatures && isStrict) {
5933
+ invariant(false, "You have rendered a `motion` component within a `LazyMotion` component. This will break tree shaking. Import and render a `m` component instead.");
5934
+ }
5935
+ for (let i = 0; i < numFeatures; i++) {
5936
+ const name = featureNames[i];
5937
+ const { isEnabled, Component } = featureDefinitions[name];
5938
+ /**
5939
+ * It might be possible in the future to use this moment to
5940
+ * dynamically request functionality. In initial tests this
5941
+ * was producing a lot of duplication amongst bundles.
5942
+ */
5943
+ if (isEnabled(renderedProps) && Component) {
5944
+ features.push(React.createElement(Component, {
5945
+ key: name,
5946
+ ...renderedProps,
5947
+ visualElement: this,
5948
+ }));
5949
+ }
5950
+ }
5951
+ if (!this.projection && ProjectionNodeConstructor) {
5952
+ this.projection = new ProjectionNodeConstructor(projectionId, this.latestValues, this.parent && this.parent.projection);
5953
+ const { layoutId, layout, drag, dragConstraints, layoutScroll } = renderedProps;
5954
+ this.projection.setOptions({
5955
+ layoutId,
5956
+ layout,
5957
+ alwaysMeasureLayout: Boolean(drag) ||
5958
+ (dragConstraints && isRefObject(dragConstraints)),
5959
+ visualElement: this,
5960
+ scheduleRender: () => this.scheduleRender(),
5961
+ /**
5962
+ * TODO: Update options in an effect. This could be tricky as it'll be too late
5963
+ * to update by the time layout animations run.
5964
+ * We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
5965
+ * ensuring it gets called if there's no potential layout animations.
5966
+ *
5967
+ */
5968
+ animationType: typeof layout === "string" ? layout : "both",
5969
+ initialPromotionConfig: initialLayoutGroupConfig,
5970
+ layoutScroll,
6223
5971
  });
6224
5972
  }
6225
- // Reapply original values
6226
- visualElement.syncRender();
6227
- // Restore scroll position
6228
- if (isBrowser && scrollY !== null) {
6229
- window.scrollTo({ top: scrollY });
5973
+ return features;
5974
+ }
5975
+ triggerBuild() {
5976
+ this.build(this.renderState, this.latestValues, this.options, this.props);
5977
+ }
5978
+ /**
5979
+ * Measure the current viewport box with or without transforms.
5980
+ * Only measures axis-aligned boxes, rotate and skew must be manually
5981
+ * removed with a re-render to work.
5982
+ */
5983
+ measureViewportBox() {
5984
+ return this.current
5985
+ ? this.measureInstanceViewportBox(this.current, this.props)
5986
+ : createBox();
5987
+ }
5988
+ getStaticValue(key) {
5989
+ return this.latestValues[key];
5990
+ }
5991
+ setStaticValue(key, value) {
5992
+ this.latestValues[key] = value;
5993
+ }
5994
+ /**
5995
+ * Make a target animatable by Popmotion. For instance, if we're
5996
+ * trying to animate width from 100px to 100vw we need to measure 100vw
5997
+ * in pixels to determine what we really need to animate to. This is also
5998
+ * pluggable to support Framer's custom value types like Color,
5999
+ * and CSS variables.
6000
+ */
6001
+ makeTargetAnimatable(target, canMutate = true) {
6002
+ return this.makeTargetAnimatableFromInstance(target, this.props, canMutate);
6003
+ }
6004
+ /**
6005
+ * Update the provided props. Ensure any newly-added motion values are
6006
+ * added to our map, old ones removed, and listeners updated.
6007
+ */
6008
+ setProps(props) {
6009
+ if (props.transformTemplate || this.props.transformTemplate) {
6010
+ this.scheduleRender();
6230
6011
  }
6231
- return { target: convertedTarget, transitionEnd };
6012
+ this.props = props;
6013
+ /**
6014
+ * Update prop event handlers ie onAnimationStart, onAnimationComplete
6015
+ */
6016
+ for (let i = 0; i < propEventHandlers.length; i++) {
6017
+ const key = propEventHandlers[i];
6018
+ if (this.propEventSubscriptions[key]) {
6019
+ this.propEventSubscriptions[key]();
6020
+ delete this.propEventSubscriptions[key];
6021
+ }
6022
+ const listener = props["on" + key];
6023
+ if (listener) {
6024
+ this.propEventSubscriptions[key] = this.on(key, listener);
6025
+ }
6026
+ }
6027
+ this.prevMotionValues = updateMotionValuesFromProps(this, this.scrapeMotionValuesFromProps(props), this.prevMotionValues);
6232
6028
  }
6233
- else {
6234
- return { target, transitionEnd };
6029
+ getProps() {
6030
+ return this.props;
6235
6031
  }
6236
- };
6237
- /**
6238
- * Convert value types for x/y/width/height/top/left/bottom/right
6239
- *
6240
- * Allows animation between `'auto'` -> `'100%'` or `0` -> `'calc(50% - 10vw)'`
6241
- *
6242
- * @internal
6243
- */
6244
- function unitConversion(visualElement, target, origin, transitionEnd) {
6245
- return hasPositionalKey(target)
6246
- ? checkAndConvertChangedValueTypes(visualElement, target, origin, transitionEnd)
6247
- : { target, transitionEnd };
6248
- }
6249
-
6250
- /**
6251
- * Parse a DOM variant to make it animatable. This involves resolving CSS variables
6252
- * and ensuring animations like "20%" => "calc(50vw)" are performed in pixels.
6253
- */
6254
- const parseDomVariant = (visualElement, target, origin, transitionEnd) => {
6255
- const resolved = resolveCSSVariables(visualElement, target, transitionEnd);
6256
- target = resolved.target;
6257
- transitionEnd = resolved.transitionEnd;
6258
- return unitConversion(visualElement, target, origin, transitionEnd);
6259
- };
6260
-
6261
- function getComputedStyle$1(element) {
6262
- return window.getComputedStyle(element);
6263
- }
6264
- const htmlConfig = {
6265
- treeType: "dom",
6266
- readValueFromInstance(domElement, key) {
6267
- if (transformProps.has(key)) {
6268
- const defaultType = getDefaultValueType(key);
6269
- return defaultType ? defaultType.default || 0 : 0;
6032
+ /**
6033
+ * Returns the variant definition with a given name.
6034
+ */
6035
+ getVariant(name) {
6036
+ var _a;
6037
+ return (_a = this.props.variants) === null || _a === void 0 ? void 0 : _a[name];
6038
+ }
6039
+ /**
6040
+ * Returns the defined default transition on this component.
6041
+ */
6042
+ getDefaultTransition() {
6043
+ return this.props.transition;
6044
+ }
6045
+ getTransformPagePoint() {
6046
+ return this.props.transformPagePoint;
6047
+ }
6048
+ getClosestVariantNode() {
6049
+ var _a;
6050
+ return this.isVariantNode ? this : (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getClosestVariantNode();
6051
+ }
6052
+ getVariantContext(startAtParent = false) {
6053
+ var _a, _b;
6054
+ if (startAtParent)
6055
+ return (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getVariantContext();
6056
+ if (!this.isControllingVariants) {
6057
+ const context = ((_b = this.parent) === null || _b === void 0 ? void 0 : _b.getVariantContext()) || {};
6058
+ if (this.props.initial !== undefined) {
6059
+ context.initial = this.props.initial;
6060
+ }
6061
+ return context;
6270
6062
  }
6271
- else {
6272
- const computedStyle = getComputedStyle$1(domElement);
6273
- const value = (isCSSVariable$1(key)
6274
- ? computedStyle.getPropertyValue(key)
6275
- : computedStyle[key]) || 0;
6276
- return typeof value === "string" ? value.trim() : value;
6063
+ const context = {};
6064
+ for (let i = 0; i < numVariantProps; i++) {
6065
+ const name = variantProps[i];
6066
+ const prop = this.props[name];
6067
+ if (isVariantLabel(prop) || prop === false) {
6068
+ context[name] = prop;
6069
+ }
6277
6070
  }
6278
- },
6279
- sortNodePosition(a, b) {
6071
+ return context;
6072
+ }
6073
+ /**
6074
+ * Add a child visual element to our set of children.
6075
+ */
6076
+ addVariantChild(child) {
6077
+ var _a;
6078
+ const closestVariantNode = this.getClosestVariantNode();
6079
+ if (closestVariantNode) {
6080
+ (_a = closestVariantNode.variantChildren) === null || _a === void 0 ? void 0 : _a.add(child);
6081
+ return () => closestVariantNode.variantChildren.delete(child);
6082
+ }
6083
+ }
6084
+ /**
6085
+ * Add a motion value and bind it to this visual element.
6086
+ */
6087
+ addValue(key, value) {
6088
+ // Remove existing value if it exists
6089
+ if (this.hasValue(key))
6090
+ this.removeValue(key);
6091
+ this.values.set(key, value);
6092
+ this.latestValues[key] = value.get();
6093
+ this.bindToMotionValue(key, value);
6094
+ }
6095
+ /**
6096
+ * Remove a motion value and unbind any active subscriptions.
6097
+ */
6098
+ removeValue(key) {
6099
+ var _a;
6100
+ this.values.delete(key);
6101
+ (_a = this.valueSubscriptions.get(key)) === null || _a === void 0 ? void 0 : _a();
6102
+ this.valueSubscriptions.delete(key);
6103
+ delete this.latestValues[key];
6104
+ this.removeValueFromRenderState(key, this.renderState);
6105
+ }
6106
+ /**
6107
+ * Check whether we have a motion value for this key
6108
+ */
6109
+ hasValue(key) {
6110
+ return this.values.has(key);
6111
+ }
6112
+ /**
6113
+ * Get a motion value for this key. If called with a default
6114
+ * value, we'll create one if none exists.
6115
+ */
6116
+ getValue(key, defaultValue) {
6117
+ if (this.props.values && this.props.values[key]) {
6118
+ return this.props.values[key];
6119
+ }
6120
+ let value = this.values.get(key);
6121
+ if (value === undefined && defaultValue !== undefined) {
6122
+ value = motionValue(defaultValue);
6123
+ this.addValue(key, value);
6124
+ }
6125
+ return value;
6126
+ }
6127
+ /**
6128
+ * If we're trying to animate to a previously unencountered value,
6129
+ * we need to check for it in our state and as a last resort read it
6130
+ * directly from the instance (which might have performance implications).
6131
+ */
6132
+ readValue(key) {
6133
+ return this.latestValues[key] !== undefined || !this.current
6134
+ ? this.latestValues[key]
6135
+ : this.readValueFromInstance(this.current, key, this.options);
6136
+ }
6137
+ /**
6138
+ * Set the base target to later animate back to. This is currently
6139
+ * only hydrated on creation and when we first read a value.
6140
+ */
6141
+ setBaseTarget(key, value) {
6142
+ this.baseTarget[key] = value;
6143
+ }
6144
+ /**
6145
+ * Find the base target for a value thats been removed from all animation
6146
+ * props.
6147
+ */
6148
+ getBaseTarget(key) {
6149
+ var _a;
6150
+ const { initial } = this.props;
6151
+ const valueFromInitial = typeof initial === "string" || typeof initial === "object"
6152
+ ? (_a = resolveVariantFromProps(this.props, initial)) === null || _a === void 0 ? void 0 : _a[key]
6153
+ : undefined;
6154
+ /**
6155
+ * If this value still exists in the current initial variant, read that.
6156
+ */
6157
+ if (initial && valueFromInitial !== undefined) {
6158
+ return valueFromInitial;
6159
+ }
6160
+ /**
6161
+ * Alternatively, if this VisualElement config has defined a getBaseTarget
6162
+ * so we can read the value from an alternative source, try that.
6163
+ */
6164
+ const target = this.getBaseTargetFromProps(this.props, key);
6165
+ if (target !== undefined && !isMotionValue(target))
6166
+ return target;
6167
+ /**
6168
+ * If the value was initially defined on initial, but it doesn't any more,
6169
+ * return undefined. Otherwise return the value as initially read from the DOM.
6170
+ */
6171
+ return this.initialValues[key] !== undefined &&
6172
+ valueFromInitial === undefined
6173
+ ? undefined
6174
+ : this.baseTarget[key];
6175
+ }
6176
+ on(eventName, callback) {
6177
+ if (!this.events[eventName]) {
6178
+ this.events[eventName] = new SubscriptionManager();
6179
+ }
6180
+ return this.events[eventName].add(callback);
6181
+ }
6182
+ notify(eventName, ...args) {
6183
+ var _a;
6184
+ (_a = this.events[eventName]) === null || _a === void 0 ? void 0 : _a.notify(...args);
6185
+ }
6186
+ }
6187
+ const variantProps = ["initial", ...variantPriorityOrder];
6188
+ const numVariantProps = variantProps.length;
6189
+
6190
+ class DOMVisualElement extends VisualElement {
6191
+ sortInstanceNodePosition(a, b) {
6280
6192
  /**
6281
6193
  * compareDocumentPosition returns a bitmask, by using the bitwise &
6282
6194
  * we're returning true if 2 in that bitmask is set to true. 2 is set
6283
6195
  * to true if b preceeds a.
6284
6196
  */
6285
6197
  return a.compareDocumentPosition(b) & 2 ? 1 : -1;
6286
- },
6287
- getBaseTarget(props, key) {
6198
+ }
6199
+ getBaseTargetFromProps(props, key) {
6288
6200
  var _a;
6289
6201
  return (_a = props.style) === null || _a === void 0 ? void 0 : _a[key];
6290
- },
6291
- measureViewportBox(element, { transformPagePoint }) {
6292
- return measureViewportBox(element, transformPagePoint);
6293
- },
6294
- /**
6295
- * Reset the transform on the current Element. This is called as part
6296
- * of a batched process across the entire layout tree. To remove this write
6297
- * cycle it'd be interesting to see if it's possible to "undo" all the current
6298
- * layout transforms up the tree in the same way this.getBoundingBoxWithoutTransforms
6299
- * works
6300
- */
6301
- resetTransform(element, domElement, props) {
6302
- const { transformTemplate } = props;
6303
- domElement.style.transform = transformTemplate
6304
- ? transformTemplate({}, "")
6305
- : "none";
6306
- // Ensure that whatever happens next, we restore our transform on the next frame
6307
- element.scheduleRender();
6308
- },
6309
- restoreTransform(instance, mutableState) {
6310
- instance.style.transform = mutableState.style.transform;
6311
- },
6202
+ }
6312
6203
  removeValueFromRenderState(key, { vars, style }) {
6313
6204
  delete vars[key];
6314
6205
  delete style[key];
6315
- },
6316
- /**
6317
- * Ensure that HTML and Framer-specific value types like `px`->`%` and `Color`
6318
- * can be animated by Motion.
6319
- */
6320
- makeTargetAnimatable(element, { transition, transitionEnd, ...target }, { transformValues }, isMounted = true) {
6321
- let origin = getOrigin(target, transition || {}, element);
6206
+ }
6207
+ makeTargetAnimatableFromInstance({ transition, transitionEnd, ...target }, { transformValues }, isMounted) {
6208
+ let origin = getOrigin(target, transition || {}, this);
6322
6209
  /**
6323
6210
  * If Framer has provided a function to convert `Color` etc value types, convert them
6324
6211
  */
@@ -6331,8 +6218,8 @@
6331
6218
  origin = transformValues(origin);
6332
6219
  }
6333
6220
  if (isMounted) {
6334
- checkTargetForNewValues(element, target, origin);
6335
- const parsed = parseDomVariant(element, target, origin, transitionEnd);
6221
+ checkTargetForNewValues(this, target, origin);
6222
+ const parsed = parseDomVariant(this, target, origin, transitionEnd);
6336
6223
  transitionEnd = parsed.transitionEnd;
6337
6224
  target = parsed.target;
6338
6225
  }
@@ -6341,44 +6228,70 @@
6341
6228
  transitionEnd,
6342
6229
  ...target,
6343
6230
  };
6344
- },
6345
- scrapeMotionValuesFromProps: scrapeMotionValuesFromProps$1,
6346
- build(element, renderState, latestValues, options, props) {
6347
- if (element.isVisible !== undefined) {
6348
- renderState.style.visibility = element.isVisible
6349
- ? "visible"
6350
- : "hidden";
6231
+ }
6232
+ }
6233
+
6234
+ function getComputedStyle$1(element) {
6235
+ return window.getComputedStyle(element);
6236
+ }
6237
+ class HTMLVisualElement extends DOMVisualElement {
6238
+ readValueFromInstance(instance, key) {
6239
+ if (transformProps.has(key)) {
6240
+ const defaultType = getDefaultValueType(key);
6241
+ return defaultType ? defaultType.default || 0 : 0;
6242
+ }
6243
+ else {
6244
+ const computedStyle = getComputedStyle$1(instance);
6245
+ const value = (isCSSVariable$1(key)
6246
+ ? computedStyle.getPropertyValue(key)
6247
+ : computedStyle[key]) || 0;
6248
+ return typeof value === "string" ? value.trim() : value;
6351
6249
  }
6250
+ }
6251
+ measureInstanceViewportBox(instance, { transformPagePoint }) {
6252
+ return measureViewportBox(instance, transformPagePoint);
6253
+ }
6254
+ build(renderState, latestValues, options, props) {
6352
6255
  buildHTMLStyles(renderState, latestValues, options, props.transformTemplate);
6353
- },
6354
- render: renderHTML,
6355
- };
6356
- const htmlVisualElement = visualElement(htmlConfig);
6256
+ }
6257
+ scrapeMotionValuesFromProps(props) {
6258
+ return scrapeMotionValuesFromProps$1(props);
6259
+ }
6260
+ renderInstance(instance, renderState, styleProp, projection) {
6261
+ renderHTML(instance, renderState, styleProp, projection);
6262
+ }
6263
+ }
6357
6264
 
6358
- const svgVisualElement = visualElement({
6359
- ...htmlConfig,
6360
- getBaseTarget(props, key) {
6265
+ class SVGVisualElement extends DOMVisualElement {
6266
+ getBaseTargetFromProps(props, key) {
6361
6267
  return props[key];
6362
- },
6363
- readValueFromInstance(domElement, key) {
6268
+ }
6269
+ readValueFromInstance(instance, key) {
6364
6270
  var _a;
6365
6271
  if (transformProps.has(key)) {
6366
6272
  return ((_a = getDefaultValueType(key)) === null || _a === void 0 ? void 0 : _a.default) || 0;
6367
6273
  }
6368
6274
  key = !camelCaseAttributes.has(key) ? camelToDash(key) : key;
6369
- return domElement.getAttribute(key);
6370
- },
6371
- scrapeMotionValuesFromProps,
6372
- build(_element, renderState, latestValues, options, props) {
6275
+ return instance.getAttribute(key);
6276
+ }
6277
+ measureInstanceViewportBox() {
6278
+ return createBox();
6279
+ }
6280
+ scrapeMotionValuesFromProps(props) {
6281
+ return scrapeMotionValuesFromProps(props);
6282
+ }
6283
+ build(renderState, latestValues, options, props) {
6373
6284
  buildSVGAttrs(renderState, latestValues, options, props.transformTemplate);
6374
- },
6375
- render: renderSVG,
6376
- });
6285
+ }
6286
+ renderInstance(instance, renderState, styleProp, projection) {
6287
+ renderSVG(instance, renderState, styleProp, projection);
6288
+ }
6289
+ }
6377
6290
 
6378
6291
  const createDomVisualElement = (Component, options) => {
6379
6292
  return isSVGComponent(Component)
6380
- ? svgVisualElement(options, { enableHardwareAcceleration: false })
6381
- : htmlVisualElement(options, { enableHardwareAcceleration: true });
6293
+ ? new SVGVisualElement(options, { enableHardwareAcceleration: false })
6294
+ : new HTMLVisualElement(options, { enableHardwareAcceleration: true });
6382
6295
  };
6383
6296
 
6384
6297
  function pixelsToPercent(pixels, axis) {
@@ -6976,7 +6889,7 @@
6976
6889
  const animationTarget = 1000;
6977
6890
  function createProjectionNode({ attachResizeListener, defaultParent, measureScroll, checkIsScrollRoot, resetTransform, }) {
6978
6891
  return class ProjectionNode {
6979
- constructor(id, latestValues = {}, parent = defaultParent === null || defaultParent === void 0 ? void 0 : defaultParent()) {
6892
+ constructor(elementId, latestValues = {}, parent = defaultParent === null || defaultParent === void 0 ? void 0 : defaultParent()) {
6980
6893
  /**
6981
6894
  * A Set containing all this component's children. This is used to iterate
6982
6895
  * through the children.
@@ -7029,7 +6942,7 @@
7029
6942
  /**
7030
6943
  * An object representing the calculated contextual/accumulated/tree scale.
7031
6944
  * This will be used to scale calculcated projection transforms, as these are
7032
- * calculated in screen-space but need to be scaled for elements to actually
6945
+ * calculated in screen-space but need to be scaled for elements to layoutly
7033
6946
  * make it to their calculated destinations.
7034
6947
  *
7035
6948
  * TODO: Lazy-init
@@ -7059,13 +6972,13 @@
7059
6972
  */
7060
6973
  // TODO Only running on root node
7061
6974
  this.sharedNodes = new Map();
7062
- this.id = id;
6975
+ this.elementId = elementId;
7063
6976
  this.latestValues = latestValues;
7064
6977
  this.root = parent ? parent.root || parent : this;
7065
6978
  this.path = parent ? [...parent.path, parent] : [];
7066
6979
  this.parent = parent;
7067
6980
  this.depth = parent ? parent.depth + 1 : 0;
7068
- id && this.root.registerPotentialNode(id, this);
6981
+ elementId && this.root.registerPotentialNode(elementId, this);
7069
6982
  for (let i = 0; i < this.path.length; i++) {
7070
6983
  this.path[i].shouldResetTransform = true;
7071
6984
  }
@@ -7099,12 +7012,12 @@
7099
7012
  instance instanceof SVGElement && instance.tagName !== "svg";
7100
7013
  this.instance = instance;
7101
7014
  const { layoutId, layout, visualElement } = this.options;
7102
- if (visualElement && !visualElement.getInstance()) {
7015
+ if (visualElement && !visualElement.current) {
7103
7016
  visualElement.mount(instance);
7104
7017
  }
7105
7018
  this.root.nodes.add(this);
7106
7019
  (_a = this.parent) === null || _a === void 0 ? void 0 : _a.children.add(this);
7107
- this.id && this.root.potentialNodes.delete(this.id);
7020
+ this.elementId && this.root.potentialNodes.delete(this.elementId);
7108
7021
  if (isLayoutDirty && (layout || layoutId)) {
7109
7022
  this.isLayoutDirty = true;
7110
7023
  }
@@ -7320,14 +7233,7 @@
7320
7233
  updateSnapshot() {
7321
7234
  if (this.snapshot || !this.instance)
7322
7235
  return;
7323
- const measured = this.measure();
7324
- const layout = this.removeTransform(this.removeElementScroll(measured));
7325
- roundBox(layout);
7326
- this.snapshot = {
7327
- measured,
7328
- layout,
7329
- latestValues: {},
7330
- };
7236
+ this.snapshot = this.measure();
7331
7237
  }
7332
7238
  updateLayout() {
7333
7239
  var _a;
@@ -7352,18 +7258,13 @@
7352
7258
  node.updateScroll();
7353
7259
  }
7354
7260
  }
7355
- const measured = this.measure();
7356
- roundBox(measured);
7357
7261
  const prevLayout = this.layout;
7358
- this.layout = {
7359
- measured,
7360
- actual: this.removeElementScroll(measured),
7361
- };
7262
+ this.layout = this.measure(false);
7362
7263
  this.layoutCorrected = createBox();
7363
7264
  this.isLayoutDirty = false;
7364
7265
  this.projectionDelta = undefined;
7365
- this.notifyListeners("measure", this.layout.actual);
7366
- (_a = this.options.visualElement) === null || _a === void 0 ? void 0 : _a.notifyLayoutMeasure(this.layout.actual, prevLayout === null || prevLayout === void 0 ? void 0 : prevLayout.actual);
7266
+ this.notifyListeners("measure", this.layout.layoutBox);
7267
+ (_a = this.options.visualElement) === null || _a === void 0 ? void 0 : _a.notify("LayoutMeasure", this.layout.layoutBox, prevLayout === null || prevLayout === void 0 ? void 0 : prevLayout.layoutBox);
7367
7268
  }
7368
7269
  updateScroll() {
7369
7270
  if (this.options.layoutScroll && this.instance) {
@@ -7389,7 +7290,25 @@
7389
7290
  this.scheduleRender();
7390
7291
  }
7391
7292
  }
7392
- measure() {
7293
+ measure(removeTransform = true) {
7294
+ const pageBox = this.measurePageBox();
7295
+ let layoutBox = this.removeElementScroll(pageBox);
7296
+ /**
7297
+ * Measurements taken during the pre-render stage
7298
+ * still have transforms applied so we remove them
7299
+ * via calculation.
7300
+ */
7301
+ if (removeTransform) {
7302
+ layoutBox = this.removeTransform(layoutBox);
7303
+ }
7304
+ roundBox(layoutBox);
7305
+ return {
7306
+ measuredBox: pageBox,
7307
+ layoutBox,
7308
+ latestValues: {},
7309
+ };
7310
+ }
7311
+ measurePageBox() {
7393
7312
  const { visualElement } = this.options;
7394
7313
  if (!visualElement)
7395
7314
  return createBox();
@@ -7470,9 +7389,9 @@
7470
7389
  continue;
7471
7390
  hasScale(node.latestValues) && node.updateSnapshot();
7472
7391
  const sourceBox = createBox();
7473
- const nodeBox = node.measure();
7392
+ const nodeBox = node.measurePageBox();
7474
7393
  copyBoxInto(sourceBox, nodeBox);
7475
- removeBoxTransforms(boxWithoutTransform, node.latestValues, (_a = node.snapshot) === null || _a === void 0 ? void 0 : _a.layout, sourceBox);
7394
+ removeBoxTransforms(boxWithoutTransform, node.latestValues, (_a = node.snapshot) === null || _a === void 0 ? void 0 : _a.layoutBox, sourceBox);
7476
7395
  }
7477
7396
  if (hasTransform(this.latestValues)) {
7478
7397
  removeBoxTransforms(boxWithoutTransform, this.latestValues);
@@ -7521,13 +7440,17 @@
7521
7440
  // TODO If this is unsuccessful this currently happens every frame
7522
7441
  if (!this.targetDelta && !this.relativeTarget) {
7523
7442
  // TODO: This is a semi-repetition of further down this function, make DRY
7524
- this.relativeParent = this.getClosestProjectingParent();
7525
- if (this.relativeParent && this.relativeParent.layout) {
7443
+ const relativeParent = this.getClosestProjectingParent();
7444
+ if (relativeParent && relativeParent.layout) {
7445
+ this.relativeParent = relativeParent;
7526
7446
  this.relativeTarget = createBox();
7527
7447
  this.relativeTargetOrigin = createBox();
7528
- calcRelativePosition(this.relativeTargetOrigin, this.layout.actual, this.relativeParent.layout.actual);
7448
+ calcRelativePosition(this.relativeTargetOrigin, this.layout.layoutBox, relativeParent.layout.layoutBox);
7529
7449
  copyBoxInto(this.relativeTarget, this.relativeTargetOrigin);
7530
7450
  }
7451
+ else {
7452
+ this.relativeParent = this.relativeTarget = undefined;
7453
+ }
7531
7454
  }
7532
7455
  /**
7533
7456
  * If we have no relative target or no target delta our target isn't valid
@@ -7556,10 +7479,10 @@
7556
7479
  else if (this.targetDelta) {
7557
7480
  if (Boolean(this.resumingFrom)) {
7558
7481
  // TODO: This is creating a new object every frame
7559
- this.target = this.applyTransform(this.layout.actual);
7482
+ this.target = this.applyTransform(this.layout.layoutBox);
7560
7483
  }
7561
7484
  else {
7562
- copyBoxInto(this.target, this.layout.actual);
7485
+ copyBoxInto(this.target, this.layout.layoutBox);
7563
7486
  }
7564
7487
  applyBoxDelta(this.target, this.targetDelta);
7565
7488
  }
@@ -7567,24 +7490,28 @@
7567
7490
  /**
7568
7491
  * If no target, use own layout as target
7569
7492
  */
7570
- copyBoxInto(this.target, this.layout.actual);
7493
+ copyBoxInto(this.target, this.layout.layoutBox);
7571
7494
  }
7572
7495
  /**
7573
7496
  * If we've been told to attempt to resolve a relative target, do so.
7574
7497
  */
7575
7498
  if (this.attemptToResolveRelativeTarget) {
7576
7499
  this.attemptToResolveRelativeTarget = false;
7577
- this.relativeParent = this.getClosestProjectingParent();
7578
- if (this.relativeParent &&
7579
- Boolean(this.relativeParent.resumingFrom) ===
7500
+ const relativeParent = this.getClosestProjectingParent();
7501
+ if (relativeParent &&
7502
+ Boolean(relativeParent.resumingFrom) ===
7580
7503
  Boolean(this.resumingFrom) &&
7581
- !this.relativeParent.options.layoutScroll &&
7582
- this.relativeParent.target) {
7504
+ !relativeParent.options.layoutScroll &&
7505
+ relativeParent.target) {
7506
+ this.relativeParent = relativeParent;
7583
7507
  this.relativeTarget = createBox();
7584
7508
  this.relativeTargetOrigin = createBox();
7585
- calcRelativePosition(this.relativeTargetOrigin, this.target, this.relativeParent.target);
7509
+ calcRelativePosition(this.relativeTargetOrigin, this.target, relativeParent.target);
7586
7510
  copyBoxInto(this.relativeTarget, this.relativeTargetOrigin);
7587
7511
  }
7512
+ else {
7513
+ this.relativeParent = this.relativeTarget = undefined;
7514
+ }
7588
7515
  }
7589
7516
  }
7590
7517
  getClosestProjectingParent() {
@@ -7620,7 +7547,7 @@
7620
7547
  * Reset the corrected box with the latest values from box, as we're then going
7621
7548
  * to perform mutative operations on it.
7622
7549
  */
7623
- copyBoxInto(this.layoutCorrected, this.layout.actual);
7550
+ copyBoxInto(this.layoutCorrected, this.layout.layoutBox);
7624
7551
  /**
7625
7552
  * Apply all the parent deltas to this box to produce the corrected box. This
7626
7553
  * is the layout box, as it will appear on screen as a result of the transforms of its parents.
@@ -7697,7 +7624,7 @@
7697
7624
  this.relativeTargetOrigin &&
7698
7625
  this.layout &&
7699
7626
  ((_a = this.relativeParent) === null || _a === void 0 ? void 0 : _a.layout)) {
7700
- calcRelativePosition(relativeLayout, this.layout.actual, this.relativeParent.layout.actual);
7627
+ calcRelativePosition(relativeLayout, this.layout.layoutBox, this.relativeParent.layout.layoutBox);
7701
7628
  mixBox(this.relativeTarget, this.relativeTargetOrigin, relativeLayout, progress);
7702
7629
  }
7703
7630
  if (isSharedLayoutAnimation) {
@@ -7781,12 +7708,12 @@
7781
7708
  if (this !== lead &&
7782
7709
  this.layout &&
7783
7710
  layout &&
7784
- shouldAnimatePositionOnly(this.options.animationType, this.layout.actual, layout.actual)) {
7711
+ shouldAnimatePositionOnly(this.options.animationType, this.layout.layoutBox, layout.layoutBox)) {
7785
7712
  target = this.target || createBox();
7786
- const xLength = calcLength(this.layout.actual.x);
7713
+ const xLength = calcLength(this.layout.layoutBox.x);
7787
7714
  target.x.min = lead.target.x.min;
7788
7715
  target.x.max = target.x.min + xLength;
7789
- const yLength = calcLength(this.layout.actual.y);
7716
+ const yLength = calcLength(this.layout.layoutBox.y);
7790
7717
  target.y.min = lead.target.y.min;
7791
7718
  target.y.max = target.y.min + yLength;
7792
7719
  }
@@ -7800,7 +7727,7 @@
7800
7727
  /**
7801
7728
  * Update the delta between the corrected box and the final target box, after
7802
7729
  * user-set transforms are applied to it. This will be used by the renderer to
7803
- * create a transform style that will reproject the element from its actual layout
7730
+ * create a transform style that will reproject the element from its layout layout
7804
7731
  * into the desired bounding box.
7805
7732
  */
7806
7733
  calcBoxDelta(this.projectionDeltaWithTransform, this.layoutCorrected, targetWithTransforms, latestValues);
@@ -7883,7 +7810,7 @@
7883
7810
  return;
7884
7811
  // Force a render of this element to apply the transform with all rotations
7885
7812
  // set to 0.
7886
- visualElement === null || visualElement === void 0 ? void 0 : visualElement.syncRender();
7813
+ visualElement === null || visualElement === void 0 ? void 0 : visualElement.render();
7887
7814
  // Put back all the values we reset
7888
7815
  for (const key in resetValues) {
7889
7816
  visualElement.setStaticValue(key, resetValues[key]);
@@ -7956,7 +7883,7 @@
7956
7883
  }
7957
7884
  else {
7958
7885
  /**
7959
- * Or we're not animating at all, set the lead component to its actual
7886
+ * Or we're not animating at all, set the lead component to its layout
7960
7887
  * opacity and other components to hidden.
7961
7888
  */
7962
7889
  styles.opacity =
@@ -8020,53 +7947,53 @@
8020
7947
  node.layout &&
8021
7948
  snapshot &&
8022
7949
  node.hasListeners("didUpdate")) {
8023
- const { actual: layout, measured: measuredLayout } = node.layout;
7950
+ const { layoutBox: layout, measuredBox: measuredLayout } = node.layout;
8024
7951
  const { animationType } = node.options;
8025
7952
  // TODO Maybe we want to also resize the layout snapshot so we don't trigger
8026
7953
  // animations for instance if layout="size" and an element has only changed position
8027
7954
  if (animationType === "size") {
8028
7955
  eachAxis((axis) => {
8029
7956
  const axisSnapshot = snapshot.isShared
8030
- ? snapshot.measured[axis]
8031
- : snapshot.layout[axis];
7957
+ ? snapshot.measuredBox[axis]
7958
+ : snapshot.layoutBox[axis];
8032
7959
  const length = calcLength(axisSnapshot);
8033
7960
  axisSnapshot.min = layout[axis].min;
8034
7961
  axisSnapshot.max = axisSnapshot.min + length;
8035
7962
  });
8036
7963
  }
8037
- else if (shouldAnimatePositionOnly(animationType, snapshot.layout, layout)) {
7964
+ else if (shouldAnimatePositionOnly(animationType, snapshot.layoutBox, layout)) {
8038
7965
  eachAxis((axis) => {
8039
7966
  const axisSnapshot = snapshot.isShared
8040
- ? snapshot.measured[axis]
8041
- : snapshot.layout[axis];
7967
+ ? snapshot.measuredBox[axis]
7968
+ : snapshot.layoutBox[axis];
8042
7969
  const length = calcLength(layout[axis]);
8043
7970
  axisSnapshot.max = axisSnapshot.min + length;
8044
7971
  });
8045
7972
  }
8046
7973
  const layoutDelta = createDelta();
8047
- calcBoxDelta(layoutDelta, layout, snapshot.layout);
7974
+ calcBoxDelta(layoutDelta, layout, snapshot.layoutBox);
8048
7975
  const visualDelta = createDelta();
8049
7976
  if (snapshot.isShared) {
8050
- calcBoxDelta(visualDelta, node.applyTransform(measuredLayout, true), snapshot.measured);
7977
+ calcBoxDelta(visualDelta, node.applyTransform(measuredLayout, true), snapshot.measuredBox);
8051
7978
  }
8052
7979
  else {
8053
- calcBoxDelta(visualDelta, layout, snapshot.layout);
7980
+ calcBoxDelta(visualDelta, layout, snapshot.layoutBox);
8054
7981
  }
8055
7982
  const hasLayoutChanged = !isDeltaZero(layoutDelta);
8056
7983
  let hasRelativeTargetChanged = false;
8057
7984
  if (!node.resumeFrom) {
8058
- node.relativeParent = node.getClosestProjectingParent();
7985
+ const relativeParent = node.getClosestProjectingParent();
8059
7986
  /**
8060
7987
  * If the relativeParent is itself resuming from a different element then
8061
7988
  * the relative snapshot is not relavent
8062
7989
  */
8063
- if (node.relativeParent && !node.relativeParent.resumeFrom) {
8064
- const { snapshot: parentSnapshot, layout: parentLayout } = node.relativeParent;
7990
+ if (relativeParent && !relativeParent.resumeFrom) {
7991
+ const { snapshot: parentSnapshot, layout: parentLayout } = relativeParent;
8065
7992
  if (parentSnapshot && parentLayout) {
8066
7993
  const relativeSnapshot = createBox();
8067
- calcRelativePosition(relativeSnapshot, snapshot.layout, parentSnapshot.layout);
7994
+ calcRelativePosition(relativeSnapshot, snapshot.layoutBox, parentSnapshot.layoutBox);
8068
7995
  const relativeLayout = createBox();
8069
- calcRelativePosition(relativeLayout, layout, parentLayout.actual);
7996
+ calcRelativePosition(relativeLayout, layout, parentLayout.layoutBox);
8070
7997
  if (!boxEquals(relativeSnapshot, relativeLayout)) {
8071
7998
  hasRelativeTargetChanged = true;
8072
7999
  }
@@ -8101,7 +8028,7 @@
8101
8028
  function resetTransformStyle(node) {
8102
8029
  const { visualElement } = node.options;
8103
8030
  if (visualElement === null || visualElement === void 0 ? void 0 : visualElement.getProps().onBeforeLayoutMeasure) {
8104
- visualElement.notifyBeforeLayoutMeasure();
8031
+ visualElement.notify("BeforeLayoutMeasure");
8105
8032
  }
8106
8033
  node.resetTransform();
8107
8034
  }
@@ -10082,23 +10009,33 @@
10082
10009
  }
10083
10010
 
10084
10011
  const createObject = () => ({});
10085
- const stateVisualElement = visualElement({
10086
- build() { },
10087
- measureViewportBox: createBox,
10088
- resetTransform() { },
10089
- restoreTransform() { },
10090
- removeValueFromRenderState() { },
10091
- render() { },
10092
- scrapeMotionValuesFromProps: createObject,
10012
+ class StateVisualElement extends VisualElement {
10013
+ build() { }
10014
+ measureInstanceViewportBox() {
10015
+ return createBox();
10016
+ }
10017
+ resetTransform() { }
10018
+ restoreTransform() { }
10019
+ removeValueFromRenderState() { }
10020
+ renderInstance() { }
10021
+ scrapeMotionValuesFromProps() {
10022
+ return createObject();
10023
+ }
10024
+ getBaseTargetFromProps() {
10025
+ return undefined;
10026
+ }
10093
10027
  readValueFromInstance(_state, key, options) {
10094
10028
  return options.initialState[key] || 0;
10095
- },
10096
- makeTargetAnimatable(element, { transition, transitionEnd, ...target }) {
10097
- const origin = getOrigin(target, transition || {}, element);
10098
- checkTargetForNewValues(element, target, origin);
10029
+ }
10030
+ sortInstanceNodePosition() {
10031
+ return 0;
10032
+ }
10033
+ makeTargetAnimatableFromInstance({ transition, transitionEnd, ...target }) {
10034
+ const origin = getOrigin(target, transition || {}, this);
10035
+ checkTargetForNewValues(this, target, origin);
10099
10036
  return { transition, transitionEnd, ...target };
10100
- },
10101
- });
10037
+ }
10038
+ }
10102
10039
  const useVisualState = makeUseVisualState({
10103
10040
  scrapeMotionValuesFromProps: createObject,
10104
10041
  createRenderState: createObject,
@@ -10110,10 +10047,12 @@
10110
10047
  function useAnimatedState(initialState) {
10111
10048
  const [animationState, setAnimationState] = React.useState(initialState);
10112
10049
  const visualState = useVisualState({}, false);
10113
- const element = useConstant(() => stateVisualElement({ props: {}, visualState }, { initialState }));
10050
+ const element = useConstant(() => {
10051
+ return new StateVisualElement({ props: {}, visualState }, { initialState });
10052
+ });
10114
10053
  React.useEffect(() => {
10115
10054
  element.mount({});
10116
- return element.unmount;
10055
+ return () => element.unmount();
10117
10056
  }, [element]);
10118
10057
  React.useEffect(() => {
10119
10058
  element.setProps({
@@ -10188,6 +10127,7 @@
10188
10127
  exports.PresenceContext = PresenceContext;
10189
10128
  exports.Reorder = Reorder;
10190
10129
  exports.SwitchLayoutGroupContext = SwitchLayoutGroupContext;
10130
+ exports.VisualElement = VisualElement;
10191
10131
  exports.addPointerEvent = addPointerEvent;
10192
10132
  exports.addScaleCorrector = addScaleCorrector;
10193
10133
  exports.animate = animate;
@@ -10246,7 +10186,6 @@
10246
10186
  exports.useViewportScroll = useViewportScroll;
10247
10187
  exports.useVisualElementContext = useVisualElementContext;
10248
10188
  exports.useWillChange = useWillChange;
10249
- exports.visualElement = visualElement;
10250
10189
  exports.wrapHandler = wrapHandler;
10251
10190
 
10252
10191
  Object.defineProperty(exports, '__esModule', { value: true });