motion-dom 14.2.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.
Files changed (38) hide show
  1. package/dist/cjs/index.js +257 -106
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/animate/effects.mjs +9 -1
  4. package/dist/es/animation/animate/effects.mjs.map +1 -1
  5. package/dist/es/animation/interfaces/visual-element-target.mjs +3 -1
  6. package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
  7. package/dist/es/animation/utils/arc.mjs +4 -4
  8. package/dist/es/animation/utils/arc.mjs.map +1 -1
  9. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +56 -8
  10. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -1
  11. package/dist/es/effects/svg/index.mjs +1 -1
  12. package/dist/es/effects/svg/index.mjs.map +1 -1
  13. package/dist/es/projection/animation/mix-values.mjs +16 -3
  14. package/dist/es/projection/animation/mix-values.mjs.map +1 -1
  15. package/dist/es/projection/node/create-projection-node.mjs +91 -73
  16. package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
  17. package/dist/es/projection/styles/transform.mjs +3 -1
  18. package/dist/es/projection/styles/transform.mjs.map +1 -1
  19. package/dist/es/projection/utils/has-transform.mjs +1 -0
  20. package/dist/es/projection/utils/has-transform.mjs.map +1 -1
  21. package/dist/es/render/dom/parse-transform.mjs +4 -2
  22. package/dist/es/render/dom/parse-transform.mjs.map +1 -1
  23. package/dist/es/render/svg/utils/scrape-motion-values.mjs +7 -2
  24. package/dist/es/render/svg/utils/scrape-motion-values.mjs.map +1 -1
  25. package/dist/es/render/utils/keys-transform.mjs +1 -0
  26. package/dist/es/render/utils/keys-transform.mjs.map +1 -1
  27. package/dist/es/utils/stagger.mjs +39 -6
  28. package/dist/es/utils/stagger.mjs.map +1 -1
  29. package/dist/es/value/types/complex/index.mjs +3 -2
  30. package/dist/es/value/types/complex/index.mjs.map +1 -1
  31. package/dist/es/value/utils/follow.mjs +19 -2
  32. package/dist/es/value/utils/follow.mjs.map +1 -1
  33. package/dist/index.d.ts +43 -6
  34. package/dist/motion-dom.dev.js +257 -106
  35. package/dist/motion-dom.js +1 -1
  36. package/dist/size-rollup-style-effect.js +1 -1
  37. package/dist/size-rollup-style-effect.js.map +1 -1
  38. package/package.json +2 -2
package/dist/cjs/index.js CHANGED
@@ -249,8 +249,9 @@ const COLOR_TOKEN = "color";
249
249
  const VAR_TOKEN = "var";
250
250
  const VAR_FUNCTION_TOKEN = "var(";
251
251
  const SPLIT_TOKEN = "${}";
