motion-dom 14.7.1 → 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 (52) hide show
  1. package/dist/cjs/index.js +370 -147
  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 +31 -35
  12. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -1
  13. package/dist/es/animation/waapi/utils/needs-mixer.mjs +11 -0
  14. package/dist/es/animation/waapi/utils/needs-mixer.mjs.map +1 -0
  15. package/dist/es/effects/svg/index.mjs +14 -0
  16. package/dist/es/effects/svg/index.mjs.map +1 -1
  17. package/dist/es/gestures/drag/index.mjs +4 -3
  18. package/dist/es/gestures/drag/index.mjs.map +1 -1
  19. package/dist/es/gestures/drag/projection.mjs +85 -0
  20. package/dist/es/gestures/drag/projection.mjs.map +1 -0
  21. package/dist/es/index.mjs +1 -1
  22. package/dist/es/layout/LayoutAnimationBuilder.mjs +34 -51
  23. package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
  24. package/dist/es/layout/visual-element.mjs +48 -0
  25. package/dist/es/layout/visual-element.mjs.map +1 -0
  26. package/dist/es/projection/geometry/round.mjs +25 -0
  27. package/dist/es/projection/geometry/round.mjs.map +1 -0
  28. package/dist/es/projection/node/DocumentProjectionNode.mjs +2 -8
  29. package/dist/es/projection/node/DocumentProjectionNode.mjs.map +1 -1
  30. package/dist/es/projection/node/create-projection-node.mjs +68 -48
  31. package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
  32. package/dist/es/projection/utils/measure.mjs +5 -1
  33. package/dist/es/projection/utils/measure.mjs.map +1 -1
  34. package/dist/es/stats/buffer.mjs +1 -0
  35. package/dist/es/stats/buffer.mjs.map +1 -1
  36. package/dist/es/stats/index.mjs +31 -0
  37. package/dist/es/stats/index.mjs.map +1 -1
  38. package/dist/es/utils/interpolate.mjs +3 -2
  39. package/dist/es/utils/interpolate.mjs.map +1 -1
  40. package/dist/es/utils/is-ref-object.mjs +8 -0
  41. package/dist/es/utils/is-ref-object.mjs.map +1 -0
  42. package/dist/es/utils/transform.mjs.map +1 -1
  43. package/dist/es/value/index.mjs +10 -1
  44. package/dist/es/value/index.mjs.map +1 -1
  45. package/dist/index.d.ts +298 -232
  46. package/dist/motion-dom.dev.js +370 -147
  47. package/dist/motion-dom.js +1 -1
  48. package/dist/size-rollup-motion-value.js +1 -1
  49. package/dist/size-rollup-motion-value.js.map +1 -1
  50. package/dist/size-rollup-style-effect.js +1 -1
  51. package/dist/size-rollup-style-effect.js.map +1 -1
  52. 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
  /**
@@ -4575,10 +4596,19 @@
4575
4596
  * effects, and resets velocity to `0`.
4576
4597
  */
4577
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
+ }
4578
4609
  this.updateAndNotify(v);
4579
4610
  this.prev = v;
4580
4611
  this.prevUpdatedAt = this.prevFrameValue = undefined;
4581
- endAnimation && this.stop();
4582
4612
  this.stopPassiveEffect?.();
4583
4613
  }
4584
4614
  dirty() {
@@ -4797,6 +4827,7 @@
4797
4827
  "from",
4798
4828
  "elapsed",
4799
4829
  "out",
4830
+ "mixer",
4800
4831
  ]);
