motion-dom 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.
Files changed (53) hide show
  1. package/dist/cjs/index.js +380 -148
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/JSAnimation.mjs +7 -1
  4. package/dist/es/animation/JSAnimation.mjs.map +1 -1
  5. package/dist/es/animation/generators/keyframes.mjs +12 -3
  6. package/dist/es/animation/generators/keyframes.mjs.map +1 -1
  7. package/dist/es/animation/utils/is-transition-defined.mjs +1 -0
  8. package/dist/es/animation/utils/is-transition-defined.mjs.map +1 -1
  9. package/dist/es/animation/waapi/supports/waapi.mjs +3 -1
  10. package/dist/es/animation/waapi/supports/waapi.mjs.map +1 -1
  11. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +32 -35
  12. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -1
  13. package/dist/es/animation/waapi/transforms/groups.mjs.map +1 -1
  14. package/dist/es/animation/waapi/utils/needs-mixer.mjs +11 -0
  15. package/dist/es/animation/waapi/utils/needs-mixer.mjs.map +1 -0
  16. package/dist/es/effects/svg/index.mjs +14 -0
  17. package/dist/es/effects/svg/index.mjs.map +1 -1
  18. package/dist/es/gestures/drag/index.mjs +4 -3
  19. package/dist/es/gestures/drag/index.mjs.map +1 -1
  20. package/dist/es/gestures/drag/projection.mjs +85 -0
  21. package/dist/es/gestures/drag/projection.mjs.map +1 -0
  22. package/dist/es/index.mjs +1 -1
  23. package/dist/es/layout/LayoutAnimationBuilder.mjs +34 -51
  24. package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
  25. package/dist/es/layout/visual-element.mjs +48 -0
  26. package/dist/es/layout/visual-element.mjs.map +1 -0
  27. package/dist/es/projection/geometry/round.mjs +25 -0
  28. package/dist/es/projection/geometry/round.mjs.map +1 -0
  29. package/dist/es/projection/node/DocumentProjectionNode.mjs +2 -8
  30. package/dist/es/projection/node/DocumentProjectionNode.mjs.map +1 -1
  31. package/dist/es/projection/node/create-projection-node.mjs +77 -49
  32. package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
  33. package/dist/es/projection/utils/measure.mjs +5 -1
  34. package/dist/es/projection/utils/measure.mjs.map +1 -1
  35. package/dist/es/stats/buffer.mjs +1 -0
  36. package/dist/es/stats/buffer.mjs.map +1 -1
  37. package/dist/es/stats/index.mjs +31 -0
  38. package/dist/es/stats/index.mjs.map +1 -1
  39. package/dist/es/utils/interpolate.mjs +3 -2
  40. package/dist/es/utils/interpolate.mjs.map +1 -1
  41. package/dist/es/utils/is-ref-object.mjs +8 -0
  42. package/dist/es/utils/is-ref-object.mjs.map +1 -0
  43. package/dist/es/utils/transform.mjs.map +1 -1
  44. package/dist/es/value/index.mjs +10 -1
  45. package/dist/es/value/index.mjs.map +1 -1
  46. package/dist/index.d.ts +298 -232
  47. package/dist/motion-dom.dev.js +380 -148
  48. package/dist/motion-dom.js +1 -1
  49. package/dist/size-rollup-motion-value.js +1 -1
  50. package/dist/size-rollup-motion-value.js.map +1 -1
  51. package/dist/size-rollup-style-effect.js +1 -1
  52. package/dist/size-rollup-style-effect.js.map +1 -1
  53. package/package.json +2 -2
@@ -1308,10 +1308,11 @@
1308
1308
 