252
- // this regex consists of the `singleCssVariableRegex|rgbHSLValueRegex|digitRegex`
253
- const complexRegex = /var\s*\(\s*--(?:[\w-]+\s*|[\w-]+\s*,(?:\s*[^)(\s]|\s*\((?:[^)(]|\([^)(]*\))*\))+\s*)\)|#[\da-f]{3,8}|(?:rgb|hsl)a?\((?:-?[\d.]+%?[,\s]+){2}-?[\d.]+%?\s*(?:[,/]\s*)?(?:\b\d+(?:\.\d+)?|\.\d+)?%?\)|-?(?:\d+(?:\.\d+)?|\.\d+)/giu;
252
+ // this regex consists of the `singleCssVariableRegex|rgbHSLValueRegex|digitRegex`.
253
+ // The lookahead skips digits in function names, like the 3 in matrix3d(.
254
+ const complexRegex = /var\s*\(\s*--(?:[\w-]+\s*|[\w-]+\s*,(?:\s*[^)(\s]|\s*\((?:[^)(]|\([^)(]*\))*\))+\s*)\)|#[\da-f]{3,8}|(?:rgb|hsl)a?\((?:-?[\d.]+%?[,\s]+){2}-?[\d.]+%?\s*(?:[,/]\s*)?(?:\b\d+(?:\.\d+)?|\.\d+)?%?\)|-?(?:\d+(?:\.\d+)?|\.\d+)(?![a-z]*\()/giu;
254
255
  /**
255
256
  * Whether analyseComplexValue(value) would find any values, without
256
257
  * tokenising the string. CSS variables are resolved before this is
@@ -2193,6 +2194,7 @@ const matrix2dParsers = {
2193
2194
  translateY: 5,
2194
2195
  scaleX: 0,
2195
2196
  scaleY: 3,
2197
+ scaleZ: () => 1,
2196
2198
  scale: (v) => (Math.abs(v[0]) + Math.abs(v[3])) / 2,
2197
2199
  rotate,
2198
2200
  rotateZ: rotate,
@@ -2207,8 +2209,8 @@ const rebaseAngle = (angle) => {
2207
2209
  return angle;
2208
2210
  };
2209
2211
  const rotateZ = rotate;
2210
- const scaleX = (v) => Math.sqrt(v[0] * v[0] + v[1] * v[1]);
2211
- const scaleY = (v) => Math.sqrt(v[4] * v[4] + v[5] * v[5]);
2212
+ const scaleX = (v) => Math.hypot(v[0], v[1]);
2213
+ const scaleY = (v) => Math.hypot(v[4], v[5]);
2212
2214
  const matrix3dParsers = {
2213
2215
  x: 12,
2214
2216
  y: 13,
@@ -2218,6 +2220,7 @@ const matrix3dParsers = {
2218
2220
  translateZ: 14,
2219
2221
  scaleX,
2220
2222
  scaleY,
2223
+ scaleZ: (v) => Math.hypot(v[8], v[9], v[10]),
2221
2224
  scale: (v) => (scaleX(v) + scaleY(v)) / 2,
2222
2225
  rotateX: (v) => rebaseAngle(radToDeg(Math.atan2(v[6], v[5]))),
2223
2226
  rotateY: (v) => rebaseAngle(radToDeg(Math.atan2(-v[2], v[0]))),
@@ -2277,6 +2280,7 @@ const transformPropOrder = [
2277
2280
  "scale",
2278
2281
  "scaleX",
2279
2282
  "scaleY",
2283
+ "scaleZ",
2280
2284
  "rotate",
2281
2285
  "rotateX",
2282
2286
  "rotateY",
@@ -3236,6 +3240,7 @@ class TransformAnimation extends JSAnimation {
3236
3240
  constructor(options) {
3237
3241
  const element = getOwner(options).current;
3238
3242
  const { driver = frameloopDriver } = options;
3243
+ let animation;
3239
3244
  /**
3240
3245
  * Before JSAnimation replaces the type with its generator.
3241
3246
  */
@@ -3265,11 +3270,12 @@ class TransformAnimation extends JSAnimation {
3265
3270
  stop: () => {
3266
3271
  js.stop();
3267
3272
  const group = getGroup(element);
3268
- group?.schedule(!group.isFinishing);
3273
+ group && !group.isFinishing && group.release(animation);
3269
3274
  },
3270
3275
  };
3271
3276
  };
3272
3277
  super(options);
3278
+ animation = this;
3273
3279
  this.canAccelerate = canAccelerate;
3274
3280
  /**
3275
3281
  * stop is bound in JSAnimation, so wrap it rather than override.
@@ -3425,8 +3431,14 @@ class TransformAnimation extends JSAnimation {
3425
3431
  }
3426
3432
  return super.time;
3427
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
+ */
3428
3439
  set time(newTime) {
3429
3440
  super.time = newTime;
3441
+ this.isAccelerated() && this.sync();
3430
3442
  }