4801
4832
  /**
4802
4833
  * Decide whether a transition is defined on a given Transition.
@@ -6353,6 +6384,20 @@
6353
6384
  else if (key.startsWith("attr")) {
6354
6385
  return addAttrValue(element, state, key, value, convertAttrKey(key));
6355
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
+ }
6356
6401
  /**
6357
6402
  * Transforms and origins aren't CSS properties (bar `x`/`y` in some
6358
6403
  * browsers), so check them by name rather than `key in element.style`.
@@ -6609,6 +6654,10 @@
6609
6654
  }
6610
6655
  return viewportBox;
6611
6656
  }
6657
+ const measureDocumentScroll = () => ({
6658
+ x: document.documentElement.scrollLeft || document.body?.scrollLeft || 0,
6659
+ y: document.documentElement.scrollTop || document.body?.scrollTop || 0,
6660
+ });
6612
6661
 
6613
6662
  const noProps = {};
6614
6663
  const getElementEffect = (element) => (isSVGElement(element)
@@ -6785,6 +6834,7 @@
6785
6834
  const statsBuffer = {
6786
6835
  value: null,
6787
6836
  addProjectionMetrics: null,
6837
+ countProjection: undefined,
6788
6838
  };
6789
6839
 
6790
6840
  /**
@@ -6807,6 +6857,12 @@
6807
6857
  return delay(callback, motionUtils.secondsToMilliseconds(timeout));
6808
6858
  }
6809
6859
 
6860
+ function isRefObject(ref) {
6861
+ return (ref &&
6862
+ typeof ref === "object" &&
6863
+ Object.prototype.hasOwnProperty.call(ref, "current"));
6864
+ }
6865
+
6810
6866
  /**
6811
6867
  * Checks if an element is specifically an SVGSVGElement (the root SVG element)
6812
6868
  * in a way that works across iframes
@@ -7021,6 +7077,27 @@
7021
7077
  removeAxisTransforms(box.y, transforms, yKeys, originBox?.y, sourceBox?.y);
7022
7078
  }
7023
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
+
7024
7101
  const createAxisDelta = () => ({
7025
7102
  translate: 0,
7026
7103
  scale: 1,
@@ -7317,11 +7394,6 @@
7317
7394
  hasEverUpdated: false,
7318
7395
  };
7319
7396
 
7320
- const metrics = {
7321
- nodes: 0,
7322
- calculatedTargetDeltas: 0,
7323
- calculatedProjections: 0,
7324
- };
7325
7397
  /**
7326
7398
  * Transforms that distort an element's layout, so must be reset before measuring.
7327
7399
  */
@@ -7369,6 +7441,13 @@
7369
7441
  cancelTreeOptimisedTransformAnimations(parent);
7370
7442
  }
7371
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
+ }
7372
7451
  function createProjectionNode({ attachResizeListener, defaultParent, measureScroll, checkIsScrollRoot, resetTransform, }) {
7373
7452
  /**
7374
7453
  * Correctors registered via addScaleCorrector before projection loaded
@@ -7381,6 +7460,57 @@
7381
7460
  ...scaleCorrectors,
7382
7461
  });
7383
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
+ }
7384
7514
  constructor(latestValues = {}, parent = defaultParent?.()) {
7385
7515
  /**
7386
7516
  * A unique ID generated for every projection node.
@@ -7493,23 +7623,14 @@
7493
7623
  */
7494
7624
  this.updateProjection = () => {
7495
7625
  this.projectionUpdateScheduled = false;
7496
- /**
7497
- * Reset debug counts. Manually resetting rather than creating a new
7498
- * object each frame.
7499
- */
7500
- if (statsBuffer.value) {
7501
- metrics.nodes =
7502
- metrics.calculatedTargetDeltas =
7503
- metrics.calculatedProjections =
7504
- 0;
7505
- }
7506
7626
  this.nodes.forEach(propagateDirtyNodes);
7507
7627
  this.nodes.forEach(resolveTargetDelta);
7508
7628
  this.nodes.forEach(calcProjection);
7509
7629
  this.nodes.forEach(cleanDirtyNodes);
7510
- if (statsBuffer.addProjectionMetrics) {
7511
- statsBuffer.addProjectionMetrics(metrics);
7512
- }
7630
+ /**
7631
+ * Record this frame's debug counts, if recordStats() is running.
7632
+ */
7633
+ statsBuffer.countProjection?.();
7513
7634
  };
7514
7635
  /**
7515
7636
  * Frame calculations
@@ -8225,9 +8346,7 @@
8225
8346
  /**
8226
8347
  * Increase debug counter for resolved target deltas
8227
8348
  */
8228
- if (statsBuffer.value) {
8229
- metrics.calculatedTargetDeltas++;
8230
- }
8349
+ statsBuffer.countProjection?.(1); // calculatedTargetDeltas
8231
8350
  }
