motion-dom 12.27.0-alpha.5 → 12.27.0-alpha.6

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
@@ -7599,6 +7599,13 @@ function createProjectionNode$1({ attachResizeListener, defaultParent, measureSc
7599
7599
  this.relativeTarget = undefined;
7600
7600
  return;
7601
7601
  }
7602
+ /**
7603
+ * Check if this is a follow element in a crossfade (exiting element
7604
+ * whose lead has resumeFrom pointing to it). These elements should
7605
+ * NOT create their own animation - they get the lead's animation via
7606
+ * resumingFrom.currentAnimation in startAnimation().
7607
+ */
7608
+ const isFollowInCrossfade = this.isPresent === false && !this.isLead();
7602
7609
  // TODO: Check here if an animation exists
7603
7610
  const layoutTransition = this.options.transition ||
7604
7611
  visualElement.getDefaultTransition() ||
@@ -7622,43 +7629,51 @@ function createProjectionNode$1({ attachResizeListener, defaultParent, measureSc
7622
7629
  * relative to its parent has indeed changed. So here we check for that.
7623
7630
  */
7624
7631
  const hasOnlyRelativeTargetChanged = !hasLayoutChanged && hasRelativeLayoutChanged;
7625
- if (this.options.layoutRoot ||
7626
- this.resumeFrom ||
7627
- hasOnlyRelativeTargetChanged ||
7628
- (hasLayoutChanged &&
7629
- (hasTargetChanged || !this.currentAnimation))) {
7630
- if (this.resumeFrom) {
7631
- this.resumingFrom = this.resumeFrom;
7632
- this.resumingFrom.resumingFrom = undefined;
7633
- }
7634
- const animationOptions = {
7635
- ...getValueTransition(layoutTransition, "layout"),
7636
- onPlay: onLayoutAnimationStart,
7637
- onComplete: onLayoutAnimationComplete,
7638
- };
7639
- if (visualElement.shouldReduceMotion ||
7640
- this.options.layoutRoot) {
7641
- animationOptions.delay = 0;
7642
- animationOptions.type = false;
7643
- }
7644
- this.startAnimation(animationOptions);
7645
- /**
7646
- * Set animation origin after starting animation to avoid layout jump
7647
- * caused by stopping previous layout animation
7648
- */
7649
- this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged);
7650
- }
7651
- else {
7652
- /**
7653
- * If the layout hasn't changed and we have an animation that hasn't started yet,
7654
- * finish it immediately. Otherwise it will be animating from a location
7655
- * that was probably never committed to screen and look like a jumpy box.
7656
- */
7657
- if (!hasLayoutChanged) {
7658
- finishAnimation(this);
7632
+ /**
7633
+ * Skip animation handling for follow elements in a crossfade.
7634
+ * These elements get the lead's animation via resumingFrom.currentAnimation
7635
+ * in startAnimation(), so they shouldn't create their own animation.
7636
+ * They also shouldn't call finishAnimation(), which would reset their state.
7637
+ */
7638
+ if (!isFollowInCrossfade) {
7639
+ if (this.options.layoutRoot ||
7640
+ this.resumeFrom ||
7641
+ hasOnlyRelativeTargetChanged ||
7642
+ (hasLayoutChanged &&
7643
+ (hasTargetChanged || !this.currentAnimation))) {
7644
+ if (this.resumeFrom) {
7645
+ this.resumingFrom = this.resumeFrom;
7646
+ this.resumingFrom.resumingFrom = undefined;
7647
+ }
7648
+ const animationOptions = {
7649
+ ...getValueTransition(layoutTransition, "layout"),
7650
+ onPlay: onLayoutAnimationStart,
7651
+ onComplete: onLayoutAnimationComplete,
7652
+ };
7653
+ if (visualElement.shouldReduceMotion ||
7654
+ this.options.layoutRoot) {
7655
+ animationOptions.delay = 0;
7656
+ animationOptions.type = false;
7657
+ }
7658
+ this.startAnimation(animationOptions);
7659
+ /**
7660
+ * Set animation origin after starting animation to avoid layout jump
7661
+ * caused by stopping previous layout animation
7662
+ */
7663
+ this.setAnimationOrigin(delta, hasOnlyRelativeTargetChanged);
7659
7664
  }
7660
- if (this.isLead() && this.options.onExitComplete) {
7661
- this.options.onExitComplete();
7665
+ else {
7666
+ /**
7667
+ * If the layout hasn't changed and we have an animation that hasn't started yet,
7668
+ * finish it immediately. Otherwise it will be animating from a location
7669
+ * that was probably never committed to screen and look like a jumpy box.
7670
+ */
7671
+ if (!hasLayoutChanged) {
7672
+ finishAnimation(this);
7673
+ }
7674
+ if (this.isLead() && this.options.onExitComplete) {
7675
+ this.options.onExitComplete();
7676
+ }
7662
7677
  }
7663
7678
  }
7664
7679
  this.targetLayout = newLayout;
@@ -9033,6 +9048,7 @@ function detectMutations(beforeRecords, scope) {
9033
9048
  const persisting = [];
9034
9049
  const sharedEntering = new Map();
9035
9050
  const sharedExiting = new Map();
9051
+ const sharedPersisting = new Map();
9036
9052
  // Find exiting elements (were in before, not in after)
9037
9053
  for (const element of beforeElements) {
9038
9054
  if (!afterElements.has(element)) {
@@ -9060,38 +9076,23 @@ function detectMutations(beforeRecords, scope) {
9060
9076
  persisting.push(element);
9061
9077
  }
9062
9078
  }
9079
+ // Find persisting elements that share a layoutId with exiting elements
9080
+ // This handles the A -> AB -> A pattern where A persists and B exits
9081
+ for (const element of persisting) {
9082
+ const layoutId = getLayoutId(element);
9083
+ if (layoutId && sharedExiting.has(layoutId)) {
9084
+ sharedPersisting.set(layoutId, element);
9085
+ }
9086
+ }
9063
9087
  return {
9064
9088
  entering,
9065
9089
  exiting,
9066
9090
  persisting,
9067
9091
  sharedEntering,
9068
9092
  sharedExiting,
9093
+ sharedPersisting,
9069
9094
  };
9070
9095
  }
9071
- /**
9072
- * Check if an element is a "root" entering element (no entering ancestors)
9073
- */
9074
- function isRootEnteringElement(element, allEntering) {
9075
- let parent = element.parentElement;
9076
- while (parent) {
9077
- if (allEntering.has(parent))
9078
- return false;
9079
- parent = parent.parentElement;
9080
- }
9081
- return true;
9082
- }
9083
- /**
9084
- * Check if an element is a "root" exiting element (no exiting ancestors)
9085
- */
9086
- function isRootExitingElement(element, allExiting) {
9087
- let parent = element.parentElement;
9088
- while (parent) {
9089
- if (allExiting.has(parent))
9090
- return false;
9091
- parent = parent.parentElement;
9092
- }
9093
- return true;
9094
- }
9095
9096
 
9096
9097
  let scaleCorrectorAdded = false;
9097
9098
  /**
@@ -9165,6 +9166,11 @@ function createProjectionNode(element, parent, options, transition) {
9165
9166
  transition: nodeTransition,
9166
9167
  ...options,
9167
9168
  });
9169
+ // Re-mount the node if it was previously unmounted
9170
+ // This re-adds it to root.nodes so didUpdate() will process it
9171
+ if (!existingNode.instance) {
9172
+ existingNode.mount(element);
9173
+ }
9168
9174
  return { node: existingNode, visualElement };
9169
9175
  }
9170
9176
  // No existing node - create a new one
@@ -9256,10 +9262,18 @@ function parseLayoutMode(value) {
9256
9262
  return "both";
9257
9263
  }
9258
9264
  /**
9259
- * Clean up projection nodes
9265
+ * Clean up projection nodes for specific elements.
9266
+ * If elementsToCleanup is provided, only those elements are cleaned up.
9267
+ * If not provided, all nodes are cleaned up.
9268
+ *
9269
+ * This allows persisting elements to keep their nodes between animations,
9270
+ * matching React's behavior where nodes persist for elements that remain in the DOM.
9260
9271
  */
9261
- function cleanupProjectionTree(context) {
9262
- for (const [element, node] of context.nodes.entries()) {
9272
+ function cleanupProjectionTree(context, elementsToCleanup) {
9273
+ const elementsToProcess = elementsToCleanup
9274
+ ? [...context.nodes.entries()].filter(([el]) => elementsToCleanup.has(el))
9275
+ : [...context.nodes.entries()];
9276
+ for (const [element, node] of elementsToProcess) {
9263
9277
  context.group.remove(node);
9264
9278
  node.unmount();
9265
9279
  // Only clear from activeProjectionNodes if this is still the active node.
@@ -9267,9 +9281,9 @@ function cleanupProjectionTree(context) {
9267
9281
  if (activeProjectionNodes.get(element) === node) {
9268
9282
  activeProjectionNodes.delete(element);
9269
9283
  }
9284
+ context.nodes.delete(element);
9285
+ context.visualElements.delete(element);
9270
9286
  }
9271
- context.nodes.clear();
9272
- context.visualElements.clear();
9273
9287
  }
9274
9288
 
9275
9289
  class LayoutAnimationBuilder {
@@ -9286,16 +9300,6 @@ class LayoutAnimationBuilder {
9286
9300
  // Queue execution on microtask to allow builder methods to be called
9287
9301
  queueMicrotask(() => this.execute());
9288
9302
  }
9289
- enter(keyframes, options) {
9290
- this.enterKeyframes = keyframes;
9291
- this.enterOptions = options;
9292
- return this;
9293
- }
9294
- exit(keyframes, options) {
9295
- this.exitKeyframes = keyframes;
9296
- this.exitOptions = options;
9297
- return this;
9298
- }
9299
9303
  shared(layoutIdOrOptions, options) {
9300
9304
  if (typeof layoutIdOrOptions === "string") {
9301
9305
  this.sharedTransitions.set(layoutIdOrOptions, options);
@@ -9339,13 +9343,16 @@ class LayoutAnimationBuilder {
9339
9343
  this.updateDom();
9340
9344
  // Phase 3: Post-mutation (Detect & Prepare)
9341
9345
  const mutationResult = detectMutations(beforeRecords, this.scope);
9342
- // Reattach exiting elements that are NOT part of shared transitions
9343
- // Shared elements are handled by the projection system via resumeFrom
9344
- const nonSharedExiting = mutationResult.exiting.filter(({ element }) => {
9346
+ // Determine which exiting elements should be reattached:
9347
+ // Non-shared exiting elements (no layoutId or no matching entering/persisting element)
9348
+ const exitingToReattach = mutationResult.exiting.filter(({ element }) => {
9345
9349
  const layoutId = element.getAttribute("data-layout-id");
9346
- return !layoutId || !mutationResult.sharedEntering.has(layoutId);
9350
+ const isSharedWithEntering = layoutId && mutationResult.sharedEntering.has(layoutId);
9351
+ const isSharedWithPersisting = layoutId && mutationResult.sharedPersisting.has(layoutId);
9352
+ // Reattach if not a shared element
9353
+ return !isSharedWithEntering && !isSharedWithPersisting;
9347
9354
  });
9348
- this.reattachExitingElements(nonSharedExiting, context);
9355
+ this.reattachExitingElements(exitingToReattach, context);
9349
9356
  // Build projection nodes for entering elements
9350
9357
  if (mutationResult.entering.length > 0) {
9351
9358
  context = buildProjectionTree(mutationResult.entering, context, this.getBuildOptions());
@@ -9354,19 +9361,23 @@ class LayoutAnimationBuilder {
9354
9361
  if (!context && mutationResult.persisting.length > 0) {
9355
9362
  context = buildProjectionTree(mutationResult.persisting, undefined, this.getBuildOptions());
9356
9363
  }
9357
- // Build set of shared exiting elements to exclude from animation collection
9358
- // Their nodes are still in the tree for resumeFrom relationship, but we don't animate them
9364
+ // Build set of shared exiting elements (they don't animate separately)
9359
9365
  const sharedExitingElements = new Set();
9360
- for (const [layoutId] of mutationResult.sharedEntering) {
9366
+ for (const [layoutId, enteringElement] of mutationResult.sharedEntering) {
9361
9367
  const exitingElement = mutationResult.sharedExiting.get(layoutId);
9362
9368
  if (exitingElement) {
9363
9369
  sharedExitingElements.add(exitingElement);
9364
- // Remove the exiting node from the shared stack so that crossfade
9365
- // doesn't trigger. When an element is removed from the DOM, it can't
9366
- // participate in crossfade (no element to fade out). The entering
9367
- // element still has resumeFrom set for position morphing.
9368
9370
  const exitingNode = context?.nodes.get(exitingElement);
9369
- if (exitingNode) {
9371
+ const enteringNode = context?.nodes.get(enteringElement);
9372
+ if (exitingNode && enteringNode) {
9373
+ // Remove exiting node from stack, no crossfade
9374
+ const stack = exitingNode.getStack();
9375
+ if (stack) {
9376
+ stack.remove(exitingNode);
9377
+ }
9378
+ }
9379
+ else if (exitingNode && !enteringNode) {
9380
+ // Fallback: If entering node doesn't exist yet, just handle exiting
9370
9381
  const stack = exitingNode.getStack();
9371
9382
  if (stack) {
9372
9383
  stack.remove(exitingNode);
@@ -9374,13 +9385,31 @@ class LayoutAnimationBuilder {
9374
9385
  }
9375
9386
  }
9376
9387
  }
9388
+ // Handle A -> AB -> A pattern: persisting element should become lead again
9389
+ // when exiting element with same layoutId is removed
9390
+ for (const [layoutId, persistingElement] of mutationResult.sharedPersisting) {
9391
+ const exitingElement = mutationResult.sharedExiting.get(layoutId);
9392
+ if (!exitingElement)
9393
+ continue;
9394
+ sharedExitingElements.add(exitingElement);
9395
+ const exitingNode = context?.nodes.get(exitingElement);
9396
+ const persistingNode = context?.nodes.get(persistingElement);
9397
+ if (exitingNode && persistingNode) {
9398
+ // Remove exiting node from stack, no crossfade
9399
+ const stack = exitingNode.getStack();
9400
+ if (stack) {
9401
+ stack.remove(exitingNode);
9402
+ }
9403
+ }
9404
+ }
9377
9405
  // Phase 4: Animate
9378
9406
  if (context) {
9379
9407
  // Trigger layout animations via didUpdate
9380
9408
  context.root.didUpdate();
9381
9409
  // Wait for animations to be created (they're scheduled via frame.update)
9382
9410
  await new Promise((resolve) => frame.postRender(() => resolve()));
9383
- // Collect layout animations from projection nodes (excluding shared exiting elements)
9411
+ // Collect layout animations from projection nodes
9412
+ // Skip shared exiting elements (they don't animate)
9384
9413
  for (const [element, node] of context.nodes.entries()) {
9385
9414
  if (sharedExitingElements.has(element))
9386
9415
  continue;
@@ -9388,31 +9417,25 @@ class LayoutAnimationBuilder {
9388
9417
  animations.push(node.currentAnimation);
9389
9418
  }
9390
9419
  }
9391
- // Apply enter keyframes to root entering elements
9392
- if (this.enterKeyframes) {
9393
- const enterAnimations = this.animateEntering(mutationResult, context);
9394
- animations.push(...enterAnimations);
9395
- }
9396
- // Apply exit keyframes to root exiting elements
9397
- if (this.exitKeyframes) {
9398
- const exitAnimations = this.animateExiting(mutationResult, context);
9399
- animations.push(...exitAnimations);
9400
- }
9401
9420
  }
9402
9421
  // Create and return group animation
9403
9422
  const groupAnimation = new GroupAnimation(animations);
9404
9423
  // Phase 5: Setup cleanup on complete
9424
+ // Only cleanup exiting elements - persisting elements keep their nodes
9425
+ // This matches React's behavior where nodes persist for elements in the DOM
9426
+ const exitingElements = new Set(mutationResult.exiting.map(({ element }) => element));
9405
9427
  groupAnimation.finished.then(() => {
9406
- // Only clean up non-shared exiting elements (those we reattached)
9407
- this.cleanupExitingElements(nonSharedExiting);
9428
+ // Clean up all reattached exiting elements (remove from DOM)
9429
+ this.cleanupExitingElements(exitingToReattach);
9408
9430
  if (context) {
9409
- cleanupProjectionTree(context);
9431
+ // Only cleanup projection nodes for exiting elements
9432
+ cleanupProjectionTree(context, exitingElements);
9410
9433
  }
9411
9434
  });
9412
9435
  this.notifyReady(groupAnimation);
9413
9436
  }
9414
9437
  catch (error) {
9415
- // Cleanup on error
9438
+ // Cleanup on error - cleanup all nodes since animation failed
9416
9439
  if (context) {
9417
9440
  cleanupProjectionTree(context);
9418
9441
  }
@@ -9421,8 +9444,13 @@ class LayoutAnimationBuilder {
9421
9444
  }
9422
9445
  getBuildOptions() {
9423
9446
  return {
9424
- defaultTransition: this.defaultOptions || { duration: 0.3, ease: "easeOut" },
9425
- sharedTransitions: this.sharedTransitions.size > 0 ? this.sharedTransitions : undefined,
9447
+ defaultTransition: this.defaultOptions || {
9448
+ duration: 0.3,
9449
+ ease: "easeOut",
9450
+ },
9451
+ sharedTransitions: this.sharedTransitions.size > 0
9452
+ ? this.sharedTransitions
9453
+ : undefined,
9426
9454
  };
9427
9455
  }
9428
9456
  reattachExitingElements(exiting, context) {
@@ -9462,50 +9490,6 @@ class LayoutAnimationBuilder {
9462
9490
  }
9463
9491
  }
9464
9492
  }
9465
- animateEntering(mutationResult, context) {
9466
- const enteringSet = new Set(mutationResult.entering);
9467
- // Find root entering elements
9468
- const rootEntering = mutationResult.entering.filter((el) => isRootEnteringElement(el, enteringSet));
9469
- const animations = [];
9470
- for (const element of rootEntering) {
9471
- const visualElement = context.visualElements.get(element);
9472
- if (!visualElement)
9473
- continue;
9474
- // If entering with opacity: 1, start from opacity: 0
9475
- const keyframes = { ...this.enterKeyframes };
9476
- if (keyframes.opacity !== undefined) {
9477
- const targetOpacity = Array.isArray(keyframes.opacity)
9478
- ? keyframes.opacity[keyframes.opacity.length - 1]
9479
- : keyframes.opacity;
9480
- if (targetOpacity === 1) {
9481
- element.style.opacity = "0";
9482
- }
9483
- }
9484
- const options = this.enterOptions || this.defaultOptions || {};
9485
- const enterAnims = animateTarget(visualElement, keyframes, {
9486
- transitionOverride: options,
9487
- });
9488
- animations.push(...enterAnims);
9489
- }
9490
- return animations;
9491
- }
9492
- animateExiting(mutationResult, context) {
9493
- const exitingSet = new Set(mutationResult.exiting.map((r) => r.element));
9494
- // Find root exiting elements
9495
- const rootExiting = mutationResult.exiting.filter((r) => isRootExitingElement(r.element, exitingSet));
9496
- const animations = [];
9497
- for (const { element } of rootExiting) {
9498
- const visualElement = context.visualElements.get(element);
9499
- if (!visualElement)
9500
- continue;
9501
- const options = this.exitOptions || this.defaultOptions || {};
9502
- const exitAnims = animateTarget(visualElement, this.exitKeyframes, {
9503
- transitionOverride: options,
9504
- });
9505
- animations.push(...exitAnims);
9506
- }
9507
- return animations;
9508
- }
9509
9493
  }
9510
9494
  /**
9511
9495
  * Parse arguments for animateLayout overloads
@@ -9547,12 +9531,10 @@ function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
9547
9531
  * // With options
9548
9532
  * await animateLayout(() => update(), { duration: 0.5 })
9549
9533
  *
9550
- * // Builder pattern for enter/exit animations
9534
+ * // Configure shared element transitions
9551
9535
  * animateLayout(".cards", () => {
9552
9536
  * container.innerHTML = newCards
9553
9537
  * }, { duration: 0.3 })
9554
- * .enter({ opacity: 1, scale: 1 }, { duration: 0.2 })
9555
- * .exit({ opacity: 0, scale: 0.8 })
9556
9538
  * .shared("hero", { type: "spring" })
9557
9539
  * ```
9558
9540
  *
@@ -1,7 +1,6 @@
1
1
  import { noop } from 'motion-utils';
2
2
  import { GroupAnimation } from '../animation/GroupAnimation.mjs';
3
- import { animateTarget } from '../animation/interfaces/visual-element-target.mjs';
4
- import { getLayoutElements, trackLayoutElements, detectMutations, isRootEnteringElement, isRootExitingElement } from './detect-mutations.mjs';
3
+ import { getLayoutElements, trackLayoutElements, detectMutations } from './detect-mutations.mjs';
5
4
  import { buildProjectionTree, cleanupProjectionTree } from './projection-tree.mjs';
6
5
  import { resolveElements } from '../utils/resolve-elements.mjs';
7
6
  import { frame } from '../frameloop/frame.mjs';
@@ -20,16 +19,6 @@ class LayoutAnimationBuilder {
20
19
  // Queue execution on microtask to allow builder methods to be called
21
20
  queueMicrotask(() => this.execute());
22
21
  }
23
- enter(keyframes, options) {
24
- this.enterKeyframes = keyframes;
25
- this.enterOptions = options;
26
- return this;
27
- }
28
- exit(keyframes, options) {
29
- this.exitKeyframes = keyframes;
30
- this.exitOptions = options;
31
- return this;
32
- }
33
22
  shared(layoutIdOrOptions, options) {
34
23
  if (typeof layoutIdOrOptions === "string") {
35
24
  this.sharedTransitions.set(layoutIdOrOptions, options);
@@ -73,13 +62,16 @@ class LayoutAnimationBuilder {
73
62
  this.updateDom();
74
63
  // Phase 3: Post-mutation (Detect & Prepare)
75
64
  const mutationResult = detectMutations(beforeRecords, this.scope);
76
- // Reattach exiting elements that are NOT part of shared transitions
77
- // Shared elements are handled by the projection system via resumeFrom
78
- const nonSharedExiting = mutationResult.exiting.filter(({ element }) => {
65
+ // Determine which exiting elements should be reattached:
66
+ // Non-shared exiting elements (no layoutId or no matching entering/persisting element)
67
+ const exitingToReattach = mutationResult.exiting.filter(({ element }) => {
79
68
  const layoutId = element.getAttribute("data-layout-id");
80
- return !layoutId || !mutationResult.sharedEntering.has(layoutId);
69
+ const isSharedWithEntering = layoutId && mutationResult.sharedEntering.has(layoutId);
70
+ const isSharedWithPersisting = layoutId && mutationResult.sharedPersisting.has(layoutId);
71
+ // Reattach if not a shared element
72
+ return !isSharedWithEntering && !isSharedWithPersisting;
81
73
  });
82
- this.reattachExitingElements(nonSharedExiting, context);
74
+ this.reattachExitingElements(exitingToReattach, context);
83
75
  // Build projection nodes for entering elements
84
76
  if (mutationResult.entering.length > 0) {
85
77
  context = buildProjectionTree(mutationResult.entering, context, this.getBuildOptions());
@@ -88,19 +80,23 @@ class LayoutAnimationBuilder {
88
80
  if (!context && mutationResult.persisting.length > 0) {
89
81
  context = buildProjectionTree(mutationResult.persisting, undefined, this.getBuildOptions());
90
82
  }
91
- // Build set of shared exiting elements to exclude from animation collection
92
- // Their nodes are still in the tree for resumeFrom relationship, but we don't animate them
83
+ // Build set of shared exiting elements (they don't animate separately)
93
84
  const sharedExitingElements = new Set();
94
- for (const [layoutId] of mutationResult.sharedEntering) {
85
+ for (const [layoutId, enteringElement] of mutationResult.sharedEntering) {
95
86
  const exitingElement = mutationResult.sharedExiting.get(layoutId);
96
87
  if (exitingElement) {
97
88
  sharedExitingElements.add(exitingElement);
98
- // Remove the exiting node from the shared stack so that crossfade
99
- // doesn't trigger. When an element is removed from the DOM, it can't
100
- // participate in crossfade (no element to fade out). The entering
101
- // element still has resumeFrom set for position morphing.
102
89
  const exitingNode = context?.nodes.get(exitingElement);
103
- if (exitingNode) {
90
+ const enteringNode = context?.nodes.get(enteringElement);
91
+ if (exitingNode && enteringNode) {
92
+ // Remove exiting node from stack, no crossfade
93
+ const stack = exitingNode.getStack();
94
+ if (stack) {
95
+ stack.remove(exitingNode);
96
+ }
97
+ }
98
+ else if (exitingNode && !enteringNode) {
99
+ // Fallback: If entering node doesn't exist yet, just handle exiting
104
100
  const stack = exitingNode.getStack();
105
101
  if (stack) {
106
102
  stack.remove(exitingNode);
@@ -108,13 +104,31 @@ class LayoutAnimationBuilder {
108
104
  }
109
105
  }
110
106
  }
107
+ // Handle A -> AB -> A pattern: persisting element should become lead again
108
+ // when exiting element with same layoutId is removed
109
+ for (const [layoutId, persistingElement] of mutationResult.sharedPersisting) {
110
+ const exitingElement = mutationResult.sharedExiting.get(layoutId);
111
+ if (!exitingElement)
112
+ continue;
113
+ sharedExitingElements.add(exitingElement);
114
+ const exitingNode = context?.nodes.get(exitingElement);
115
+ const persistingNode = context?.nodes.get(persistingElement);
116
+ if (exitingNode && persistingNode) {
117
+ // Remove exiting node from stack, no crossfade
118
+ const stack = exitingNode.getStack();
119
+ if (stack) {
120
+ stack.remove(exitingNode);
121
+ }
122
+ }
123
+ }
111
124
  // Phase 4: Animate
112
125
  if (context) {
113
126
  // Trigger layout animations via didUpdate
114
127
  context.root.didUpdate();
115
128
  // Wait for animations to be created (they're scheduled via frame.update)
116
129
  await new Promise((resolve) => frame.postRender(() => resolve()));
117
- // Collect layout animations from projection nodes (excluding shared exiting elements)
130
+ // Collect layout animations from projection nodes
131
+ // Skip shared exiting elements (they don't animate)
118
132
  for (const [element, node] of context.nodes.entries()) {
119
133
  if (sharedExitingElements.has(element))
120
134
  continue;
@@ -122,31 +136,25 @@ class LayoutAnimationBuilder {
122
136
  animations.push(node.currentAnimation);
123
137
  }
124
138
  }
125
- // Apply enter keyframes to root entering elements
126
- if (this.enterKeyframes) {
127
- const enterAnimations = this.animateEntering(mutationResult, context);
128
- animations.push(...enterAnimations);
129
- }
130
- // Apply exit keyframes to root exiting elements
131
- if (this.exitKeyframes) {
132
- const exitAnimations = this.animateExiting(mutationResult, context);
133
- animations.push(...exitAnimations);
134
- }
135
139
  }
136
140
  // Create and return group animation
137
141
  const groupAnimation = new GroupAnimation(animations);
138
142
  // Phase 5: Setup cleanup on complete
143
+ // Only cleanup exiting elements - persisting elements keep their nodes
144
+ // This matches React's behavior where nodes persist for elements in the DOM
145
+ const exitingElements = new Set(mutationResult.exiting.map(({ element }) => element));
139
146
  groupAnimation.finished.then(() => {
140
- // Only clean up non-shared exiting elements (those we reattached)
141
- this.cleanupExitingElements(nonSharedExiting);
147
+ // Clean up all reattached exiting elements (remove from DOM)
148
+ this.cleanupExitingElements(exitingToReattach);
142
149
  if (context) {
143
- cleanupProjectionTree(context);
150
+ // Only cleanup projection nodes for exiting elements
151
+ cleanupProjectionTree(context, exitingElements);
144
152
  }
145
153
  });
146
154
  this.notifyReady(groupAnimation);
147
155
  }
148
156
  catch (error) {
149
- // Cleanup on error
157
+ // Cleanup on error - cleanup all nodes since animation failed
150
158
  if (context) {
151
159
  cleanupProjectionTree(context);
152
160
  }
@@ -155,8 +163,13 @@ class LayoutAnimationBuilder {
155
163
  }
156
164
  getBuildOptions() {
157
165
  return {
158
- defaultTransition: this.defaultOptions || { duration: 0.3, ease: "easeOut" },
159
- sharedTransitions: this.sharedTransitions.size > 0 ? this.sharedTransitions : undefined,
166
+ defaultTransition: this.defaultOptions || {
167
+ duration: 0.3,
168
+ ease: "easeOut",
169
+ },
170
+ sharedTransitions: this.sharedTransitions.size > 0
171
+ ? this.sharedTransitions
172
+ : undefined,
160
173
  };
161
174
  }
162
175
  reattachExitingElements(exiting, context) {
@@ -196,50 +209,6 @@ class LayoutAnimationBuilder {
196
209
  }
197
210
  }
198
211
  }
199
- animateEntering(mutationResult, context) {
200
- const enteringSet = new Set(mutationResult.entering);
201
- // Find root entering elements
202
- const rootEntering = mutationResult.entering.filter((el) => isRootEnteringElement(el, enteringSet));
203
- const animations = [];
204
- for (const element of rootEntering) {
205
- const visualElement = context.visualElements.get(element);
206
- if (!visualElement)
207
- continue;
208
- // If entering with opacity: 1, start from opacity: 0
209
- const keyframes = { ...this.enterKeyframes };
210
- if (keyframes.opacity !== undefined) {
211
- const targetOpacity = Array.isArray(keyframes.opacity)
212
- ? keyframes.opacity[keyframes.opacity.length - 1]
213
- : keyframes.opacity;
214
- if (targetOpacity === 1) {
215
- element.style.opacity = "0";
216
- }
217
- }
218
- const options = this.enterOptions || this.defaultOptions || {};
219
- const enterAnims = animateTarget(visualElement, keyframes, {
220
- transitionOverride: options,
221
- });
222
- animations.push(...enterAnims);
223
- }
224
- return animations;
225
- }
226
- animateExiting(mutationResult, context) {
227
- const exitingSet = new Set(mutationResult.exiting.map((r) => r.element));
228
- // Find root exiting elements
229
- const rootExiting = mutationResult.exiting.filter((r) => isRootExitingElement(r.element, exitingSet));
230
- const animations = [];
231
- for (const { element } of rootExiting) {
232
- const visualElement = context.visualElements.get(element);
233
- if (!visualElement)
234
- continue;
235
- const options = this.exitOptions || this.defaultOptions || {};
236
- const exitAnims = animateTarget(visualElement, this.exitKeyframes, {
237
- transitionOverride: options,
238
- });
239
- animations.push(...exitAnims);
240
- }
241
- return animations;
242
- }
243
212
  }
244
213
  /**
245
214
  * Parse arguments for animateLayout overloads
@@ -18,12 +18,10 @@ import { parseAnimateLayoutArgs, LayoutAnimationBuilder } from './LayoutAnimatio
18
18
  * // With options
19
19
  * await animateLayout(() => update(), { duration: 0.5 })
20
20
  *
21
- * // Builder pattern for enter/exit animations
21
+ * // Configure shared element transitions
22
22
  * animateLayout(".cards", () => {
23
23
  * container.innerHTML = newCards
24
24
  * }, { duration: 0.3 })
25
- * .enter({ opacity: 1, scale: 1 }, { duration: 0.2 })
26
- * .exit({ opacity: 0, scale: 0.8 })
27
25
  * .shared("hero", { type: "spring" })
28
26
  * ```
29
27
  *