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.
@@ -290,82 +290,92 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
290
290
  if (layoutId) {
291
291
  this.root.registerSharedNode(layoutId, this);
292
292
  }
293
- // Only register the handler if it requires layout animation
294
- if (this.options.animate !== false &&
295
- visualElement &&
296
- (layoutId || layout)) {
297
- this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
298
- if (this.isAnimationBlocked) {
299
- this.target = undefined;
300
- this.relativeTarget = undefined;
301
- return;
293
+ this.addLayoutListener();
294
+ }
295
+ /**
296
+ * Only register the layout animation handler if this node requires
297
+ * layout animation. This is called on mount, and again whenever options
298
+ * change, as `layout` or `layoutId` may be enabled after mount.
299
+ */
300
+ addLayoutListener() {
301
+ const { layoutId, layout, visualElement, animate } = this.options;
302
+ if (!this.instance ||
303
+ this.hasLayoutListener ||
304
+ animate === false ||
305
+ !visualElement ||
306
+ !(layoutId || layout))
307
+ return;
308
+ this.hasLayoutListener = true;
309
+ this.addEventListener("didUpdate", ({ delta, hasLayoutChanged, hasRelativeLayoutChanged, layout: newLayout, }) => {
310
+ if (this.isAnimationBlocked) {
311
+ this.target = undefined;
312
+ this.relativeTarget = undefined;
313
+ return;
314
+ }
315
+ // TODO: Check here if an animation exists
316
+ const layoutTransition = this.options.transition ||
317
+ visualElement.getDefaultTransition() ||
318
+ defaultLayoutTransition;
319
+ const { onLayoutAnimationStart, onLayoutAnimationComplete, } = visualElement.getProps();
320
+ /**
321
+ * The target layout of the element might stay the same,
322
+ * but its position relative to its parent has changed.
323
+ */
324
+ const hasTargetChanged = !this.targetLayout ||
325
+ !boxEqualsRounded(this.targetLayout, newLayout);
326
+ /*
327
+ * Note: Disabled to fix relative animations always triggering new
328
+ * layout animations. If this causes further issues, we can try
329
+ * a different approach to detecting relative target changes.
330
+ */
331
+ // || hasRelativeLayoutChanged
332
+ /**
333
+ * If the layout hasn't seemed to have changed, it might be that the
334
+ * element is visually in the same place in the document but its position
335
+ * relative to its parent has indeed changed. So here we check for that.
336
+ */
337
+ const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeLayoutChanged;
338
+ if (this.options.layoutRoot ||
339
+ this.resumeFrom ||
340
+ hasOnlyRelativeTargetChanged ||
341
+ (hasLayoutChanged &&
342
+ (hasTargetChanged || !this.currentAnimation))) {
343
+ if (this.resumeFrom) {
344
+ this.resumingFrom = this.resumeFrom;
345
+ this.resumingFrom.resumingFrom = undefined;
346
+ }
347
+ const animationOptions = {
348
+ ...getValueTransition(layoutTransition, "layout"),
349
+ onPlay: onLayoutAnimationStart,
350
+ onComplete: onLayoutAnimationComplete,
351
+ };
352
+ if (visualElement.shouldReduceMotion ||
353
+ this.options.layoutRoot) {
354
+ animationOptions.delay = 0;
355
+ animationOptions.type = false;
302
356
  }
303
- // TODO: Check here if an animation exists
304
- const layoutTransition = this.options.transition ||
305
- visualElement.getDefaultTransition() ||
306
- defaultLayoutTransition;
307
- const { onLayoutAnimationStart, onLayoutAnimationComplete, } = visualElement.getProps();
357
+ this.startAnimation(animationOptions);
308
358
  /**
309
- * The target layout of the element might stay the same,
310
- * but its position relative to its parent has changed.
359
+ * Set animation origin after starting animation to avoid layout jump
360
+ * caused by stopping previous layout animation
311
361
  */
312
- const hasTargetChanged = !this.targetLayout ||
313
- !boxEqualsRounded(this.targetLayout, newLayout);
314
- /*
315
- * Note: Disabled to fix relative animations always triggering new
316
- * layout animations. If this causes further issues, we can try
317
- * a different approach to detecting relative target changes.
318
- */
319
- // || hasRelativeLayoutChanged
362
+ this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged, animationOptions.path);
363
+ }
364
+ else {
320
365
  /**
321
- * If the layout hasn't seemed to have changed, it might be that the
322
- * element is visually in the same place in the document but its position
323
- * relative to its parent has indeed changed. So here we check for that.
366
+ * If the layout hasn't changed and we have an animation that hasn't started yet,
367
+ * finish it immediately. Otherwise it will be animating from a location
368
+ * that was probably never committed to screen and look like a jumpy box.
324
369
  */
325
- const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeLayoutChanged;
326
- if (this.options.layoutRoot ||
327
- this.resumeFrom ||
328
- hasOnlyRelativeTargetChanged ||
329
- (hasLayoutChanged &&
330
- (hasTargetChanged || !this.currentAnimation))) {
331
- if (this.resumeFrom) {
332
- this.resumingFrom = this.resumeFrom;
333
- this.resumingFrom.resumingFrom = undefined;
334
- }
335
- const animationOptions = {
336
- ...getValueTransition(layoutTransition, "layout"),
337
- onPlay: onLayoutAnimationStart,
338
- onComplete: onLayoutAnimationComplete,
339
- };
340
- if (visualElement.shouldReduceMotion ||
341
- this.options.layoutRoot) {
342
- animationOptions.delay = 0;
343
- animationOptions.type = false;
344
- }
345
- this.startAnimation(animationOptions);
346
- /**
347
- * Set animation origin after starting animation to avoid layout jump
348
- * caused by stopping previous layout animation
349
- */
350
- this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged, animationOptions
351
- .path);
370
+ if (!hasLayoutChanged) {
371
+ finishAnimation(this);
352
372
  }
353
- else {
354
- /**
355
- * If the layout hasn't changed and we have an animation that hasn't started yet,
356
- * finish it immediately. Otherwise it will be animating from a location
357
- * that was probably never committed to screen and look like a jumpy box.
358
- */
359
- if (!hasLayoutChanged) {
360
- finishAnimation(this);
361
- }
362
- if (this.isLead() && this.options.onExitComplete) {
363
- this.options.onExitComplete();
364
- }
373
+ if (this.isLead() && this.options.onExitComplete) {
374
+ this.options.onExitComplete();
365
375
  }
366
- this.targetLayout = newLayout;
367
- });
368
- }
376
+ }
377
+ this.targetLayout = newLayout;
378
+ });
369
379
  }