8232
8351
  getClosestProjectingParent() {
8233
8352
  if (!this.parent ||
@@ -8375,9 +8494,7 @@
8375
8494
  /**
8376
8495
  * Increase debug counter for recalculated projections
8377
8496
  */
8378
- if (statsBuffer.value) {
8379
- metrics.calculatedProjections++;
8380
- }
8497
+ statsBuffer.countProjection?.(2); // calculatedProjections
8381
8498
  }
8382
8499
  hide() {
8383
8500
  this.isVisible = false;
@@ -8904,9 +9021,7 @@
8904
9021
  /**
8905
9022
  * Increase debug counter for nodes encountered this frame
8906
9023
  */
8907
- if (statsBuffer.value) {
8908
- metrics.nodes++;
8909
- }
9024
+ statsBuffer.countProjection?.(0); // nodes
8910
9025
  if (!node.parent)
8911
9026
  return;
8912
9027
  /**
@@ -9037,26 +9152,6 @@
9037
9152
  duration: 0.45,
9038
9153
  ease: [0.4, 0, 0.1, 1],
9039
9154
  };
9040
- const userAgentContains = (string) => typeof navigator !== "undefined" &&
9041
- navigator.userAgent &&
9042
- navigator.userAgent.toLowerCase().includes(string);
9043
- /**
9044
- * Measured bounding boxes must be rounded in Safari and
9045
- * left untouched in Chrome, otherwise non-integer layouts within scaled-up elements
9046
- * can appear to jump.
9047
- */
9048
- const roundPoint = userAgentContains("applewebkit/") && !userAgentContains("chrome/")
9049
- ? Math.round
9050
- : motionUtils.noop;
9051
- function roundAxis(axis) {
9052
- // Round to the nearest .5 pixels to support subpixel layouts
9053
- axis.min = roundPoint(axis.min);
9054
- axis.max = roundPoint(axis.max);
9055
- }
9056
- function roundBox(box) {
9057
- roundAxis(box.x);
9058
- roundAxis(box.y);
9059
- }
9060
9155
  /**
9061
9156
  * Align an axis to the start of another, keeping the length of a layout axis.
9062
9157
  * The length is read first, as output can be the layout axis itself.
@@ -9081,14 +9176,7 @@
9081
9176
  const DocumentProjectionNode =
9082
9177
  /* @__PURE__ */ createProjectionNode({
9083
9178
  attachResizeListener: (ref, notify) => addDomEvent(ref, "resize", notify),
9084
- measureScroll: () => ({
9085
- x: document.documentElement.scrollLeft ||
9086
- document.body?.scrollLeft ||
9087
- 0,
9088
- y: document.documentElement.scrollTop ||
9089
- document.body?.scrollTop ||
9090
- 0,
9091
- }),
9179
+ measureScroll: measureDocumentScroll,
9092
9180
  checkIsScrollRoot: () => true,
9093
9181
  });
9094
9182
 
@@ -9854,6 +9942,124 @@
9854
9942
  }
9855
9943
  }
9856
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
+
9857
10063
  const layoutSelector = "[data-layout],[data-layout-id]";
9858
10064
  /**
9859
10065
  * All imperatively-created projection nodes live in one persistent tree,
@@ -9906,21 +10112,6 @@
9906
10112
  }
9907
10113
  return undefined;
9908
10114
  }
9909
- function createVisualElement() {
9910
- return new HTMLVisualElement({
9911
- props: {},
9912
- presenceContext: null,
9913
- visualState: {
9914
- latestValues: {},
9915
- renderState: {
9916
- transform: {},
9917
- transformOrigin: {},
9918
- style: {},
9919
- vars: {},
9920
- },
9921
- },
9922
- }, { allowProjection: true });
9923
- }
9924
10115
  function readNodeOptions(element, transition) {
9925
10116
  const layoutAttr = element.getAttribute("data-layout");
9926
10117
  const layoutId = element.getAttribute("data-layout-id") ?? undefined;
@@ -9936,27 +10127,7 @@
9936
10127
  function prepareNode(element, transition) {
9937
10128
  let node = layoutNodes.get(element);
9938
10129
  if (!node) {
9939
- let visualElement = visualElementStore.get(element);
9940
- if (!visualElement) {
9941
- visualElement = createVisualElement();
9942
- /**
9943
- * Values animate() has been rendering on this element move to
9944
- * the VisualElement, so the layout animation composes with
9945
- * them instead of overwriting them.
9946
- */
9947
- handOffElementState(element, visualElement);
9948
- }
9949
- /**
9950
- * A first-time element may carry a projection transform in its
9951
- * inline style (e.g. it was cloned from an element mid-animation).
9952
- * That transform isn't tracked in latestValues so the engine can't
9953
- * reset it before measuring - clear it now so the first layout
9954
- * measurement isn't inflated.
9955
- */
9956
- if (element.style.transform &&
9957
- !hasTransform(visualElement.latestValues)) {
9958
- element.style.transform = "";
9959
- }
10130
+ const visualElement = resolveVisualElement(element);
9960
10131
  node = new HTMLProjectionNode(visualElement.latestValues, getProjectionParent(element));
