motion-dom 12.36.0 → 12.37.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 (31) hide show
  1. package/dist/cjs/index.js +134 -65
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/AsyncMotionValueAnimation.mjs +15 -6
  4. package/dist/es/animation/AsyncMotionValueAnimation.mjs.map +1 -1
  5. package/dist/es/animation/JSAnimation.mjs +16 -3
  6. package/dist/es/animation/JSAnimation.mjs.map +1 -1
  7. package/dist/es/animation/interfaces/motion-value.mjs +1 -1
  8. package/dist/es/animation/interfaces/motion-value.mjs.map +1 -1
  9. package/dist/es/animation/utils/active-animations.mjs +5 -2
  10. package/dist/es/animation/utils/active-animations.mjs.map +1 -1
  11. package/dist/es/animation/utils/is-transition-defined.mjs +18 -2
  12. package/dist/es/animation/utils/is-transition-defined.mjs.map +1 -1
  13. package/dist/es/animation/waapi/supports/waapi.mjs +21 -12
  14. package/dist/es/animation/waapi/supports/waapi.mjs.map +1 -1
  15. package/dist/es/animation/waapi/utils/is-browser-color.mjs +13 -0
  16. package/dist/es/animation/waapi/utils/is-browser-color.mjs.map +1 -0
  17. package/dist/es/index.mjs +1 -1
  18. package/dist/es/projection/animation/mix-values.mjs +8 -3
  19. package/dist/es/projection/animation/mix-values.mjs.map +1 -1
  20. package/dist/es/projection/geometry/delta-apply.mjs +4 -6
  21. package/dist/es/projection/geometry/delta-apply.mjs.map +1 -1
  22. package/dist/es/projection/node/create-projection-node.mjs +24 -9
  23. package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
  24. package/dist/es/value/follow-value.mjs +3 -0
  25. package/dist/es/value/follow-value.mjs.map +1 -1
  26. package/dist/index.d.ts +12 -7
  27. package/dist/motion-dom.dev.js +134 -65
  28. package/dist/motion-dom.js +1 -1
  29. package/package.json +2 -2
  30. package/dist/es/animation/utils/get-final-keyframe.mjs +0 -13
  31. package/dist/es/animation/utils/get-final-keyframe.mjs.map +0 -1
package/dist/cjs/index.js CHANGED
@@ -1324,9 +1324,9 @@ function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "e
1324
1324
  };
1325
1325
  }
1326
1326
 