3431
3443
  /**
3432
3444
  * Scroll timelines set time every frame, so run these on the main
@@ -3454,6 +3466,7 @@ function createGroup(owner) {
3454
3466
  let segments = [];
3455
3467
  let needsBuild = false;
3456
3468
  let isScheduled = false;
3469
+ let isEnded = false;
3457
3470
  let hasChurned = false;
3458
3471
  let lastChurn = -Infinity;
3459
3472
  const has = (key) => {
@@ -3570,8 +3583,19 @@ function createGroup(owner) {
3570
3583
  for (const track of moving) {
3571
3584
  const values = at.map((t) => track.numberAt(t));
3572
3585
  const [first] = values;
3573
- const range = values[values.length - 1] - first;
3574
- 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);
3575
3599
  /**
3576
3600
  * A value that doesn't change, like one still in its delay,
3577
3601
  * fits any easing. One that changes but ends where it started
@@ -3873,6 +3897,18 @@ function createGroup(owner) {
3873
3897
  }
3874
3898
  return true;
3875
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
+ };
3876
3912
  const build = () => {
3877
3913
  const now = time.now();
3878
3914
  /**
@@ -3900,15 +3936,14 @@ function createGroup(owner) {
3900
3936
  group.isFinishing = false;
3901
3937
  }
3902
3938
  isScheduled = false;
3939
+ if (isEnded)
3940
+ return;
3903
3941
  tracks.forEach(({ state }, track) => {
3904
3942
  if (state === "idle" || state === "finished")
3905
3943
  tracks.delete(track);
3906
3944
  });
3907
- if (!tracks.size) {
3908
- transformGroups.delete(element);
3909
- group.isJS || stop();
3945
+ if (end())
3910
3946
  return;
3911
- }
3912
3947
  for (const track of tracks) {
3913
3948
  if (!track.canAccelerate)
3914
3949
  return group.demote(true);
@@ -3940,6 +3975,8 @@ function createGroup(owner) {
3940
3975
  isJS: false,
3941
3976
  isFinishing: false,
3942
3977
  schedule(build_, isChurn) {
3978
+ if (isEnded)
3979
+ return;
3943
3980
  needsBuild || (needsBuild = build_);
3944
3981
  hasChurned || (hasChurned = !!isChurn);
3945
3982
  /**
@@ -3954,6 +3991,21 @@ function createGroup(owner) {
3954
3991
  : queueMicrotask(build);
3955
3992
  }
3956
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
+ },
3957
4009
  /**
3958
4010
  * An outside write. Rebuild once, but if it keeps happening, move
3959
4011
  * to the main thread.
@@ -3968,7 +4020,7 @@ function createGroup(owner) {
3968
4020
  }
3969
4021
  },
3970
4022
  demote(canReturn) {
3971
- if (group.isJS)
4023
+ if (group.isJS || isEnded)
3972
4024
  return;
3973
4025
  /**
3974
4026
  * Unless only another animation is holding the group on the
@@ -4975,11 +5027,11 @@ function arc(options = {}) {
4975
5027
  }
4976
5028
  return sample({ x: tx, y: ty }, { x: 0, y: 0 });
4977
5029
  },
4978
- animateVisualElement(visualElement, target, transition, delay, animations) {
5030
+ animateValues(getValue, target, transition, delay, animations) {
4979
5031
  if (!("x" in target || "y" in target))
4980
5032
  return;
4981
- const xValue = visualElement.getValue("x", visualElement.latestValues["x"] ?? 0);
4982
- const yValue = visualElement.getValue("y", visualElement.latestValues["y"] ?? 0);
5033
+ const xValue = getValue("x");
5034
+ const yValue = getValue("y");
4983
5035
  const xRaw = target.x;
4984
5036
  const yRaw = target.y;
4985
5037
  const xFrom = (Array.isArray(xRaw) && xRaw[0] != null
@@ -5002,7 +5054,7 @@ function arc(options = {}) {
5002
5054
  // concurrent rotate animation composes and nothing accumulates
5003
5055
  // on interrupt.
5004
5056
  const pathRotationValue = interpolate(0).rotate !== undefined
5005
- ? visualElement.getValue("pathRotation", 0)
5057
+ ? getValue("pathRotation")
5006
5058
  : undefined;
5007
5059
  const pathTransition = {
5008
5060
  delay,
@@ -5212,7 +5264,9 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
5212
5264
  const path = transition?.path;
5213
5265
  if (path) {
5214
5266
  // path mutates `target` to claim x/y; loop below skips them.
5215
- 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);
5216
5270
  }
5217
5271
  for (const key in target) {
5218
5272
  const value = visualElement.getValue(key, visualElement.latestValues[key] ?? null);
@@ -5671,8 +5725,16 @@ function findEffect(subject) {
5671
5725
  */
5672
5726
  function animateValues(getValue, keyframes, transition = {}, element) {
5673
5727
  const animations = [];
5674
- const { velocity } = transition;
5728
+ const { velocity, path } = transition;
5675
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
+ }
5676
5738
  for (const key in keyframes) {
5677
5739
  if (key === "transition" || key === "transitionEnd")
5678
5740
  continue;
@@ -6277,7 +6339,7 @@ function addSVGPathValue(element, state, key, value) {
6277
6339
  }
6278
6340
  }
