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.
@@ -812,7 +812,6 @@
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 @@
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 @@
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 @@
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 @@
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
+ {
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 @@
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 @@
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 @@
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 @@
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 @@
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();
@@ -3139,7 +3147,7 @@
3139
3147
 
3140
3148
  class GroupAnimation {
3141
3149
  constructor(animations) {
3142
- // Bound to accomadate common `return animation.stop` pattern
3150
+ // Bound to accommodate common `return animation.stop` pattern
3143
3151
  this.stop = () => this.runAll("stop");
3144
3152
  this.animations = animations.filter(Boolean);
3145
3153
  }
@@ -5471,7 +5479,7 @@
5471
5479
  translateAxis(box.y, -node.scroll.offset.y);
5472
5480
  }
5473
5481
  if (delta) {
5474
- // Incoporate each ancestor's scale into a cumulative treeScale for this component
5482
+ // Incorporate each ancestor's scale into a cumulative treeScale for this component
5475
5483
  treeScale.x *= delta.x.scale;
5476
5484
  treeScale.y *= delta.y.scale;
5477
5485
  // Apply each ancestor's calculated delta into this component's recorded layout box
@@ -5679,7 +5687,7 @@
5679
5687
  /**
5680
5688
  * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
5681
5689
  * in that it has an easier syntax, filters out polyfilled touch events, interoperates
5682
- * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
5690
+ * with drag gestures, and automatically removes the "pointerleave" event listener when the hover ends.
5683
5691
  *
5684
5692
  * @public
5685
5693
  */
@@ -7415,7 +7423,7 @@
7415
7423
  /**
7416
7424
  * Iterate backwards over the builders array. We can ignore the
7417
7425
  * "wait" animations. If we have an interrupting animation in the
7418
- * queue then we need to batch all preceeding animations into it.
7426
+ * queue then we need to batch all preceding animations into it.
7419
7427
  * Currently this only batches the update functions but will also
7420
7428
  * need to batch the targets.
7421
7429
  */
@@ -8295,7 +8303,7 @@
8295
8303
  /**
8296
8304
  * compareDocumentPosition returns a bitmask, by using the bitwise &
8297
8305
  * we're returning true if 2 in that bitmask is set to true. 2 is set
8298
- * to true if b preceeds a.
8306
+ * to true if b precedes a.
8299
8307
  */
8300
8308
  return a.compareDocumentPosition(b) & 2 ? 1 : -1;
8301
8309
  }
@@ -8787,7 +8795,7 @@
8787
8795
  }
8788
8796
  /**
8789
8797
  * Set all encountered keys so far as the protected keys for this type. This will
8790
- * be any key that has been animated or otherwise handled by active, higher-priortiy types.
8798
+ * be any key that has been animated or otherwise handled by active, higher-priority types.
8791
8799
  */
8792
8800
  typeState.protectedKeys = { ...encounteredKeys };
8793
8801
  // Check if we can skip analysing this prop early
@@ -9612,8 +9620,8 @@
9612
9620
  this.hasCheckedOptimisedAppear = false;
9613
9621
  /**
9614
9622
  * An object representing the calculated contextual/accumulated/tree scale.
9615
- * This will be used to scale calculcated projection transforms, as these are
9616
- * calculated in screen-space but need to be scaled for elements to layoutly
9623
+ * This will be used to scale calculated projection transforms, as these are
9624
+ * calculated in screen-space but need to be scaled for elements to visually
9617
9625
  * make it to their calculated destinations.
9618
9626
  *
9619
9627
  * TODO: Lazy-init
@@ -9624,7 +9632,6 @@
9624
9632
  */
9625
9633
  this.eventHandlers = new Map();
9626
9634
  this.hasTreeAnimated = false;
9627
- this.layoutVersion = 0;
9628
9635
  // Note: Currently only running on root node
9629
9636
  this.updateScheduled = false;
9630
9637
  this.scheduleUpdate = () => this.update();
@@ -9664,7 +9671,6 @@
9664
9671
  * Frame calculations
9665
9672
  */
9666
9673
  this.resolvedRelativeTargetAt = 0.0;
9667
- this.linkedParentVersion = 0;
9668
9674
  this.hasProjected = false;
9669
9675
  this.isVisible = true;
9670
9676
  this.animationProgress = 0;
@@ -9945,6 +9951,7 @@
9945
9951
  * get measured even when memoized (willUpdate skipped).
9946
9952
  */
9947
9953
  this.nodes.forEach(ensureDraggedNodesSnapshotted);
9954
+ this.nodes.forEach(syncRelativeLayout);
9948
9955
  /**
9949
9956
  * Write
9950
9957
  */
@@ -10042,7 +10049,6 @@
10042
10049
  }
10043
10050
  const prevLayout = this.layout;
10044
10051
  this.layout = this.measure(false);
10045
- this.layoutVersion++;
10046
10052
  if (!this.layoutCorrected)
10047
10053
  this.layoutCorrected = createBox();
10048
10054
  this.isLayoutDirty = false;
@@ -10262,11 +10268,6 @@
10262
10268
  return;
10263
10269
  this.resolvedRelativeTargetAt = frameData.timestamp;
10264
10270
  const relativeParent = this.getClosestProjectingParent();
10265
- if (relativeParent &&
10266
- this.linkedParentVersion !== relativeParent.layoutVersion &&
10267
- !relativeParent.options.layoutRoot) {
10268
- this.removeRelativeTarget();
10269
- }
10270
10271
  /**
10271
10272
  * If we don't have a targetDelta but do have a layout, we can attempt to resolve
10272
10273
  * a relativeParent. This will allow a component to perform scale correction
@@ -10369,7 +10370,6 @@
10369
10370
  }
10370
10371
  createRelativeTarget(relativeParent, layout, parentLayout) {
10371
10372
  this.relativeParent = relativeParent;
10372
- this.linkedParentVersion = relativeParent.layoutVersion;
10373
10373
  this.forceRelativeParentToResolveTarget();
10374
10374
  this.relativeTarget = createBox();
10375
10375
  this.relativeTargetOrigin = createBox();
@@ -11079,6 +11079,26 @@
11079
11079
  node.isLayoutDirty = true;
11080
11080
  }
11081
11081
  }
11082
+ /**
11083
+ * If a node's relative parent is about to be re-measured but the node isn't
11084
+ * (e.g. it's in another LayoutGroup), its relative target is out of date.
11085
+ * If it's layout animating, re-measure it too, so its relative target keeps
11086
+ * resolving against current layouts. Otherwise its layout is still valid
11087
+ * relative to its parent, so drop the relative target, and any targetDelta
11088
+ * left over from a finished animation while an ancestor still animates, to be
11089
+ * rebuilt from its layout.
11090
+ */
11091
+ function syncRelativeLayout(node) {
11092
+ if (node.relativeTarget && node.relativeParent?.isLayoutDirty) {
11093
+ if (node.currentAnimation) {
11094
+ node.isLayoutDirty = true;
11095
+ }
11096
+ else {
11097
+ node.targetDelta = undefined;
11098
+ node.removeRelativeTarget();
11099
+ }
11100
+ }
11101
+ }
11082
11102
  function resetTransformStyle(node) {
11083
11103
  const { visualElement } = node.options;
11084
11104
  if (visualElement && visualElement.getProps().onBeforeLayoutMeasure) {