1327
- const isNotNull$1 = (value) => value !== null;
1328
- function getFinalKeyframe$1(keyframes, { repeat, repeatType = "loop" }, finalKeyframe, speed = 1) {
1329
- const resolvedKeyframes = keyframes.filter(isNotNull$1);
1327
+ const isNotNull = (value) => value !== null;
1328
+ function getFinalKeyframe(keyframes, { repeat, repeatType = "loop" }, finalKeyframe, speed = 1) {
1329
+ const resolvedKeyframes = keyframes.filter(isNotNull);
1330
1330
  const useFirstKeyframe = speed < 0 || (repeat && repeatType !== "loop" && repeat % 2 === 1);
1331
1331
  const index = useFirstKeyframe ? 0 : resolvedKeyframes.length - 1;
1332
1332
  return !index || finalKeyframe === undefined
@@ -1391,6 +1391,14 @@ class JSAnimation extends WithPromise {
1391
1391
  * Playback speed as a factor. 0 would be stopped, -1 reverse and 2 double speed.
1392
1392
  */
1393
1393
  this.playbackSpeed = 1;
1394
+ /**
1395
+ * Reusable state object for the delay phase to avoid
1396
+ * allocating a new object every frame.
1397
+ */
1398
+ this.delayState = {
1399
+ done: false,
1400
+ value: undefined,
1401
+ };
1394
1402
  /**
1395
1403
  * This method is bound to the instance to fix a pattern where
1396
1404
  * animation.stop is returned as a reference from a useEffect.
@@ -1553,9 +1561,14 @@ class JSAnimation extends WithPromise {
1553
1561
  * This prevents delay: x, duration: 0 animations from finishing
1554
1562
  * instantly.
1555
1563
  */
1556
- const state = isInDelayPhase
1557
- ? { done: false, value: keyframes[0] }
1558
- : frameGenerator.next(elapsed);
1564
+ let state;
1565
+ if (isInDelayPhase) {
1566
+ this.delayState.value = keyframes[0];
1567
+ state = this.delayState;
1568
+ }
1569
+ else {
1570
+ state = frameGenerator.next(elapsed);
1571
+ }
1559
1572
  if (mixKeyframes && !isInDelayPhase) {
1560
1573
  state.value = mixKeyframes(state.value);
1561
1574
  }
@@ -1570,7 +1583,7 @@ class JSAnimation extends WithPromise {
1570
1583
  (this.state === "finished" || (this.state === "running" && done));
1571
1584
  // TODO: The exception for inertia could be cleaner here
1572
1585
  if (isAnimationFinished && type !== inertia) {
1573
- state.value = getFinalKeyframe$1(keyframes, this.options, finalKeyframe, this.speed);
1586
+ state.value = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
1574
1587
  }
1575
1588
  if (onUpdate) {
1576
1589
  onUpdate(state.value);
@@ -2178,7 +2191,7 @@ class NativeAnimation extends WithPromise {
2178
2191
  this.animation.onfinish = () => {
2179
2192
  this.finishedTime = this.time;
2180
2193
  if (!pseudoElement) {
2181
- const keyframe = getFinalKeyframe$1(keyframes, this.options, finalKeyframe, this.speed);
2194
+ const keyframe = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
2182
2195
  if (this.updateMotionValue) {
2183
2196
  this.updateMotionValue(keyframe);
2184
2197
  }
@@ -2484,17 +2497,42 @@ function makeAnimationInstant(options) {
2484
2497
  /**
2485
2498
  * A list of values that can be hardware-accelerated.
2486
2499
  */
2487
- const acceleratedValues$1 = new Set([
2500
+ const acceleratedValues = new Set([
2488
2501
  "opacity",
2489
2502
  "clipPath",
2490
2503
  "filter",
2491
2504
  "transform",
2492
- // TODO: Could be re-enabled now we have support for linear() easing
2505
+ // TODO: Can be accelerated but currently disabled until https://issues.chromium.org/issues/41491098 is resolved
2506
+ // or until we implement support for linear() easing.
2493
2507
  // "background-color"
2494
2508
  ]);
2509
+
2510
+ const browserColorFunctions = /^(?:oklch|oklab|lab|lch|color|color-mix|light-dark)\(/;
2511
+ function hasBrowserOnlyColors(keyframes) {
2512
+ for (let i = 0; i < keyframes.length; i++) {
2513
+ if (typeof keyframes[i] === "string" &&
2514
+ browserColorFunctions.test(keyframes[i])) {
2515
+ return true;
2516
+ }
2517
+ }
2518
+ return false;
2519
+ }
2520
+
2521
+ const colorProperties = new Set([
2522
+ "color",
2523
+ "backgroundColor",
2524
+ "outlineColor",
2525
+ "fill",
2526
+ "stroke",
2527
+ "borderColor",
2528
+ "borderTopColor",
2529
+ "borderRightColor",
2530
+ "borderBottomColor",
2531
+ "borderLeftColor",
2532
+ ]);
2495
2533
  const supportsWaapi = /*@__PURE__*/ motionUtils.memo(() => Object.hasOwnProperty.call(Element.prototype, "animate"));
2496
2534
  function supportsBrowserAnimation(options) {
2497
- const { motionValue, name, repeatDelay, repeatType, damping, type } = options;
2535
+ const { motionValue, name, repeatDelay, repeatType, damping, type, keyframes, } = options;
2498
2536
  const subject = motionValue?.owner?.current;
2499
2537
  /**
2500
2538
  * We use this check instead of isHTMLElement() because we explicitly
@@ -2508,7 +2546,13 @@ function supportsBrowserAnimation(options) {
2508
2546
  const { onUpdate, transformTemplate } = motionValue.owner.getProps();
2509
2547
  return (supportsWaapi() &&
2510
2548
  name &&
2511
- acceleratedValues$1.has(name) &&
2549
+ /**
2550
+ * Force WAAPI for color properties with browser-only color formats
2551
+ * (oklch, oklab, lab, lch, etc.) that the JS animation path can't parse.
2552
+ */
2553
+ (acceleratedValues.has(name) ||
2554
+ (colorProperties.has(name) &&
2555
+ hasBrowserOnlyColors(keyframes))) &&
2512
2556
  (name !== "transform" || !transformTemplate) &&
2513
2557
  /**
2514
2558
  * If we're outputting values to onUpdate then we can't use WAAPI as there's
@@ -2572,7 +2616,7 @@ class AsyncMotionValueAnimation extends WithPromise {
2572
2616
  if (!canAnimate(keyframes, name, type, velocity)) {
2573
2617
  canAnimateValue = false;
2574
2618
  if (motionUtils.MotionGlobalConfig.instantAnimations || !delay) {
2575
- onUpdate?.(getFinalKeyframe$1(keyframes, options, finalKeyframe));
2619
+ onUpdate?.(getFinalKeyframe(keyframes, options, finalKeyframe));
2576
2620
  }
2577
2621
  keyframes[0] = keyframes[keyframes.length - 1];
2578
2622
  makeAnimationInstant(options);
@@ -2615,12 +2659,21 @@ class AsyncMotionValueAnimation extends WithPromise {
2615
2659
  !isHandoff &&
2616
2660
  supportsBrowserAnimation(resolvedOptions);
2617
2661
  const element = resolvedOptions.motionValue?.owner?.current;
2618
- const animation = useWaapi
2619
- ? new NativeAnimationExtended({
2620
- ...resolvedOptions,
2621
- element,
2622
- })
2623
- : new JSAnimation(resolvedOptions);
2662
+ let animation;
2663
+ if (useWaapi) {
2664
+ try {
2665
+ animation = new NativeAnimationExtended({
2666
+ ...resolvedOptions,
2667
+ element,
2668
+ });
2669
+ }
2670
+ catch {
2671
+ animation = new JSAnimation(resolvedOptions);
2672
+ }
2673
+ }
2674
+ else {
2675
+ animation = new JSAnimation(resolvedOptions);
2676
+ }
2624
2677
  animation.finished.then(() => {
2625
2678
  this.notifyFinished();
2626
2679
  }).catch(motionUtils.noop);
@@ -2798,8 +2851,11 @@ class NativeAnimationWrapper extends NativeAnimation {
2798
2851
  const animationMaps = new WeakMap();
2799
2852
  const animationMapKey = (name, pseudoElement = "") => `${name}:${pseudoElement}`;
2800
2853
  function getAnimationMap(element) {
2801
- const map = animationMaps.get(element) || new Map();
2802
- animationMaps.set(element, map);
2854
+ let map = animationMaps.get(element);
2855
+ if (!map) {
2856
+ map = new Map();
2857
+ animationMaps.set(element, map);
2858
+ }
2803
2859
  return map;
2804
2860
  }
2805
2861
 
@@ -2891,17 +2947,6 @@ const getDefaultTransition = (valueKey, { keyframes }) => {
2891
2947
  return ease;
2892
2948
  };
2893
2949
 
2894
- const isNotNull = (value) => value !== null;
2895
- function getFinalKeyframe(keyframes, { repeat, repeatType = "loop" }, finalKeyframe) {
2896
- const resolvedKeyframes = keyframes.filter(isNotNull);
2897
- const index = repeat && repeatType !== "loop" && repeat % 2 === 1
2898
- ? 0
2899
- : resolvedKeyframes.length - 1;
2900
- return !index || finalKeyframe === undefined
2901
- ? resolvedKeyframes[index]
2902
- : finalKeyframe;
2903
- }
2904
-
2905
2950
  /**
2906
2951
  * If `transition` has `inherit: true`, shallow-merge it with
2907
2952
  * `parentTransition` (child keys win) and strip the `inherit` key.
@@ -2925,13 +2970,29 @@ function getValueTransition(transition, key) {
2925
2970
  return valueTransition;
2926
2971
  }
2927
2972
 
2973
+ const orchestrationKeys = new Set([
2974
+ "when",
2975
+ "delay",
2976
+ "delayChildren",
2977
+ "staggerChildren",
2978
+ "staggerDirection",
2979
+ "repeat",
2980
+ "repeatType",
2981
+ "repeatDelay",
2982
+ "from",
2983
+ "elapsed",
2984
+ ]);
2928
2985
  /**
2929
2986
  * Decide whether a transition is defined on a given Transition.
2930
2987
  * This filters out orchestration options and returns true
2931
2988
  * if any options are left.
2932
2989
  */
2933
- function isTransitionDefined({ when, delay: _delay, delayChildren, staggerChildren, staggerDirection, repeat, repeatType, repeatDelay, from, elapsed, ...transition }) {
2934
- return !!Object.keys(transition).length;
2990
+ function isTransitionDefined(transition) {
2991
+ for (const key in transition) {
2992
+ if (!orchestrationKeys.has(key))
2993
+ return true;
2994
+ }
2995
+ return false;
2935
2996
  }
2936
2997
 
2937
2998
  const animateMotionValue = (name, value, target, transition = {}, element, isHandoff) => (onComplete) => {
@@ -4063,19 +4124,6 @@ const supportsPartialKeyframes = /*@__PURE__*/ motionUtils.memo(() => {
4063
4124
  return true;
4064
4125
  });
4065
4126
 
4066
- /**
4067
- * A list of values that can be hardware-accelerated.
4068
- */
4069
- const acceleratedValues = new Set([
4070
- "opacity",
4071
- "clipPath",
4072
- "filter",
4073
- "transform",
4074
- // TODO: Can be accelerated but currently disabled until https://issues.chromium.org/issues/41491098 is resolved
4075
- // or until we implement support for linear() easing.
4076
- // "background-color"
4077
- ]);
4078
-
4079
4127
  function resolveElements(elementOrSelector, scope, selectorCache) {
4080
4128
  if (elementOrSelector == null) {
4081
4129
  return [];
@@ -4961,6 +5009,7 @@ function attachFollow(value, source, options = {}) {
4961
5009
  activeAnimation.stop();
4962
5010
  activeAnimation = null;
4963
5011
  }
5012
+ value.animation = undefined;
4964
5013
  };
4965
5014
  const startAnimation = () => {
4966
5015
  const currentValue = asNumber$1(value.get());
@@ -4992,8 +5041,10 @@ function attachFollow(value, source, options = {}) {
4992
5041
  // multiple calls within the same frame (e.g. rapid mouse events)
4993
5042
  const scheduleAnimation = () => {
4994
5043
  startAnimation();
5044
+ value.animation = activeAnimation ?? undefined;
4995
5045
  value["events"].animationStart?.notify();
4996
5046
  activeAnimation?.then(() => {
5047
+ value.animation = undefined;
4997
5048
  value["events"].animationComplete?.notify();
4998
5049
  });
4999
5050
  };
@@ -6292,10 +6343,8 @@ function applyTreeDeltas(box, treeScale, treePath, isSharedTransition = false) {
6292
6343
  node.options.layoutScroll &&
6293
6344
  node.scroll &&
6294
6345
  node !== node.root) {
6295
- transformBox(box, {
6296
- x: -node.scroll.offset.x,
6297
- y: -node.scroll.offset.y,
6298
- });
6346
+ translateAxis(box.x, -node.scroll.offset.x);
6347
+ translateAxis(box.y, -node.scroll.offset.y);
6299
6348
  }
6300
6349
  if (delta) {
6301
6350
  // Incoporate each ancestor's scale into a cumulative treeScale for this component
@@ -6322,8 +6371,8 @@ function applyTreeDeltas(box, treeScale, treePath, isSharedTransition = false) {
6322
6371
  }
6323
6372
  }
6324
6373
  function translateAxis(axis, distance) {
6325
- axis.min = axis.min + distance;
6326
- axis.max = axis.max + distance;
6374
+ axis.min += distance;
6375
+ axis.max += distance;
6327
6376
  }
6328
6377
  /**
6329
6378
  * Apply a transform to an axis from the latest resolved motion values.
@@ -7490,8 +7539,13 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
7490
7539
  return transform || "none";
7491
7540
  }
7492
7541
 
7493
- const borders = ["TopLeft", "TopRight", "BottomLeft", "BottomRight"];
7494
- const numBorders = borders.length;
7542
+ const borderLabels = [
7543
+ "borderTopLeftRadius",
7544
+ "borderTopRightRadius",
7545
+ "borderBottomLeftRadius",
7546
+ "borderBottomRightRadius",
7547
+ ];
7548
+ const numBorders = borderLabels.length;
7495
7549
  const asNumber = (value) => typeof value === "string" ? parseFloat(value) : value;
7496
7550
  const isPx = (value) => typeof value === "number" || px.test(value);
7497
7551
  function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnlyMember) {
@@ -7506,7 +7560,7 @@ function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnl
7506
7560
  * Mix border radius
7507
7561
  */
7508
7562
  for (let i = 0; i < numBorders; i++) {
7509
- const borderLabel = `border${borders[i]}Radius`;
7563
+ const borderLabel = borderLabels[i];
7510
7564
  let followRadius = getRadius(follow, borderLabel);
7511
7565
  let leadRadius = getRadius(lead, borderLabel);
7512
7566
  if (followRadius === undefined && leadRadius === undefined)
@@ -8151,6 +8205,11 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
8151
8205
  }
8152
8206
  else {
8153
8207
  this.isUpdating = false;
8208
+ /**
8209
+ * Ensure animation-blocked nodes (e.g. during drag)
8210
+ * get measured even when memoized (willUpdate skipped).
8211
+ */
8212
+ this.nodes.forEach(ensureDraggedNodesSnapshotted);
8154
8213
  /**
8155
8214
  * Write
8156
8215
  */
@@ -8249,7 +8308,8 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
8249
8308
  const prevLayout = this.layout;
8250
8309
  this.layout = this.measure(false);
8251
8310
  this.layoutVersion++;
8252
- this.layoutCorrected = createBox();
8311
+ if (!this.layoutCorrected)
8312
+ this.layoutCorrected = createBox();
8253
8313
  this.isLayoutDirty = false;
8254
8314
  this.projectionDelta = undefined;
8255
8315
  this.notifyListeners("measure", this.layout.layoutBox);
@@ -8360,8 +8420,8 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
8360
8420
  }
8361
8421
  return boxWithoutScroll;
8362
8422
  }
8363
- applyTransform(box, transformOnly = false) {
8364
- const withTransforms = createBox();
8423
+ applyTransform(box, transformOnly = false, output) {
8424
+ const withTransforms = output || createBox();
8365
8425
  copyBoxInto(withTransforms, box);
8366
8426
  for (let i = 0; i < this.path.length; i++) {
8367
8427
  const node = this.path[i];
@@ -8369,10 +8429,8 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
8369
8429
  node.options.layoutScroll &&
8370
8430
  node.scroll &&
8371
8431
  node !== node.root) {
8372
- transformBox(withTransforms, {
8373
- x: -node.scroll.offset.x,
8374
- y: -node.scroll.offset.y,
8375
- });
8432
+ translateAxis(withTransforms.x, -node.scroll.offset.x);
8433
+ translateAxis(withTransforms.y, -node.scroll.offset.y);
8376
8434
  }
8377
8435
  if (!hasTransform(node.latestValues))
8378
8436
  continue;
@@ -8515,8 +8573,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
8515
8573
  }
8516
8574
  else if (this.targetDelta) {
8517
8575
  if (Boolean(this.resumingFrom)) {
8518
- // TODO: This is creating a new object every frame
8519
- this.target = this.applyTransform(this.layout.layoutBox);
8576
+ this.applyTransform(this.layout.layoutBox, false, this.target);
8520
8577
  }
8521
8578
  else {
8522
8579
  copyBoxInto(this.target, this.layout.layoutBox);
@@ -9247,6 +9304,18 @@ function clearMeasurements(node) {
9247
9304
  function clearIsLayoutDirty(node) {
9248
9305
  node.isLayoutDirty = false;
9249
9306
  }
9307
+ /**
9308
+ * When a node is animation-blocked (e.g. during drag) and its component
9309
+ * didn't re-render (memoized), willUpdate() is never called so there's
9310
+ * no snapshot. Use the previous layout as a snapshot and mark dirty so
9311
+ * resetTransform/updateLayout/notifyLayoutUpdate process it normally.
9312
+ */
9313
+ function ensureDraggedNodesSnapshotted(node) {
9314
+ if (node.isAnimationBlocked && node.layout && !node.isLayoutDirty) {
9315
+ node.snapshot = node.layout;
9316
+ node.isLayoutDirty = true;
9317
+ }
9318
+ }
9250
9319
  function resetTransformStyle(node) {
9251
9320
  const { visualElement } = node.options;
9252
9321
  if (visualElement && visualElement.getProps().onBeforeLayoutMeasure) {