6279
6341
  const addSVGValue = (element, state, key, value) => {
6280
- if (key.startsWith("path")) {
6342
+ if (key.startsWith("path") && !transformProps.has(key)) {
6281
6343
  return addSVGPathValue(element, state, key, value);
6282
6344
  }
6283
6345
  else if (key.startsWith("attr")) {
@@ -6378,6 +6440,7 @@ function hasTransform(values) {
6378
6440
  values.rotate ||
6379
6441
  values.rotateX ||
6380
6442
  values.rotateY ||
6443
+ values.rotateZ ||
6381
6444
  values.skewX ||
6382
6445
  values.skewY);
6383
6446
  }
@@ -7061,13 +7124,46 @@ function getOriginIndex(from, total) {
7061
7124
  return from === "last" ? lastIndex : lastIndex / 2;
7062
7125
  }
7063
7126
  }
7064
- function stagger(duration = 0.1, { startDelay = 0, from = 0, ease } = {}) {
7127
+ function stagger(duration = 0.1, { startDelay = 0, from = 0, ease, grid } = {}) {
7128
+ /**
7129
+ * Distances from the origin for each element count. The final entry
7130
+ * holds the distance used to normalise easing.
7131
+ */
7132
+ const cache = {};
7065
7133
  return (i, total) => {
7066
- const fromIndex = typeof from === "number" ? from : getOriginIndex(from, total);
7067
- const distance = Math.abs(fromIndex - i);
7068
- let delay = duration * distance;
7069
- if (ease) {
7070
- const maxDelay = total * duration;
7134
+ let distances = cache[total];
7135
+ if (!distances) {
7136
+ const [columns, rows] = grid || [Infinity, 1];
7137
+ const lastX = Math.min(columns, total) - 1;
7138
+ const lastY = Math.max(rows, Math.ceil(total / columns)) - 1;
7139
+ const isIndex = typeof from === "number";
7140
+ const scale = from === "first" ? 0 : from === "last" ? 1 : 0.5;
7141
+ const originX = isIndex ? from % columns : lastX * scale;
7142
+ const originY = isIndex ? Math.floor(from / columns) : lastY * scale;
7143
+ distances = cache[total] = [];
7144
+ for (let j = 0; j < total; j++) {
7145
+ const x = j % columns;
7146
+ const y = Math.floor(j / columns);
7147
+ const edgeX = Math.min(x, lastX - x);
7148
+ const edgeY = Math.min(y, lastY - y);
7149
+ if (from === "random") {
7150
+ // Inside-out shuffle, so each element gets a distinct step
7151
+ const k = (Math.random() * (j + 1)) | 0;
7152
+ distances[j] = distances[k];
7153
+ distances[k] = j;
7154
+ }
7155
+ else {
7156
+ distances[j] =
7157
+ from === "edges"
7158
+ ? Math.min(lastX ? edgeX : edgeY, lastY ? edgeY : edgeX)
7159
+ : Math.hypot(x - originX, y - originY);
7160
+ }
7161
+ }
7162
+ distances.push(grid ? Math.max(...distances) : total);
7163
+ }
7164
+ let delay = duration * distances[i];
7165
+ const maxDelay = duration * distances[total];
7166
+ if (ease && maxDelay) {
7071
7167
  const easingFunction = motionUtils.easingDefinitionToFunction(ease);
7072
7168
  delay = easingFunction(delay / maxDelay) * maxDelay;
7073
7169
  }
@@ -7301,7 +7397,7 @@ function follow(value, source, options) {
7301
7397
  }, stopAnimation);
7302
7398
  if (isMotionValue(source)) {
7303
7399
  let skipNextAnimation = options.skipInitialAnimation === true;
7304
- const removeSourceOnChange = source.on("change", (v) => {
7400
+ const onSourceChange = (v) => {
7305
7401
  if (skipNextAnimation) {
7306
7402
  skipNextAnimation = false;
7307
7403
  value.jump(parseValue(v, unit), false);
@@ -7309,11 +7405,28 @@ function follow(value, source, options) {
7309
7405
  else {
7310
7406
  value.set(parseValue(v, unit));
7311
7407
  }
7312
- });
7408
+ };
7409
+ const removeSourceOnChange = source.on("change", onSourceChange);
7410
+ /**
7411
+ * Catch up on a change made before we subscribed, for instance
7412
+ * between a React render reading the source and the effect
7413
+ * attaching to it. Subtracting is falsy for equal or non-numeric
7414
+ * (NaN) values.
7415
+ */
7416
+ const latest = source.get();
7417
+ if (asNumber(latest) - asNumber(value.get()))
7418
+ onSourceChange(latest);
7313
7419
  const removeValueOnDestroy = value.on("destroy", removeSourceOnChange);
7420
+ /**
7421
+ * Stop our animation on detach, otherwise a re-attach (new
7422
+ * options, Strict Mode) starts a second animation that fights
7423
+ * this orphaned one. The catch-up above resumes from the
7424
+ * current value and velocity.
7425
+ */
7314
7426
  return () => {
7315
7427
  removeSourceOnChange();
7316
7428
  removeValueOnDestroy();
7429
+ stopAnimation();
7317
7430
  };
7318
7431
  }
7319
7432
  return stopAnimation;
@@ -9476,8 +9589,13 @@ function renderSVG(element, renderState, _styleProp, projection) {
9476
9589
  function scrapeMotionValuesFromProps(props, prevProps, visualElement) {
9477
9590
  const newValues = scrapeMotionValuesFromProps$1(props, prevProps, visualElement);
9478
9591
  for (const key in props) {
9479
- if (isMotionValue(props[key]) ||
9480
- isMotionValue(prevProps[key])) {
9592
+ /**
9593
+ * A MotionValue child is rendered as text content by
9594
+ * DOMVisualElement, so it mustn't also be written as an attribute.
9595
+ */
9596
+ if (key !== "children" &&
9597
+ (isMotionValue(props[key]) ||
9598
+ isMotionValue(prevProps[key]))) {
9481
9599
  const targetKey = transformPropOrder.indexOf(key) !== -1
9482
9600
  ? "attr" + key.charAt(0).toUpperCase() + key.substring(1)
9483
9601
  : key;
@@ -10109,7 +10227,7 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
10109
10227
  transform += `scale(${1 / treeScale.x}, ${1 / treeScale.y}) `;
10110
10228
  }
10111
10229
  if (latestTransform) {
10112
- const { transformPerspective, rotate, pathRotation, rotateX, rotateY, skewX, skewY, } = latestTransform;
10230
+ const { transformPerspective, rotate, pathRotation, rotateX, rotateY, rotateZ, skewX, skewY, } = latestTransform;
10113
10231
  if (transformPerspective)
10114
10232
  transform = `perspective(${transformPerspective}px) ${transform}`;
10115
10233
  if (rotate)
@@ -10121,6 +10239,8 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
10121
10239
  transform += `rotateX(${rotateX}deg) `;
10122
10240
  if (rotateY)
10123
10241
  transform += `rotateY(${rotateY}deg) `;
10242
+ if (rotateZ)
10243
+ transform += `rotateZ(${rotateZ}deg) `;
10124
10244
  if (skewX)
10125
10245
  transform += `skewX(${skewX}deg) `;
10126
10246
  if (skewY)
@@ -10139,6 +10259,15 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
10139
10259
  }
10140
10260
 
10141
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;
10142
10271
  const isPx = (value) => typeof value === "number" || px.test(value);
10143
10272
  function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnlyMember) {
10144
10273
  if (shouldCrossfadeOpacity) {
@@ -10173,10 +10302,14 @@ function mixValues(target, follow, lead, progress, shouldCrossfadeOpacity, isOnl
10173
10302
  }
10174
10303
  }
10175
10304
  /**
10176
- * 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.
10177
10307
  */
10178
- if (follow.rotate || lead.rotate) {
10179
- 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
+ }
10180
10313
  }
10181
10314
  }
10182
10315
  function getRadius(values, radiusName) {
@@ -10626,82 +10759,92 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10626
10759
  if (layoutId) {
10627
10760
  this.root.registerSharedNode(layoutId, this);
10628
10761
  }
10629
- // Only register the handler if it requires layout animation
10630
- if (this.options.animate !== false &&
10631
- visualElement &&
10632
- (layoutId || layout)) {
10633
- this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
10634
- if (this.isAnimationBlocked) {
10635
- this.target = undefined;
10636
- this.relativeTarget = undefined;
10637
- 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;
10638
10825
  }
10639
- // TODO: Check here if an animation exists
10640
- const layoutTransition = this.options.transition ||
10641
- visualElement.getDefaultTransition() ||
10642
- defaultLayoutTransition;
10643
- const { onLayoutAnimationStart, onLayoutAnimationComplete, } = visualElement.getProps();
10826
+ this.startAnimation(animationOptions);
10644
10827
  /**
10645
- * The target layout of the element might stay the same,
10646
- * but its position relative to its parent has changed.
10647
- */
10648
- const hasTargetChanged = !this.targetLayout ||
10649
- !boxEqualsRounded(this.targetLayout, newLayout);
10650
- /*
10651
- * Note: Disabled to fix relative animations always triggering new
10652
- * layout animations. If this causes further issues, we can try
10653
- * 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
10654
10830
  */
10655
- // || hasRelativeLayoutChanged
10831
+ this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged, animationOptions.path);
10832
+ }
10833
+ else {
10656
10834
  /**
10657
- * If the layout hasn't seemed to have changed, it might be that the
10658
- * element is visually in the same place in the document but its position
10659
- * 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.
10660
10838
  */
10661
- const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeLayoutChanged;
10662
- if (this.options.layoutRoot ||
10663
- this.resumeFrom ||
10664
- hasOnlyRelativeTargetChanged ||
10665
- (hasLayoutChanged &&
10666
- (hasTargetChanged || !this.currentAnimation))) {
10667
- if (this.resumeFrom) {
10668
- this.resumingFrom = this.resumeFrom;
10669
- this.resumingFrom.resumingFrom = undefined;
10670
- }
10671
- const animationOptions = {
10672
- ...getValueTransition(layoutTransition, "layout"),
10673
- onPlay: onLayoutAnimationStart,
10674
- onComplete: onLayoutAnimationComplete,
10675
- };
10676
- if (visualElement.shouldReduceMotion ||
10677
- this.options.layoutRoot) {
10678
- animationOptions.delay = 0;
10679
- animationOptions.type = false;
10680
- }
10681
- this.startAnimation(animationOptions);
10682
- /**
10683
- * Set animation origin after starting animation to avoid layout jump
10684
- * caused by stopping previous layout animation
10685
- */
10686
- this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged, animationOptions
10687
- .path);
10839
+ if (!hasLayoutChanged) {
10840
+ finishAnimation(this);
10688
10841
  }
10689
- else {
10690
- /**
10691
- * If the layout hasn't changed and we have an animation that hasn't started yet,
10692
- * finish it immediately. Otherwise it will be animating from a location
10693
- * that was probably never committed to screen and look like a jumpy box.
10694
- */
10695
- if (!hasLayoutChanged) {
10696
- finishAnimation(this);
10697
- }
10698
- if (this.isLead() && this.options.onExitComplete) {
10699
- this.options.onExitComplete();
10700
- }
10842
+ if (this.isLead() && this.options.onExitComplete) {
10843
+ this.options.onExitComplete();
10701
10844
  }
10702
- this.targetLayout = newLayout;
10703
- });
10704
- }
10845
+ }
10846
+ this.targetLayout = newLayout;
10847
+ });
10705
10848
  }
10706
10849
  unmount() {
10707
10850
  this.options.layoutId && this.willUpdate();
@@ -10711,6 +10854,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10711
10854
  this.parent && this.parent.children.delete(this);
10712
10855
  this.instance = undefined;
10713
10856
  this.eventHandlers.clear();
10857
+ this.hasLayoutListener = false;
10714
10858
  cancelFrame(this.updateProjection);
10715
10859
  }
10716
10860
  // only on the root
@@ -10881,7 +11025,16 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
10881
11025
  }
10882
11026
  clearAllSnapshots() {
10883
11027
  this.nodes.forEach(clearSnapshot);
10884
- 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));
10885
11038
  }
10886
11039
  scheduleUpdateProjection() {
10887
11040
  if (!this.projectionUpdateScheduled) {
@@ -11105,6 +11258,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
11105
11258
  ...options,
11106
11259
  crossfade: options.crossfade !== undefined ? options.crossfade : true,
11107
11260
  };
11261
+ this.addLayoutListener();
11108
11262
  }
11109
11263
  clearMeasurements() {
11110
11264
  this.scroll = undefined;
@@ -12034,9 +12188,6 @@ function calcProjection(node) {
12034
12188
  function resetSkewAndRotation(node) {
12035
12189
  node.resetSkewAndRotation();
12036
12190
  }
12037
- function removeLeadSnapshots(stack) {
12038
- stack.removeLeadSnapshot();
12039
- }
12040
12191
  function mixAxisDeltaLinear(output, delta, p) {
12041
12192
  output.translate = mixNumber$1(delta.translate, 0, p);
12042
12193
  output.scale = mixNumber$1(delta.scale, 1, p);