motion-dom 13.4.2 → 13.4.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.
package/dist/cjs/index.js CHANGED
@@ -812,7 +812,6 @@ const springDefaults = {
812
812
  stiffness: 100,
813
813
  damping: 10,
814
814
  mass: 1.0,
815
- velocity: 0.0,
816
815
  // Default duration/bounce-based options
817
816
  duration: 800, // in ms
818
817
  bounce: 0.3,
@@ -847,7 +846,12 @@ function approximateRoot(envelope, derivative, initialGuess) {
847
846
  * This is ported from the Framer implementation of duration-based spring resolution.
848
847
  */
849
848
  const safeMin = 0.001;
850
- function findSpring({ duration = springDefaults.duration, bounce = springDefaults.bounce, velocity = springDefaults.velocity, mass = springDefaults.mass, }) {
849
+ /**
850
+ * Assumes zero initial velocity and the default mass: time-defined springs
851
+ * ignore inherited velocity and only resolve without valid physics. Returns
852
+ * NaN physics when the root search doesn't converge.
853
+ */
854
+ function findSpring({ duration = springDefaults.duration, bounce = springDefaults.bounce, }) {
851
855
  let envelope;
852
856
  let derivative;
853
857
  motionUtils.warning(duration <= motionUtils.secondsToMilliseconds(springDefaults.maxDuration), "Spring duration must be 10 seconds or less", "spring-duration-limit");
@@ -864,15 +868,13 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
864
868
  envelope = (undampedFreq) => {
865
869
  const exponentialDecay = undampedFreq * dampingRatio;
866
870
  const delta = exponentialDecay * duration;
867
- const a = exponentialDecay - velocity;
868
871
  const b = calcAngularFreq(undampedFreq, dampingRatio);
869
872
  const c = Math.exp(-delta);
870
- return safeMin - (a / b) * c;
873
+ return safeMin - (exponentialDecay / b) * c;
871
874
  };
872
875
  derivative = (undampedFreq) => {
873
876
  const exponentialDecay = undampedFreq * dampingRatio;
874
877
  const delta = exponentialDecay * duration;
875
- const d = delta * velocity + velocity;
876
878
  const e = dampingRatio *
877
879
  dampingRatio *
878
880
  undampedFreq *
@@ -881,7 +883,7 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
881
883
  const f = Math.exp(-delta);
882
884
  const g = calcAngularFreq(undampedFreq * undampedFreq, dampingRatio);
883
885
  const factor = -envelope(undampedFreq) + safeMin > 0 ? -1 : 1;
884
- return (factor * ((d - e) * f)) / g;
886
+ return (factor * -e * f) / g;
885
887
  };
886
888
  }
887
889
  else {
@@ -890,79 +892,89 @@ function findSpring({ duration = springDefaults.duration, bounce = springDefault
890
892
  */
891
893
  envelope = (undampedFreq) => {
892
894
  const a = Math.exp(-undampedFreq * duration);
893
- const b = (undampedFreq - velocity) * duration + 1;
895
+ const b = undampedFreq * duration + 1;
894
896
  return -safeMin + a * b;
895
897
  };
896
898
  derivative = (undampedFreq) => {
897
899
  const a = Math.exp(-undampedFreq * duration);
898
- const b = (velocity - undampedFreq) * (duration * duration);
900
+ const b = -undampedFreq * (duration * duration);
899
901
  return a * b;
900
902
  };
901
903
  }
902
904
  const initialGuess = 5 / duration;
903
905
  const undampedFreq = approximateRoot(envelope, derivative, initialGuess);
904
- duration = motionUtils.secondsToMilliseconds(duration);
905
- if (isNaN(undampedFreq)) {
906
- return {
907
- stiffness: springDefaults.stiffness,
908
- damping: springDefaults.damping,
909
- duration,
910
- };
911
- }
912
- else {
913
- const stiffness = undampedFreq * undampedFreq * mass;
914
- return {
915
- stiffness,
916
- damping: dampingRatio * 2 * Math.sqrt(mass * stiffness),
917
- duration,
918
- };
919
- }
906
+ const stiffness = undampedFreq * undampedFreq;
907
+ return {
908
+ stiffness,
909
+ damping: dampingRatio * 2 * Math.sqrt(stiffness),
910
+ duration: motionUtils.secondsToMilliseconds(duration),
911
+ };
920
912
  }
921
- const durationKeys = ["duration", "bounce"];
922
- const physicsKeys = ["stiffness", "damping", "mass"];
923
- function isSpringType(options, keys) {
924
- return keys.some((key) => options[key] !== undefined);
913
+ /**
914
+ * Spring physics must be finite. stiffness and mass are also divisors so must
915
+ * be positive, whereas a damping of 0 is a valid, perpetually oscillating
916
+ * spring. Relational rather than Number.isFinite so numeric strings still
917
+ * coerce.
918
+ */
919
+ const isValidPhysics = (value, canBeZero) => (canBeZero ? value >= 0 : value > 0) && value < Infinity;
920
+ /**
921
+ * Returns value if it's usable spring physics, otherwise undefined so callers
922
+ * fall back to the default. An explicit `undefined`, e.g. from a forwarded
923
+ * optional prop, must fall back too. Invalid physics would otherwise resolve
924
+ * to NaN spring values that never report done.
925
+ */
926
+ function resolvePhysics(value, canBeZero) {
927
+ if (isValidPhysics(value, canBeZero))
928
+ return value;
929
+ if (process.env.NODE_ENV !== "production") {
930
+ motionUtils.warning(value === undefined, "Spring stiffness and mass must be positive, damping 0 or greater", "spring-invalid-physics");
931
+ }
932
+ return undefined;
925
933
  }
926
934
  function getSpringOptions(options) {
927
- let springOptions = {
928
- velocity: springDefaults.velocity,
929
- stiffness: springDefaults.stiffness,
930
- damping: springDefaults.damping,
931
- mass: springDefaults.mass,
932
- isResolvedFromDuration: false,
935
+ /**
936
+ * Resolve physics before choosing between physics- and duration-based
937
+ * resolution, so an invalid stiffness doesn't also silently discard a
938
+ * valid duration/bounce.
939
+ */
940
+ const validStiffness = resolvePhysics(options.stiffness);
941
+ const validDamping = resolvePhysics(options.damping, true);
942
+ const validMass = resolvePhysics(options.mass);
943
+ const springOptions = {
933
944
  ...options,
945
+ stiffness: validStiffness ?? springDefaults.stiffness,
946
+ damping: validDamping ?? springDefaults.damping,
947
+ mass: validMass ?? springDefaults.mass,
948
+ isResolvedFromDuration: false,
949
+ // stiffness/damping/mass overrides duration/bounce
950
+ isTimeDefined: (validStiffness ?? validDamping ?? validMass) === undefined &&
951
+ (options.duration !== undefined || options.bounce !== undefined),
934
952
  };
935
- // stiffness/damping/mass overrides duration/bounce
936
- if (!isSpringType(options, physicsKeys) &&
937
- isSpringType(options, durationKeys)) {
938
- // Time-defined springs should ignore inherited velocity.
939
- // Velocity from interrupted animations can cause findSpring()
940
- // to compute wildly different spring parameters, leading to
941
- // massive oscillation on small-range animations.
942
- springOptions.velocity = 0;
953
+ if (springOptions.isTimeDefined) {
943
954
  if (options.visualDuration) {
944
- const visualDuration = options.visualDuration;
945
- const root = (2 * Math.PI) / (visualDuration * 1.2);
946
- const stiffness = root * root;
947
- const damping = 2 *
948
- motionUtils.clamp(0.05, 1, 1 - (options.bounce || 0)) *
949
- Math.sqrt(stiffness);
950
- springOptions = {
951
- ...springOptions,
952
- mass: springDefaults.mass,
953
- stiffness,
954
- damping,
955
- };
955
+ const root = (2 * Math.PI) / (options.visualDuration * 1.2);
956
+ springOptions.stiffness = root * root;
957
+ springOptions.damping =
958
+ 2 *
959
+ motionUtils.clamp(0.05, 1, 1 - (options.bounce || 0)) *
960
+ Math.sqrt(springOptions.stiffness);
956
961
  }
957
962
  else {
958
- const derived = findSpring({ ...options, velocity: 0 });
959
- springOptions = {
960
- ...springOptions,
961
- ...derived,
962
- mass: springDefaults.mass,
963
- };
963
+ Object.assign(springOptions, findSpring(springOptions));
964
964
  springOptions.isResolvedFromDuration = true;
965
965
  }
966
+ /**
967
+ * Time options can degenerate: a NaN bounce gives a NaN damping, an
968
+ * infinite visualDuration a 0 stiffness, and findSpring NaN for both
969
+ * when it doesn't converge. Replace the two together, so the
970
+ * relationship duration resolution establishes between them is never
971
+ * left half-overwritten.
972
+ */
973
+ if (!isValidPhysics(springOptions.stiffness) ||
974
+ !isValidPhysics(springOptions.damping, true)) {
975
+ springOptions.stiffness = springDefaults.stiffness;
976
+ springOptions.damping = springDefaults.damping;
977
+ }
966
978
  }
967
979
  return springOptions;
968
980
  }
@@ -981,10 +993,13 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
981
993
  * to reduce GC during animation.
982
994
  */
983
995
  const state = { done: false, value: origin };
984
- const { stiffness, damping, mass, duration, velocity, isResolvedFromDuration, } = getSpringOptions({
985
- ...options,
986
- velocity: -motionUtils.millisecondsToSeconds(options.velocity || 0),
987
- });
996
+ const { stiffness, damping, mass, duration, isResolvedFromDuration, isTimeDefined, } = getSpringOptions({ ...options });
997
+ /**
998
+ * Time-defined springs ignore inherited velocity. Velocity from
999
+ * interrupted animations causes massive oscillation on small-range
1000
+ * animations.
1001
+ */
1002
+ const inheritVelocity = (velocity) => isTimeDefined ? 0 : -motionUtils.millisecondsToSeconds(velocity);
988
1003
  const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
989
1004
  const undampedAngularFreq = motionUtils.millisecondsToSeconds(Math.sqrt(stiffness / mass));
990
1005
  const decay = dampingRatio * undampedAngularFreq;
@@ -998,7 +1013,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
998
1013
  const s = {
999
1014
  target,
1000
1015
  delta: target - origin,
1001
- velocity: velocity || 0.0,
1016
+ velocity: inheritVelocity(options.velocity || 0) || 0,
1002
1017
  restSpeed: 0,
1003
1018
  restDelta: 0,
1004
1019
  };
@@ -1006,7 +1021,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
1006
1021
  * If we're working on a granular scale, use smaller defaults for determining
1007
1022
  * when the spring is finished.
1008
1023
  *
1009
- * These defaults have been selected emprically based on what strikes a good
1024
+ * These defaults have been selected empirically based on what strikes a good
1010
1025
  * ratio between feeling good and finishing as soon as changes are imperceptible.
1011
1026
  */
1012
1027
  const setRestThresholds = () => {
@@ -1104,11 +1119,6 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
1104
1119
  };
1105
1120
  }
1106
1121
  update();
1107
- /**
1108
- * Time-defined springs ignore inherited velocity, see getSpringOptions.
1109
- */
1110
- const ignoreVelocity = !isSpringType(options, physicsKeys) &&
1111
- isSpringType(options, durationKeys);
1112
1122
  const calculatedDuration = isResolvedFromDuration ? duration || null : null;
1113
1123
  const generator = {
1114
1124
  calculatedDuration,
@@ -1119,9 +1129,7 @@ function spring(optionsOrVisualDuration = springDefaults.visualDuration, bounce
1119
1129
  retarget: (keyframes, newVelocity) => {
1120
1130
  s.target = keyframes[keyframes.length - 1];
1121
1131
  s.delta = s.target - keyframes[0];
1122
- s.velocity = ignoreVelocity
1123
- ? 0
1124
- : -motionUtils.millisecondsToSeconds(newVelocity);
1132
+ s.velocity = inheritVelocity(newVelocity);
1125
1133
  // Default thresholds depend on the scale of the new delta
1126
1134
  if (!(options.restSpeed && options.restDelta))
1127
1135
  setRestThresholds();
@@ -3140,7 +3148,7 @@ class AsyncMotionValueAnimation extends WithPromise {
3140
3148
 
3141
3149
  class GroupAnimation {
3142
3150
  constructor(animations) {
3143
- // Bound to accomadate common `return animation.stop` pattern
3151
+ // Bound to accommodate common `return animation.stop` pattern
3144
3152
  this.stop = () => this.runAll("stop");
3145
3153
  this.animations = animations.filter(Boolean);
3146
3154
  }
@@ -5472,7 +5480,7 @@ function applyTreeDeltas(box, treeScale, treePath, isSharedTransition = false) {
5472
5480
  translateAxis(box.y, -node.scroll.offset.y);
5473
5481
  }
5474
5482
  if (delta) {
5475
- // Incoporate each ancestor's scale into a cumulative treeScale for this component
5483
+ // Incorporate each ancestor's scale into a cumulative treeScale for this component
5476
5484
  treeScale.x *= delta.x.scale;
5477
5485
  treeScale.y *= delta.y.scale;
5478
5486
  // Apply each ancestor's calculated delta into this component's recorded layout box
@@ -5680,7 +5688,7 @@ function isValidHover(event) {
5680
5688
  /**
5681
5689
  * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
5682
5690
  * in that it has an easier syntax, filters out polyfilled touch events, interoperates
5683
- * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
5691
+ * with drag gestures, and automatically removes the "pointerleave" event listener when the hover ends.
5684
5692
  *
5685
5693
  * @public
5686
5694
  */
@@ -7416,7 +7424,7 @@ function processQueue() {
7416
7424
  /**
7417
7425
  * Iterate backwards over the builders array. We can ignore the
7418
7426
  * "wait" animations. If we have an interrupting animation in the
7419
- * queue then we need to batch all preceeding animations into it.
7427
+ * queue then we need to batch all preceding animations into it.
7420
7428
  * Currently this only batches the update functions but will also
7421
7429
  * need to batch the targets.
7422
7430
  */
@@ -8296,7 +8304,7 @@ class DOMVisualElement extends VisualElement {
8296
8304
  /**
8297
8305
  * compareDocumentPosition returns a bitmask, by using the bitwise &
8298
8306
  * we're returning true if 2 in that bitmask is set to true. 2 is set
8299
- * to true if b preceeds a.
8307
+ * to true if b precedes a.
8300
8308
  */
8301
8309
  return a.compareDocumentPosition(b) & 2 ? 1 : -1;
8302
8310
  }
@@ -8788,7 +8796,7 @@ function createAnimationState(visualElement) {
8788
8796
  }
8789
8797
  /**
8790
8798
  * Set all encountered keys so far as the protected keys for this type. This will
8791
- * be any key that has been animated or otherwise handled by active, higher-priortiy types.
8799
+ * be any key that has been animated or otherwise handled by active, higher-priority types.
8792
8800
  */
8793
8801
  typeState.protectedKeys = { ...encounteredKeys };
8794
8802
  // Check if we can skip analysing this prop early
@@ -9613,8 +9621,8 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9613
9621
  this.hasCheckedOptimisedAppear = false;
9614
9622
  /**
9615
9623
  * An object representing the calculated contextual/accumulated/tree scale.
9616
- * This will be used to scale calculcated projection transforms, as these are
9617
- * calculated in screen-space but need to be scaled for elements to layoutly
9624
+ * This will be used to scale calculated projection transforms, as these are
9625
+ * calculated in screen-space but need to be scaled for elements to visually
9618
9626
  * make it to their calculated destinations.
9619
9627
  *
9620
9628
  * TODO: Lazy-init
@@ -9625,7 +9633,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9625
9633
  */
9626
9634
  this.eventHandlers = new Map();
9627
9635
  this.hasTreeAnimated = false;
9628
- this.layoutVersion = 0;
9629
9636
  // Note: Currently only running on root node
9630
9637
  this.updateScheduled = false;
9631
9638
  this.scheduleUpdate = () => this.update();
@@ -9665,7 +9672,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9665
9672
  * Frame calculations
9666
9673
  */
9667
9674
  this.resolvedRelativeTargetAt = 0.0;
9668
- this.linkedParentVersion = 0;
9669
9675
  this.hasProjected = false;
9670
9676
  this.isVisible = true;
9671
9677
  this.animationProgress = 0;
@@ -9946,6 +9952,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9946
9952
  * get measured even when memoized (willUpdate skipped).
9947
9953
  */
9948
9954
  this.nodes.forEach(ensureDraggedNodesSnapshotted);
9955
+ this.nodes.forEach(syncRelativeLayout);
9949
9956
  /**
9950
9957
  * Write
9951
9958
  */
@@ -10043,7 +10050,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10043
10050
  }
10044
10051
  const prevLayout = this.layout;
10045
10052
  this.layout = this.measure(false);
10046
- this.layoutVersion++;
10047
10053
  if (!this.layoutCorrected)
10048
10054
  this.layoutCorrected = createBox();
10049
10055
  this.isLayoutDirty = false;
@@ -10263,11 +10269,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10263
10269
  return;
10264
10270
  this.resolvedRelativeTargetAt = frameData.timestamp;
10265
10271
  const relativeParent = this.getClosestProjectingParent();
10266
- if (relativeParent &&
10267
- this.linkedParentVersion !== relativeParent.layoutVersion &&
10268
- !relativeParent.options.layoutRoot) {
10269
- this.removeRelativeTarget();
10270
- }
10271
10272
  /**
10272
10273
  * If we don't have a targetDelta but do have a layout, we can attempt to resolve
10273
10274
  * a relativeParent. This will allow a component to perform scale correction
@@ -10370,7 +10371,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10370
10371
  }
10371
10372
  createRelativeTarget(relativeParent, layout, parentLayout) {
10372
10373
  this.relativeParent = relativeParent;
10373
- this.linkedParentVersion = relativeParent.layoutVersion;
10374
10374
  this.forceRelativeParentToResolveTarget();
10375
10375
  this.relativeTarget = createBox();
10376
10376
  this.relativeTargetOrigin = createBox();
@@ -11080,6 +11080,26 @@ function ensureDraggedNodesSnapshotted(node) {
11080
11080
  node.isLayoutDirty = true;
11081
11081
  }
11082
11082
  }
11083
+ /**
11084
+ * If a node's relative parent is about to be re-measured but the node isn't
11085
+ * (e.g. it's in another LayoutGroup), its relative target is out of date.
11086
+ * If it's layout animating, re-measure it too, so its relative target keeps
11087
+ * resolving against current layouts. Otherwise its layout is still valid
11088
+ * relative to its parent, so drop the relative target, and any targetDelta
11089
+ * left over from a finished animation while an ancestor still animates, to be
11090
+ * rebuilt from its layout.
11091
+ */
11092
+ function syncRelativeLayout(node) {
11093
+ if (node.relativeTarget && node.relativeParent?.isLayoutDirty) {
11094
+ if (node.currentAnimation) {
11095
+ node.isLayoutDirty = true;
11096
+ }
11097
+ else {
11098
+ node.targetDelta = undefined;
11099
+ node.removeRelativeTarget();
11100
+ }
11101
+ }
11102
+ }
11083
11103
  function resetTransformStyle(node) {
11084
11104
  const { visualElement } = node.options;
11085
11105
  if (visualElement && visualElement.getProps().onBeforeLayoutMeasure) {