1309
1309
  function createMixers(output, ease, customMixer) {
1310
1310
  const mixers = [];
1311
- const mixerFactory = customMixer || motionUtils.MotionGlobalConfig.mix || mix;
1311
+ const mixerFactory = motionUtils.MotionGlobalConfig.mix || mix;
1312
1312
  const numMixers = output.length - 1;
1313
1313
  for (let i = 0; i < numMixers; i++) {
1314
- let mixer = mixerFactory(output[i], output[i + 1]);
1314
+ let mixer = customMixer?.(output[i], output[i + 1]) ||
1315
+ mixerFactory(output[i], output[i + 1]);
1315
1316
  if (ease) {
1316
1317
  const easingFunction = Array.isArray(ease) ? ease[i] || motionUtils.noop : ease;
1317
1318
  mixer = motionUtils.pipe(easingFunction, mixer);
@@ -1397,7 +1398,13 @@
1397
1398
  function defaultEasing(values, easing) {
1398
1399
  return values.map(() => easing || motionUtils.easeInOut).splice(0, values.length - 1);
1399
1400
  }
1400
- function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "easeInOut", }) {
1401
+ function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "easeInOut", mixer: transitionMixer, }) {
1402
+ /**
1403
+ * A mixer only blends values that aren't numbers. Numbers blend as
1404
+ * usual, as they do in springs, and so do the progress animations
1405
+ * behind layout and path animations, which take the same transition.
1406
+ */
1407
+ const customMixer = typeof keyframeValues[0] === "number" ? undefined : transitionMixer;
1401
1408
  /**
1402
1409
  * Easing functions can be externally defined as strings. Here we convert them
1403
1410
  * into actual functions.
@@ -1414,9 +1421,11 @@
1414
1421
  value: keyframeValues[0],
1415
1422
  };
1416
1423
  /**
1417
- * Fast setup path for two keyframes one easing.
1424
+ * Fast setup path for two keyframes one easing. interpolate() handles
1425
+ * a custom mixer, and falls back to the usual one where it declines.
1418
1426
  */
1419
- if (keyframeValues.length === 2 &&
1427
+ if (!customMixer &&
1428
+ keyframeValues.length === 2 &&
1420
1429
  !Array.isArray(easingFunctions) &&
1421
1430
  (!times ||
1422
1431
  times.length !== 2 ||
@@ -1449,6 +1458,7 @@
1449
1458
  ease: Array.isArray(easingFunctions)
1450
1459
  ? easingFunctions
1451
1460
  : defaultEasing(keyframeValues, easingFunctions),
1461
+ mixer: customMixer,
1452
1462
  });
1453
1463
  return {
1454
1464
  calculatedDuration: duration,
@@ -1656,9 +1666,15 @@
1656
1666
  if (generatorFactory !== keyframes) {
1657
1667
  motionUtils.invariant(keyframes$1.length <= 2, `Only two keyframes currently supported with spring and inertia animations. Trying to animate ${keyframes$1}`, "spring-two-frames");
1658
1668
  }
1669
+ /**
1670
+ * This runs again when a spring is flattened to attach it to a
1671
+ * timeline, and the keyframes generator blends values itself.
1672
+ */
1673
+ this.mixKeyframes = undefined;
1659
1674
  if (generatorFactory !== keyframes &&
1660
1675
  typeof keyframes$1[0] !== "number") {
1661
- this.mixKeyframes = motionUtils.pipe(percentToProgress, mix(keyframes$1[0], keyframes$1[1]));
1676
+ this.mixKeyframes = motionUtils.pipe(percentToProgress, options.mixer?.(keyframes$1[0], keyframes$1[1]) ||
1677
+ mix(keyframes$1[0], keyframes$1[1]));
1662
1678
  keyframes$1 = [0, 100];
1663
1679
  }
1664
1680
  const generator = generatorFactory(keyframes$1 === options.keyframes
@@ -3012,6 +3028,15 @@
3012
3028
  return false;
3013
3029
  }
3014
3030
 
3031
+ /**
3032
+ * Whether an animation needs its mixer, which WAAPI can't call. Numbers
3033
+ * blend as usual, and an animation whose mixer declines every pair runs
3034
+ * as it would without one.
3035
+ */
3036
+ const needsMixer = ({ mixer, keyframes }) => mixer &&
3037
+ typeof keyframes[0] !== "number" &&
3038
+ keyframes.some((keyframe, i) => i && mixer(keyframes[i - 1], keyframe));
3039
+
3015
3040
  const colorProperties = new Set([
3016
3041
  "color",
3017
3042
  "backgroundColor",
@@ -3071,7 +3096,8 @@
3071
3096
  !repeatDelay &&
3072
3097
  repeatType !== "mirror" &&
3073
3098
  damping !== 0 &&
3074
- type !== "inertia");
3099
+ type !== "inertia" &&
3100
+ !needsMixer(options));
3075
3101
  }
3076
3102
 
3077
3103
  /**
@@ -3170,18 +3196,21 @@
3170
3196
  * Named easings that are the same cubic-bezier in JS and CSS.
3171
3197
  */
3172
3198
  const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
3199
+ /**
3200
+ * Whether two eased progresses match. False if either is NaN.
3201
+ */
3202
+ const isNear$1 = (a, b) => Math.abs(a - b) <= easingTolerance;
3173
3203
  const byTime = (a, b) => a - b;
3174
3204
  /**
3175
3205
  * n + 1 evenly spaced timestamps, from one timestamp to another.
3176
3206
  */
3177
- const spread = (from, to, n) => {
3178
- const result = [];
3179
- for (let i = 0; i <= n; i++)
3180
- result.push(mixNumber$1(from, to, i / n));
3181
- return result;
3182
- };
3207
+ const spread = (from, to, n) => defaultOffset(Array(n + 1)).map((p) => mixNumber$1(from, to, p));
3183
3208
  const getOwner = (options) => options.motionValue.owner;
3184
3209
  const getGroup = (element) => transformGroups.get(element);
3210
+ /**
3211
+ * Whether an animation has ended, so can leave its group.
3212
+ */
3213
+ const hasEnded = ({ state }) => state === "idle" || state === "finished";
3185
3214
  const read = (owner, key) => owner.latestValues ? owner.latestValues[key] : owner.getValue(key)?.get();
3186
3215
  /**
3187
3216
  * Whether this animation should join its element's transform group.
@@ -3220,7 +3249,12 @@
3220
3249
  /**
3221
3250
  * Layout animations write transform every frame.
3222
3251
  */
3223
- !owner.projection?.isProjecting());
3252
+ !owner.projection?.isProjecting() &&
3253
+ /**
3254
+ * A mixer that blends these values makes them every frame. It's
3255
+ * asked last, as it's user code.
3256
+ */
3257
+ !needsMixer(options));
3224
3258
  }
