framer-motion 6.2.0 → 6.2.5

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.
@@ -240,7 +240,9 @@
240
240
 
241
241
  // Does this device prefer reduced motion? Returns `null` server-side.
242
242
  var prefersReducedMotion = { current: null };
243
+ var hasDetected = false;
243
244
  function initPrefersReducedMotion() {
245
+ hasDetected = true;
244
246
  if (typeof window === "undefined")
245
247
  return;
246
248
  if (window.matchMedia) {
@@ -285,7 +287,7 @@
285
287
  /**
286
288
  * Lazy initialisation of prefersReducedMotion
287
289
  */
288
- !prefersReducedMotion && initPrefersReducedMotion();
290
+ !hasDetected && initPrefersReducedMotion();
289
291
  var _a = __read(React.useState(prefersReducedMotion.current), 1), shouldReduceMotion = _a[0];
290
292
  /**
291
293
  * TODO See if people miss automatically updating shouldReduceMotion setting
@@ -3128,9 +3130,6 @@
3128
3130
  };
3129
3131
  this.hasProjected = false;
3130
3132
  this.isVisible = true;
3131
- /**
3132
- * Animation
3133
- */
3134
3133
  this.animationProgress = 0;
3135
3134
  /**
3136
3135
  * Shared layout
@@ -3255,6 +3254,15 @@
3255
3254
  _this.startAnimation(animationOptions);
3256
3255
  }
3257
3256
  else {
3257
+ /**
3258
+ * If the layout hasn't changed and we have an animation that hasn't started yet,
3259
+ * finish it immediately. Otherwise it will be animating from a location
3260
+ * that was probably never commited to screen and look like a jumpy box.
3261
+ */
3262
+ if (!hasLayoutChanged &&
3263
+ _this.animationProgress === 0) {
3264
+ _this.finishAnimation();
3265
+ }
3258
3266
  _this.isLead() && ((_f = (_e = _this.options).onExitComplete) === null || _f === void 0 ? void 0 : _f.call(_e));
3259
3267
  }
3260
3268
  _this.targetLayout = newLayout;
@@ -3744,6 +3752,7 @@
3744
3752
  !isOnlyMember &&
3745
3753
  this.options.crossfade === true &&
3746
3754
  !this.path.some(hasOpacityCrossfade));
3755
+ this.animationProgress = 0;
3747
3756
  this.mixTargetDelta = function (latest) {
3748
3757
  var _a;
3749
3758
  var progress = latest / 1000;
@@ -3763,6 +3772,7 @@
3763
3772
  }
3764
3773
  _this.root.scheduleUpdateProjection();
3765
3774
  _this.scheduleRender();
3775
+ _this.animationProgress = progress;
3766
3776
  };
3767
3777
  this.mixTargetDelta(0);
3768
3778
  };
@@ -4128,6 +4138,10 @@
4128
4138
  node.clearMeasurements();
4129
4139
  }
4130
4140
  function resetTransformStyle(node) {
4141
+ var visualElement = node.options.visualElement;
4142
+ if (visualElement === null || visualElement === void 0 ? void 0 : visualElement.getProps().onBeforeLayoutMeasure) {
4143
+ visualElement.notifyBeforeLayoutMeasure();
4144
+ }
4131
4145
  node.resetTransform();
4132
4146
  }
4133
4147
  function finishAnimation(node) {
@@ -5568,6 +5582,12 @@
5568
5582
  hover: makeRenderlessComponent(useHoverGesture),
5569
5583
  };
5570
5584
 
5585
+ var counter = 0;
5586
+ var incrementId = function () { return counter++; };
5587
+ var useId = React__namespace.useId
5588
+ ? React__namespace.useId
5589
+ : function () { return useConstant(incrementId); };
5590
+
5571
5591
  /**
5572
5592
  * When a component is the child of `AnimatePresence`, it can use `usePresence`
5573
5593
  * to access information about whether it's still present in the React tree.
@@ -5598,8 +5618,8 @@
5598
5618
  var isPresent = context.isPresent, onExitComplete = context.onExitComplete, register = context.register;
5599
5619
  // It's safe to call the following hooks conditionally (after an early return) because the context will always
5600
5620
  // either be null or non-null for the lifespan of the component.
5601
- // Replace with useOpaqueId when released in React
5602
- var id = useUniqueId();
5621
+ // Replace with useId when released in React
5622
+ var id = useId();
5603
5623
  React.useEffect(function () { return register(id); }, []);
5604
5624
  var safeToRemove = function () { return onExitComplete === null || onExitComplete === void 0 ? void 0 : onExitComplete(id); };
5605
5625
  return !isPresent && onExitComplete ? [false, safeToRemove] : [true];
@@ -5630,9 +5650,6 @@
5630
5650
  function isPresent(context) {
5631
5651
  return context === null ? true : context.isPresent;
5632
5652
  }
5633
- var counter = 0;
5634
- var incrementId = function () { return counter++; };
5635
- var useUniqueId = function () { return useConstant(incrementId); };
5636
5653
 
5637
5654
  function shallowCompare(next, prev) {
5638
5655
  if (!Array.isArray(prev))
@@ -7134,7 +7151,7 @@
7134
7151
  for (var _i = 0; _i < arguments.length; _i++) {
7135
7152
  args[_i] = arguments[_i];
7136
7153
  }
7137
- manager.notify.apply(manager, __spreadArray([], __read(args), false));
7154
+ return manager.notify.apply(manager, __spreadArray([], __read(args), false));
7138
7155
  };
7139
7156
  });
7140
7157
  return lifecycles;
@@ -7337,6 +7354,7 @@
7337
7354
  if (isVariantNode && parent && !isControllingVariants) {
7338
7355
  removeFromVariantTree = parent === null || parent === void 0 ? void 0 : parent.addVariantChild(element);
7339
7356
  }
7357
+ values.forEach(function (value, key) { return bindToMotionValue(key, value); });
7340
7358
  parent === null || parent === void 0 ? void 0 : parent.children.add(element);
7341
7359
  element.setProps(props);
7342
7360
  },
@@ -8317,12 +8335,22 @@
8317
8335
  */
