motion-dom 12.27.0-alpha.5 → 12.27.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.
package/dist/cjs/index.js CHANGED
@@ -2660,6 +2660,20 @@ function getAnimationMap(element) {
2660
2660
  return map;
2661
2661
  }
2662
2662
 
2663
+ function calcChildStagger(children, child, delayChildren, staggerChildren = 0, staggerDirection = 1) {
2664
+ const index = Array.from(children)
2665
+ .sort((a, b) => a.sortNodePosition(b))
2666
+ .indexOf(child);
2667
+ const numChildren = children.size;
2668
+ const maxStaggerDuration = (numChildren - 1) * staggerChildren;
2669
+ const delayIsFunction = typeof delayChildren === "function";
2670
+ return delayIsFunction
2671
+ ? delayChildren(index, numChildren)
2672
+ : staggerDirection === 1
2673
+ ? index * staggerChildren
2674
+ : maxStaggerDuration - index * staggerChildren;
2675
+ }
2676
+
2663
2677
  /**
2664
2678
  * Parse Framer's special CSS variable format into a CSS token and a fallback.
2665
2679
  *
@@ -2697,12 +2711,6 @@ function getVariableValue(current, element, depth = 1) {
2697
2711
  : fallback;
2698
2712
  }
2699
2713
 
2700
- function getValueTransition(transition, key) {
2701
- return (transition?.[key] ??
2702
- transition?.["default"] ??
2703
- transition);
2704
- }
2705
-
2706
2714
  const underDampedSpring = {
2707
2715
  type: "spring",
2708
2716
  stiffness: 500,
@@ -2740,15 +2748,6 @@ const getDefaultTransition = (valueKey, { keyframes }) => {
2740
2748
  return ease;
2741
2749
  };
2742
2750
 
2743
- /**
2744
- * Decide whether a transition is defined on a given Transition.
2745
- * This filters out orchestration options and returns true
2746
- * if any options are left.
2747
- */
2748
- function isTransitionDefined({ when, delay: _delay, delayChildren, staggerChildren, staggerDirection, repeat, repeatType, repeatDelay, from, elapsed, ...transition }) {
2749
- return !!Object.keys(transition).length;
2750
- }
2751
-
2752
2751
  const isNotNull = (value) => value !== null;