3225
3259
  class TransformAnimation extends JSAnimation {
3226
3260
  constructor(options) {
@@ -3577,9 +3611,8 @@
3577
3611
  let shared;
3578
3612
  let progressAt;
3579
3613
  for (const track of moving) {
3580
- const values = at.map((t) => track.numberAt(t));
3581
- const [first] = values;
3582
- const changes = values.map((value) => value - first);
3614
+ const first = track.numberAt(from);
3615
+ const changes = at.map((t) => track.numberAt(t) - first);
3583
3616
  const end = changes[changes.length - 1];
3584
3617
  /**
3585
3618
  * A value that ends only a tiny way from where it started, as
@@ -3593,25 +3626,20 @@
3593
3626
  : end;
3594
3627
  const progress = changes.map((change) => range ? change / range : change);
3595
3628
  /**
3596
- * A value that doesn't change, like one still in its delay,
3597
- * fits any easing. One that changes but ends where it started
3598
- * fits none.
3629
+ * The first value that moves sets the easing, and every other
3630
+ * one must follow it. A value that doesn't change, like one
3631
+ * still in its delay, fits any easing. One that changes but
3632
+ * ends where it started fits none.
3599
3633
  */
3600
- if (!range) {
3601
- if (progress.some((p) => !(Math.abs(p) <= easingTolerance))) {
3602
- return;
3603
- }
3604
- }
3605
- else if (!shared) {
3634
+ if (range && !shared) {
3606
3635
  shared = progress;
3607
3636
  progressAt = (t) => (track.numberAt(t) - first) / range;
3608
3637
  }
3609
- else if (progress.some((p, i) => !(Math.abs(p - shared[i]) <= easingTolerance))) {
3638
+ else if (progress.some((p, i) => !isNear$1(p, range && shared[i]))) {
3610
3639
  return;
3611
3640
  }
3612
3641
  }
3613
- if (!shared ||
3614
- shared.every((p, i) => Math.abs(p - position(at[i])) <= easingTolerance)) {
3642
+ if (!shared || shared.every((p, i) => isNear$1(p, position(at[i])))) {
3615
3643
  return "linear";
3616
3644
  }
3617
3645
  /**
@@ -3698,7 +3726,7 @@
3698
3726
  /**
3699
3727
  * A WAAPI animation of the composed transform between two timestamps.
3700
3728
  */
3701
- const run = (moving, from, to, options = { fill: "both" }, settled, nativeEasing) => {
3729
+ const run = (moving, from, to, nativeEasing, options = { fill: "both" }, settled) => {
3702
3730
  const bounds = moving.flatMap((track) => track.boundaries(from, to));
3703
3731
  const easing = nativeEasing || getEasing(moving, from, to, bounds);
3704
3732
  const times = easing ? [from, to] : sample(moving, from, to, bounds);
@@ -3784,9 +3812,7 @@
3784
3812
  if (byKey.has(key))
3785
3813
  return true;
3786
3814
  const value = (statics[key] = read(owner, key));
3787
- return (value !== undefined &&
3788
- parseFloat(value) !==
3789
- (key.startsWith("scale") ? 1 : 0));
3815
+ return value !== undefined && !isDefaultTransform(key, value);
3790
3816
  });
3791
3817
  stop(false);
3792
3818
  /**
@@ -3797,7 +3823,7 @@
3797
3823
  try {
3798
3824
  if (tween && tween[1] > now) {
3799
3825
  const [from, to, easing] = tween;
3800
- add(run(moving, from, to, undefined, undefined, easing), from - now, to - from);
3826
+ add(run(moving, from, to, easing), from - now, to - from);
3801
3827
  }
3802
3828
  else if (end > now) {
3803
3829
  add(run(moving, now, end), 0, end - now);
@@ -3814,7 +3840,7 @@
3814
3840
  * seamlessly, and start it part way through.
3815
3841
  */
3816
3842
  const from = cycleStart + Math.floor((end - cycleStart) / cycle) * cycle;
3817
- add(run(looping, from, from + cycle, {
3843
+ add(run(looping, from, from + cycle, undefined, {
3818
3844
  iterations: Infinity,
3819
3845
  iterationStart: (end - from) / cycle,
3820
3846
  /**
@@ -3953,10 +3979,7 @@
3953
3979
  isScheduled = false;
3954
3980
  if (isEnded)
3955
3981
  return;
3956
- tracks.forEach(({ state }, track) => {
3957
- if (state === "idle" || state === "finished")
3958
- tracks.delete(track);
3959
- });
3982
+ tracks.forEach((track) => hasEnded(track) && tracks.delete(track));
3960
3983
  if (end())
3961
3984
  return;
3962
3985
  for (const track of tracks) {
@@ -4016,9 +4039,7 @@
4016
4039
  * Only drop the stopped animation. Others can be idle because
4017
4040
  * they're part way through play(), and are about to run.
4018
4041
  */
4019
- const { state } = track;
4020
- if (state === "idle" || state === "finished")
4021
- tracks.delete(track);
4042
+ hasEnded(track) && tracks.delete(track);
4022
4043
  end() || group.schedule(true);
4023
4044
  },
4024
4045
  /**
@@ -4049,6 +4070,7 @@
4049
4070
  built = new Map();
4050
4071
  stop();
4051
4072
  tracks.forEach((track) => track.state === "running" && track.js?.start());
4073
+ return true;
4052
4074
  },
4053
4075
  };
4054
4076
  transformGroups.set(element, group);
@@ -4574,10 +4596,19 @@
4574
4596
  * effects, and resets velocity to `0`.
4575
4597
  */
4576
4598
  jump(v, endAnimation = true) {
4599
+ if (endAnimation) {
4600
+ /**
4601
+ * Stop first, as stopping a running animation can set the
4602
+ * value it's showing, which would replace v. Marking the value
4603
+ * as updated now skips a JS animation's catch-up tick on stop,
4604
+ * which could otherwise complete it.
4605
+ */
4606
+ this.updatedAt = time.now();
4607
+ this.stop();
4608
+ }
4577
4609
  this.updateAndNotify(v);
4578
4610
  this.prev = v;
4579
4611
  this.prevUpdatedAt = this.prevFrameValue = undefined;
4580
- endAnimation && this.stop();
4581
4612
  this.stopPassiveEffect?.();
4582
4613
  }
4583
4614
  dirty() {
@@ -4796,6 +4827,7 @@
4796
4827
  "from",
4797
4828
  "elapsed",
4798
4829
  "out",
4830
+ "mixer",
4799
4831
  ]);
4800
4832
  /**
4801
4833
  * Decide whether a transition is defined on a given Transition.
@@ -6352,6 +6384,20 @@
6352
6384
  else if (key.startsWith("attr")) {
6353
6385
  return addAttrValue(element, state, key, value, convertAttrKey(key));
6354
6386
  }
6387
+ else if (key === "d") {
6388
+ /**
6389
+ * `d` is a CSS property in Chrome and Firefox, but only as
6390
+ * `path(...)`, so path data goes to the attribute, taken out of
6391
+ * `path(...)` if it's written that way. Path data itself has no
6392
+ * quotes or brackets.
6393
+ */
6394
+ return state.set(key, value, () => {
6395
+ const d = value.get()?.replace(/path\(|["')]/g, "");
6396
+ d === undefined
6397
+ ? element.removeAttribute(key)
6398
+ : element.setAttribute(key, d);
6399
+ });
6400
+ }
6355
6401
  /**
6356
6402
  * Transforms and origins aren't CSS properties (bar `x`/`y` in some
6357
6403
  * browsers), so check them by name rather than `key in element.style`.
@@ -6608,6 +6654,10 @@
6608
6654
  }
6609
6655
  return viewportBox;
6610
6656
  }
6657
+ const measureDocumentScroll = () => ({
6658
+ x: document.documentElement.scrollLeft || document.body?.scrollLeft || 0,
6659
+ y: document.documentElement.scrollTop || document.body?.scrollTop || 0,
6660
+ });
6611
6661
 
6612
6662
  const noProps = {};
6613
6663
  const getElementEffect = (element) => (isSVGElement(element)
@@ -6784,6 +6834,7 @@
6784
6834
  const statsBuffer = {
6785
6835
  value: null,
6786
6836
  addProjectionMetrics: null,
6837
+ countProjection: undefined,
6787
6838
  };
6788
6839
 
6789
6840
  /**
@@ -6806,6 +6857,12 @@
6806
6857
  return delay(callback, motionUtils.secondsToMilliseconds(timeout));
6807
6858
  }
6808
6859
 
6860
+ function isRefObject(ref) {
6861
+ return (ref &&
6862
+ typeof ref === "object" &&
6863
+ Object.prototype.hasOwnProperty.call(ref, "current"));
6864
+ }
6865
+
6809
6866
  /**
6810
6867
  * Checks if an element is specifically an SVGSVGElement (the root SVG element)
6811
6868
  * in a way that works across iframes
@@ -7020,6 +7077,27 @@
7020
7077
  removeAxisTransforms(box.y, transforms, yKeys, originBox?.y, sourceBox?.y);
7021
7078
  }
7022
7079
 
7080
+ const userAgentContains = (string) => typeof navigator !== "undefined" &&
7081
+ navigator.userAgent &&
7082
+ navigator.userAgent.toLowerCase().includes(string);
7083
+ /**
7084
+ * Measured bounding boxes must be rounded in Safari and
7085
+ * left untouched in Chrome, otherwise non-integer layouts within scaled-up elements
7086
+ * can appear to jump.
7087
+ */
7088
+ const roundPoint = userAgentContains("applewebkit/") && !userAgentContains("chrome/")
7089
+ ? Math.round
7090
+ : motionUtils.noop;
7091
+ function roundAxis(axis) {
7092
+ // Round to the nearest .5 pixels to support subpixel layouts
7093
+ axis.min = roundPoint(axis.min);
7094
+ axis.max = roundPoint(axis.max);
7095
+ }
7096
+ function roundBox(box) {
7097
+ roundAxis(box.x);
7098
+ roundAxis(box.y);
7099
+ }
7100
+
7023
7101
  const createAxisDelta = () => ({
7024
7102
  translate: 0,
7025
7103
  scale: 1,
@@ -7316,11 +7394,6 @@
7316
7394
  hasEverUpdated: false,
7317
7395
  };
7318
7396
 
7319
- const metrics = {
7320
- nodes: 0,
7321
- calculatedTargetDeltas: 0,
7322
- calculatedProjections: 0,
7323
- };
7324
7397
  /**
7325
7398
  * Transforms that distort an element's layout, so must be reset before measuring.
7326
7399
  */
@@ -7342,7 +7415,15 @@
7342
7415
  }
7343
7416
  }
7344
7417
  }
7345
- const demoteTransforms = ({ instance }) => instance && transformGroups.get(instance)?.demote();
7418
+ /**
7419
+ * Move accelerated transforms back to the main thread. The WAAPI animation
7420
+ * hid startUpdate()'s reset of rotations, skews and z, and moving off it
7421
+ * writes the whole transform back, so reset them again before this node
7422
+ * is measured.
7423
+ */
7424
+ const demoteTransforms = (node) => node.instance &&
7425
+ transformGroups.get(node.instance)?.demote() &&
7426
+ node.resetSkewAndRotation();
7346
7427
  function cancelTreeOptimisedTransformAnimations(projectionNode) {
7347
7428
  projectionNode.hasCheckedOptimisedAppear = true;
7348
7429
  if (projectionNode.root === projectionNode)
@@ -7360,6 +7441,13 @@
7360
7441
  cancelTreeOptimisedTransformAnimations(parent);
7361
7442
  }
7362
7443
  }
7444
+ function getClosestProjectingNode(visualElement) {
7445
+ if (!visualElement)
7446
+ return undefined;
7447
+ return visualElement.options.allowProjection !== false
7448
+ ? visualElement.projection
7449
+ : getClosestProjectingNode(visualElement.parent);
7450
+ }
7363
7451
  function createProjectionNode({ attachResizeListener, defaultParent, measureScroll, checkIsScrollRoot, resetTransform, }) {
7364
7452
  /**
7365
7453
  * Correctors registered via addScaleCorrector before projection loaded
@@ -7372,6 +7460,57 @@
7372
7460
  ...scaleCorrectors,
7373
7461
  });
7374
7462
  return class ProjectionNode {
7463
+ /**
7464
+ * Create the projection node of a `motion` component's VisualElement,
7465
+ * from its props. This runs while the component renders, before its
7466
+ * children. It's here rather than in the component so React
7467
+ * bundles only include it with the features that supply a
7468
+ * ProjectionNode. Vanilla bundles that use the projection node
7469
+ * (drag(), animateLayout()) include it too, as a class static
7470
+ * can't be tree-shaken.
7471
+ */
7472
+ static create(visualElement, props, initialPromotionConfig) {
7473
+ const { layoutId, layout, drag, dragConstraints, layoutScroll, layoutRoot, layoutAnchor, layoutCrossfade, } = props;
7474
+ const projection = (visualElement.projection = new this(visualElement.latestValues, props["data-framer-portal-id"]
7475
+ ? undefined
7476
+ : getClosestProjectingNode(visualElement.parent)));
7477
+ projection.setOptions({
7478
+ layoutId,
7479
+ layout,
7480
+ alwaysMeasureLayout: Boolean(drag) ||
7481
+ (dragConstraints && isRefObject(dragConstraints)),
7482
+ visualElement,
7483
+ /**
7484
+ * TODO: Update options in an effect. This could be tricky as it'll be too late
7485
+ * to update by the time layout animations run.
7486
+ * We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
7487
+ * ensuring it gets called if there's no potential layout animations.
7488
+ *
7489
+ */
7490
+ animationType: typeof layout === "string" ? layout : "both",
7491
+ initialPromotionConfig,
7492
+ crossfade: layoutCrossfade,
7493
+ layoutScroll,
7494
+ layoutRoot,
7495
+ layoutAnchor,
7496
+ });
7497
+ /**
7498
+ * This node will take over from its layoutId's current lead, so snapshot
7499
+ * the lead now, while rendering. By the time the lead unmounts (or this
7500
+ * node mounts) React may already have committed DOM changes that come
7501
+ * before it in the tree, like a sibling changing height, and a snapshot
7502
+ * taken then starts the animation from a position never seen on screen.
7503
+ *
7504
+ * If React discards this render (e.g. it suspends), nothing else
7505
+ * completes the update, so check it after the next frame as an
7506
+ * unmounting node does.
7507
+ */
7508
+ const lead = projection.getStack()?.lead;
7509
+ if (lead && !projection.root.isUpdateBlocked()) {
7510
+ lead.willUpdate();
7511
+ lead.scheduleCheckAfterUnmount();
7512
+ }
7513
+ }
7375
7514
  constructor(latestValues = {}, parent = defaultParent?.()) {
7376
7515
  /**
7377
7516
  * A unique ID generated for every projection node.
@@ -7484,23 +7623,14 @@
7484
7623
  */
7485
7624
  this.updateProjection = () => {
7486
7625
  this.projectionUpdateScheduled = false;
7487
- /**
7488
- * Reset debug counts. Manually resetting rather than creating a new
7489
- * object each frame.
7490
- */
7491
- if (statsBuffer.value) {
7492
- metrics.nodes =
7493
- metrics.calculatedTargetDeltas =
7494
- metrics.calculatedProjections =
7495
- 0;
7496
- }
7497
7626
  this.nodes.forEach(propagateDirtyNodes);
7498
7627
  this.nodes.forEach(resolveTargetDelta);
7499
7628
  this.nodes.forEach(calcProjection);
7500
7629
  this.nodes.forEach(cleanDirtyNodes);
7501
- if (statsBuffer.addProjectionMetrics) {
7502
- statsBuffer.addProjectionMetrics(metrics);
7503
- }
7630
+ /**
7631
+ * Record this frame's debug counts, if recordStats() is running.
7632
+ */
7633
+ statsBuffer.countProjection?.();
7504
7634
  };
7505
7635
  /**
7506
7636
  * Frame calculations
@@ -8216,9 +8346,7 @@
8216
8346
  /**
8217
8347
  * Increase debug counter for resolved target deltas
8218
8348
  */
8219
- if (statsBuffer.value) {
8220
- metrics.calculatedTargetDeltas++;
8221
- }
8349
+ statsBuffer.countProjection?.(1); // calculatedTargetDeltas
8222
8350
  }
8223
8351
  getClosestProjectingParent() {
8224
8352
  if (!this.parent ||
@@ -8366,9 +8494,7 @@
8366
8494
  /**
8367
8495
  * Increase debug counter for recalculated projections
8368
8496
  */
8369
- if (statsBuffer.value) {
8370
- metrics.calculatedProjections++;
8371
- }
8497
+ statsBuffer.countProjection?.(2); // calculatedProjections
8372
8498
  }
8373
8499
  hide() {
8374
8500
  this.isVisible = false;
@@ -8895,9 +9021,7 @@
8895
9021
  /**
8896
9022
  * Increase debug counter for nodes encountered this frame
8897
9023
  */
8898
- if (statsBuffer.value) {
8899
- metrics.nodes++;
8900
- }
9024
+ statsBuffer.countProjection?.(0); // nodes
8901
9025
  if (!node.parent)
8902
9026
  return;
8903
9027
  /**
@@ -9028,26 +9152,6 @@
9028
9152
  duration: 0.45,
9029
9153
  ease: [0.4, 0, 0.1, 1],
9030
9154
  };
9031
- const userAgentContains = (string) => typeof navigator !== "undefined" &&
9032
- navigator.userAgent &&
9033
- navigator.userAgent.toLowerCase().includes(string);
9034
- /**
9035
- * Measured bounding boxes must be rounded in Safari and
9036
- * left untouched in Chrome, otherwise non-integer layouts within scaled-up elements
9037
- * can appear to jump.
9038
- */
9039
- const roundPoint = userAgentContains("applewebkit/") && !userAgentContains("chrome/")
9040
- ? Math.round
9041
- : motionUtils.noop;
9042
- function roundAxis(axis) {
9043
- // Round to the nearest .5 pixels to support subpixel layouts
9044
- axis.min = roundPoint(axis.min);
9045
- axis.max = roundPoint(axis.max);
9046
- }
9047
- function roundBox(box) {
9048
- roundAxis(box.x);
9049
- roundAxis(box.y);
9050
- }
9051
9155
  /**
9052
9156
  * Align an axis to the start of another, keeping the length of a layout axis.
9053
9157
  * The length is read first, as output can be the layout axis itself.
@@ -9072,14 +9176,7 @@
9072
9176
  const DocumentProjectionNode =
9073
9177
  /* @__PURE__ */ createProjectionNode({
9074
9178
  attachResizeListener: (ref, notify) => addDomEvent(ref, "resize", notify),
9075
- measureScroll: () => ({
9076
- x: document.documentElement.scrollLeft ||
9077
- document.body?.scrollLeft ||
9078
- 0,
9079
- y: document.documentElement.scrollTop ||
9080
- document.body?.scrollTop ||
9081
- 0,
9082
- }),
9179
+ measureScroll: measureDocumentScroll,
9083
9180
  checkIsScrollRoot: () => true,
9084
9181
  });
9085
9182
 
@@ -9845,6 +9942,124 @@
9845
9942
  }
9846
9943
  }
9847
9944
 
9945
+ /**
9946
+ * Get the VisualElement that imperative APIs like animateLayout() and
9947
+ * drag() render an element with, creating one if it has none, ready for
9948
+ * a projection node to be attached.
9949
+ */
9950
+ function resolveVisualElement(element) {
9951
+ let visualElement = visualElementStore.get(element);
9952
+ if (!visualElement) {
9953
+ visualElement = new HTMLVisualElement({
9954
+ props: {},
9955
+ presenceContext: null,
9956
+ visualState: {
9957
+ latestValues: {},
9958
+ renderState: {
9959
+ transform: {},
9960
+ transformOrigin: {},
9961
+ style: {},
9962
+ vars: {},
9963
+ },
9964
+ },
9965
+ }, { allowProjection: true });
9966
+ /**
9967
+ * Values animate() has been rendering on this element move to
9968
+ * the VisualElement, so the layout animation composes with
9969
+ * them instead of overwriting them.
9970
+ */
9971
+ handOffElementState(element, visualElement);
9972
+ }
9973
+ /**
9974
+ * A first-time element may carry a projection transform in its
9975
+ * inline style (e.g. it was cloned from an element mid-animation).
9976
+ * That transform isn't tracked in latestValues so the engine can't
9977
+ * reset it before measuring - clear it now so the first layout
9978
+ * measurement isn't inflated.
9979
+ */
9980
+ if (element.style.transform && !hasTransform(visualElement.latestValues)) {
9981
+ element.style.transform = "";
9982
+ }
9983
+ return visualElement;
9984
+ }
9985
+
9986
+ /**
9987
+ * animateLayout() registers a function here that creates nodes in the
9988
+ * layout projection tree. When it's in the bundle, drag() uses those, so
9989
+ * the two work together (e.g. a drag follows a layout change mid-gesture).
9990
+ * Otherwise drag() makes do with a node that only measures. Bundles that
9991
+ * don't tree-shake (dev pre-bundling, CDN builds) always register it, and
9992
+ * a drag() started before animateLayout() is lazy loaded keeps its node.
9993
+ */
9994
+ const layoutProjection = {};
9995
+ /**
9996
+ * The document root of measurement-only nodes. Like the projection tree's
9997
+ * root, it keeps its scroll until drag controls clear it to re-measure.
9998
+ */
9999
+ const root = {
10000
+ updateScroll: () => {
10001
+ root.scroll || (root.scroll = { offset: measureDocumentScroll() });
10002
+ },
10003
+ };
10004
+ /**
10005
+ * The part of a projection node that drag controls use: measuring the
10006
+ * element's page box, for its layout and constraints. It never projects
10007
+ * or joins a layout update.
10008
+ */
10009
+ const createMeasureNode = (element) => {
10010
+ const visualElement = resolveVisualElement(element);
10011
+ const measureListeners = new motionUtils.SubscriptionManager();
10012
+ const node = {
10013
+ instance: element,
10014
+ root,
10015
+ path: [root],
10016
+ setOptions: motionUtils.noop,
10017
+ /**
10018
+ * Truthy like the full node's method, which HTMLVisualElement reads
10019
+ * (rather than calls) to default transform values.
10020
+ */
10021
+ isProjecting: motionUtils.noop,
10022
+ applyProjectionStyles: motionUtils.noop,
10023
+ removeElementScroll: (box) => box,
10024
+ updateLayout: () => {
10025
+ const layoutBox = measurePageBox(element, root, visualElement.getTransformPagePoint());
10026
+ roundBox(layoutBox);
10027
+ node.layout = { layoutBox };
10028
+ measureListeners.notify();
10029
+ },
10030
+ addEventListener: (name, handler) => name === "measure" ? measureListeners.add(handler) : motionUtils.noop,
10031
+ /**
10032
+ * Detach from the VisualElement too, so it doesn't later remount
10033
+ * (or a `motion` component's layout feature adopt) a node that
10034
+ * can't do either.
10035
+ */
10036
+ unmount: () => {
10037
+ node.instance = undefined;
10038
+ if (visualElement.projection === node) {
10039
+ visualElement.projection = undefined;
10040
+ }
10041
+ },
10042
+ };
10043
+ visualElement.projection = node;
10044
+ visualElement.current || visualElement.mount(element);
10045
+ return [node, node.unmount];
10046
+ };
10047
+ /**
10048
+ * Get a projection node for drag() to measure an element with, and a
10049
+ * function that releases it. Reuses a node a `motion` component or
10050
+ * animateLayout() owns.
10051
+ */
10052
+ const acquireDragNode = (element) => {
10053
+ const owned = visualElementStore.get(element)?.projection;
10054
+ /**
10055
+ * A VisualElement can outlive its node (e.g. an unmounted `motion`
10056
+ * component's), so only reuse a node that's still mounted.
10057
+ */
10058
+ return owned?.instance
10059
+ ? [owned, motionUtils.noop]
10060
+ : (layoutProjection.create || createMeasureNode)(element);
10061
+ };
10062
+
9848
10063
  const layoutSelector = "[data-layout],[data-layout-id]";
9849
10064
  /**
9850
10065
  * All imperatively-created projection nodes live in one persistent tree,
@@ -9897,21 +10112,6 @@
9897
10112
  }
9898
10113
  return undefined;
9899
10114
  }
9900
- function createVisualElement() {
9901
- return new HTMLVisualElement({
9902
- props: {},
9903
- presenceContext: null,
9904
- visualState: {
9905
- latestValues: {},
9906
- renderState: {
9907
- transform: {},
9908
- transformOrigin: {},
9909
- style: {},
9910
- vars: {},
9911
- },
9912
- },
9913
- }, { allowProjection: true });
9914
- }
9915
10115
  function readNodeOptions(element, transition) {
9916
10116
  const layoutAttr = element.getAttribute("data-layout");
9917
10117
  const layoutId = element.getAttribute("data-layout-id") ?? undefined;
@@ -9927,27 +10127,7 @@
9927
10127
  function prepareNode(element, transition) {
9928
10128
  let node = layoutNodes.get(element);
9929
10129
  if (!node) {
9930
- let visualElement = visualElementStore.get(element);
9931
- if (!visualElement) {
9932
- visualElement = createVisualElement();
9933
- /**
9934
- * Values animate() has been rendering on this element move to
9935
- * the VisualElement, so the layout animation composes with
9936
- * them instead of overwriting them.
9937
- */
9938
- handOffElementState(element, visualElement);
9939
- }
9940
- /**
9941
- * A first-time element may carry a projection transform in its
9942
- * inline style (e.g. it was cloned from an element mid-animation).
9943
- * That transform isn't tracked in latestValues so the engine can't
9944
- * reset it before measuring - clear it now so the first layout
9945
- * measurement isn't inflated.
9946
- */
9947
- if (element.style.transform &&
9948
- !hasTransform(visualElement.latestValues)) {
9949
- element.style.transform = "";
9950
- }
10130
+ const visualElement = resolveVisualElement(element);
9951
10131
  node = new HTMLProjectionNode(visualElement.latestValues, getProjectionParent(element));
9952
10132
  visualElement.projection = node;
9953
10133
  node.setOptions({
@@ -9967,10 +10147,26 @@
9967
10147
  return node;
9968
10148
  }
9969
10149
  /**
9970
- * Get a projection node for an element, so imperative gestures like
9971
- * drag() can measure its layout. Reuses a node owned by a `motion`
9972
- * component or animateLayout(), otherwise creates one in the shared
9973
- * imperative tree. Returns the node and a function that releases it.
10150
+ * Create a node for an element in the shared imperative tree. Returns the
10151
+ * node and a function that releases it.
10152
+ */
10153
+ function createLayoutNode(element) {
10154
+ const node = prepareNode(element);
10155
+ return [
10156
+ node,
10157
+ () => layoutNodes.get(element) === node && dropNode(element, node),
10158
+ ];
10159
+ }
10160
+ /**
10161
+ * Let drag() create its nodes in the shared imperative tree, so drag
10162
+ * gestures and layout animations work together.
10163
+ */
10164
+ layoutProjection.create = createLayoutNode;
10165
+ /**
10166
+ * Get a projection node for an element, so motion/remix can measure and
10167
+ * animate its layout. Reuses a node owned by a `motion` component or
10168
+ * animateLayout(), otherwise creates one in the shared imperative tree.
10169
+ * Returns the node and a function that releases it.
9974
10170
  */
9975
10171
  function acquireProjectionNode(element) {
9976
10172
  const owned = layoutNodes.get(element) || visualElementStore.get(element)?.projection;
@@ -9978,13 +10174,7 @@
9978
10174
  * A VisualElement can outlive a node we released (e.g. a stopped
9979
10175
  * drag()), so only reuse a node that's still mounted.
9980
10176
  */
9981
- if (owned?.instance)
9982
- return [owned, () => { }];
9983
- const node = prepareNode(element);
9984
- return [
9985
- node,
9986
- () => layoutNodes.get(element) === node && dropNode(element, node),
9987
- ];
10177
+ return owned?.instance ? [owned, () => { }] : createLayoutNode(element);
9988
10178
  }
9989
10179
  function sortDocumentOrder(elements) {
9990
10180
  return [...elements].sort((a, b) => a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
@@ -10004,6 +10194,8 @@
10004
10194
  }
10005
10195
  function flushPendingBuilders() {
10006
10196
  const builders = pendingBuilders;
10197
+ if (!builders)
10198
+ return;
10007
10199
  pendingBuilders = undefined;
10008
10200
  flushPendingFrame();
10009
10201
  /**
@@ -10068,6 +10260,13 @@
10068
10260
  };
10069
10261
  updatePromises.length ? Promise.all(updatePromises).then(commit) : commit();
10070
10262
  }
10263
+ /**
10264
+ * Snapshot every pending animateLayout() now, rather than on the next
10265
+ * microtask. For renderers that tell us a DOM update is about to happen
10266
+ * and then make it synchronously (e.g. a beforeUpdate/commit pair), with
10267
+ * updateDom returning a promise that resolves on commit.
10268
+ */
10269
+ const flushLayoutSnapshots = flushPendingBuilders;
10071
10270
  class LayoutAnimationBuilder {
10072
10271
  constructor(scope, updateDom, defaultOptions) {
10073
10272
  this.scope = scope;
@@ -11465,13 +11664,13 @@
11465
11664
  }
11466
11665
  const cleanups = resolveElements(elementOrSelector).map((element) => {
11467
11666
  scope?.track(element);
11468
- const [node, release] = acquireProjectionNode(element);
11667
+ const [node, release] = acquireDragNode(element);
11469
11668
  /**
11470
11669
  * Measure layout even when not animating it, as constraints and
11471
11670
  * layout-shift correction are computed from it.
11472
11671
  */
11473
11672
  node.setOptions({ alwaysMeasureLayout: true });
11474
- const controls = new VisualElementDragControls(node.options.visualElement, () => props);
11673
+ const controls = new VisualElementDragControls(visualElementStore.get(element), () => props);
11475
11674
  const removeListeners = controls.addListeners();
11476
11675
  const removeStyles = applyDragStyles(element, options.axis);
11477
11676
  return () => {
@@ -11877,9 +12076,19 @@
11877
12076
  return scope;
11878
12077
  }
11879
12078
 
12079
+ /**
12080
+ * The metrics projection counts, in the order of the index it passes to
12081
+ * statsBuffer.countProjection.
12082
+ */
12083
+ const projectionMetrics = [
12084
+ "nodes",
12085
+ "calculatedTargetDeltas",
12086
+ "calculatedProjections",
12087
+ ];
11880
12088
  function clearStatsBuffer() {
11881
12089
  statsBuffer.value = null;
11882
12090
  statsBuffer.addProjectionMetrics = null;
12091
+ statsBuffer.countProjection = undefined;
11883
12092
  }
11884
12093
  function recordStats() {
11885
12094
  if (statsBuffer.value) {
@@ -11900,6 +12109,27 @@
11900
12109
  layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
11901
12110
  layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
11902
12111
  };
12112
+ /**
12113
+ * Counting lives here rather than in projection, so bundles that never
12114
+ * call recordStats() don't ship it.
12115
+ */
12116
+ const metrics = {
12117
+ nodes: 0,
12118
+ calculatedTargetDeltas: 0,
12119
+ calculatedProjections: 0,
12120
+ };
12121
+ buffer.countProjection = (metric) => {
12122
+ if (metric !== undefined) {
12123
+ metrics[projectionMetrics[metric]]++;
12124
+ }
12125
+ else {
12126
+ buffer.addProjectionMetrics(metrics);
12127
+ metrics.nodes =
12128
+ metrics.calculatedTargetDeltas =
12129
+ metrics.calculatedProjections =
12130
+ 0;
12131
+ }
12132
+ };
11903
12133
  }
11904
12134
 
11905
12135
  function getOriginIndex(from, total) {
@@ -14097,6 +14327,7 @@
14097
14327
  exports.VisualElement = VisualElement;
14098
14328
  exports.VisualElementDragControls = VisualElementDragControls;
14099
14329
  exports.acceleratedValues = acceleratedValues;
14330
+ exports.acquireProjectionNode = acquireProjectionNode;
14100
14331
  exports.activeScope = activeScope;
14101
14332
  exports.addAttrValue = addAttrValue;
14102
14333
  exports.addBuilderToScope = addBuilderToScope;
@@ -14206,6 +14437,7 @@
14206
14437
  exports.findEffect = findEffect;
14207
14438
  exports.findValueType = findValueType;
14208
14439
  exports.flushKeyframeResolvers = flushKeyframeResolvers;
14440
+ exports.flushLayoutSnapshots = flushLayoutSnapshots;
14209
14441
  exports.followValue = followValue;
14210
14442
  exports.frame = frame;
14211
14443
  exports.frameData = frameData;