motion-dom 14.3.0 → 14.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -3240,6 +3240,7 @@ class TransformAnimation extends JSAnimation {
3240
3240
  constructor(options) {
3241
3241
  const element = getOwner(options).current;
3242
3242
  const { driver = frameloopDriver } = options;
3243
+ let animation;
3243
3244
  /**
3244
3245
  * Before JSAnimation replaces the type with its generator.
3245
3246
  */
@@ -3269,11 +3270,12 @@ class TransformAnimation extends JSAnimation {
3269
3270
  stop: () => {
3270
3271
  js.stop();
3271
3272
  const group = getGroup(element);
3272
- group?.schedule(!group.isFinishing);
3273
+ group && !group.isFinishing && group.release(animation);
3273
3274
  },
3274
3275
  };
3275
3276
  };
3276
3277
  super(options);
3278
+ animation = this;
3277
3279
  this.canAccelerate = canAccelerate;
3278
3280
  /**
3279
3281
  * stop is bound in JSAnimation, so wrap it rather than override.
@@ -3429,8 +3431,14 @@ class TransformAnimation extends JSAnimation {
3429
3431
  }
3430
3432
  return super.time;
3431
3433
  }
3434
+ /**
3435
+ * A JS animation sets its motion value from a seek on its next tick.
3436
+ * This doesn't tick, so set it now, rather than leave it to be set
3437
+ * when this next stops.
3438
+ */
3432
3439
  set time(newTime) {
3433
3440
  super.time = newTime;
3441
+ this.isAccelerated() && this.sync();
3434
3442
  }
3435
3443
  /**
3436
3444
  * Scroll timelines set time every frame, so run these on the main
@@ -3458,6 +3466,7 @@ function createGroup(owner) {
3458
3466
  let segments = [];
3459
3467
  let needsBuild = false;
3460
3468
  let isScheduled = false;
3469
+ let isEnded = false;
3461
3470
  let hasChurned = false;
3462
3471
  let lastChurn = -Infinity;
3463
3472
  const has = (key) => {
@@ -3574,8 +3583,19 @@ function createGroup(owner) {
3574
3583
  for (const track of moving) {
3575
3584
  const values = at.map((t) => track.numberAt(t));
3576
3585
  const [first] = values;
3577
- const range = values[values.length - 1] - first;
3578
- const progress = values.map((value) => range ? (value - first) / range : value - first);
3586
+ const changes = values.map((value) => value - first);
3587
+ const end = changes[changes.length - 1];
3588
+ /**
3589
+ * A value that ends only a tiny way from where it started, as
3590
+ * a round trip does if it's built just after it starts, or
3591
+ * from a float residue, ends where it started. Dividing by
3592
+ * that tiny range would stretch the easing to values the
3593
+ * compositor can't render.
3594
+ */
3595
+ const range = changes.some((change) => Math.abs(change) * easingTolerance > Math.abs(end))
3596
+ ? 0
3597
+ : end;
3598
+ const progress = changes.map((change) => range ? change / range : change);
3579
3599
  /**
3580
3600
  * A value that doesn't change, like one still in its delay,
3581
3601
  * fits any easing. One that changes but ends where it started
@@ -3877,6 +3897,18 @@ function createGroup(owner) {
3877
3897
  }
3878
3898
  return true;
3879
3899
  };
3900
+ /**
3901
+ * If no animations are left, write where they got to and cancel the
3902
+ * WAAPI animations. Returns whether the group ended.
3903
+ */
3904
+ const end = () => {
3905
+ if (tracks.size)
3906
+ return false;
3907
+ isEnded = true;
3908
+ transformGroups.delete(element);
3909
+ group.isJS || stop();
3910
+ return true;
3911
+ };
3880
3912
  const build = () => {
3881
3913
  const now = time.now();
3882
3914
  /**
@@ -3904,15 +3936,14 @@ function createGroup(owner) {
3904
3936
  group.isFinishing = false;
3905
3937
  }
3906
3938
  isScheduled = false;
3939
+ if (isEnded)
3940
+ return;
3907
3941
  tracks.forEach(({ state }, track) => {
3908
3942
  if (state === "idle" || state === "finished")
3909
3943
  tracks.delete(track);
3910
3944
  });
3911
- if (!tracks.size) {
3912
- transformGroups.delete(element);
3913
- group.isJS || stop();
3945
+ if (end())
3914
3946
  return;
3915
- }
3916
3947
  for (const track of tracks) {
3917
3948
  if (!track.canAccelerate)
3918
3949
  return group.demote(true);
@@ -3944,6 +3975,8 @@ function createGroup(owner) {
3944
3975
  isJS: false,
3945
3976
  isFinishing: false,
3946
3977
  schedule(build_, isChurn) {
3978
+ if (isEnded)
3979
+ return;
3947
3980
  needsBuild || (needsBuild = build_);
3948
3981
  hasChurned || (hasChurned = !!isChurn);
3949
3982
  /**
@@ -3958,6 +3991,21 @@ function createGroup(owner) {
3958
3991
  : queueMicrotask(build);
3959
3992
  }
3960
3993
  },
3994
+ /**
3995
+ * End the group now, rather than when it next builds, so the
3996
+ * element's transform is the caller's again as soon as its last
3997
+ * animation is stopped.
3998
+ */
3999
+ release(track) {
4000
+ /**
4001
+ * Only drop the stopped animation. Others can be idle because
4002
+ * they're part way through play(), and are about to run.
4003
+ */
4004
+ const { state } = track;
4005
+ if (state === "idle" || state === "finished")
4006
+ tracks.delete(track);
4007
+ end() || group.schedule(true);
4008
+ },
3961
4009
  /**
3962
4010
  * An outside write. Rebuild once, but if it keeps happening, move
3963
4011
  * to the main thread.
@@ -3972,7 +4020,7 @@ function createGroup(owner) {
3972
4020
  }
3973
4021
  },
3974
4022
  demote(canReturn) {
3975
- if (group.isJS)
4023
+ if (group.isJS || isEnded)
3976
4024
  return;
3977
4025
  /**
3978
4026
  * Unless only another animation is holding the group on the
@@ -4979,11 +5027,11 @@ function arc(options = {}) {
4979
5027
  }
4980
5028
  return sample({ x: tx, y: ty }, { x: 0, y: 0 });
4981
5029
  },
4982
- animateVisualElement(visualElement, target, transition, delay, animations) {
5030
+ animateValues(getValue, target, transition, delay, animations) {
4983
5031
  if (!("x" in target || "y" in target))
4984
5032
  return;
4985
- const xValue = visualElement.getValue("x", visualElement.latestValues["x"] ?? 0);
4986
- const yValue = visualElement.getValue("y", visualElement.latestValues["y"] ?? 0);
5033
+ const xValue = getValue("x");
5034
+ const yValue = getValue("y");
4987
5035
  const xRaw = target.x;
4988
5036
  const yRaw = target.y;
4989
5037
  const xFrom = (Array.isArray(xRaw) && xRaw[0] != null
@@ -5006,7 +5054,7 @@ function arc(options = {}) {
5006
5054
  // concurrent rotate animation composes and nothing accumulates
5007
5055
  // on interrupt.
5008
5056
  const pathRotationValue = interpolate(0).rotate !== undefined
5009
- ? visualElement.getValue("pathRotation", 0)
5057
+ ? getValue("pathRotation")
5010
5058
  : undefined;
5011
5059
  const pathTransition = {
5012
5060
  delay,
@@ -5216,7 +5264,9 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
5216
5264
  const path = transition?.path;
5217
5265
  if (path) {
5218
5266
  // path mutates `target` to claim x/y; loop below skips them.
5219
- path.animateVisualElement(visualElement, target, transition, delay, animations);
5267
+ path.animateValues
5268
+ ? path.animateValues((key) => visualElement.getValue(key, visualElement.latestValues[key] ?? 0), target, transition, delay, animations)
5269
+ : path.animateVisualElement?.(visualElement, target, transition, delay, animations);
5220
5270
  }
5221
5271
  for (const key in target) {
5222
5272
  const value = visualElement.getValue(key, visualElement.latestValues[key] ?? null);
@@ -5675,8 +5725,16 @@ function findEffect(subject) {
5675
5725
  */
5676
5726
  function animateValues(getValue, keyframes, transition = {}, element) {
5677
5727
  const animations = [];
5678
- const { velocity } = transition;
5728
+ const { velocity, path } = transition;
5679
5729
  const reduceMotion = transition.reduceMotion ?? element?.shouldReduceMotion;
5730
+ /**
5731
+ * Only elements have x/y to move along a path. path claims them by
5732
+ * deleting them, so hand it a copy.
5733
+ */
5734
+ if (path?.animateValues && element) {
5735
+ keyframes = { ...keyframes };
5736
+ path.animateValues(getValue, keyframes, transition, 0, animations);
5737
+ }
5680
5738
  for (const key in keyframes) {
5681
5739
  if (key === "transition" || key === "transitionEnd")
5682
5740
  continue;
@@ -6281,7 +6339,7 @@ function addSVGPathValue(element, state, key, value) {
6281
6339
  }
6282
6340
  }
6283
6341
  const addSVGValue = (element, state, key, value) => {
6284
- if (key.startsWith("path")) {
6342
+ if (key.startsWith("path") && !transformProps.has(key)) {
6285
6343
  return addSVGPathValue(element, state, key, value);
6286
6344
  }
6287
6345
  else if (key.startsWith("attr")) {
@@ -6382,6 +6440,7 @@ function hasTransform(values) {
6382
6440
  values.rotate ||
6383
6441
  values.rotateX ||
6384
6442
  values.rotateY ||
6443
+ values.rotateZ ||
6385
6444
  values.skewX ||
6386
6445
  values.skewY);
6387
6446
  }
@@ -10168,7 +10227,7 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
10168
10227
  transform += `scale(${1 / treeScale.x}, ${1 / treeScale.y}) `;
10169
10228
  }
10170
10229
  if (latestTransform) {
10171
- const { transformPerspective, rotate, pathRotation, rotateX, rotateY, skewX, skewY, } = latestTransform;
10230
+ const { transformPerspective, rotate, pathRotation, rotateX, rotateY, rotateZ, skewX, skewY, } = latestTransform;
10172
10231
  if (transformPerspective)
10173
10232
  transform = `perspective(${transformPerspective}px) ${transform}`;
10174
10233
  if (rotate)
@@ -10180,6 +10239,8 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
10180
10239
  transform += `rotateX(${rotateX}deg) `;
10181
10240
  if (rotateY)
10182
10241
  transform += `rotateY(${rotateY}deg) `;
10242
+ if (rotateZ)
10243
+ transform += `rotateZ(${rotateZ}deg) `;
10183
10244
  if (skewX)
10184
10245
  transform += `skewX(${skewX}deg) `;
10185
10246
  if (skewY)
@@ -10198,6 +10259,15 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
10198
10259
  }
10199
10260
 
10200
10261
  const numBorders = cornerRadiusProps.length;
10262
+ const mixedTransforms = [
10263
+ "rotate",
10264
+ "rotateX",
10265
+ "rotateY",
10266
+ "rotateZ",
10267
+ "skewX",
10268
+ "skewY",
10269
+ ];
10270
+ const numMixedTransforms = mixedTransforms.length;
10201
10271
  const isPx = (value) => typeof value === "number" || px.test(value);
10202
10272
  function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnlyMember) {
10203
10273
  if (shouldCrossfadeOpacity) {
@@ -10232,10 +10302,14 @@ function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnl
10232
10302
  }
10233
10303
  }
10234
10304
  /**
10235
- * Mix rotation
10305
+ * Mix rotation and skew. These can't be projected, so mix them every
10306
+ * frame rather than leaving them frozen at the animation-start snapshot.
10236
10307
  */
10237
- if (follow.rotate || lead.rotate) {
10238
- target.rotate = mixNumber$1(follow.rotate || 0, lead.rotate || 0, progress);
10308
+ for (let i = 0; i < numMixedTransforms; i++) {
10309
+ const key = mixedTransforms[i];
10310
+ if (follow[key] || lead[key]) {
10311
+ target[key] = mixNumber$1(follow[key] || 0, lead[key] || 0, progress);
10312
+ }
10239
10313
  }
10240
10314
  }
10241
10315
  function getRadius(values, radiusName) {
@@ -10685,82 +10759,92 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10685
10759
  if (layoutId) {
10686
10760
  this.root.registerSharedNode(layoutId, this);
10687
10761
  }
10688
- // Only register the handler if it requires layout animation
10689
- if (this.options.animate !== false &&
10690
- visualElement &&
10691
- (layoutId || layout)) {
10692
- this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
10693
- if (this.isAnimationBlocked) {
10694
- this.target = undefined;
10695
- this.relativeTarget = undefined;
10696
- return;
10762
+ this.addLayoutListener();
10763
+ }
10764
+ /**
10765
+ * Only register the layout animation handler if this node requires
10766
+ * layout animation. This is called on mount, and again whenever options
10767
+ * change, as `layout` or `layoutId` may be enabled after mount.
10768
+ */
10769
+ addLayoutListener() {
10770
+ const { layoutId, layout, visualElement, animate } = this.options;
10771
+ if (!this.instance ||
10772
+ this.hasLayoutListener ||
10773
+ animate === false ||
10774
+ !visualElement ||
10775
+ !(layoutId || layout))
10776
+ return;
10777
+ this.hasLayoutListener = true;
10778
+ this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
10779
+ if (this.isAnimationBlocked) {
10780
+ this.target = undefined;
10781
+ this.relativeTarget = undefined;
10782
+ return;
10783
+ }
10784
+ // TODO: Check here if an animation exists
10785
+ const layoutTransition = this.options.transition ||
10786
+ visualElement.getDefaultTransition() ||
10787
+ defaultLayoutTransition;
10788
+ const { onLayoutAnimationStart, onLayoutAnimationComplete, } = visualElement.getProps();
10789
+ /**
10790
+ * The target layout of the element might stay the same,
10791
+ * but its position relative to its parent has changed.
10792
+ */
10793
+ const hasTargetChanged = !this.targetLayout ||
10794
+ !boxEqualsRounded(this.targetLayout, newLayout);
10795
+ /*
10796
+ * Note: Disabled to fix relative animations always triggering new
10797
+ * layout animations. If this causes further issues, we can try
10798
+ * a different approach to detecting relative target changes.
10799
+ */
10800
+ // || hasRelativeLayoutChanged
10801
+ /**
10802
+ * If the layout hasn't seemed to have changed, it might be that the
10803
+ * element is visually in the same place in the document but its position
10804
+ * relative to its parent has indeed changed. So here we check for that.
10805
+ */
10806
+ const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeLayoutChanged;
10807
+ if (this.options.layoutRoot ||
10808
+ this.resumeFrom ||
10809
+ hasOnlyRelativeTargetChanged ||
10810
+ (hasLayoutChanged &&
10811
+ (hasTargetChanged || !this.currentAnimation))) {
10812
+ if (this.resumeFrom) {
10813
+ this.resumingFrom = this.resumeFrom;
10814
+ this.resumingFrom.resumingFrom = undefined;
10815
+ }
10816
+ const animationOptions = {
10817
+ ...getValueTransition(layoutTransition, "layout"),
10818
+ onPlay: onLayoutAnimationStart,
10819
+ onComplete: onLayoutAnimationComplete,
10820
+ };
10821
+ if (visualElement.shouldReduceMotion ||
10822
+ this.options.layoutRoot) {
10823
+ animationOptions.delay = 0;
10824
+ animationOptions.type = false;
10697
10825
  }
10698
- // TODO: Check here if an animation exists
10699
- const layoutTransition = this.options.transition ||
10700
- visualElement.getDefaultTransition() ||
10701
- defaultLayoutTransition;
10702
- const { onLayoutAnimationStart, onLayoutAnimationComplete, } = visualElement.getProps();
10826
+ this.startAnimation(animationOptions);
10703
10827
  /**
10704
- * The target layout of the element might stay the same,
10705
- * but its position relative to its parent has changed.
10706
- */
10707
- const hasTargetChanged = !this.targetLayout ||
10708
- !boxEqualsRounded(this.targetLayout, newLayout);
10709
- /*
10710
- * Note: Disabled to fix relative animations always triggering new
10711
- * layout animations. If this causes further issues, we can try
10712
- * a different approach to detecting relative target changes.
10828
+ * Set animation origin after starting animation to avoid layout jump
10829
+ * caused by stopping previous layout animation
10713
10830
  */
10714
- // || hasRelativeLayoutChanged
10831
+ this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged, animationOptions.path);
10832
+ }
10833
+ else {
10715
10834
  /**
10716
- * If the layout hasn't seemed to have changed, it might be that the
10717
- * element is visually in the same place in the document but its position
10718
- * relative to its parent has indeed changed. So here we check for that.
10835
+ * If the layout hasn't changed and we have an animation that hasn't started yet,
10836
+ * finish it immediately. Otherwise it will be animating from a location
10837
+ * that was probably never committed to screen and look like a jumpy box.
10719
10838
  */
10720
- const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeLayoutChanged;
10721
- if (this.options.layoutRoot ||
10722
- this.resumeFrom ||
10723
- hasOnlyRelativeTargetChanged ||
10724
- (hasLayoutChanged &&
10725
- (hasTargetChanged || !this.currentAnimation))) {
10726
- if (this.resumeFrom) {
10727
- this.resumingFrom = this.resumeFrom;
10728
- this.resumingFrom.resumingFrom = undefined;
10729
- }
10730
- const animationOptions = {
10731
- ...getValueTransition(layoutTransition, "layout"),
10732
- onPlay: onLayoutAnimationStart,
10733
- onComplete: onLayoutAnimationComplete,
10734
- };
10735
- if (visualElement.shouldReduceMotion ||
10736
- this.options.layoutRoot) {
10737
- animationOptions.delay = 0;
10738
- animationOptions.type = false;
10739
- }
10740
- this.startAnimation(animationOptions);
10741
- /**
10742
- * Set animation origin after starting animation to avoid layout jump
10743
- * caused by stopping previous layout animation
10744
- */
10745
- this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged, animationOptions
10746
- .path);
10839
+ if (!hasLayoutChanged) {
10840
+ finishAnimation(this);
10747
10841
  }
10748
- else {
10749
- /**
10750
- * If the layout hasn't changed and we have an animation that hasn't started yet,
10751
- * finish it immediately. Otherwise it will be animating from a location
10752
- * that was probably never committed to screen and look like a jumpy box.
10753
- */
10754
- if (!hasLayoutChanged) {
10755
- finishAnimation(this);
10756
- }
10757
- if (this.isLead() && this.options.onExitComplete) {
10758
- this.options.onExitComplete();
10759
- }
10842
+ if (this.isLead() && this.options.onExitComplete) {
10843
+ this.options.onExitComplete();
10760
10844
  }
10761
- this.targetLayout = newLayout;
10762
- });
10763
- }
10845
+ }
10846
+ this.targetLayout = newLayout;
10847
+ });
10764
10848
  }
