motion-dom 14.0.0 → 14.1.0

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
@@ -1489,6 +1489,19 @@ function replaceTransitionType(transition) {
1489
1489
  }
1490
1490
  }
1491
1491
 
1492
+ /**
1493
+ * If an animation's keyframes resolve, or its first frame renders, more than
1494
+ * this long after its start time, it starts from then instead. While we prefer
1495
+ * to "start" an animation as soon as it's triggered, this avoids a visual jump
1496
+ * into it after main thread work.
1497
+ *
1498
+ * Manual timing steps frames by however long it likes, so never lags.
1499
+ */
1500
+ const MAX_START_DELAY = 40;
1501
+ const resolveStartTime = (startTime, now) => now - startTime > MAX_START_DELAY && !motionUtils.MotionGlobalConfig.useManualTiming
1502
+ ? now
1503
+ : startTime;
1504
+
1492
1505
  function baseRecord(kind, animation) {
1493
1506
  return {
1494
1507
  kind,
@@ -1685,6 +1698,10 @@ class JSAnimation extends WithPromise {
1685
1698
  if (this.startTime === null)
1686
1699
  return generator.next(0);
1687
1700
  const { delay = 0, keyframes, repeat, repeatType, repeatDelay, type, onUpdate, finalKeyframe, } = this.options;
1701
+ if (this.startTime === this.pendingStartTime) {
1702
+ this.startTime = resolveStartTime(this.startTime, timestamp);
1703
+ }
1704
+ this.pendingStartTime = undefined;
1688
1705
  /**
1689
1706
  * requestAnimationFrame timestamps can come through as lower than
1690
1707
  * the startTime as set by performance.now(). Here we prevent this,
@@ -1869,10 +1886,10 @@ class JSAnimation extends WithPromise {
1869
1886
  this.startTime = now;
1870
1887
  }
1871
1888
  else if (this.holdTime !== null) {
1872
- this.startTime = now - this.holdTime;
1889
+ this.startTime = now - this.holdTime / (this.playbackSpeed || 1);
1873
1890
  }
1874
1891
  else if (!this.startTime) {
1875
- this.startTime = startTime ?? now;
1892
+ this.startTime = startTime ?? (this.pendingStartTime = now);
1876
1893
  }
1877
1894
  if (this.state === "finished" && this.speed < 0) {
1878
1895
  this.startTime += this.calculatedDuration;
@@ -2992,15 +3009,6 @@ function supportsBrowserAnimation(options) {
2992
3009
  type !== "inertia");
2993
3010
  }
2994
3011
 
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
3012
  class AsyncMotionValueAnimation extends WithPromise {
3005
3013
  constructor(options) {
3006
3014
  super();
@@ -3049,31 +3057,7 @@ class AsyncMotionValueAnimation extends WithPromise {
3049
3057
  makeAnimationInstant(options);
3050
3058
  options.repeat = 0;
3051
3059
  }
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
3060
  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
3061
  options.finalKeyframe = finalKeyframe;
3078
3062
  options.keyframes = keyframes;
3079
3063
  /**
@@ -3094,6 +3078,24 @@ class AsyncMotionValueAnimation extends WithPromise {
3094
3078
  * values may not be valid CSS and would trigger browser warnings.
3095
3079
  */
3096
3080
  const useWaapi = canAnimateValue && !isHandoff && supportsBrowserAnimation(options);
3081
+ /**
3082
+ * Resolve startTime for the animation. A startTime passed in options
3083
+ * (an optimised appear handoff syncing to its WAAPI animation) takes
3084
+ * precedence.
3085
+ *
3086
+ * *Ideally*, we would use the createdAt time as t=0 as the following
3087
+ * frame would then be the first frame of the animation in progress,
3088
+ * which would feel snappier. If keyframes resolved on a later frame,
3089
+ * and long after creation, we start from then instead.
3090
+ *
3091
+ * If they resolved immediately, a JSAnimation picks its own start
3092
+ * time and resolves it the same way on its first frame, so every
3093
+ * animation started in that moment stays in sync. WAAPI has no frame
3094
+ * of ours to do that on.
3095
+ */
3096
+ if (sync && (useWaapi || this.resolvedAt !== this.createdAt)) {
3097
+ options.startTime ?? (options.startTime = resolveStartTime(this.createdAt, this.resolvedAt));
3098
+ }
3097
3099
  let animation;
3098
3100
  if (useWaapi) {
3099
3101
  /**
@@ -5193,9 +5195,16 @@ function buildSVGPath(attrs, length, spacing = 1, offset = 0, useDashCase = true
5193
5195
  attrs[keys.array] = `${length} ${spacing}`;
5194
5196
  }
5195
5197
 
5198
+ /**
5199
+ * Values rendered via style rather than as attributes. This must include
5200
+ * every value WAAPI can accelerate, as WAAPI writes its final value to
5201
+ * style, which would otherwise override later attribute writes.
5202
+ */
5196
5203
  const cssStyleProperties = [
5197
5204
  "transform",
5198
5205
  "opacity",
5206
+ "clipPath",
5207
+ "filter",
5199
5208
  "offsetDistance",
5200
5209
  "offsetPath",
5201
5210
  "offsetRotate",
@@ -6209,6 +6218,10 @@ class FollowAnimation extends WithPromise {
6209
6218
  tick(timestamp) {
6210
6219
  const { options, hasNextTarget: retargeted } = this;
6211
6220
  const { delay = 0, onUpdate, onPlay } = options;
6221
+ // Retargets happen on a tick, so only the first start can lag
6222
+ if (!this.started) {
6223
+ this.startTime = resolveStartTime(this.startTime, timestamp);
6224
+ }
6212
6225
  const elapsed = Math.round(timestamp - this.startTime) - delay;
6213
6226
  const t = (this.currentTime = Math.max(0, elapsed));
6214
6227
  const state = this.generator.next(t);
@@ -8765,6 +8778,21 @@ function createAnimationState(visualElement) {
8765
8778
  : visualElement.initialValues[key] === undefined
8766
8779
  ? visualElement.baseTarget[key]
8767
8780
  : undefined) ?? null;
8781
+ /**
8782
+ * If it stays where it is, that's now its base. Otherwise a
8783
+ * later removal (like a gesture ending) would also stay,
8784
+ * leaving the value stuck. Stop first so accelerated
8785
+ * animations sync the value they reached. In practice initial
8786
+ * only changes like this with Fast Refresh, so this is
8787
+ * dev-only (#3787).
8788
+ */
8789
+ if (process.env.NODE_ENV !== "production" &&
8790
+ fallbackAnimation[key] === null &&
8791
+ visualElement.initialValues[key] !== undefined) {
8792
+ delete visualElement.initialValues[key];
8793
+ motionValue?.stop();
8794
+ visualElement.baseTarget[key] = motionValue?.get();
8795
+ }
8768
8796
  });
8769
8797
  animations.push({ animation: fallbackAnimation });
8770
8798
  }
@@ -9636,7 +9664,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9636
9664
  visualElement &&
9637
9665
  (layoutId || layout)) {
9638
9666
  this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
9639
- if (this.isTreeAnimationBlocked()) {
9667
+ if (this.isAnimationBlocked) {
9640
9668
  this.target = undefined;
9641
9669
  this.relativeTarget = undefined;
9642
9670
  return;
@@ -9728,11 +9756,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9728
9756
  isUpdateBlocked() {
9729
9757
  return this.updateManuallyBlocked || this.updateBlockedByResize;
9730
9758
  }
9731
- isTreeAnimationBlocked() {
9732
- return (this.isAnimationBlocked ||
9733
- (this.parent && this.parent.isTreeAnimationBlocked()) ||
9734
- false);
9735
- }
9736
9759
  // Note: currently only running on root node
9737
9760
  startUpdate() {
9738
9761
  if (this.isUpdateBlocked())
@@ -9786,6 +9809,14 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9786
9809
  if (node.options.layoutRoot) {
9787
9810
  node.willUpdate(false);
9788
9811
  }
9812
+ /**
9813
+ * Measure the nearest dragged ancestor before the DOM
9814
+ * changes, so this node can follow its layout change.
9815
+ */
9816
+ if (node.isAnimationBlocked) {
9817
+ node.dragSnapshot || (node.dragSnapshot = node.measure());
9818
+ this.dragAncestor = node;
9819
+ }
9789
9820
  }
9790
9821
  const { layoutId, layout } = this.options;
9791
9822
  if (layoutId === undefined && !layout)
@@ -10793,7 +10824,11 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10793
10824
  }
10794
10825
  }
10795
10826
  clearSnapshot() {
10796
- this.resumeFrom = this.snapshot = undefined;
10827
+ this.resumeFrom =
10828
+ this.snapshot =
10829
+ this.dragSnapshot =
10830
+ this.dragAncestor =
10831
+ undefined;
10797
10832
  }
10798
10833
  // Only run on root
10799
10834
  resetTree() {
@@ -10803,8 +10838,13 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10803
10838
  }
10804
10839
  };
10805
10840
  }
10841
+ /**
10842
+ * Nodes are depth-ordered, so a dragged ancestor's layout is updated before
10843
+ * its descendants'.
10844
+ */
10806
10845
  function updateLayout(node) {
10807
10846
  node.updateLayout();
10847
+ followDraggedAncestor(node);
10808
10848
  }
10809
10849
  function notifyLayoutUpdate(node) {
10810
10850
  const snapshot = node.resumeFrom?.snapshot || node.snapshot;
@@ -10953,6 +10993,19 @@ function forceLayoutMeasure(node) {
10953
10993
  function clearIsLayoutDirty(node) {
10954
10994
  node.isLayoutDirty = false;
10955
10995
  }
10996
+ /**
10997
+ * A dragged node doesn't layout animate, so it jumps to its new layout. Shift
10998
+ * its descendants' snapshots by the same amount, so they only animate layout
10999
+ * changes relative to it, rather than lagging behind.
11000
+ */
11001
+ function followDraggedAncestor(node) {
11002
+ const { snapshot, dragAncestor } = node;
11003
+ const to = dragAncestor?.layout;
11004
+ if (!snapshot || !to || node.resumeFrom || node.isAnimationBlocked)
11005
+ return;
11006
+ const from = dragAncestor.dragSnapshot;
11007
+ eachAxis((axis) => translateAxis(snapshot.layoutBox[axis], to.layoutBox[axis].min - from.layoutBox[axis].min));
11008
+ }
10956
11009
  /**
10957
11010
  * When a node is animation-blocked (e.g. during drag) and its component
10958
11011
  * didn't re-render (memoized), willUpdate() is never called so there's
@@ -10961,7 +11014,7 @@ function clearIsLayoutDirty(node) {
10961
11014
  */
10962
11015
  function ensureDraggedNodesSnapshotted(node) {
10963
11016
  if (node.isAnimationBlocked && node.layout && !node.isLayoutDirty) {
10964
- node.snapshot = node.layout;
11017
+ node.snapshot = node.dragSnapshot || node.layout;
10965
11018
  node.isLayoutDirty = true;
10966
11019
  }
10967
11020
  }
@@ -11462,6 +11515,21 @@ function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
11462
11515
  };
11463
11516
  }
11464
11517
 
11518
+ /**
11519
+ * Animate layout changes made within `updateDom` on elements tagged
11520
+ * with `data-layout` or `data-layout-id`.
11521
+ *
11522
+ * ```
11523
+ * animateLayout(() => {
11524
+ * element.style.justifyContent = "flex-end"
11525
+ * })
11526
+ * ```
11527
+ */
11528
+ function animateLayout(scopeOrUpdateDom, updateDomOrOptions, options) {
11529
+ const { scope, updateDom, defaultOptions } = parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options);
11530
+ return new LayoutAnimationBuilder(scope, updateDom, defaultOptions);
11531
+ }
11532
+
11465
11533
  /**
11466
11534
  * @deprecated
11467
11535
  *
@@ -11513,6 +11581,7 @@ exports.alpha = alpha;
11513
11581
  exports.analyseComplexValue = analyseComplexValue;
11514
11582
  exports.animateEffectSubject = animateEffectSubject;
11515
11583
  exports.animateElement = animateElement;
11584
+ exports.animateLayout = animateLayout;
11516
11585
  exports.animateMotionValue = animateMotionValue;
11517
11586
  exports.animateSingleValue = animateSingleValue;
11518
11587
  exports.animateTarget = animateTarget;