9961
10132
  visualElement.projection = node;
9962
10133
  node.setOptions({
@@ -9976,10 +10147,26 @@
9976
10147
  return node;
9977
10148
  }
9978
10149
  /**
9979
- * Get a projection node for an element, so imperative gestures like
9980
- * drag() can measure its layout. Reuses a node owned by a `motion`
9981
- * component or animateLayout(), otherwise creates one in the shared
9982
- * 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.
9983
10170
  */
9984
10171
  function acquireProjectionNode(element) {
9985
10172
  const owned = layoutNodes.get(element) || visualElementStore.get(element)?.projection;
@@ -9987,13 +10174,7 @@
9987
10174
  * A VisualElement can outlive a node we released (e.g. a stopped
9988
10175
  * drag()), so only reuse a node that's still mounted.
9989
10176
  */
9990
- if (owned?.instance)
9991
- return [owned, () => { }];
9992
- const node = prepareNode(element);
9993
- return [
9994
- node,
9995
- () => layoutNodes.get(element) === node && dropNode(element, node),
9996
- ];
10177
+ return owned?.instance ? [owned, () => { }] : createLayoutNode(element);
9997
10178
  }
9998
10179
  function sortDocumentOrder(elements) {
9999
10180
  return [...elements].sort((a, b) => a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
@@ -10013,6 +10194,8 @@
10013
10194
  }
10014
10195
  function flushPendingBuilders() {
10015
10196
  const builders = pendingBuilders;
10197
+ if (!builders)
10198
+ return;
10016
10199
  pendingBuilders = undefined;
10017
10200
  flushPendingFrame();
10018
10201
  /**
@@ -10077,6 +10260,13 @@
10077
10260
  };
10078
10261
  updatePromises.length ? Promise.all(updatePromises).then(commit) : commit();
10079
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;
10080
10270
  class LayoutAnimationBuilder {
10081
10271
  constructor(scope, updateDom, defaultOptions) {
10082
10272
  this.scope = scope;
@@ -11474,13 +11664,13 @@
11474
11664
  }
11475
11665
  const cleanups = resolveElements(elementOrSelector).map((element) => {
11476
11666
  scope?.track(element);
11477
- const [node, release] = acquireProjectionNode(element);
11667
+ const [node, release] = acquireDragNode(element);
11478
11668
  /**
11479
11669
  * Measure layout even when not animating it, as constraints and
11480
11670
  * layout-shift correction are computed from it.
11481
11671
  */
11482
11672
  node.setOptions({ alwaysMeasureLayout: true });
11483
- const controls = new VisualElementDragControls(node.options.visualElement, () => props);
11673
+ const controls = new VisualElementDragControls(visualElementStore.get(element), () => props);
11484
11674
  const removeListeners = controls.addListeners();
11485
11675
  const removeStyles = applyDragStyles(element, options.axis);
11486
11676
  return () => {
@@ -11886,9 +12076,19 @@
11886
12076
  return scope;
11887
12077
  }
11888
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
+ ];
11889
12088
  function clearStatsBuffer() {
11890
12089
  statsBuffer.value = null;
11891
12090
  statsBuffer.addProjectionMetrics = null;
12091
+ statsBuffer.countProjection = undefined;
11892
12092
  }
11893
12093
  function recordStats() {
11894
12094
  if (statsBuffer.value) {
@@ -11909,6 +12109,27 @@
11909
12109
  layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
11910
12110
  layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
11911
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
+ };
11912
12133
  }
11913
12134
 
11914
12135
  function getOriginIndex(from, total) {
@@ -14106,6 +14327,7 @@
14106
14327
  exports.VisualElement = VisualElement;
14107
14328
  exports.VisualElementDragControls = VisualElementDragControls;
14108
14329
  exports.acceleratedValues = acceleratedValues;
14330
+ exports.acquireProjectionNode = acquireProjectionNode;
14109
14331
  exports.activeScope = activeScope;
14110
14332
  exports.addAttrValue = addAttrValue;
14111
14333
  exports.addBuilderToScope = addBuilderToScope;
@@ -14215,6 +14437,7 @@
14215
14437
  exports.findEffect = findEffect;
14216
14438
  exports.findValueType = findValueType;
14217
14439
  exports.flushKeyframeResolvers = flushKeyframeResolvers;
14440
+ exports.flushLayoutSnapshots = flushLayoutSnapshots;
14218
14441
  exports.followValue = followValue;
14219
14442
  exports.frame = frame;
14220
14443
  exports.frameData = frameData;