motion-dom 13.5.1 → 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;
@@ -2733,16 +2750,13 @@ class NativeAnimation extends WithPromise {
2733
2750
  /**
2734
2751
  * Attaches a timeline to the animation, for instance the `ScrollTimeline`.
2735
2752
  */
2736
- attachTimeline({ timeline, onAttach, observe,
2737
- // Deprecated rangeStart/rangeEnd from framer-motion 13.0–13.4. Removed in v14.
2738
- ...range }) {
2753
+ attachTimeline({ timeline, onAttach, observe, }) {
2739
2754
  if (this.allowFlatten) {
2740
2755
  this.animation.effect?.updateTiming({ easing: "linear" });
2741
2756
  }
2742
2757
  this.animation.onfinish = null;
2743
2758
  if (timeline && supportsScrollTimeline()) {
2744
2759
  this.animation.timeline = timeline;
2745
- Object.assign(this.animation, range);
2746
2760
  onAttach?.(this.animation);
2747
2761
  return motionUtils.noop;
2748
2762
  }
@@ -2995,15 +3009,6 @@ function supportsBrowserAnimation(options) {
2995
3009
  type !== "inertia");
2996
3010
  }
2997
3011
 
2998
- /**
2999
- * Maximum time allowed between an animation being created and it being
3000
- * resolved for us to use the latter as the start time.
3001
- *
3002
- * This is to ensure that while we prefer to "start" an animation as soon
3003
- * as it's triggered, we also want to avoid a visual jump if there's a big delay
3004
- * between these two moments.
3005
- */
3006
- const MAX_RESOLVE_DELAY = 40;
3007
3012
  class AsyncMotionValueAnimation extends WithPromise {
3008
3013
  constructor(options) {
3009
3014
  super();
@@ -3052,31 +3057,7 @@ class AsyncMotionValueAnimation extends WithPromise {
3052
3057
  makeAnimationInstant(options);
3053
3058
  options.repeat = 0;
3054
3059
  }
3055
- /**
3056
- * Resolve startTime for the animation.
3057
- *
3058
- * This method uses the createdAt and resolvedAt to calculate the
3059
- * animation startTime. *Ideally*, we would use the createdAt time as t=0
3060
- * as the following frame would then be the first frame of the animation in
3061
- * progress, which would feel snappier.
3062
- *
3063
- * However, if there's a delay (main thread work) between the creation of
3064
- * the animation and the first committed frame, we prefer to use resolvedAt
3065
- * to avoid a sudden jump into the animation.
3066
- */
3067
- const startTime = sync
3068
- ? !this.resolvedAt
3069
- ? this.createdAt
3070
- : this.resolvedAt - this.createdAt > MAX_RESOLVE_DELAY
3071
- ? this.resolvedAt
3072
- : this.createdAt
3073
- : undefined;
3074
3060
  const { onComplete } = options;
3075
- /**
3076
- * A startTime passed in options (an optimised appear handoff syncing
3077
- * to its WAAPI animation) takes precedence over the derived one.
3078
- */
3079
- options.startTime ?? (options.startTime = startTime);
3080
3061
  options.finalKeyframe = finalKeyframe;
3081
3062
  options.keyframes = keyframes;
3082
3063
  /**
@@ -3097,6 +3078,24 @@ class AsyncMotionValueAnimation extends WithPromise {
3097
3078
  * values may not be valid CSS and would trigger browser warnings.
3098
3079
  */
3099
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
+ }
3100
3099
  let animation;
3101
3100
  if (useWaapi) {
3102
3101
  /**
@@ -5196,9 +5195,16 @@ function buildSVGPath(attrs, length, spacing = 1, offset = 0, useDashCase = true
5196
5195
  attrs[keys.array] = `${length} ${spacing}`;
5197
5196
  }
5198
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
+ */
5199
5203
  const cssStyleProperties = [
5200
5204
  "transform",
5201
5205
  "opacity",
5206
+ "clipPath",
5207
+ "filter",
5202
5208
  "offsetDistance",
5203
5209
  "offsetPath",
5204
5210
  "offsetRotate",
@@ -6054,25 +6060,6 @@ function resize(a, b) {
6054
6060
  return typeof a === "function" ? resizeWindow(a) : resizeElement(a, b);
6055
6061
  }
6056
6062
 
6057
- /**
6058
- * @deprecated Kept because framer-motion 13.0–13.4 import it from
6059
- * motion-dom with a ^ range. Removed in v14.
6060
- */
6061
- function observeTimeline(update, timeline) {
6062
- let prevProgress;
6063
- const onFrame = () => {
6064
- const { currentTime } = timeline;
6065
- const percentage = currentTime === null ? 0 : currentTime.value;
6066
- const progress = percentage / 100;
6067
- if (prevProgress !== progress) {
6068
- update(progress);
6069
- }
6070
- prevProgress = progress;
6071
- };
6072
- frame.preUpdate(onFrame, true);
6073
- return () => cancelFrame(onFrame);
6074
- }
6075
-
6076
6063
  const statsBuffer = {
6077
6064
  value: null,
6078
6065
  addProjectionMetrics: null,
@@ -6231,6 +6218,10 @@ class FollowAnimation extends WithPromise {
6231
6218
  tick(timestamp) {
6232
6219
  const { options, hasNextTarget: retargeted } = this;
6233
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
+ }
6234
6225
  const elapsed = Math.round(timestamp - this.startTime) - delay;
6235
6226
  const t = (this.currentTime = Math.max(0, elapsed));
6236
6227
  const state = this.generator.next(t);
@@ -8787,6 +8778,21 @@ function createAnimationState(visualElement) {
8787
8778
  : visualElement.initialValues[key] === undefined
8788
8779
  ? visualElement.baseTarget[key]
8789
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
+ }
8790
8796
  });
8791
8797
  animations.push({ animation: fallbackAnimation });
8792
8798
  }
@@ -9658,7 +9664,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9658
9664
  visualElement &&
9659
9665
  (layoutId || layout)) {
9660
9666
  this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
9661
- if (this.isTreeAnimationBlocked()) {
9667
+ if (this.isAnimationBlocked) {
9662
9668
  this.target = undefined;
9663
9669
  this.relativeTarget = undefined;
9664
9670
  return;
@@ -9750,11 +9756,6 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9750
9756
  isUpdateBlocked() {
9751
9757
  return this.updateManuallyBlocked || this.updateBlockedByResize;
9752
9758
  }
9753
- isTreeAnimationBlocked() {
9754
- return (this.isAnimationBlocked ||
9755
- (this.parent && this.parent.isTreeAnimationBlocked()) ||
9756
- false);
9757
- }
9758
9759
  // Note: currently only running on root node
9759
9760
  startUpdate() {
9760
9761
  if (this.isUpdateBlocked())
@@ -9808,6 +9809,14 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
9808
9809
  if (node.options.layoutRoot) {
9809
9810
  node.willUpdate(false);
9810
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
+ }
9811
9820
  }
9812
9821
  const { layoutId, layout } = this.options;
9813
9822
  if (layoutId === undefined && !layout)
@@ -10815,7 +10824,11 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10815
10824
  }
10816
10825
  }
10817
10826
  clearSnapshot() {
10818
- this.resumeFrom = this.snapshot = undefined;
10827
+ this.resumeFrom =
10828
+ this.snapshot =
10829
+ this.dragSnapshot =
10830
+ this.dragAncestor =
10831
+ undefined;
10819
10832
  }
10820
10833
  // Only run on root
10821
10834
  resetTree() {
@@ -10825,8 +10838,13 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10825
10838
  }
10826
10839
  };
10827
10840
  }
10841
+ /**
10842
+ * Nodes are depth-ordered, so a dragged ancestor's layout is updated before
10843
+ * its descendants'.
10844
+ */
10828
10845
  function updateLayout(node) {
10829
10846
  node.updateLayout();
10847
+ followDraggedAncestor(node);
10830
10848
  }
10831
10849
  function notifyLayoutUpdate(node) {
10832
10850
  const snapshot = node.resumeFrom?.snapshot || node.snapshot;
@@ -10975,6 +10993,19 @@ function forceLayoutMeasure(node) {
10975
10993
  function clearIsLayoutDirty(node) {
10976
10994
  node.isLayoutDirty = false;
10977
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
+ }
10978
11009
  /**
10979
11010
  * When a node is animation-blocked (e.g. during drag) and its component
10980
11011
  * didn't re-render (memoized), willUpdate() is never called so there's
@@ -10983,7 +11014,7 @@ function clearIsLayoutDirty(node) {
10983
11014
  */
10984
11015
  function ensureDraggedNodesSnapshotted(node) {
10985
11016
  if (node.isAnimationBlocked && node.layout && !node.isLayoutDirty) {
10986
- node.snapshot = node.layout;
11017
+ node.snapshot = node.dragSnapshot || node.layout;
10987
11018
  node.isLayoutDirty = true;
10988
11019
  }
10989
11020
  }
@@ -11484,6 +11515,21 @@ function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
11484
11515
  };
11485
11516
  }
11486
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
+
11487
11533
  /**
11488
11534
  * @deprecated
11489
11535
  *
@@ -11535,6 +11581,7 @@ exports.alpha = alpha;
11535
11581
  exports.analyseComplexValue = analyseComplexValue;
11536
11582
  exports.animateEffectSubject = animateEffectSubject;
11537
11583
  exports.animateElement = animateElement;
11584
+ exports.animateLayout = animateLayout;
11538
11585
  exports.animateMotionValue = animateMotionValue;
11539
11586
  exports.animateSingleValue = animateSingleValue;
11540
11587
  exports.animateTarget = animateTarget;
@@ -11702,7 +11749,6 @@ exports.motionValue = motionValue;
11702
11749
  exports.nodeGroup = nodeGroup;
11703
11750
  exports.number = number;
11704
11751
  exports.numberValueTypes = numberValueTypes;
11705
- exports.observeTimeline = observeTimeline;
11706
11752
  exports.optimizedAppearDataAttribute = optimizedAppearDataAttribute;
11707
11753
  exports.optimizedAppearDataId = optimizedAppearDataId;
11708
11754
  exports.originProps = originProps;