motion-dom 14.0.0 → 14.1.1

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
@@ -805,7 +805,20 @@ function calcGeneratorDuration(generator, timeStep = 50, maxDuration = maxGenera
805
805
  * Create a progress => progress easing function from a generator.
806
806
  */
807
807
  function createGeneratorEasing(options, scale = 100, createGenerator) {
808
- const generator = createGenerator({ ...options, keyframes: [0, scale] });
808
+ /**
809
+ * The easing is generated over 0 - scale, but velocity is in the
810
+ * value's own units, so scale it to match. Otherwise an interrupted
811
+ * spring hands off the wrong speed, or direction. Keyframes with
812
+ * units (colours, filters, mixed units) give NaN, which is falsy,
813
+ * so their velocity passes through unscaled.
814
+ */
815
+ const { keyframes: k, velocity: v } = options;
816
+ const delta = k && k[k.length - 1] - k[0];
817
+ const generator = createGenerator({
818
+ ...options,
819
+ velocity: v && delta ? (v * scale) / delta : v,
820
+ keyframes: [0, scale],
821
+ });
809
822
  const duration = Math.min(calcGeneratorDuration(generator), maxGeneratorDuration);
810
823
  return {
811
824
  type: "keyframes",
@@ -1489,6 +1502,19 @@ function replaceTransitionType(transition) {
1489
1502
  }
1490
1503
  }
1491
1504
 
1505
+ /**
1506
+ * If an animation's keyframes resolve, or its first frame renders, more than
1507
+ * this long after its start time, it starts from then instead. While we prefer
1508
+ * to "start" an animation as soon as it's triggered, this avoids a visual jump
1509
+ * into it after main thread work.
1510
+ *
1511
+ * Manual timing steps frames by however long it likes, so never lags.
1512
+ */
1513
+ const MAX_START_DELAY = 40;
1514
+ const resolveStartTime = (startTime, now) => now - startTime > MAX_START_DELAY && !motionUtils.MotionGlobalConfig.useManualTiming
1515
+ ? now
1516
+ : startTime;
1517
+
1492
1518
  function baseRecord(kind, animation) {
1493
1519
  return {
1494
1520
  kind,
@@ -1685,6 +1711,10 @@ class JSAnimation extends WithPromise {
1685
1711
  if (this.startTime === null)
1686
1712
  return generator.next(0);
1687
1713
  const { delay = 0, keyframes, repeat, repeatType, repeatDelay, type, onUpdate, finalKeyframe, } = this.options;
1714
+ if (this.startTime === this.pendingStartTime) {
1715
+ this.startTime = resolveStartTime(this.startTime, timestamp);
1716
+ }
1717
+ this.pendingStartTime = undefined;
1688
1718
  /**
1689
1719
  * requestAnimationFrame timestamps can come through as lower than
1690
1720
  * the startTime as set by performance.now(). Here we prevent this,
@@ -1869,10 +1899,10 @@ class JSAnimation extends WithPromise {
1869
1899
  this.startTime = now;
1870
1900
  }
1871
1901
  else if (this.holdTime !== null) {
1872
- this.startTime = now - this.holdTime;
1902
+ this.startTime = now - this.holdTime / (this.playbackSpeed || 1);
1873
1903
  }
1874
1904
  else if (!this.startTime) {
1875
- this.startTime = startTime ?? now;
1905
+ this.startTime = startTime ?? (this.pendingStartTime = now);
1876
1906
  }
1877
1907
  if (this.state === "finished" && this.speed < 0) {
1878
1908
  this.startTime += this.calculatedDuration;
@@ -2992,15 +3022,6 @@ function supportsBrowserAnimation(options) {
2992
3022
  type !== "inertia");
2993
3023
  }
2994
3024
 
2995
- /**
2996
- * Maximum time allowed between an animation being created and it being
2997
- * resolved for us to use the latter as the start time.
2998
- *
2999
- * This is to ensure that while we prefer to "start" an animation as soon
3000
- * as it's triggered, we also want to avoid a visual jump if there's a big delay
3001
- * between these two moments.
3002
- */
3003
- const MAX_RESOLVE_DELAY = 40;
3004
3025
  class AsyncMotionValueAnimation extends WithPromise {
3005
3026
  constructor(options) {
3006
3027
  super();
@@ -3049,31 +3070,7 @@ class AsyncMotionValueAnimation extends WithPromise {
3049
3070
  makeAnimationInstant(options);
3050
3071
  options.repeat = 0;
3051
3072
  }
3052
- /**
3053
- * Resolve startTime for the animation.
3054
- *
3055
- * This method uses the createdAt and resolvedAt to calculate the
3056
- * animation startTime. *Ideally*, we would use the createdAt time as t=0
3057
- * as the following frame would then be the first frame of the animation in
3058
- * progress, which would feel snappier.
3059
- *
3060
- * However, if there's a delay (main thread work) between the creation of
3061
- * the animation and the first committed frame, we prefer to use resolvedAt
3062
- * to avoid a sudden jump into the animation.
3063
- */
3064
- const startTime = sync
3065
- ? !this.resolvedAt
3066
- ? this.createdAt
3067
- : this.resolvedAt - this.createdAt > MAX_RESOLVE_DELAY
3068
- ? this.resolvedAt
3069
- : this.createdAt
3070
- : undefined;
3071
3073
  const { onComplete } = options;
3072
- /**
3073
- * A startTime passed in options (an optimised appear handoff syncing
3074
- * to its WAAPI animation) takes precedence over the derived one.
3075
- */
3076
- options.startTime ?? (options.startTime = startTime);
3077
3074
  options.finalKeyframe = finalKeyframe;
3078
3075
  options.keyframes = keyframes;
3079
3076
  /**
@@ -3094,6 +3091,24 @@ class AsyncMotionValueAnimation extends WithPromise {
3094
3091
  * values may not be valid CSS and would trigger browser warnings.
3095
3092
  */
3096
3093
  const useWaapi = canAnimateValue && !isHandoff && supportsBrowserAnimation(options);
3094
+ /**
3095
+ * Resolve startTime for the animation. A startTime passed in options
3096
+ * (an optimised appear handoff syncing to its WAAPI animation) takes
3097
+ * precedence.
3098
+ *
3099
+ * *Ideally*, we would use the createdAt time as t=0 as the following
3100
+ * frame would then be the first frame of the animation in progress,
3101
+ * which would feel snappier. If keyframes resolved on a later frame,
3102
+ * and long after creation, we start from then instead.
3103
+ *
3104
+ * If they resolved immediately, a JSAnimation picks its own start
3105
+ * time and resolves it the same way on its first frame, so every
3106
+ * animation started in that moment stays in sync. WAAPI has no frame
3107
+ * of ours to do that on.
3108
+ */
3109
+ if (sync && (useWaapi || this.resolvedAt !== this.createdAt)) {
3110
+ options.startTime ?? (options.startTime = resolveStartTime(this.createdAt, this.resolvedAt));
3111
+ }
3097
3112
  let animation;
3098
3113
  if (useWaapi) {
3099
3114
  /**
@@ -5193,9 +5208,16 @@ function buildSVGPath(attrs, length, spacing = 1, offset = 0, useDashCase = true
5193
5208
  attrs[keys.array] = `${length} ${spacing}`;
5194
5209
  }
5195
5210
 
5211
+ /**
5212
+ * Values rendered via style rather than as attributes. This must include
5213
+ * every value WAAPI can accelerate, as WAAPI writes its final value to
5214
+ * style, which would otherwise override later attribute writes.
5215
+ */
5196
5216
  const cssStyleProperties = [
5197
5217
  "transform",
5198
5218
  "opacity",
5219
+ "clipPath",
5220
+ "filter",
5199
5221
  "offsetDistance",
5200
5222
  "offsetPath",
5201
5223
  "offsetRotate",
@@ -6209,6 +6231,10 @@ class FollowAnimation extends WithPromise {
6209
6231
  tick(timestamp) {
6210
6232
  const { options, hasNextTarget: retargeted } = this;
6211
6233
  const { delay = 0, onUpdate, onPlay } = options;
6234
+ // Retargets happen on a tick, so only the first start can lag
6235
+ if (!this.started) {
6236
+ this.startTime = resolveStartTime(this.startTime, timestamp);
6237
+ }
6212
6238
  const elapsed = Math.round(timestamp - this.startTime) - delay;
6213
6239
  const t = (this.currentTime = Math.max(0, elapsed));
6214
6240
  const state = this.generator.next(t);
@@ -8765,6 +8791,21 @@ function createAnimationState(visualElement) {
8765
8791
  : visualElement.initialValues[key] === undefined
8766
8792
  ? visualElement.baseTarget[key]
8767
8793
  : undefined) ?? null;
8794
+ /**
8795
+ * If it stays where it is, that's now its base. Otherwise a
8796
+ * later removal (like a gesture ending) would also stay,
8797
+ * leaving the value stuck. Stop first so accelerated
8798
+ * animations sync the value they reached. In practice initial
8799
+ * only changes like this with Fast Refresh, so this is
8800
+ * dev-only (#3787).
8801
+ */
8802
+ if (process.env.NODE_ENV !== "production" &&
8803
+ fallbackAnimation[key] === null &&
8804
+ visualElement.initialValues[key] !== undefined) {
8805
+ delete visualElement.initialValues[key];
8806
+ motionValue?.stop();
8807
+ visualElement.baseTarget[key] = motionValue?.get();
8808
+ }
8768
8809
  });
8769
8810
  animations.push({ animation: fallbackAnimation });
8770
8811
  }
@@ -9636,7 +9677,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9636
9677
  visualElement &&
9637
9678
  (layoutId || layout)) {
9638
9679
  this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
9639
- if (this.isTreeAnimationBlocked()) {
9680
+ if (this.isAnimationBlocked) {
9640
9681
  this.target = undefined;
9641
9682
  this.relativeTarget = undefined;
9642
9683
  return;
@@ -9728,11 +9769,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9728
9769
  isUpdateBlocked() {
9729
9770
  return this.updateManuallyBlocked || this.updateBlockedByResize;
9730
9771
  }
9731
- isTreeAnimationBlocked() {
9732
- return (this.isAnimationBlocked ||
9733
- (this.parent && this.parent.isTreeAnimationBlocked()) ||
9734
- false);
9735
- }
9736
9772
  // Note: currently only running on root node
9737
9773
  startUpdate() {
9738
9774
  if (this.isUpdateBlocked())
@@ -9786,6 +9822,14 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9786
9822
  if (node.options.layoutRoot) {
9787
9823
  node.willUpdate(false);
9788
9824
  }
9825
+ /**
9826
+ * Measure the nearest dragged ancestor before the DOM
9827
+ * changes, so this node can follow its layout change.
9828
+ */
9829
+ if (node.isAnimationBlocked) {
9830
+ node.dragSnapshot || (node.dragSnapshot = node.measure());
9831
+ this.dragAncestor = node;
9832
+ }
9789
9833
  }
9790
9834
  const { layoutId, layout } = this.options;
9791
9835
  if (layoutId === undefined && !layout)
@@ -10793,7 +10837,11 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10793
10837
  }
10794
10838
  }
10795
10839
  clearSnapshot() {
10796
- this.resumeFrom = this.snapshot = undefined;
10840
+ this.resumeFrom =
10841
+ this.snapshot =
10842
+ this.dragSnapshot =
10843
+ this.dragAncestor =
10844
+ undefined;
10797
10845
  }
10798
10846
  // Only run on root
10799
10847
  resetTree() {
@@ -10803,8 +10851,13 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10803
10851
  }
10804
10852
  };
10805
10853
  }
10854
+ /**
10855
+ * Nodes are depth-ordered, so a dragged ancestor's layout is updated before
10856
+ * its descendants'.
10857
+ */
10806
10858
  function updateLayout(node) {
10807
10859
  node.updateLayout();
10860
+ followDraggedAncestor(node);
10808
10861
  }
10809
10862
  function notifyLayoutUpdate(node) {
10810
10863
  const snapshot = node.resumeFrom?.snapshot || node.snapshot;
@@ -10953,6 +11006,19 @@ function forceLayoutMeasure(node) {
10953
11006
  function clearIsLayoutDirty(node) {
10954
11007
  node.isLayoutDirty = false;
10955
11008
  }
11009
+ /**
11010
+ * A dragged node doesn't layout animate, so it jumps to its new layout. Shift
11011
+ * its descendants' snapshots by the same amount, so they only animate layout
11012
+ * changes relative to it, rather than lagging behind.
11013
+ */
11014
+ function followDraggedAncestor(node) {
11015
+ const { snapshot, dragAncestor } = node;
11016
+ const to = dragAncestor?.layout;
11017
+ if (!snapshot || !to || node.resumeFrom || node.isAnimationBlocked)
11018
+ return;
11019
+ const from = dragAncestor.dragSnapshot;
11020
+ eachAxis((axis) => translateAxis(snapshot.layoutBox[axis], to.layoutBox[axis].min - from.layoutBox[axis].min));
11021
+ }
10956
11022
  /**
10957
11023
  * When a node is animation-blocked (e.g. during drag) and its component
10958
11024
  * didn't re-render (memoized), willUpdate() is never called so there's
@@ -10961,7 +11027,7 @@ function clearIsLayoutDirty(node) {
10961
11027
  */
10962
11028
  function ensureDraggedNodesSnapshotted(node) {
10963
11029
  if (node.isAnimationBlocked && node.layout && !node.isLayoutDirty) {
10964
- node.snapshot = node.layout;
11030
+ node.snapshot = node.dragSnapshot || node.layout;
10965
11031
  node.isLayoutDirty = true;
10966
11032
  }
10967
11033
  }
@@ -11462,6 +11528,21 @@ function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
11462
11528
  };
11463
11529
  }
11464
11530
 
11531
+ /**
11532
+ * Animate layout changes made within `updateDom` on elements tagged
11533
+ * with `data-layout` or `data-layout-id`.
11534
+ *
11535
+ * ```
11536
+ * animateLayout(() => {
11537
+ * element.style.justifyContent = "flex-end"
11538
+ * })
11539
+ * ```
11540
+ */
11541
+ function animateLayout(scopeOrUpdateDom, updateDomOrOptions, options) {
11542
+ const { scope, updateDom, defaultOptions } = parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options);
11543
+ return new LayoutAnimationBuilder(scope, updateDom, defaultOptions);
11544
+ }
11545
+
11465
11546
  /**
11466
11547
  * @deprecated
11467
11548
  *
@@ -11513,6 +11594,7 @@ exports.alpha = alpha;
11513
11594
  exports.analyseComplexValue = analyseComplexValue;
11514
11595
  exports.animateEffectSubject = animateEffectSubject;
11515
11596
  exports.animateElement = animateElement;
11597
+ exports.animateLayout = animateLayout;
11516
11598
  exports.animateMotionValue = animateMotionValue;
11517
11599
  exports.animateSingleValue = animateSingleValue;
11518
11600
  exports.animateTarget = animateTarget;