10765
10849
  unmount() {
10766
10850
  this.options.layoutId && this.willUpdate();
@@ -10770,6 +10854,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10770
10854
  this.parent && this.parent.children.delete(this);
10771
10855
  this.instance = undefined;
10772
10856
  this.eventHandlers.clear();
10857
+ this.hasLayoutListener = false;
10773
10858
  cancelFrame(this.updateProjection);
10774
10859
  }
10775
10860
  // only on the root
@@ -10940,7 +11025,16 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10940
11025
  }
10941
11026
  clearAllSnapshots() {
10942
11027
  this.nodes.forEach(clearSnapshot);
10943
- this.sharedNodes.forEach(removeLeadSnapshots);
11028
+ /**
11029
+ * A stack's last member can unmount while a replacement with the
11030
+ * same layoutId mounts in the same commit, so the stack (and its
11031
+ * lead) must outlive the unmount for the replacement to resume
11032
+ * from it. By now that handoff has happened, so stacks that are
11033
+ * still empty only retain unmounted nodes and can be released.
11034
+ */
11035
+ this.sharedNodes.forEach((stack, layoutId) => stack.members.length
11036
+ ? stack.removeLeadSnapshot()
11037
+ : this.sharedNodes.delete(layoutId));
10944
11038
  }
10945
11039
  scheduleUpdateProjection() {
10946
11040
  if (!this.projectionUpdateScheduled) {
@@ -11164,6 +11258,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
11164
11258
  ...options,
11165
11259
  crossfade: options.crossfade !== undefined ? options.crossfade : true,
11166
11260
  };
11261
+ this.addLayoutListener();
11167
11262
  }
11168
11263
  clearMeasurements() {
11169
11264
  this.scroll = undefined;
@@ -12093,9 +12188,6 @@ function calcProjection(node) {
12093
12188
  function resetSkewAndRotation(node) {
12094
12189
  node.resetSkewAndRotation();
12095
12190
  }
12096
- function removeLeadSnapshots(stack) {
12097
- stack.removeLeadSnapshot();
12098
- }
12099
12191
  function mixAxisDeltaLinear(output, delta, p) {
12100
12192
  output.translate = mixNumber$1(delta.translate, 0, p);
12101
12193
  output.scale = mixNumber$1(delta.scale, 1, p);