motion 14.7.0 → 14.8.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.
@@ -1619,10 +1619,11 @@
1619
1619
 
1620
1620
  function createMixers(output, ease, customMixer) {
1621
1621
  const mixers = [];
1622
- const mixerFactory = customMixer || MotionGlobalConfig.mix || mix;
1622
+ const mixerFactory = MotionGlobalConfig.mix || mix;
1623
1623
  const numMixers = output.length - 1;
1624
1624
  for (let i = 0; i < numMixers; i++) {
1625
- let mixer = mixerFactory(output[i], output[i + 1]);
1625
+ let mixer = customMixer?.(output[i], output[i + 1]) ||
1626
+ mixerFactory(output[i], output[i + 1]);
1626
1627
  if (ease) {
1627
1628
  const easingFunction = Array.isArray(ease) ? ease[i] || noop : ease;
1628
1629
  mixer = pipe(easingFunction, mixer);
@@ -1708,7 +1709,13 @@
1708
1709
  function defaultEasing(values, easing) {
1709
1710
  return values.map(() => easing || easeInOut).splice(0, values.length - 1);
1710
1711
  }
1711
- function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "easeInOut", }) {
1712
+ function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "easeInOut", mixer: transitionMixer, }) {
1713
+ /**
1714
+ * A mixer only blends values that aren't numbers. Numbers blend as
1715
+ * usual, as they do in springs, and so do the progress animations
1716
+ * behind layout and path animations, which take the same transition.
1717
+ */
1718
+ const customMixer = typeof keyframeValues[0] === "number" ? undefined : transitionMixer;
1712
1719
  /**
1713
1720
  * Easing functions can be externally defined as strings. Here we convert them
1714
1721
  * into actual functions.
@@ -1725,9 +1732,11 @@
1725
1732
  value: keyframeValues[0],
1726
1733
  };
1727
1734
  /**
1728
- * Fast setup path for two keyframes one easing.
1735
+ * Fast setup path for two keyframes one easing. interpolate() handles
1736
+ * a custom mixer, and falls back to the usual one where it declines.
1729
1737
  */
1730
- if (keyframeValues.length === 2 &&
1738
+ if (!customMixer &&
1739
+ keyframeValues.length === 2 &&
1731
1740
  !Array.isArray(easingFunctions) &&
1732
1741
  (!times ||
1733
1742
  times.length !== 2 ||
@@ -1760,6 +1769,7 @@
1760
1769
  ease: Array.isArray(easingFunctions)
1761
1770
  ? easingFunctions
1762
1771
  : defaultEasing(keyframeValues, easingFunctions),
1772
+ mixer: customMixer,
1763
1773
  });
1764
1774
  return {
1765
1775
  calculatedDuration: duration,
@@ -1967,9 +1977,15 @@
1967
1977
  if (generatorFactory !== keyframes) {
1968
1978
  exports.invariant(keyframes$1.length <= 2, `Only two keyframes currently supported with spring and inertia animations. Trying to animate ${keyframes$1}`, "spring-two-frames");
1969
1979
  }
1980
+ /**
1981
+ * This runs again when a spring is flattened to attach it to a
1982
+ * timeline, and the keyframes generator blends values itself.
1983
+ */
1984
+ this.mixKeyframes = undefined;
1970
1985
  if (generatorFactory !== keyframes &&
1971
1986
  typeof keyframes$1[0] !== "number") {
1972
- this.mixKeyframes = pipe(percentToProgress, mix(keyframes$1[0], keyframes$1[1]));
1987
+ this.mixKeyframes = pipe(percentToProgress, options.mixer?.(keyframes$1[0], keyframes$1[1]) ||
1988
+ mix(keyframes$1[0], keyframes$1[1]));
1973
1989
  keyframes$1 = [0, 100];
1974
1990
  }
1975
1991
  const generator = generatorFactory(keyframes$1 === options.keyframes
@@ -3323,6 +3339,15 @@
3323
3339
  return false;
3324
3340
  }
3325
3341
 
3342
+ /**
3343
+ * Whether an animation needs its mixer, which WAAPI can't call. Numbers
3344
+ * blend as usual, and an animation whose mixer declines every pair runs
3345
+ * as it would without one.
3346
+ */
3347
+ const needsMixer = ({ mixer, keyframes }) => mixer &&
3348
+ typeof keyframes[0] !== "number" &&
3349
+ keyframes.some((keyframe, i) => i && mixer(keyframes[i - 1], keyframe));
3350
+
3326
3351
  const colorProperties = new Set([
3327
3352
  "color",
3328
3353
  "backgroundColor",
@@ -3382,7 +3407,8 @@
3382
3407
  !repeatDelay &&
3383
3408
  repeatType !== "mirror" &&
3384
3409
  damping !== 0 &&
3385
- type !== "inertia");
3410
+ type !== "inertia" &&
3411
+ !needsMixer(options));
3386
3412
  }
3387
3413
 
3388
3414
  /**
@@ -3481,18 +3507,21 @@
3481
3507
  * Named easings that are the same cubic-bezier in JS and CSS.
3482
3508
  */
3483
3509
  const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
3510
+ /**
3511
+ * Whether two eased progresses match. False if either is NaN.
3512
+ */
3513
+ const isNear$1 = (a, b) => Math.abs(a - b) <= easingTolerance;
3484
3514
  const byTime = (a, b) => a - b;
3485
3515
  /**
3486
3516
  * n + 1 evenly spaced timestamps, from one timestamp to another.
3487
3517
  */
3488
- const spread = (from, to, n) => {
3489
- const result = [];
3490
- for (let i = 0; i <= n; i++)
3491
- result.push(mixNumber$1(from, to, i / n));
3492
- return result;
3493
- };
3518
+ const spread = (from, to, n) => defaultOffset$1(Array(n + 1)).map((p) => mixNumber$1(from, to, p));
3494
3519
  const getOwner = (options) => options.motionValue.owner;
3495
3520
  const getGroup = (element) => transformGroups.get(element);
3521
+ /**
3522
+ * Whether an animation has ended, so can leave its group.
3523
+ */
3524
+ const hasEnded = ({ state }) => state === "idle" || state === "finished";
3496
3525
  const read = (owner, key) => owner.latestValues ? owner.latestValues[key] : owner.getValue(key)?.get();
3497
3526
  /**
3498
3527
  * Whether this animation should join its element's transform group.
@@ -3531,7 +3560,12 @@
3531
3560
  /**
3532
3561
  * Layout animations write transform every frame.
3533
3562
  */
3534
- !owner.projection?.isProjecting());
3563
+ !owner.projection?.isProjecting() &&
3564
+ /**
3565
+ * A mixer that blends these values makes them every frame. It's
3566
+ * asked last, as it's user code.
3567
+ */
3568
+ !needsMixer(options));
3535
3569
  }