8318
8336
  var m = createMotionProxy(createDomMotionConfig);
8319
8337
 
8338
+ function useIsMounted() {
8339
+ var isMounted = React.useRef(false);
8340
+ React.useLayoutEffect(function () {
8341
+ isMounted.current = true;
8342
+ return function () {
8343
+ isMounted.current = false;
8344
+ };
8345
+ }, []);
8346
+ return isMounted;
8347
+ }
8348
+
8320
8349
  function useForceUpdate() {
8321
- var isUnmountingRef = React.useRef(false);
8350
+ var isMounted = useIsMounted();
8322
8351
  var _a = __read(React.useState(0), 2), forcedRenderCount = _a[0], setForcedRenderCount = _a[1];
8323
- useUnmountEffect(function () { return (isUnmountingRef.current = true); });
8324
8352
  var forceRender = React.useCallback(function () {
8325
- !isUnmountingRef.current && setForcedRenderCount(forcedRenderCount + 1);
8353
+ isMounted.current && setForcedRenderCount(forcedRenderCount + 1);
8326
8354
  }, [forcedRenderCount]);
8327
8355
  /**
8328
8356
  * Defer this to the end of the next animation frame in case there are multiple
@@ -8332,16 +8360,10 @@
8332
8360
  return [deferredForceRender, forcedRenderCount];
8333
8361
  }
8334
8362
 
8335
- var presenceId = 0;
8336
- function getPresenceId() {
8337
- var id = presenceId;
8338
- presenceId++;
8339
- return id;
8340
- }
8341
8363
  var PresenceChild = function (_a) {
8342
8364
  var children = _a.children, initial = _a.initial, isPresent = _a.isPresent, onExitComplete = _a.onExitComplete, custom = _a.custom, presenceAffectsLayout = _a.presenceAffectsLayout;
8343
8365
  var presenceChildren = useConstant(newChildrenMap);
8344
- var id = useConstant(getPresenceId);
8366
+ var id = useId();
8345
8367
  var context = React.useMemo(function () { return ({
8346
8368
  id: id,
8347
8369
  initial: initial,
@@ -8393,17 +8415,13 @@
8393
8415
  return new Map();
8394
8416
  }
8395
8417
 
8396
- function getChildKey(child) {
8397
- return child.key || "";
8398
- }
8418
+ var getChildKey = function (child) { return child.key || ""; };
8399
8419
  function updateChildLookup(children, allChildren) {
8400
8420
  var seenChildren = new Set() ;
8401
8421
  children.forEach(function (child) {
8402
8422
  var key = getChildKey(child);
8403
- if (seenChildren) {
8404
- if (seenChildren.has(key)) {
8405
- console.warn("Children of AnimatePresence require unique keys. \"".concat(key, "\" is a duplicate."));
8406
- }
8423
+ if (seenChildren && seenChildren.has(key)) {
8424
+ console.warn("Children of AnimatePresence require unique keys. \"".concat(key, "\" is a duplicate."));
8407
8425
  seenChildren.add(key);
8408
8426
  }
8409
8427
  allChildren.set(key, child);
@@ -8459,29 +8477,34 @@
8459
8477
  var forceRenderLayoutGroup = React.useContext(LayoutGroupContext).forceRender;
8460
8478
  if (forceRenderLayoutGroup)
8461
8479
  forceRender = forceRenderLayoutGroup;
8462
- var isInitialRender = React.useRef(true);
8463
- var isMounted = React.useRef(true);
8464
- React.useEffect(function () { return function () {
8465
- isMounted.current = false;
8466
- }; }, []);
8480
+ var isMounted = useIsMounted();
8467
8481
  // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key
8468
8482
  var filteredChildren = onlyElements(children);
8483
+ var childrenToRender = filteredChildren;
8484
+ var exiting = new Set();
8469
8485
  // Keep a living record of the children we're actually rendering so we
8470
8486
  // can diff to figure out which are entering and exiting
8471
- var presentChildren = React.useRef(filteredChildren);
8487
+ var presentChildren = React.useRef(childrenToRender);
8472
8488
  // A lookup table to quickly reference components by key
8473
8489
  var allChildren = React.useRef(new Map()).current;
8474
- // A living record of all currently exiting components.
8475
- var exiting = React.useRef(new Set()).current;
8476
- updateChildLookup(filteredChildren, allChildren);
8477
8490
  // If this is the initial component render, just deal with logic surrounding whether
8478
8491
  // we play onMount animations or not.
8479
- if (isInitialRender.current) {
8492
+ var isInitialRender = React.useRef(true);
8493
+ useIsomorphicLayoutEffect(function () {
8480
8494
  isInitialRender.current = false;
8481
- return (React__namespace.createElement(React__namespace.Fragment, null, filteredChildren.map(function (child) { return (React__namespace.createElement(PresenceChild, { key: getChildKey(child), isPresent: true, initial: initial ? undefined : false, presenceAffectsLayout: presenceAffectsLayout }, child)); })));
8495
+ updateChildLookup(filteredChildren, allChildren);
8496
+ presentChildren.current = childrenToRender;
8497
+ });
8498
+ useUnmountEffect(function () {
8499
+ isInitialRender.current = true;
8500
+ allChildren.clear();
8501
+ exiting.clear();
8502
+ });
8503
+ if (isInitialRender.current) {
8504
+ return (React__namespace.createElement(React__namespace.Fragment, null, childrenToRender.map(function (child) { return (React__namespace.createElement(PresenceChild, { key: getChildKey(child), isPresent: true, initial: initial ? undefined : false, presenceAffectsLayout: presenceAffectsLayout }, child)); })));
8482
8505
  }
8483
8506
  // If this is a subsequent render, deal with entering and exiting children
8484
- var childrenToRender = __spreadArray([], __read(filteredChildren), false);
8507
+ childrenToRender = __spreadArray([], __read(childrenToRender), false);
8485
8508
  // Diff the keys of the currently-present and target children to update our
8486
8509
  // exiting list.
8487
8510
  var presentKeys = presentChildren.current.map(getChildKey);
@@ -8493,10 +8516,6 @@
8493
8516
  if (targetKeys.indexOf(key) === -1) {
8494
8517
  exiting.add(key);
8495
8518
  }
8496
- else {
8497
- // In case this key has re-entered, remove from the exiting list
8498
- exiting.delete(key);
8499
- }
8500
8519
  }
8501
8520
  // If we currently have exiting children, and we're deferring rendering incoming children
8502
8521
  // until after all current children have exiting, empty the childrenToRender array
@@ -8522,9 +8541,8 @@
8522
8541
  // Defer re-rendering until all exiting children have indeed left
8523
8542
  if (!exiting.size) {
8524
8543
  presentChildren.current = filteredChildren;
8525
- if (isMounted.current === false) {
8544
+ if (isMounted.current === false)
8526
8545
  return;
8527
- }
8528
8546
  forceRender();
8529
8547
  onExitComplete && onExitComplete();
8530
8548
  }
@@ -8537,7 +8555,6 @@
8537
8555
  var key = child.key;
8538
8556
  return exiting.has(key) ? (child) : (React__namespace.createElement(PresenceChild, { key: getChildKey(child), isPresent: true, presenceAffectsLayout: presenceAffectsLayout }, child));
8539
8557
  });
8540
- presentChildren.current = childrenToRender;
8541
8558
  if (exitBeforeEnter &&
8542
8559
  childrenToRender.length > 1) {
8543
8560
  console.warn("You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.");
@@ -9507,13 +9524,15 @@
9507
9524
  });
9508
9525
  React.useEffect(function () {
9509
9526
  element.mount({});
9510
- return element.unmount();
9511
- }, []);
9527
+ return element.unmount;
9528
+ }, [element]);
9512
9529
  React.useEffect(function () {
9513
9530
  element.setProps({
9514
- onUpdate: function (v) { return setAnimationState(__assign({}, v)); },
9531
+ onUpdate: function (v) {
9532
+ setAnimationState(__assign({}, v));
9533
+ },
9515
9534
  });
9516
- });
9535
+ }, [setAnimationState, element]);
9517
9536
  var startAnimation = useConstant(function () { return function (animationDefinition) {
9518
9537
  return animateVisualElement(element, animationDefinition);
9519
9538
  }; });
@@ -9624,6 +9643,7 @@
9624
9643
  exports.useMotionValue = useMotionValue;
9625
9644
  exports.usePresence = usePresence;
9626
9645
  exports.useReducedMotion = useReducedMotion;
9646
+ exports.useReducedMotionConfig = useReducedMotionConfig;
9627
9647
  exports.useResetProjection = useResetProjection;
9628
9648
  exports.useSpring = useSpring;
9629
9649
  exports.useTime = useTime;