2753
2752
  function getFinalKeyframe(keyframes, { repeat, repeatType = "loop" }, finalKeyframe) {
2754
2753
  const resolvedKeyframes = keyframes.filter(isNotNull);
@@ -2760,18 +2759,19 @@ function getFinalKeyframe(keyframes, { repeat, repeatType = "loop" }, finalKeyfr
2760
2759
  : finalKeyframe;
2761
2760
  }
2762
2761
 
2763
- function calcChildStagger(children, child, delayChildren, staggerChildren = 0, staggerDirection = 1) {
2764
- const index = Array.from(children)
2765
- .sort((a, b) => a.sortNodePosition(b))
2766
- .indexOf(child);
2767
- const numChildren = children.size;
2768
- const maxStaggerDuration = (numChildren - 1) * staggerChildren;
2769
- const delayIsFunction = typeof delayChildren === "function";
2770
- return delayIsFunction
2771
- ? delayChildren(index, numChildren)
2772
- : staggerDirection === 1
2773
- ? index * staggerChildren
2774
- : maxStaggerDuration - index * staggerChildren;
2762
+ function getValueTransition(transition, key) {
2763
+ return (transition?.[key] ??
2764
+ transition?.["default"] ??
2765
+ transition);
2766
+ }
2767
+
2768
+ /**
2769
+ * Decide whether a transition is defined on a given Transition.
2770
+ * This filters out orchestration options and returns true
2771
+ * if any options are left.
2772
+ */
2773
+ function isTransitionDefined({ when, delay: _delay, delayChildren, staggerChildren, staggerDirection, repeat, repeatType, repeatDelay, from, elapsed, ...transition }) {
2774
+ return !!Object.keys(transition).length;
2775
2775
  }
2776
2776
 
2777
2777
  const animateMotionValue = (name, value, target, transition = {}, element, isHandoff) => (onComplete) => {
@@ -2865,6 +2865,46 @@ const animateMotionValue = (name, value, target, transition = {}, element, isHan
2865
2865
  : new AsyncMotionValueAnimation(options);
2866
2866
  };
2867
2867
 
2868
+ function getValueState(visualElement) {
2869
+ const state = [{}, {}];
2870
+ visualElement?.values.forEach((value, key) => {
2871
+ state[0][key] = value.get();
2872
+ state[1][key] = value.getVelocity();
2873
+ });
2874
+ return state;
2875
+ }
2876
+ function resolveVariantFromProps(props, definition, custom, visualElement) {
2877
+ /**
2878
+ * If the variant definition is a function, resolve.
2879
+ */
2880
+ if (typeof definition === "function") {
2881
+ const [current, velocity] = getValueState(visualElement);
2882
+ definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
2883
+ }
2884
+ /**
2885
+ * If the variant definition is a variant label, or
2886
+ * the function returned a variant label, resolve.
2887
+ */
2888
+ if (typeof definition === "string") {
2889
+ definition = props.variants && props.variants[definition];
2890
+ }
2891
+ /**
2892
+ * At this point we've resolved both functions and variant labels,
2893
+ * but the resolved variant label might itself have been a function.
2894
+ * If so, resolve. This can only have returned a valid target object.
2895
+ */
2896
+ if (typeof definition === "function") {
2897
+ const [current, velocity] = getValueState(visualElement);
2898
+ definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
2899
+ }
2900
+ return definition;
2901
+ }
2902
+
2903
+ function resolveVariant(visualElement, definition, custom) {
2904
+ const props = visualElement.getProps();
2905
+ return resolveVariantFromProps(props, definition, custom !== undefined ? custom : props.custom, visualElement);
2906
+ }
2907
+
2868
2908
  const positionalKeys = new Set([
2869
2909
  "width",
2870
2910
  "height",
@@ -3193,46 +3233,6 @@ function motionValue(init, options) {
3193
3233
  return new MotionValue(init, options);
3194
3234
  }
3195
3235
 
3196
- function getValueState(visualElement) {
3197
- const state = [{}, {}];
3198
- visualElement?.values.forEach((value, key) => {
3199
- state[0][key] = value.get();
3200
- state[1][key] = value.getVelocity();
3201
- });
3202
- return state;
3203
- }
3204
- function resolveVariantFromProps(props, definition, custom, visualElement) {
3205
- /**
3206
- * If the variant definition is a function, resolve.
3207
- */
3208
- if (typeof definition === "function") {
3209
- const [current, velocity] = getValueState(visualElement);
3210
- definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
3211
- }
3212
- /**
3213
- * If the variant definition is a variant label, or
3214
- * the function returned a variant label, resolve.
3215
- */
3216
- if (typeof definition === "string") {
3217
- definition = props.variants && props.variants[definition];
3218
- }
3219
- /**
3220
- * At this point we've resolved both functions and variant labels,
3221
- * but the resolved variant label might itself have been a function.
3222
- * If so, resolve. This can only have returned a valid target object.
3223
- */
3224
- if (typeof definition === "function") {
3225
- const [current, velocity] = getValueState(visualElement);
3226
- definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
3227
- }
3228
- return definition;
3229
- }
3230
-
3231
- function resolveVariant(visualElement, definition, custom) {
3232
- const props = visualElement.getProps();
3233
- return resolveVariantFromProps(props, definition, custom !== undefined ? custom : props.custom, visualElement);
3234
- }
3235
-
3236
3236
  const isKeyframesTarget = (v) => {
3237
3237
  return Array.isArray(v);
3238
3238
  };
@@ -5763,19 +5763,6 @@ class VisualElement {
5763
5763
  }
5764
5764
  }
5765
5765
 
5766
- /**
5767
- * Feature base class for extending VisualElement functionality.
5768
- * Features are plugins that can be mounted/unmounted to add behavior
5769
- * like gestures, animations, or layout tracking.
5770
- */
5771
- class Feature {
5772
- constructor(node) {
5773
- this.isMounted = false;
5774
- this.node = node;
5775
- }
5776
- update() { }
5777
- }
5778
-
5779
5766
  class DOMVisualElement extends VisualElement {
5780
5767
  constructor() {
5781
5768
  super(...arguments);
@@ -5813,6 +5800,19 @@ class DOMVisualElement extends VisualElement {
5813
5800
  }
5814
5801
  }
5815
5802
 
5803
+ /**
5804
+ * Feature base class for extending VisualElement functionality.
5805
+ * Features are plugins that can be mounted/unmounted to add behavior
5806
+ * like gestures, animations, or layout tracking.
5807
+ */
5808
+ class Feature {
5809
+ constructor(node) {
5810
+ this.isMounted = false;
5811
+ this.node = node;
5812
+ }
5813
+ update() { }
5814
+ }
5815
+
5816
5816
  /**
5817
5817
  * Bounding boxes tend to be defined as top, left, right, bottom. For various operations
5818
5818
  * it's easier to consider each axis individually. This function returns a bounding box
@@ -6281,6 +6281,43 @@ class HTMLVisualElement extends DOMVisualElement {
6281
6281
  }
6282
6282
  }
6283
6283
 
6284
+ function isObjectKey(key, object) {
6285
+ return key in object;
6286
+ }
6287
+ class ObjectVisualElement extends VisualElement {
6288
+ constructor() {
6289
+ super(...arguments);
6290
+ this.type = "object";
6291
+ }
6292
+ readValueFromInstance(instance, key) {
6293
+ if (isObjectKey(key, instance)) {
6294
+ const value = instance[key];
6295
+ if (typeof value === "string" || typeof value === "number") {
6296
+ return value;
6297
+ }
6298
+ }
6299
+ return undefined;
6300
+ }
6301
+ getBaseTargetFromProps() {
6302
+ return undefined;
6303
+ }
6304
+ removeValueFromRenderState(key, renderState) {
6305
+ delete renderState.output[key];
6306
+ }
6307
+ measureInstanceViewportBox() {
6308
+ return createBox();
6309
+ }
6310
+ build(renderState, latestValues) {
6311
+ Object.assign(renderState.output, latestValues);
6312
+ }
6313
+ renderInstance(instance, { output }) {
6314
+ Object.assign(instance, output);
6315
+ }
6316
+ sortInstanceNodePosition() {
6317
+ return 0;
6318
+ }
6319
+ }
6320
+
6284
6321
  const dashKeys = {
6285
6322
  offset: "stroke-dashoffset",
6286
6323
  array: "stroke-dasharray",
@@ -6463,43 +6500,6 @@ class SVGVisualElement extends DOMVisualElement {
6463
6500
  }
6464
6501
  }
6465
6502
 
6466
- function isObjectKey(key, object) {
6467
- return key in object;
6468
- }
6469
- class ObjectVisualElement extends VisualElement {
6470
- constructor() {
6471
- super(...arguments);
6472
- this.type = "object";
6473
- }
6474
- readValueFromInstance(instance, key) {
6475
- if (isObjectKey(key, instance)) {
6476
- const value = instance[key];
6477
- if (typeof value === "string" || typeof value === "number") {
6478
- return value;
6479
- }
6480
- }
6481
- return undefined;
6482
- }
6483
- getBaseTargetFromProps() {
6484
- return undefined;
6485
- }
6486
- removeValueFromRenderState(key, renderState) {
6487
- delete renderState.output[key];
6488
- }
6489
- measureInstanceViewportBox() {
6490
- return createBox();
6491
- }
6492
- build(renderState, latestValues) {
6493
- Object.assign(renderState.output, latestValues);
6494
- }
6495
- renderInstance(instance, { output }) {
6496
- Object.assign(instance, output);
6497
- }
6498
- sortInstanceNodePosition() {
6499
- return 0;
6500
- }
6501
- }
6502
-
6503
6503
  const numVariantProps = variantProps.length;
6504
6504
  /**
6505
6505
  * Get variant context from a visual element's parent chain.
@@ -6894,6 +6894,36 @@ function createState() {
6894
6894
  };
6895
6895
  }
6896
6896
 
6897
+ /**
6898
+ * Reset an axis to the provided origin box.
6899
+ *
6900
+ * This is a mutative operation.
6901
+ */
6902
+ function copyAxisInto(axis, originAxis) {
6903
+ axis.min = originAxis.min;
6904
+ axis.max = originAxis.max;
6905
+ }
6906
+ /**
6907
+ * Reset a box to the provided origin box.
6908
+ *
6909
+ * This is a mutative operation.
6910
+ */
6911
+ function copyBoxInto(box, originBox) {
6912
+ copyAxisInto(box.x, originBox.x);
6913
+ copyAxisInto(box.y, originBox.y);
6914
+ }
6915
+ /**
6916
+ * Reset a delta to the provided origin box.
6917
+ *
6918
+ * This is a mutative operation.
6919
+ */
6920
+ function copyAxisDeltaInto(delta, originDelta) {
6921
+ delta.translate = originDelta.translate;
6922
+ delta.scale = originDelta.scale;
6923
+ delta.originPoint = originDelta.originPoint;
6924
+ delta.origin = originDelta.origin;
6925
+ }
6926
+
6897
6927
  const SCALE_PRECISION = 0.0001;
6898
6928
  const SCALE_MIN = 1 - SCALE_PRECISION;
6899
6929
  const SCALE_MAX = 1 + SCALE_PRECISION;
@@ -6992,36 +7022,6 @@ function removeBoxTransforms(box, transforms, originBox, sourceBox) {
6992
7022
  removeAxisTransforms(box.y, transforms, yKeys, originBox ? originBox.y : undefined, sourceBox ? sourceBox.y : undefined);
6993
7023
  }
6994
7024
 
6995
- /**
6996
- * Reset an axis to the provided origin box.
6997
- *
6998
- * This is a mutative operation.
6999
- */
7000
- function copyAxisInto(axis, originAxis) {
7001
- axis.min = originAxis.min;
7002
- axis.max = originAxis.max;
7003
- }
7004
- /**
7005
- * Reset a box to the provided origin box.
7006
- *
7007
- * This is a mutative operation.
7008
- */
7009
- function copyBoxInto(box, originBox) {
7010
- copyAxisInto(box.x, originBox.x);
7011
- copyAxisInto(box.y, originBox.y);
7012
- }
7013
- /**
7014
- * Reset a delta to the provided origin box.
7015
- *
7016
- * This is a mutative operation.
7017
- */
7018
- function copyAxisDeltaInto(delta, originDelta) {
7019
- delta.translate = originDelta.translate;
7020
- delta.scale = originDelta.scale;
7021
- delta.originPoint = originDelta.originPoint;
7022
- delta.origin = originDelta.origin;
7023
- }
7024
-
7025
7025
  function isAxisDeltaZero(delta) {
7026
7026
  return delta.translate === 0 && delta.scale === 1;
7027
7027
  }
@@ -7163,23 +7163,10 @@ function compress(min, max, easing) {
7163
7163
  };
7164
7164
  }
7165
7165
 
7166
- /**
7167
- * Timeout defined in ms
7168
- */
7169
- function delay(callback, timeout) {
7170
- const start = time.now();
7171
- const checkElapsed = ({ timestamp }) => {
7172
- const elapsed = timestamp - start;
7173
- if (elapsed >= timeout) {
7174
- cancelFrame(checkElapsed);
7175
- callback(elapsed - timeout);
7176
- }
7177
- };
7178
- frame.setup(checkElapsed, true);
7179
- return () => cancelFrame(checkElapsed);
7180
- }
7181
- function delayInSeconds(callback, timeout) {
7182
- return delay(callback, motionUtils.secondsToMilliseconds(timeout));
7166
+ function animateSingleValue(value, keyframes, options) {
7167
+ const motionValue$1 = isMotionValue(value) ? value : motionValue(value);
7168
+ motionValue$1.start(animateMotionValue("", motionValue$1, keyframes, options));
7169
+ return motionValue$1.animation;
7183
7170
  }
7184
7171
 
7185
7172
  function addDomEvent(target, eventName, handler, options = { passive: true }) {
@@ -7187,19 +7174,6 @@ function addDomEvent(target, eventName, handler, options = { passive: true }) {
7187
7174
  return () => target.removeEventListener(eventName, handler);
7188
7175
  }
7189
7176
 
7190
- /**
7191
- * If the provided value is a MotionValue, this returns the actual value, otherwise just the value itself
7192
- */
7193
- function resolveMotionValue(value) {
7194
- return isMotionValue(value) ? value.get() : value;
7195
- }
7196
-
7197
- function animateSingleValue(value, keyframes, options) {
7198
- const motionValue$1 = isMotionValue(value) ? value : motionValue(value);
7199
- motionValue$1.start(animateMotionValue("", motionValue$1, keyframes, options));
7200
- return motionValue$1.animation;
7201
- }
7202
-
7203
7177
  const compareByDepth = (a, b) => a.depth - b.depth;
7204
7178
 
7205
7179
  class FlatTree {
@@ -7222,6 +7196,32 @@ class FlatTree {
7222
7196
  }
7223
7197
  }
7224
7198
 
7199
+ /**
7200
+ * Timeout defined in ms
7201
+ */
7202
+ function delay(callback, timeout) {
7203
+ const start = time.now();
7204
+ const checkElapsed = ({ timestamp }) => {
7205
+ const elapsed = timestamp - start;
7206
+ if (elapsed >= timeout) {
7207
+ cancelFrame(checkElapsed);
7208
+ callback(elapsed - timeout);
7209
+ }
7210
+ };
7211
+ frame.setup(checkElapsed, true);
7212
+ return () => cancelFrame(checkElapsed);
7213
+ }
7214
+ function delayInSeconds(callback, timeout) {
7215
+ return delay(callback, motionUtils.secondsToMilliseconds(timeout));
7216
+ }
7217
+
7218
+ /**
7219
+ * If the provided value is a MotionValue, this returns the actual value, otherwise just the value itself
7220
+ */
7221
+ function resolveMotionValue(value) {
7222
+ return isMotionValue(value) ? value.get() : value;
7223
+ }
7224
+
7225
7225
  class NodeStack {
7226
7226
  constructor() {
7227
7227
  this.members = [];
@@ -8943,29 +8943,6 @@ const DocumentProjectionNode = createProjectionNode$1({
8943
8943
  checkIsScrollRoot: () => true,
8944
8944
  });
8945
8945
 
8946
- const rootProjectionNode = {
8947
- current: undefined,
8948
- };
8949
- const HTMLProjectionNode = createProjectionNode$1({
8950
- measureScroll: (instance) => ({
8951
- x: instance.scrollLeft,
8952
- y: instance.scrollTop,
8953
- }),
8954
- defaultParent: () => {
8955
- if (!rootProjectionNode.current) {
8956
- const documentNode = new DocumentProjectionNode({});
8957
- documentNode.mount(window);
8958
- documentNode.setOptions({ layoutScroll: true });
8959
- rootProjectionNode.current = documentNode;
8960
- }
8961
- return rootProjectionNode.current;
8962
- },
8963
- resetTransform: (instance, value) => {
8964
- instance.style.transform = value !== undefined ? value : "none";
8965
- },
8966
- checkIsScrollRoot: (instance) => Boolean(window.getComputedStyle(instance).position === "fixed"),
8967
- });
8968
-
8969
8946
  const notify = (node) => !node.isLayoutDirty && node.willUpdate(false);
8970
8947
  function nodeGroup() {
8971
8948
  const nodes = new Set();
@@ -8989,6 +8966,29 @@ function nodeGroup() {
8989
8966
  };
8990
8967
  }
8991
8968
 
8969
+ const rootProjectionNode = {
8970
+ current: undefined,
8971
+ };
8972
+ const HTMLProjectionNode = createProjectionNode$1({
8973
+ measureScroll: (instance) => ({
8974
+ x: instance.scrollLeft,
8975
+ y: instance.scrollTop,
8976
+ }),
8977
+ defaultParent: () => {
8978
+ if (!rootProjectionNode.current) {
8979
+ const documentNode = new DocumentProjectionNode({});
8980
+ documentNode.mount(window);
8981
+ documentNode.setOptions({ layoutScroll: true });
8982
+ rootProjectionNode.current = documentNode;
8983
+ }
8984
+ return rootProjectionNode.current;
8985
+ },
8986
+ resetTransform: (instance, value) => {
8987
+ instance.style.transform = value !== undefined ? value : "none";
8988
+ },
8989
+ checkIsScrollRoot: (instance) => Boolean(window.getComputedStyle(instance).position === "fixed"),
8990
+ });
8991
+
8992
8992
  const LAYOUT_SELECTOR = "[data-layout], [data-layout-id]";
8993
8993
  function getLayoutElements(scope) {
8994
8994
  const elements = Array.from(scope.querySelectorAll(LAYOUT_SELECTOR));
@@ -9005,93 +9005,6 @@ function hasLayout(element) {
9005
9005
  return (element.hasAttribute("data-layout") ||
9006
9006
  element.hasAttribute("data-layout-id"));
9007
9007
  }
9008
- /**
9009
- * Track layout elements before mutation.
9010
- * Does NOT measure bounds - that's handled by the projection system via willUpdate().
9011
- */
9012
- function trackLayoutElements(scope) {
9013
- const elements = getLayoutElements(scope);
9014
- const records = new Map();
9015
- for (const element of elements) {
9016
- records.set(element, {
9017
- element,
9018
- parentElement: element.parentElement,
9019
- nextSibling: element.nextSibling,
9020
- layoutId: getLayoutId(element),
9021
- });
9022
- }
9023
- return records;
9024
- }
9025
- /**
9026
- * Compare before/after records to detect entering/exiting/persisting elements
9027
- */
9028
- function detectMutations(beforeRecords, scope) {
9029
- const afterElements = new Set(getLayoutElements(scope));
9030
- const beforeElements = new Set(beforeRecords.keys());
9031
- const entering = [];
9032
- const exiting = [];
9033
- const persisting = [];
9034
- const sharedEntering = new Map();
9035
- const sharedExiting = new Map();
9036
- // Find exiting elements (were in before, not in after)
9037
- for (const element of beforeElements) {
9038
- if (!afterElements.has(element)) {
9039
- const record = beforeRecords.get(element);
9040
- exiting.push({
9041
- element,
9042
- parentElement: record.parentElement,
9043
- nextSibling: record.nextSibling,
9044
- });
9045
- if (record.layoutId) {
9046
- sharedExiting.set(record.layoutId, element);
9047
- }
9048
- }
9049
- }
9050
- // Find entering and persisting elements
9051
- for (const element of afterElements) {
9052
- if (!beforeElements.has(element)) {
9053
- entering.push(element);
9054
- const layoutId = getLayoutId(element);
9055
- if (layoutId) {
9056
- sharedEntering.set(layoutId, element);
9057
- }
9058
- }
9059
- else {
9060
- persisting.push(element);
9061
- }
9062
- }
9063
- return {
9064
- entering,
9065
- exiting,
9066
- persisting,
9067
- sharedEntering,
9068
- sharedExiting,
9069
- };
9070
- }
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
9008
 
9096
9009
  let scaleCorrectorAdded = false;
9097
9010
  /**
@@ -9165,6 +9078,11 @@ function createProjectionNode(element, parent, options, transition) {
9165
9078
  transition: nodeTransition,
9166
9079
  ...options,
9167
9080
  });
9081
+ // Re-mount the node if it was previously unmounted
9082
+ // This re-adds it to root.nodes so didUpdate() will process it
9083
+ if (!existingNode.instance) {
9084
+ existingNode.mount(element);
9085
+ }
9168
9086
  return { node: existingNode, visualElement };
9169
9087
  }
9170
9088
  // No existing node - create a new one
@@ -9256,10 +9174,16 @@ function parseLayoutMode(value) {
9256
9174
  return "both";
9257
9175
  }
9258
9176
  /**
9259
- * Clean up projection nodes
9177
+ * Clean up projection nodes for specific elements.
9178
+ * If elementsToCleanup is provided, only those elements are cleaned up.
9179
+ * If not provided, all nodes are cleaned up.
9180
+ *
9181
+ * This allows persisting elements to keep their nodes between animations,
9182
+ * matching React's behavior where nodes persist for elements that remain in the DOM.
9260
9183
  */
9261
- function cleanupProjectionTree(context) {
9262
- for (const [element, node] of context.nodes.entries()) {
9184
+ function cleanupProjectionTree(context, elementsToCleanup) {
9185
+ const elementsToProcess = [...context.nodes.entries()];
9186
+ for (const [element, node] of elementsToProcess) {
9263
9187
  context.group.remove(node);
9264
9188
  node.unmount();
9265
9189
  // Only clear from activeProjectionNodes if this is still the active node.
@@ -9267,9 +9191,9 @@ function cleanupProjectionTree(context) {
9267
9191
  if (activeProjectionNodes.get(element) === node) {
9268
9192
  activeProjectionNodes.delete(element);
9269
9193
  }
9194
+ context.nodes.delete(element);
9195
+ context.visualElements.delete(element);
9270
9196
  }
9271
- context.nodes.clear();
9272
- context.visualElements.clear();
9273
9197
  }
9274
9198
 
9275
9199
  class LayoutAnimationBuilder {
@@ -9286,22 +9210,8 @@ class LayoutAnimationBuilder {
9286
9210
  // Queue execution on microtask to allow builder methods to be called
9287
9211
  queueMicrotask(() => this.execute());
9288
9212
  }
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
- shared(layoutIdOrOptions, options) {
9300
- if (typeof layoutIdOrOptions === "string") {
9301
- this.sharedTransitions.set(layoutIdOrOptions, options);
9302
- }
9303
- // For now, we ignore default shared options as the projection system
9304
- // handles shared transitions automatically
9213
+ shared(id, options) {
9214
+ this.sharedTransitions.set(id, options);
9305
9215
  return this;
9306
9216
  }
9307
9217
  then(onfulfilled, onrejected) {
@@ -9311,200 +9221,67 @@ class LayoutAnimationBuilder {
9311
9221
  if (this.executed)
9312
9222
  return;
9313
9223
  this.executed = true;
9314
- const animations = [];
9315
9224
  let context;
9316
- try {
9317
- // Phase 1: Pre-mutation - Build projection tree and take snapshots
9318
- const existingElements = getLayoutElements(this.scope);
9319
- // Build projection tree for existing elements FIRST
9320
- // This allows the projection system to handle measurements correctly
9321
- if (existingElements.length > 0) {
9322
- context = buildProjectionTree(existingElements, undefined, this.getBuildOptions());
9323
- // Start update cycle
9324
- context.root.startUpdate();
9325
- // Call willUpdate on all nodes to capture snapshots via projection system
9326
- // This handles transforms, scroll, etc. correctly
9327
- for (const node of context.nodes.values()) {
9328
- // Reset isLayoutDirty so willUpdate can take a snapshot.
9329
- // When hasTreeAnimated is true on the global root, newly mounted nodes
9330
- // get isLayoutDirty=true, which causes willUpdate to skip snapshot capture.
9331
- node.isLayoutDirty = false;
9332
- node.willUpdate();
9333
- }
9334
- }
9335
- // Track DOM structure (parent, sibling) for detecting removals
9336
- // No bounds measurement here - projection system already handled that
9337
- const beforeRecords = trackLayoutElements(this.scope);
9338
- // Phase 2: Execute DOM update
9339
- this.updateDom();
9340
- // Phase 3: Post-mutation (Detect & Prepare)
9341
- 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 }) => {
9345
- const layoutId = element.getAttribute("data-layout-id");
9346
- return !layoutId || !mutationResult.sharedEntering.has(layoutId);
9347
- });
9348
- this.reattachExitingElements(nonSharedExiting, context);
9349
- // Build projection nodes for entering elements
9350
- if (mutationResult.entering.length > 0) {
9351
- context = buildProjectionTree(mutationResult.entering, context, this.getBuildOptions());
9352
- }
9353
- // Also ensure persisting elements have nodes if context didn't exist
9354
- if (!context && mutationResult.persisting.length > 0) {
9355
- context = buildProjectionTree(mutationResult.persisting, undefined, this.getBuildOptions());
9356
- }
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
9359
- const sharedExitingElements = new Set();
9360
- for (const [layoutId] of mutationResult.sharedEntering) {
9361
- const exitingElement = mutationResult.sharedExiting.get(layoutId);
9362
- if (exitingElement) {
9363
- 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
- const exitingNode = context?.nodes.get(exitingElement);
9369
- if (exitingNode) {
9370
- const stack = exitingNode.getStack();
9371
- if (stack) {
9372
- stack.remove(exitingNode);
9373
- }
9374
- }
9375
- }
9376
- }
9377
- // Phase 4: Animate
9378
- if (context) {
9379
- // Trigger layout animations via didUpdate
9380
- context.root.didUpdate();
9381
- // Wait for animations to be created (they're scheduled via frame.update)
9382
- await new Promise((resolve) => frame.postRender(() => resolve()));
9383
- // Collect layout animations from projection nodes (excluding shared exiting elements)
9384
- for (const [element, node] of context.nodes.entries()) {
9385
- if (sharedExitingElements.has(element))
9386
- continue;
9387
- if (node.currentAnimation) {
9388
- animations.push(node.currentAnimation);
9389
- }
9390
- }
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
- }
9402
- // Create and return group animation
9403
- const groupAnimation = new GroupAnimation(animations);
9404
- // Phase 5: Setup cleanup on complete
9405
- groupAnimation.finished.then(() => {
9406
- // Only clean up non-shared exiting elements (those we reattached)
9407
- this.cleanupExitingElements(nonSharedExiting);
9408
- if (context) {
9409
- cleanupProjectionTree(context);
9410
- }
9411
- });
9412
- this.notifyReady(groupAnimation);
9225
+ // Phase 1: Pre-mutation - Build projection tree and take snapshots
9226
+ const beforeElements = getLayoutElements(this.scope);
9227
+ if (beforeElements.length > 0) {
9228
+ context = buildProjectionTree(beforeElements, undefined, this.getBuildOptions());
9229
+ context.root.startUpdate();
9230
+ for (const node of context.nodes.values()) {
9231
+ node.isLayoutDirty = false;
9232
+ node.willUpdate();
9233
+ }
9234
+ }
9235
+ // Phase 2: Execute DOM update
9236
+ this.updateDom();
9237
+ // Phase 3: Post-mutation - Compare before/after elements
9238
+ const afterElements = getLayoutElements(this.scope);
9239
+ const beforeSet = new Set(beforeElements);
9240
+ const afterSet = new Set(afterElements);
9241
+ const entering = afterElements.filter((el) => !beforeSet.has(el));
9242
+ const exiting = beforeElements.filter((el) => !afterSet.has(el));
9243
+ // Build projection nodes for entering elements
9244
+ if (entering.length > 0) {
9245
+ context = buildProjectionTree(entering, context, this.getBuildOptions());
9246
+ }
9247
+ // No layout elements - return empty animation
9248
+ if (!context) {
9249
+ this.notifyReady(new GroupAnimation([]));
9250
+ return;
9413
9251
  }
9414
- catch (error) {
9415
- // Cleanup on error
9416
- if (context) {
9417
- cleanupProjectionTree(context);
9418
- }
9419
- throw error;
9252
+ // Handle shared elements
9253
+ for (const element of exiting) {
9254
+ const node = context.nodes.get(element);
9255
+ node?.getStack()?.remove(node);
9420
9256
  }
9421
- }
9422
- getBuildOptions() {
9423
- return {
9424
- defaultTransition: this.defaultOptions || { duration: 0.3, ease: "easeOut" },
9425
- sharedTransitions: this.sharedTransitions.size > 0 ? this.sharedTransitions : undefined,
9426
- };
9427
- }
9428
- reattachExitingElements(exiting, context) {
9429
- for (const { element, parentElement, nextSibling } of exiting) {
9430
- // Check if parent still exists in DOM
9431
- if (!parentElement.isConnected)
9432
- continue;
9433
- // Get bounds from projection node snapshot (measured correctly via projection system)
9434
- const node = context?.nodes.get(element);
9435
- const snapshot = node?.snapshot;
9436
- if (!snapshot)
9437
- continue;
9438
- const { layoutBox } = snapshot;
9439
- // Reattach element
9440
- if (nextSibling && nextSibling.parentNode === parentElement) {
9441
- parentElement.insertBefore(element, nextSibling);
9442
- }
9443
- else {
9444
- parentElement.appendChild(element);
9445
- }
9446
- // Apply absolute positioning to prevent layout shift
9447
- // Use layoutBox from projection system which has transform-free measurements
9448
- const htmlElement = element;
9449
- htmlElement.style.position = "absolute";
9450
- htmlElement.style.top = `${layoutBox.y.min}px`;
9451
- htmlElement.style.left = `${layoutBox.x.min}px`;
9452
- htmlElement.style.width = `${layoutBox.x.max - layoutBox.x.min}px`;
9453
- htmlElement.style.height = `${layoutBox.y.max - layoutBox.y.min}px`;
9454
- htmlElement.style.margin = "0";
9455
- htmlElement.style.pointerEvents = "none";
9257
+ for (const element of entering) {
9258
+ context.nodes.get(element)?.promote();
9456
9259
  }
9457
- }
9458
- cleanupExitingElements(exiting) {
9459
- for (const { element } of exiting) {
9460
- if (element.parentElement) {
9461
- element.parentElement.removeChild(element);
9462
- }
9463
- }
9464
- }
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));
9260
+ // Phase 4: Animate
9261
+ context.root.didUpdate();
9262
+ await new Promise((resolve) => frame.postRender(() => resolve()));
9469
9263
  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
- }
9264
+ for (const node of context.nodes.values()) {
9265
+ if (node.currentAnimation) {
9266
+ animations.push(node.currentAnimation);
9483
9267
  }
9484
- const options = this.enterOptions || this.defaultOptions || {};
9485
- const enterAnims = animateTarget(visualElement, keyframes, {
9486
- transitionOverride: options,
9487
- });
9488
- animations.push(...enterAnims);
9489
9268
  }
9490
- return animations;
9269
+ const groupAnimation = new GroupAnimation(animations);
9270
+ groupAnimation.finished.then(() => {
9271
+ cleanupProjectionTree(context);
9272
+ });
9273
+ this.notifyReady(groupAnimation);
9491
9274
  }
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;
9275
+ getBuildOptions() {
9276
+ return {
9277
+ defaultTransition: this.defaultOptions || {
9278
+ duration: 0.3,
9279
+ ease: "easeOut",
9280
+ },
9281
+ sharedTransitions: this.sharedTransitions.size > 0
9282
+ ? this.sharedTransitions
9283
+ : undefined,
9284
+ };
9508
9285
  }
9509
9286
  }
9510
9287
  /**
@@ -9529,47 +9306,6 @@ function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
9529
9306
  };
9530
9307
  }
9531
9308
 
9532
- /**
9533
- * Animate layout changes within a DOM tree.
9534
- *
9535
- * @example
9536
- * ```typescript
9537
- * // Basic usage - animates all elements with data-layout or data-layout-id
9538
- * await animateLayout(() => {
9539
- * container.innerHTML = newContent
9540
- * })
9541
- *
9542
- * // With scope - only animates within the container
9543
- * await animateLayout(".container", () => {
9544
- * updateContent()
9545
- * })
9546
- *
9547
- * // With options
9548
- * await animateLayout(() => update(), { duration: 0.5 })
9549
- *
9550
- * // Builder pattern for enter/exit animations
9551
- * animateLayout(".cards", () => {
9552
- * container.innerHTML = newCards
9553
- * }, { duration: 0.3 })
9554
- * .enter({ opacity: 1, scale: 1 }, { duration: 0.2 })
9555
- * .exit({ opacity: 0, scale: 0.8 })
9556
- * .shared("hero", { type: "spring" })
9557
- * ```
9558
- *
9559
- * Elements are animated if they have:
9560
- * - `data-layout` attribute (layout animation only)
9561
- * - `data-layout-id` attribute (shared element transitions)
9562
- *
9563
- * @param scopeOrUpdateDom - Either a scope selector/element, or the DOM update function
9564
- * @param updateDomOrOptions - Either the DOM update function or animation options
9565
- * @param options - Animation options (when scope is provided)
9566
- * @returns A builder that resolves to animation controls
9567
- */
9568
- function unstable_animateLayout(scopeOrUpdateDom, updateDomOrOptions, options) {
9569
- const { scope, updateDom, defaultOptions } = parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options);
9570
- return new LayoutAnimationBuilder(scope, updateDom, defaultOptions);
9571
- }
9572
-
9573
9309
  /**
9574
9310
  * @deprecated
9575
9311
  *
@@ -9778,6 +9514,7 @@ exports.numberValueTypes = numberValueTypes;
9778
9514
  exports.observeTimeline = observeTimeline;
9779
9515
  exports.optimizedAppearDataAttribute = optimizedAppearDataAttribute;
9780
9516
  exports.optimizedAppearDataId = optimizedAppearDataId;
9517
+ exports.parseAnimateLayoutArgs = parseAnimateLayoutArgs;
9781
9518
  exports.parseCSSVariable = parseCSSVariable;
9782
9519
  exports.parseValueFromTransform = parseValueFromTransform;
9783
9520
  exports.percent = percent;
@@ -9839,7 +9576,6 @@ exports.transformProps = transformProps;
9839
9576
  exports.transformValue = transformValue;
9840
9577
  exports.transformValueTypes = transformValueTypes;
9841
9578
  exports.translateAxis = translateAxis;
9842
- exports.unstable_animateLayout = unstable_animateLayout;
9843
9579
  exports.updateMotionValuesFromProps = updateMotionValuesFromProps;
9844
9580
  exports.variantPriorityOrder = variantPriorityOrder;
9845
9581
  exports.variantProps = variantProps;