370
380
  unmount() {
371
381
  this.options.layoutId && this.willUpdate();
@@ -375,6 +385,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
375
385
  this.parent && this.parent.children.delete(this);
376
386
  this.instance = undefined;
377
387
  this.eventHandlers.clear();
388
+ this.hasLayoutListener = false;
378
389
  cancelFrame(this.updateProjection);
379
390
  }
380
391
  // only on the root
@@ -545,7 +556,16 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
545
556
  }
546
557
  clearAllSnapshots() {
547
558
  this.nodes.forEach(clearSnapshot);
548
- this.sharedNodes.forEach(removeLeadSnapshots);
559
+ /**
560
+ * A stack's last member can unmount while a replacement with the
561
+ * same layoutId mounts in the same commit, so the stack (and its
562
+ * lead) must outlive the unmount for the replacement to resume
563
+ * from it. By now that handoff has happened, so stacks that are
564
+ * still empty only retain unmounted nodes and can be released.
565
+ */
566
+ this.sharedNodes.forEach((stack, layoutId) => stack.members.length
567
+ ? stack.removeLeadSnapshot()
568
+ : this.sharedNodes.delete(layoutId));
549
569
  }
550
570
  scheduleUpdateProjection() {
551
571
  if (!this.projectionUpdateScheduled) {
@@ -769,6 +789,7 @@ function createProjectionNode({ attachResizeListener, defaultParent, measureScro
769
789
  ...options,
770
790
  crossfade: options.crossfade !== undefined ? options.crossfade : true,
771
791
  };
792
+ this.addLayoutListener();
772
793
  }
773
794
  clearMeasurements() {
774
795
  this.scroll = undefined;
@@ -1698,9 +1719,6 @@ function calcProjection(node) {
1698
1719
  function resetSkewAndRotation(node) {
1699
1720
  node.resetSkewAndRotation();
1700
1721
  }
1701
- function removeLeadSnapshots(stack) {
1702
- stack.removeLeadSnapshot();
1703
- }
1704
1722
  function mixAxisDeltaLinear(output, delta, p) {
1705
1723
  output.translate = mixNumber(delta.translate, 0, p);
1706
1724
  output.scale = mixNumber(delta.scale, 1, p);