3536
3570
  class TransformAnimation extends JSAnimation {
3537
3571
  constructor(options) {
@@ -3888,9 +3922,8 @@
3888
3922
  let shared;
3889
3923
  let progressAt;
3890
3924
  for (const track of moving) {
3891
- const values = at.map((t) => track.numberAt(t));
3892
- const [first] = values;
3893
- const changes = values.map((value) => value - first);
3925
+ const first = track.numberAt(from);
3926
+ const changes = at.map((t) => track.numberAt(t) - first);
3894
3927
  const end = changes[changes.length - 1];
3895
3928
  /**
3896
3929
  * A value that ends only a tiny way from where it started, as
@@ -3904,25 +3937,20 @@
3904
3937
  : end;
3905
3938
  const progress = changes.map((change) => range ? change / range : change);
3906
3939
  /**
3907
- * A value that doesn't change, like one still in its delay,
3908
- * fits any easing. One that changes but ends where it started
3909
- * fits none.
3940
+ * The first value that moves sets the easing, and every other
3941
+ * one must follow it. A value that doesn't change, like one
3942
+ * still in its delay, fits any easing. One that changes but
3943
+ * ends where it started fits none.
3910
3944
  */
3911
- if (!range) {
3912
- if (progress.some((p) => !(Math.abs(p) <= easingTolerance))) {
3913
- return;
3914
- }
3915
- }
3916
- else if (!shared) {
3945
+ if (range && !shared) {
3917
3946
  shared = progress;
3918
3947
  progressAt = (t) => (track.numberAt(t) - first) / range;
3919
3948
  }
3920
- else if (progress.some((p, i) => !(Math.abs(p - shared[i]) <= easingTolerance))) {
3949
+ else if (progress.some((p, i) => !isNear$1(p, range && shared[i]))) {
3921
3950
  return;
3922
3951
  }
3923
3952
  }
3924
- if (!shared ||
3925
- shared.every((p, i) => Math.abs(p - position(at[i])) <= easingTolerance)) {
3953
+ if (!shared || shared.every((p, i) => isNear$1(p, position(at[i])))) {
3926
3954
  return "linear";
3927
3955
  }
3928
3956
  /**
@@ -4009,7 +4037,7 @@
4009
4037
  /**
4010
4038
  * A WAAPI animation of the composed transform between two timestamps.
4011
4039
  */
4012
- const run = (moving, from, to, options = { fill: "both" }, settled, nativeEasing) => {
4040
+ const run = (moving, from, to, nativeEasing, options = { fill: "both" }, settled) => {
4013
4041
  const bounds = moving.flatMap((track) => track.boundaries(from, to));
4014
4042
  const easing = nativeEasing || getEasing(moving, from, to, bounds);
4015
4043
  const times = easing ? [from, to] : sample(moving, from, to, bounds);
@@ -4095,9 +4123,7 @@
4095
4123
  if (byKey.has(key))
4096
4124
  return true;
4097
4125
  const value = (statics[key] = read(owner, key));
4098
- return (value !== undefined &&
4099
- parseFloat(value) !==
4100
- (key.startsWith("scale") ? 1 : 0));
4126
+ return value !== undefined && !isDefaultTransform(key, value);
4101
4127
  });
4102
4128
  stop(false);
4103
4129
  /**
@@ -4108,7 +4134,7 @@
4108
4134
  try {
4109
4135
  if (tween && tween[1] > now) {
4110
4136
  const [from, to, easing] = tween;
4111
- add(run(moving, from, to, undefined, undefined, easing), from - now, to - from);
4137
+ add(run(moving, from, to, easing), from - now, to - from);
4112
4138
  }
4113
4139
  else if (end > now) {
4114
4140
  add(run(moving, now, end), 0, end - now);
@@ -4125,7 +4151,7 @@
4125
4151
  * seamlessly, and start it part way through.
4126
4152
  */
4127
4153
  const from = cycleStart + Math.floor((end - cycleStart) / cycle) * cycle;
4128
- add(run(looping, from, from + cycle, {
4154
+ add(run(looping, from, from + cycle, undefined, {
4129
4155
  iterations: Infinity,
4130
4156
  iterationStart: (end - from) / cycle,
4131
4157
  /**
@@ -4264,10 +4290,7 @@
4264
4290
  isScheduled = false;
4265
4291
  if (isEnded)
4266
4292
  return;
4267
- tracks.forEach(({ state }, track) => {
4268
- if (state === "idle" || state === "finished")
4269
- tracks.delete(track);
4270
- });
4293
+ tracks.forEach((track) => hasEnded(track) && tracks.delete(track));
4271
4294
  if (end())
4272
4295
  return;
4273
4296
  for (const track of tracks) {
@@ -4327,9 +4350,7 @@
4327
4350
  * Only drop the stopped animation. Others can be idle because
4328
4351
  * they're part way through play(), and are about to run.
4329
4352
  */
4330
- const { state } = track;
4331
- if (state === "idle" || state === "finished")
4332
- tracks.delete(track);
4353
+ hasEnded(track) && tracks.delete(track);
4333
4354
  end() || group.schedule(true);
4334
4355
  },
4335
4356
  /**
@@ -4360,6 +4381,7 @@
4360
4381
  built = new Map();
4361
4382
  stop();
4362
4383
  tracks.forEach((track) => track.state === "running" && track.js?.start());
4384
+ return true;
4363
4385
  },
4364
4386
  };
4365
4387
  transformGroups.set(element, group);
@@ -4885,10 +4907,19 @@
4885
4907
  * effects, and resets velocity to `0`.
4886
4908
  */
4887
4909
  jump(v, endAnimation = true) {
4910
+ if (endAnimation) {
4911
+ /**
4912
+ * Stop first, as stopping a running animation can set the
4913
+ * value it's showing, which would replace v. Marking the value
4914
+ * as updated now skips a JS animation's catch-up tick on stop,
4915
+ * which could otherwise complete it.
4916
+ */
4917
+ this.updatedAt = time.now();
4918
+ this.stop();
4919
+ }
4888
4920
  this.updateAndNotify(v);
4889
4921
  this.prev = v;
4890
4922
  this.prevUpdatedAt = this.prevFrameValue = undefined;
4891
- endAnimation && this.stop();
4892
4923
  this.stopPassiveEffect?.();
4893
4924
  }
4894
4925
  dirty() {
@@ -5107,6 +5138,7 @@
5107
5138
  "from",
5108
5139
  "elapsed",
5109
5140
  "out",
5141
+ "mixer",
5110
5142
  ]);
5111
5143
  /**
5112
5144
  * Decide whether a transition is defined on a given Transition.
@@ -6663,6 +6695,20 @@
6663
6695
  else if (key.startsWith("attr")) {
6664
6696
  return addAttrValue(element, state, key, value, convertAttrKey(key));
6665
6697
  }
6698
+ else if (key === "d") {
6699
+ /**
6700
+ * `d` is a CSS property in Chrome and Firefox, but only as
6701
+ * `path(...)`, so path data goes to the attribute, taken out of
6702
+ * `path(...)` if it's written that way. Path data itself has no
6703
+ * quotes or brackets.
6704
+ */
6705
+ return state.set(key, value, () => {
6706
+ const d = value.get()?.replace(/path\(|["')]/g, "");
6707
+ d === undefined
6708
+ ? element.removeAttribute(key)
6709
+ : element.setAttribute(key, d);
6710
+ });
6711
+ }
6666
6712
  /**
6667
6713
  * Transforms and origins aren't CSS properties (bar `x`/`y` in some
6668
6714
  * browsers), so check them by name rather than `key in element.style`.
@@ -6919,6 +6965,10 @@
6919
6965
  }
6920
6966
  return viewportBox;
6921
6967
  }
6968
+ const measureDocumentScroll = () => ({
6969
+ x: document.documentElement.scrollLeft || document.body?.scrollLeft || 0,
6970
+ y: document.documentElement.scrollTop || document.body?.scrollTop || 0,
6971
+ });
6922
6972
 
6923
6973
  const noProps = {};
6924
6974
  const getElementEffect = (element) => (isSVGElement(element)
@@ -7088,6 +7138,7 @@
7088
7138
  const statsBuffer = {
7089
7139
  value: null,
7090
7140
  addProjectionMetrics: null,
7141
+ countProjection: undefined,
7091
7142
  };
7092
7143
 
7093
7144
  /**
@@ -7110,6 +7161,12 @@
7110
7161
  return delay(callback, secondsToMilliseconds(timeout));
7111
7162
  }
7112
7163
 
7164
+ function isRefObject(ref) {
7165
+ return (ref &&
7166
+ typeof ref === "object" &&
7167
+ Object.prototype.hasOwnProperty.call(ref, "current"));
7168
+ }
7169
+
7113
7170
  /**
7114
7171
  * Checks if an element is specifically an SVGSVGElement (the root SVG element)
7115
7172
  * in a way that works across iframes
@@ -7324,6 +7381,27 @@
7324
7381
  removeAxisTransforms(box.y, transforms, yKeys, originBox?.y, sourceBox?.y);
7325
7382
  }
7326
7383
 
7384
+ const userAgentContains = (string) => typeof navigator !== "undefined" &&
7385
+ navigator.userAgent &&
7386
+ navigator.userAgent.toLowerCase().includes(string);
7387
+ /**
7388
+ * Measured bounding boxes must be rounded in Safari and
7389
+ * left untouched in Chrome, otherwise non-integer layouts within scaled-up elements
7390
+ * can appear to jump.
7391
+ */
7392
+ const roundPoint = userAgentContains("applewebkit/") && !userAgentContains("chrome/")
7393
+ ? Math.round
7394
+ : noop;
7395
+ function roundAxis(axis) {
7396
+ // Round to the nearest .5 pixels to support subpixel layouts
7397
+ axis.min = roundPoint(axis.min);
7398
+ axis.max = roundPoint(axis.max);
7399
+ }
7400
+ function roundBox(box) {
7401
+ roundAxis(box.x);
7402
+ roundAxis(box.y);
7403
+ }
7404
+
7327
7405
  const createAxisDelta = () => ({
7328
7406
  translate: 0,
7329
7407
  scale: 1,
@@ -7620,11 +7698,6 @@
7620
7698
  hasEverUpdated: false,
7621
7699
  };
7622
7700
 
7623
- const metrics = {
7624
- nodes: 0,
7625
- calculatedTargetDeltas: 0,
7626
- calculatedProjections: 0,
7627
- };
7628
7701
  /**
7629
7702
  * Transforms that distort an element's layout, so must be reset before measuring.
7630
7703
  */
@@ -7646,7 +7719,15 @@
7646
7719
  }
7647
7720
  }
7648
7721
  }
7649
- const demoteTransforms = ({ instance }) => instance && transformGroups.get(instance)?.demote();
7722
+ /**
7723
+ * Move accelerated transforms back to the main thread. The WAAPI animation
7724
+ * hid startUpdate()'s reset of rotations, skews and z, and moving off it
7725
+ * writes the whole transform back, so reset them again before this node
7726
+ * is measured.
7727
+ */
7728
+ const demoteTransforms = (node) => node.instance &&
7729
+ transformGroups.get(node.instance)?.demote() &&
7730
+ node.resetSkewAndRotation();
7650
7731
  function cancelTreeOptimisedTransformAnimations(projectionNode) {
7651
7732
  projectionNode.hasCheckedOptimisedAppear = true;
7652
7733
  if (projectionNode.root === projectionNode)
@@ -7664,6 +7745,13 @@
7664
7745
  cancelTreeOptimisedTransformAnimations(parent);
7665
7746
  }
7666
7747
  }
7748
+ function getClosestProjectingNode(visualElement) {
7749
+ if (!visualElement)
7750
+ return undefined;
7751
+ return visualElement.options.allowProjection !== false
7752
+ ? visualElement.projection
7753
+ : getClosestProjectingNode(visualElement.parent);
7754
+ }
7667
7755
  function createProjectionNode({ attachResizeListener, defaultParent, measureScroll, checkIsScrollRoot, resetTransform, }) {
7668
7756
  /**
7669
7757
  * Correctors registered via addScaleCorrector before projection loaded
@@ -7676,6 +7764,57 @@
7676
7764
  ...scaleCorrectors,
7677
7765
  });
7678
7766
  return class ProjectionNode {
7767
+ /**
7768
+ * Create the projection node of a `motion` component's VisualElement,
7769
+ * from its props. This runs while the component renders, before its
7770
+ * children. It's here rather than in the component so React
7771
+ * bundles only include it with the features that supply a
7772
+ * ProjectionNode. Vanilla bundles that use the projection node
7773
+ * (drag(), animateLayout()) include it too, as a class static
7774
+ * can't be tree-shaken.
7775
+ */
7776
+ static create(visualElement, props, initialPromotionConfig) {
7777
+ const { layoutId, layout, drag, dragConstraints, layoutScroll, layoutRoot, layoutAnchor, layoutCrossfade, } = props;
7778
+ const projection = (visualElement.projection = new this(visualElement.latestValues, props["data-framer-portal-id"]
7779
+ ? undefined
7780
+ : getClosestProjectingNode(visualElement.parent)));
7781
+ projection.setOptions({
7782
+ layoutId,
7783
+ layout,
7784
+ alwaysMeasureLayout: Boolean(drag) ||
7785
+ (dragConstraints && isRefObject(dragConstraints)),
7786
+ visualElement,
7787
+ /**
7788
+ * TODO: Update options in an effect. This could be tricky as it'll be too late
7789
+ * to update by the time layout animations run.
7790
+ * We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
7791
+ * ensuring it gets called if there's no potential layout animations.
7792
+ *
7793
+ */
7794
+ animationType: typeof layout === "string" ? layout : "both",
7795
+ initialPromotionConfig,
7796
+ crossfade: layoutCrossfade,
7797
+ layoutScroll,
7798
+ layoutRoot,
7799
+ layoutAnchor,
7800
+ });
7801
+ /**
7802
+ * This node will take over from its layoutId's current lead, so snapshot
7803
+ * the lead now, while rendering. By the time the lead unmounts (or this
7804
+ * node mounts) React may already have committed DOM changes that come
7805
+ * before it in the tree, like a sibling changing height, and a snapshot
7806
+ * taken then starts the animation from a position never seen on screen.
7807
+ *
7808
+ * If React discards this render (e.g. it suspends), nothing else
7809
+ * completes the update, so check it after the next frame as an
7810
+ * unmounting node does.
7811
+ */
7812
+ const lead = projection.getStack()?.lead;
7813
+ if (lead && !projection.root.isUpdateBlocked()) {
7814
+ lead.willUpdate();
7815
+ lead.scheduleCheckAfterUnmount();
7816
+ }
7817
+ }
7679
7818
  constructor(latestValues = {}, parent = defaultParent?.()) {
7680
7819
  /**
7681
7820
  * A unique ID generated for every projection node.
@@ -7788,23 +7927,14 @@
7788
7927
  */
7789
7928
  this.updateProjection = () => {
7790
7929
  this.projectionUpdateScheduled = false;
7791
- /**
7792
- * Reset debug counts. Manually resetting rather than creating a new
7793
- * object each frame.
7794
- */
7795
- if (statsBuffer.value) {
7796
- metrics.nodes =
7797
- metrics.calculatedTargetDeltas =
7798
- metrics.calculatedProjections =
7799
- 0;
7800
- }
7801
7930
  this.nodes.forEach(propagateDirtyNodes);
7802
7931
  this.nodes.forEach(resolveTargetDelta);
7803
7932
  this.nodes.forEach(calcProjection);
7804
7933
  this.nodes.forEach(cleanDirtyNodes);
7805
- if (statsBuffer.addProjectionMetrics) {
7806
- statsBuffer.addProjectionMetrics(metrics);
7807
- }
7934
+ /**
7935
+ * Record this frame's debug counts, if recordStats() is running.
7936
+ */
7937
+ statsBuffer.countProjection?.();
7808
7938
  };
7809
7939
  /**
7810
7940
  * Frame calculations
@@ -8520,9 +8650,7 @@
8520
8650
  /**
8521
8651
  * Increase debug counter for resolved target deltas
8522
8652
  */
8523
- if (statsBuffer.value) {
8524
- metrics.calculatedTargetDeltas++;
8525
- }
8653
+ statsBuffer.countProjection?.(1); // calculatedTargetDeltas
8526
8654
  }
8527
8655
  getClosestProjectingParent() {
8528
8656
  if (!this.parent ||
@@ -8670,9 +8798,7 @@
8670
8798
  /**
8671
8799
  * Increase debug counter for recalculated projections
8672
8800
  */
8673
- if (statsBuffer.value) {
8674
- metrics.calculatedProjections++;
8675
- }
8801
+ statsBuffer.countProjection?.(2); // calculatedProjections
8676
8802
  }
8677
8803
  hide() {
8678
8804
  this.isVisible = false;
@@ -9199,9 +9325,7 @@
9199
9325
  /**
9200
9326
  * Increase debug counter for nodes encountered this frame
9201
9327
  */
9202
- if (statsBuffer.value) {
9203
- metrics.nodes++;
9204
- }
9328
+ statsBuffer.countProjection?.(0); // nodes
9205
9329
  if (!node.parent)
9206
9330
  return;
9207
9331
  /**
@@ -9332,26 +9456,6 @@
9332
9456
  duration: 0.45,
9333
9457
  ease: [0.4, 0, 0.1, 1],
9334
9458
  };
9335
- const userAgentContains = (string) => typeof navigator !== "undefined" &&
9336
- navigator.userAgent &&
9337
- navigator.userAgent.toLowerCase().includes(string);
9338
- /**
9339
- * Measured bounding boxes must be rounded in Safari and
9340
- * left untouched in Chrome, otherwise non-integer layouts within scaled-up elements
9341
- * can appear to jump.
9342
- */
9343
- const roundPoint = userAgentContains("applewebkit/") && !userAgentContains("chrome/")
9344
- ? Math.round
9345
- : noop;
9346
- function roundAxis(axis) {
9347
- // Round to the nearest .5 pixels to support subpixel layouts
9348
- axis.min = roundPoint(axis.min);
9349
- axis.max = roundPoint(axis.max);
9350
- }
9351
- function roundBox(box) {
9352
- roundAxis(box.x);
9353
- roundAxis(box.y);
9354
- }
9355
9459
  /**
9356
9460
  * Align an axis to the start of another, keeping the length of a layout axis.
9357
9461
  * The length is read first, as output can be the layout axis itself.
@@ -9376,14 +9480,7 @@
9376
9480
  const DocumentProjectionNode =
9377
9481
  /* @__PURE__ */ createProjectionNode({
9378
9482
  attachResizeListener: (ref, notify) => addDomEvent(ref, "resize", notify),
9379
- measureScroll: () => ({
9380
- x: document.documentElement.scrollLeft ||
9381
- document.body?.scrollLeft ||
9382
- 0,
9383
- y: document.documentElement.scrollTop ||
9384
- document.body?.scrollTop ||
9385
- 0,
9386
- }),
9483
+ measureScroll: measureDocumentScroll,
9387
9484
  checkIsScrollRoot: () => true,
9388
9485
  });
9389
9486
 
@@ -10156,6 +10253,124 @@
10156
10253
  }
10157
10254
  }
10158
10255
 
10256
+ /**
10257
+ * Get the VisualElement that imperative APIs like animateLayout() and
10258
+ * drag() render an element with, creating one if it has none, ready for
10259
+ * a projection node to be attached.
10260
+ */
10261
+ function resolveVisualElement(element) {
10262
+ let visualElement = visualElementStore.get(element);
10263
+ if (!visualElement) {
10264
+ visualElement = new HTMLVisualElement({
10265
+ props: {},
10266
+ presenceContext: null,
10267
+ visualState: {
10268
+ latestValues: {},
10269
+ renderState: {
10270
+ transform: {},
10271
+ transformOrigin: {},
10272
+ style: {},
10273
+ vars: {},
10274
+ },
10275
+ },
10276
+ }, { allowProjection: true });
10277
+ /**
10278
+ * Values animate() has been rendering on this element move to
10279
+ * the VisualElement, so the layout animation composes with
10280
+ * them instead of overwriting them.
10281
+ */
10282
+ handOffElementState(element, visualElement);
10283
+ }
10284
+ /**
10285
+ * A first-time element may carry a projection transform in its
10286
+ * inline style (e.g. it was cloned from an element mid-animation).
10287
+ * That transform isn't tracked in latestValues so the engine can't
10288
+ * reset it before measuring - clear it now so the first layout
10289
+ * measurement isn't inflated.
10290
+ */
10291
+ if (element.style.transform && !hasTransform(visualElement.latestValues)) {
10292
+ element.style.transform = "";
10293
+ }
10294
+ return visualElement;
10295
+ }
10296
+
10297
+ /**
10298
+ * animateLayout() registers a function here that creates nodes in the
10299
+ * layout projection tree. When it's in the bundle, drag() uses those, so
10300
+ * the two work together (e.g. a drag follows a layout change mid-gesture).
10301
+ * Otherwise drag() makes do with a node that only measures. Bundles that
10302
+ * don't tree-shake (dev pre-bundling, CDN builds) always register it, and
10303
+ * a drag() started before animateLayout() is lazy loaded keeps its node.
10304
+ */
10305
+ const layoutProjection = {};
10306
+ /**
10307
+ * The document root of measurement-only nodes. Like the projection tree's
10308
+ * root, it keeps its scroll until drag controls clear it to re-measure.
10309
+ */
10310
+ const root = {
10311
+ updateScroll: () => {
10312
+ root.scroll || (root.scroll = { offset: measureDocumentScroll() });
10313
+ },
10314
+ };
10315
+ /**
10316
+ * The part of a projection node that drag controls use: measuring the
10317
+ * element's page box, for its layout and constraints. It never projects
10318
+ * or joins a layout update.
10319
+ */
10320
+ const createMeasureNode = (element) => {
10321
+ const visualElement = resolveVisualElement(element);
10322
+ const measureListeners = new SubscriptionManager();
10323
+ const node = {
10324
+ instance: element,
10325
+ root,
10326
+ path: [root],
10327
+ setOptions: noop,
10328
+ /**
10329
+ * Truthy like the full node's method, which HTMLVisualElement reads
10330
+ * (rather than calls) to default transform values.
10331
+ */
10332
+ isProjecting: noop,
10333
+ applyProjectionStyles: noop,
10334
+ removeElementScroll: (box) => box,
10335
+ updateLayout: () => {
10336
+ const layoutBox = measurePageBox(element, root, visualElement.getTransformPagePoint());
10337
+ roundBox(layoutBox);
10338
+ node.layout = { layoutBox };
10339
+ measureListeners.notify();
10340
+ },
10341
+ addEventListener: (name, handler) => name === "measure" ? measureListeners.add(handler) : noop,
10342
+ /**
10343
+ * Detach from the VisualElement too, so it doesn't later remount
10344
+ * (or a `motion` component's layout feature adopt) a node that
10345
+ * can't do either.
10346
+ */
10347
+ unmount: () => {
10348
+ node.instance = undefined;
10349
+ if (visualElement.projection === node) {
10350
+ visualElement.projection = undefined;
10351
+ }
10352
+ },
10353
+ };
10354
+ visualElement.projection = node;
10355
+ visualElement.current || visualElement.mount(element);
10356
+ return [node, node.unmount];
10357
+ };
10358
+ /**
10359
+ * Get a projection node for drag() to measure an element with, and a
10360
+ * function that releases it. Reuses a node a `motion` component or
10361
+ * animateLayout() owns.
10362
+ */
10363
+ const acquireDragNode = (element) => {
10364
+ const owned = visualElementStore.get(element)?.projection;
10365
+ /**
10366
+ * A VisualElement can outlive its node (e.g. an unmounted `motion`
10367
+ * component's), so only reuse a node that's still mounted.
10368
+ */
10369
+ return owned?.instance
10370
+ ? [owned, noop]
10371
+ : (layoutProjection.create || createMeasureNode)(element);
10372
+ };
10373
+
10159
10374
  const layoutSelector = "[data-layout],[data-layout-id]";
10160
10375
  /**
10161
10376
  * All imperatively-created projection nodes live in one persistent tree,
@@ -10208,21 +10423,6 @@
10208
10423
  }
10209
10424
  return undefined;
10210
10425
  }
10211
- function createVisualElement() {
10212
- return new HTMLVisualElement({
10213
- props: {},
10214
- presenceContext: null,
10215
- visualState: {
10216
- latestValues: {},
10217
- renderState: {
10218
- transform: {},
10219
- transformOrigin: {},
10220
- style: {},
10221
- vars: {},
10222
- },
10223
- },
10224
- }, { allowProjection: true });
10225
- }
10226
10426
  function readNodeOptions(element, transition) {
10227
10427
  const layoutAttr = element.getAttribute("data-layout");
10228
10428
  const layoutId = element.getAttribute("data-layout-id") ?? undefined;
@@ -10238,27 +10438,7 @@
10238
10438
  function prepareNode(element, transition) {
10239
10439
  let node = layoutNodes.get(element);
10240
10440
  if (!node) {
10241
- let visualElement = visualElementStore.get(element);
10242
- if (!visualElement) {
10243
- visualElement = createVisualElement();
10244
- /**
10245
- * Values animate() has been rendering on this element move to
10246
- * the VisualElement, so the layout animation composes with
10247
- * them instead of overwriting them.
10248
- */
10249
- handOffElementState(element, visualElement);
10250
- }
10251
- /**
10252
- * A first-time element may carry a projection transform in its
10253
- * inline style (e.g. it was cloned from an element mid-animation).
10254
- * That transform isn't tracked in latestValues so the engine can't
10255
- * reset it before measuring - clear it now so the first layout
10256
- * measurement isn't inflated.
10257
- */
10258
- if (element.style.transform &&
10259
- !hasTransform(visualElement.latestValues)) {
10260
- element.style.transform = "";
10261
- }
10441
+ const visualElement = resolveVisualElement(element);
10262
10442
  node = new HTMLProjectionNode(visualElement.latestValues, getProjectionParent(element));
10263
10443
  visualElement.projection = node;
10264
10444
  node.setOptions({
@@ -10278,10 +10458,26 @@
10278
10458
  return node;
10279
10459
  }
10280
10460
  /**
10281
- * Get a projection node for an element, so imperative gestures like
10282
- * drag() can measure its layout. Reuses a node owned by a `motion`
10283
- * component or animateLayout(), otherwise creates one in the shared
10284
- * imperative tree. Returns the node and a function that releases it.
10461
+ * Create a node for an element in the shared imperative tree. Returns the
10462
+ * node and a function that releases it.
10463
+ */
10464
+ function createLayoutNode(element) {
10465
+ const node = prepareNode(element);
10466
+ return [
10467
+ node,
10468
+ () => layoutNodes.get(element) === node && dropNode(element, node),
10469
+ ];
10470
+ }
10471
+ /**
10472
+ * Let drag() create its nodes in the shared imperative tree, so drag
10473
+ * gestures and layout animations work together.
10474
+ */
10475
+ layoutProjection.create = createLayoutNode;
10476
+ /**
10477
+ * Get a projection node for an element, so motion/remix can measure and
10478
+ * animate its layout. Reuses a node owned by a `motion` component or
10479
+ * animateLayout(), otherwise creates one in the shared imperative tree.
10480
+ * Returns the node and a function that releases it.
10285
10481
  */
10286
10482
  function acquireProjectionNode(element) {
10287
10483
  const owned = layoutNodes.get(element) || visualElementStore.get(element)?.projection;
@@ -10289,13 +10485,7 @@
10289
10485
  * A VisualElement can outlive a node we released (e.g. a stopped
10290
10486
  * drag()), so only reuse a node that's still mounted.
10291
10487
  */
10292
- if (owned?.instance)
10293
- return [owned, () => { }];
10294
- const node = prepareNode(element);
10295
- return [
10296
- node,
10297
- () => layoutNodes.get(element) === node && dropNode(element, node),
10298
- ];
10488
+ return owned?.instance ? [owned, () => { }] : createLayoutNode(element);
10299
10489
  }
10300
10490
  function sortDocumentOrder(elements) {
10301
10491
  return [...elements].sort((a, b) => a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
@@ -10315,6 +10505,8 @@
10315
10505
  }
10316
10506
  function flushPendingBuilders() {
10317
10507
  const builders = pendingBuilders;
10508
+ if (!builders)
10509
+ return;
10318
10510
  pendingBuilders = undefined;
10319
10511
  flushPendingFrame();
10320
10512
  /**
@@ -10379,6 +10571,13 @@
10379
10571
  };
10380
10572
  updatePromises.length ? Promise.all(updatePromises).then(commit) : commit();
10381
10573
  }
10574
+ /**
10575
+ * Snapshot every pending animateLayout() now, rather than on the next
10576
+ * microtask. For renderers that tell us a DOM update is about to happen
10577
+ * and then make it synchronously (e.g. a beforeUpdate/commit pair), with
10578
+ * updateDom returning a promise that resolves on commit.
10579
+ */
10580
+ const flushLayoutSnapshots = flushPendingBuilders;
10382
10581
  class LayoutAnimationBuilder {
10383
10582
  constructor(scope, updateDom, defaultOptions) {
10384
10583
  this.scope = scope;
@@ -11776,13 +11975,13 @@
11776
11975
  }
11777
11976
  const cleanups = resolveElements(elementOrSelector).map((element) => {
11778
11977
  scope?.track(element);
11779
- const [node, release] = acquireProjectionNode(element);
11978
+ const [node, release] = acquireDragNode(element);
11780
11979
  /**
11781
11980
  * Measure layout even when not animating it, as constraints and
11782
11981
  * layout-shift correction are computed from it.
11783
11982
  */
11784
11983
  node.setOptions({ alwaysMeasureLayout: true });
11785
- const controls = new VisualElementDragControls(node.options.visualElement, () => props);
11984
+ const controls = new VisualElementDragControls(visualElementStore.get(element), () => props);
11786
11985
  const removeListeners = controls.addListeners();
11787
11986
  const removeStyles = applyDragStyles(element, options.axis);
11788
11987
  return () => {
@@ -12188,9 +12387,19 @@
12188
12387
  return scope;
12189
12388
  }
12190
12389
 
12390
+ /**
12391
+ * The metrics projection counts, in the order of the index it passes to
12392
+ * statsBuffer.countProjection.
12393
+ */
12394
+ const projectionMetrics = [
12395
+ "nodes",
12396
+ "calculatedTargetDeltas",
12397
+ "calculatedProjections",
12398
+ ];
12191
12399
  function clearStatsBuffer() {
12192
12400
  statsBuffer.value = null;
12193
12401
  statsBuffer.addProjectionMetrics = null;
12402
+ statsBuffer.countProjection = undefined;
12194
12403
  }
12195
12404
  function recordStats() {
12196
12405
  if (statsBuffer.value) {
@@ -12211,6 +12420,27 @@
12211
12420
  layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
12212
12421
  layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
12213
12422
  };
12423
+ /**
12424
+ * Counting lives here rather than in projection, so bundles that never
12425
+ * call recordStats() don't ship it.
12426
+ */
12427
+ const metrics = {
12428
+ nodes: 0,
12429
+ calculatedTargetDeltas: 0,
12430
+ calculatedProjections: 0,
12431
+ };
12432
+ buffer.countProjection = (metric) => {
12433
+ if (metric !== undefined) {
12434
+ metrics[projectionMetrics[metric]]++;
12435
+ }
12436
+ else {
12437
+ buffer.addProjectionMetrics(metrics);
12438
+ metrics.nodes =
12439
+ metrics.calculatedTargetDeltas =
12440
+ metrics.calculatedProjections =
12441
+ 0;
12442
+ }
12443
+ };
12214
12444
  }
12215
12445
 
12216
12446
  function getOriginIndex(from, total) {
@@ -14569,6 +14799,11 @@
14569
14799
  const valueKeyframesAsList = keyframesAsList(valueKeyframes);
14570
14800
  const { delay = 0, times = defaultOffset$1(valueKeyframesAsList), type = defaultTransition.type || "keyframes", repeat, repeatType, repeatDelay = 0, ...remainingTransition } = valueTransition;
14571
14801
  let { ease = defaultTransition.ease || "easeOut", duration } = valueTransition;
14802
+ /**
14803
+ * The value animates as one, so it takes the first mixer
14804
+ * given for it, or else the default transition's.
14805
+ */
14806
+ valueSequence.mixer || (valueSequence.mixer = valueTransition.mixer);
14572
14807
  /**
14573
14808
  * Resolve stagger() if defined.
14574
14809
  */
@@ -14802,6 +15037,7 @@
14802
15037
  duration: totalDuration,
14803
15038
  ease: valueEasing,
14804
15039
  times: valueOffset,
15040
+ mixer: valueSequence.mixer || defaultTransition.mixer,
14805
15041
  ...sequenceTransition,
14806
15042
  };
14807
15043
  }
@@ -14968,6 +15204,16 @@
14968
15204
  this.isDone = false;
14969
15205
  this.isRebuilding = false;
14970
15206
  this.playbackSpeed = 1;
15207
+ /**
15208
+ * The scopeMotion() scope this was created in. Rebuilds run in it, so
15209
+ * selectors resolve in its root and cancel() restores what they
15210
+ * animate.
15211
+ */
15212
+ this.motionScope = activeScope.current;
15213
+ /**
15214
+ * Set when the scope stops, so later changes don't restart it.
15215
+ */
15216
+ this.isKilled = false;
14971
15217
  /**
14972
15218
  * True while this plays the animations it was created with, before
14973
15219
  * any change compiles it as a sequence.
@@ -15197,7 +15443,7 @@
15197
15443
  * reflect it.
15198
15444
  */
15199
15445
  flush() {
15200
- this.isDirty && !this.parent && this.build();
15446
+ this.isDirty && !this.parent && !this.isKilled && this.build();
15201
15447
  }
15202
15448
  adopt(parent) {
15203
15449
  if (this.group) {
@@ -15243,7 +15489,15 @@
15243
15489
  Object.assign(playback, options);
15244
15490
  const markers = new Map();
15245
15491
  const info = {};
15246
- const animations = animateSequence(this.toSequence(markers), { defaultTransition, ...playback }, scope, info);
15492
+ const prevScope = activeScope.current;
15493
+ activeScope.current = this.motionScope;
15494
+ let animations;
15495
+ try {
15496
+ animations = animateSequence(this.toSequence(markers), { defaultTransition, ...playback }, scope, info);
15497
+ }
15498
+ finally {
15499
+ activeScope.current = prevScope;
15500
+ }
15247
15501
  const labels = info.labels;
15248
15502
  let duration = playback.duration ?? info.duration;
15249
15503
  /**
@@ -15415,6 +15669,15 @@
15415
15669
  this.finished = new Promise((resolve) => {
15416
15670
  this.resolveFinished = resolve;
15417
15671
  });
15672
+ /**
15673
+ * Stay in the scope while playing, including after a change
15674
+ * restarts a finished timeline.
15675
+ */
15676
+ const remove = this.motionScope?.add(() => {
15677
+ this.isKilled = true;
15678
+ this.stop();
15679
+ });
15680
+ remove && this.finished.then(remove);
15418
15681
  }
15419
15682
  }
15420
15683
 
@@ -15431,8 +15694,7 @@
15431
15694
  * Implementation
15432
15695
  */
15433
15696
  function scopedAnimate(subjectOrSequence, optionsOrKeyframes, options) {
15434
- const motionScope = activeScope.current;
15435
- const scopeTransition = motionScope?.transition;
15697
+ const scopeTransition = activeScope.current?.transition;
15436
15698
  const inherited = {};
15437
15699
  if (reduceMotion !== undefined)
15438
15700
  inherited.reduceMotion = reduceMotion;
@@ -15481,9 +15743,6 @@
15481
15743
  : [subjectOrSequence, optionsOrKeyframes, transition]);
15482
15744
  timeline = new Timeline([segment], { ...inherited, onComplete, scope }, animateSubject(subjectOrSequence, optionsOrKeyframes, transition, scope));
15483
15745
  }
15484
- if (motionScope) {
15485
- timeline.finished.then(motionScope.add(() => timeline.stop()));
15486
- }
15487
15746
  return timeline;
15488
15747
  }
15489
15748
  return scopedAnimate;
@@ -16288,6 +16547,7 @@
16288
16547
  exports.VisualElement = VisualElement;
16289
16548
  exports.VisualElementDragControls = VisualElementDragControls;
16290
16549
  exports.acceleratedValues = acceleratedValues;
16550
+ exports.acquireProjectionNode = acquireProjectionNode;
16291
16551
  exports.activeScope = activeScope;
16292
16552
  exports.addAttrValue = addAttrValue;
16293
16553
  exports.addBuilderToScope = addBuilderToScope;
@@ -16414,6 +16674,7 @@
16414
16674
  exports.findEffect = findEffect;
16415
16675
  exports.findValueType = findValueType;
16416
16676
  exports.flushKeyframeResolvers = flushKeyframeResolvers;
16677
+ exports.flushLayoutSnapshots = flushLayoutSnapshots;
16417
16678
  exports.followValue = followValue;
16418
16679
  exports.frame = frame;
16419
16680
  exports.frameData = frameData;