motion-dom 12.27.2 → 12.27.3-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -7380,7 +7380,7 @@ function cancelTreeOptimisedTransformAnimations(projectionNode) {
7380
7380
  cancelTreeOptimisedTransformAnimations(parent);
7381
7381
  }
7382
7382
  }
7383
- function createProjectionNode$1({ attachResizeListener, defaultParent, measureScroll, checkIsScrollRoot, resetTransform, }) {
7383
+ function createProjectionNode({ attachResizeListener, defaultParent, measureScroll, checkIsScrollRoot, resetTransform, }) {
7384
7384
  return class ProjectionNode {
7385
7385
  constructor(latestValues = {}, parent = defaultParent?.()) {
7386
7386
  /**
@@ -8937,7 +8937,7 @@ function checkNodeWasScrollRoot(node) {
8937
8937
  return node !== node.root && node.scroll?.wasRoot;
8938
8938
  }
8939
8939
 
8940
- const DocumentProjectionNode = createProjectionNode$1({
8940
+ const DocumentProjectionNode = createProjectionNode({
8941
8941
  attachResizeListener: (ref, notify) => addDomEvent(ref, "resize", notify),
8942
8942
  measureScroll: () => ({
8943
8943
  x: document.documentElement.scrollLeft || document.body.scrollLeft,
@@ -8972,7 +8972,7 @@ function nodeGroup() {
8972
8972
  const rootProjectionNode = {
8973
8973
  current: undefined,
8974
8974
  };
8975
- const HTMLProjectionNode = createProjectionNode$1({
8975
+ const HTMLProjectionNode = createProjectionNode({
8976
8976
  measureScroll: (instance) => ({
8977
8977
  x: instance.scrollLeft,
8978
8978
  y: instance.scrollTop,
@@ -8992,315 +8992,113 @@ const HTMLProjectionNode = createProjectionNode$1({
8992
8992
  checkIsScrollRoot: (instance) => Boolean(window.getComputedStyle(instance).position === "fixed"),
8993
8993
  });
8994
8994
 
8995
- const LAYOUT_SELECTOR = "[data-layout], [data-layout-id]";
8996
- function getLayoutElements(scope) {
8997
- const elements = Array.from(scope.querySelectorAll(LAYOUT_SELECTOR));
8998
- // Include scope itself if it's an Element (not Document) and has layout attributes
8999
- if (scope instanceof Element && hasLayout(scope)) {
9000
- elements.unshift(scope);
9001
- }
9002
- return elements;
9003
- }
9004
- function getLayoutId(element) {
9005
- return element.getAttribute("data-layout-id");
9006
- }
9007
- function hasLayout(element) {
9008
- return (element.hasAttribute("data-layout") ||
9009
- element.hasAttribute("data-layout-id"));
9010
- }
9011
-
9012
- let scaleCorrectorAdded = false;
9013
- /**
9014
- * Track active projection nodes per element to handle animation interruption.
9015
- * When a new animation starts on an element that already has an active animation,
9016
- * we need to stop the old animation so the new one can start from the current
9017
- * visual position.
9018
- */
9019
- const activeProjectionNodes = new WeakMap();
9020
- function ensureScaleCorrectors() {
9021
- if (scaleCorrectorAdded)
9022
- return;
9023
- scaleCorrectorAdded = true;
9024
- addScaleCorrector({
9025
- borderRadius: {
9026
- ...correctBorderRadius,
9027
- applyTo: [
9028
- "borderTopLeftRadius",
9029
- "borderTopRightRadius",
9030
- "borderBottomLeftRadius",
9031
- "borderBottomRightRadius",
9032
- ],
9033
- },
9034
- borderTopLeftRadius: correctBorderRadius,
9035
- borderTopRightRadius: correctBorderRadius,
9036
- borderBottomLeftRadius: correctBorderRadius,
9037
- borderBottomRightRadius: correctBorderRadius,
9038
- boxShadow: correctBoxShadow,
9039
- });
9040
- }
9041
- /**
9042
- * Get DOM depth of an element
9043
- */
9044
- function getDepth(element) {
9045
- let depth = 0;
9046
- let current = element.parentElement;
9047
- while (current) {
9048
- depth++;
9049
- current = current.parentElement;
9050
- }
9051
- return depth;
9052
- }
9053
- /**
9054
- * Find the closest projection parent for an element
9055
- */
9056
- function findProjectionParent(element, nodeCache) {
9057
- let parent = element.parentElement;
9058
- while (parent) {
9059
- const node = nodeCache.get(parent);
9060
- if (node)
9061
- return node;
9062
- parent = parent.parentElement;
9063
- }
9064
- return undefined;
9065
- }
9066
- /**
9067
- * Create or reuse a projection node for an element
9068
- */
9069
- function createProjectionNode(element, parent, options, transition) {
9070
- // Check for existing active node - reuse it to preserve animation state
9071
- const existingNode = activeProjectionNodes.get(element);
9072
- if (existingNode) {
9073
- const visualElement = existingNode.options.visualElement;
9074
- // Update transition options for the new animation
9075
- const nodeTransition = transition
9076
- ? { duration: transition.duration, ease: transition.ease }
9077
- : { duration: 0.3, ease: "easeOut" };
9078
- existingNode.setOptions({
9079
- ...existingNode.options,
9080
- animate: true,
9081
- transition: nodeTransition,
9082
- ...options,
9083
- });
9084
- // Re-mount the node if it was previously unmounted
9085
- // This re-adds it to root.nodes so didUpdate() will process it
9086
- if (!existingNode.instance) {
9087
- existingNode.mount(element);
9088
- }
9089
- return { node: existingNode, visualElement };
9090
- }
9091
- // No existing node - create a new one
9092
- const latestValues = {};
9093
- const visualElement = new HTMLVisualElement({
9094
- visualState: {
9095
- latestValues,
9096
- renderState: {
9097
- transformOrigin: {},
9098
- transform: {},
9099
- style: {},
9100
- vars: {},
9101
- },
9102
- },
9103
- presenceContext: null,
9104
- props: {},
9105
- });
9106
- const node = new HTMLProjectionNode(latestValues, parent);
9107
- // Convert AnimationOptions to transition format for the projection system
9108
- const nodeTransition = transition
9109
- ? { duration: transition.duration, ease: transition.ease }
9110
- : { duration: 0.3, ease: "easeOut" };
9111
- node.setOptions({
9112
- visualElement,
9113
- layout: true,
9114
- animate: true,
9115
- transition: nodeTransition,
9116
- ...options,
9117
- });
9118
- node.mount(element);
9119
- visualElement.projection = node;
9120
- // Track this node as the active one for this element
9121
- activeProjectionNodes.set(element, node);
9122
- return { node, visualElement };
9123
- }
9124
- /**
9125
- * Build a projection tree from a list of elements
9126
- */
9127
- function buildProjectionTree(elements, existingContext, options) {
9128
- ensureScaleCorrectors();
9129
- const nodes = existingContext?.nodes ?? new Map();
9130
- const visualElements = existingContext?.visualElements ?? new Map();
9131
- const group = existingContext?.group ?? nodeGroup();
9132
- const defaultTransition = options?.defaultTransition;
9133
- const sharedTransitions = options?.sharedTransitions;
9134
- // Sort elements by DOM depth (parents before children)
9135
- const sorted = [...elements].sort((a, b) => getDepth(a) - getDepth(b));
9136
- let root = existingContext?.root;
9137
- for (const element of sorted) {
9138
- // Skip if already has a node
9139
- if (nodes.has(element))
9140
- continue;
9141
- const parent = findProjectionParent(element, nodes);
9142
- const layoutId = getLayoutId(element);
9143
- const layoutMode = element.getAttribute("data-layout");
9144
- const nodeOptions = {
9145
- layoutId: layoutId ?? undefined,
9146
- animationType: parseLayoutMode(layoutMode),
9147
- };
9148
- // Use layoutId-specific transition if available, otherwise use default
9149
- const transition = layoutId && sharedTransitions?.get(layoutId)
9150
- ? sharedTransitions.get(layoutId)
9151
- : defaultTransition;
9152
- const { node, visualElement } = createProjectionNode(element, parent, nodeOptions, transition);
9153
- nodes.set(element, node);
9154
- visualElements.set(element, visualElement);
9155
- group.add(node);
9156
- if (!root) {
9157
- root = node.root;
9158
- }
9159
- }
9160
- return {
9161
- nodes,
9162
- visualElements,
9163
- group,
9164
- root: root,
9165
- };
9166
- }
9167
- /**
9168
- * Parse the data-layout attribute value
9169
- */
9170
- function parseLayoutMode(value) {
9171
- if (value === "position")
9172
- return "position";
9173
- if (value === "size")
9174
- return "size";
9175
- if (value === "preserve-aspect")
9176
- return "preserve-aspect";
9177
- return "both";
9178
- }
9179
- /**
9180
- * Clean up projection nodes for specific elements.
9181
- * If elementsToCleanup is provided, only those elements are cleaned up.
9182
- * If not provided, all nodes are cleaned up.
9183
- *
9184
- * This allows persisting elements to keep their nodes between animations,
9185
- * matching React's behavior where nodes persist for elements that remain in the DOM.
9186
- */
9187
- function cleanupProjectionTree(context, elementsToCleanup) {
9188
- const elementsToProcess = elementsToCleanup
9189
- ? [...context.nodes.entries()].filter(([el]) => elementsToCleanup.has(el))
9190
- : [...context.nodes.entries()];
9191
- for (const [element, node] of elementsToProcess) {
9192
- context.group.remove(node);
9193
- node.unmount();
9194
- // Only clear from activeProjectionNodes if this is still the active node.
9195
- // A newer animation might have already taken over.
9196
- if (activeProjectionNodes.get(element) === node) {
9197
- activeProjectionNodes.delete(element);
9198
- }
9199
- context.nodes.delete(element);
9200
- context.visualElements.delete(element);
9201
- }
9202
- }
9203
-
8995
+ const layoutSelector = "[data-layout], [data-layout-id]";
8996
+ const noop = () => { };
9204
8997
  class LayoutAnimationBuilder {
9205
8998
  constructor(scope, updateDom, defaultOptions) {
9206
8999
  this.sharedTransitions = new Map();
9207
- this.notifyReady = motionUtils.noop;
9208
- this.executed = false;
9000
+ this.notifyReady = noop;
9001
+ this.rejectReady = noop;
9209
9002
  this.scope = scope;
9210
9003
  this.updateDom = updateDom;
9211
9004
  this.defaultOptions = defaultOptions;
9212
- this.readyPromise = new Promise((resolve) => {
9005
+ this.readyPromise = new Promise((resolve, reject) => {
9213
9006
  this.notifyReady = resolve;
9007
+ this.rejectReady = reject;
9008
+ });
9009
+ microtask.read(() => {
9010
+ this.start().then(this.notifyReady).catch(this.rejectReady);
9214
9011
  });
9215
- // Queue execution on microtask to allow builder methods to be called
9216
- queueMicrotask(() => this.execute());
9217
9012
  }
9218
- shared(id, options) {
9219
- this.sharedTransitions.set(id, options);
9013
+ shared(id, transition) {
9014
+ this.sharedTransitions.set(id, transition);
9220
9015
  return this;
9221
9016
  }
9222
- then(onfulfilled, onrejected) {
9223
- return this.readyPromise.then(onfulfilled, onrejected);
9017
+ then(resolve, reject) {
9018
+ return this.readyPromise.then(resolve, reject);
9224
9019
  }
9225
- async execute() {
9226
- if (this.executed)
9227
- return;
9228
- this.executed = true;
9229
- let context;
9230
- // Phase 1: Pre-mutation - Build projection tree and take snapshots
9231
- const beforeElements = getLayoutElements(this.scope);
9232
- if (beforeElements.length > 0) {
9233
- context = buildProjectionTree(beforeElements, undefined, this.getBuildOptions());
9234
- context.root.startUpdate();
9235
- for (const node of context.nodes.values()) {
9236
- node.isLayoutDirty = false;
9237
- node.willUpdate();
9238
- }
9239
- }
9240
- // Phase 2: Execute DOM update
9241
- this.updateDom();
9242
- // Phase 3: Post-mutation - Compare before/after elements
9243
- const afterElements = getLayoutElements(this.scope);
9244
- const beforeSet = new Set(beforeElements);
9245
- const afterSet = new Set(afterElements);
9246
- const entering = afterElements.filter((el) => !beforeSet.has(el));
9247
- const exiting = beforeElements.filter((el) => !afterSet.has(el));
9248
- // Build projection nodes for entering elements
9249
- if (entering.length > 0) {
9250
- context = buildProjectionTree(entering, context, this.getBuildOptions());
9251
- }
9252
- // No layout elements - return empty animation
9253
- if (!context) {
9254
- this.notifyReady(new GroupAnimation([]));
9255
- return;
9256
- }
9257
- // Handle shared elements
9258
- for (const element of exiting) {
9259
- const node = context.nodes.get(element);
9260
- node?.getStack()?.remove(node);
9020
+ async start() {
9021
+ const beforeElements = collectLayoutElements(this.scope);
9022
+ const beforeRecords = this.buildRecords(beforeElements);
9023
+ const exitCandidates = collectExitCandidates(beforeRecords);
9024
+ beforeRecords.forEach(({ projection }) => {
9025
+ seedProjectionSnapshot(projection);
9026
+ projection.isPresent = true;
9027
+ projection.willUpdate();
9028
+ });
9029
+ await this.updateDom();
9030
+ const afterElements = collectLayoutElements(this.scope);
9031
+ const afterRecords = this.buildRecords(afterElements);
9032
+ const exitRecords = this.handleExitingElements(beforeRecords, afterRecords, exitCandidates);
9033
+ afterRecords.forEach(({ projection }) => {
9034
+ projection.isPresent = true;
9035
+ });
9036
+ const root = getProjectionRoot(afterRecords, beforeRecords);
9037
+ root?.didUpdate();
9038
+ await new Promise((resolve) => {
9039
+ frame.postRender(() => resolve());
9040
+ });
9041
+ const animations = collectAnimations(afterRecords, exitRecords);
9042
+ const animation = new GroupAnimation(animations);
9043
+ if (exitRecords.length) {
9044
+ const cleanup = () => {
9045
+ exitRecords.forEach(({ element, visualElement }) => {
9046
+ if (element.isConnected) {
9047
+ element.remove();
9048
+ }
9049
+ visualElement.unmount();
9050
+ visualElementStore.delete(element);
9051
+ });
9052
+ };
9053
+ animation.finished.then(cleanup, cleanup);
9261
9054
  }
9262
- for (const element of entering) {
9263
- context.nodes.get(element)?.promote();
9055
+ return animation;
9056
+ }
9057
+ buildRecords(elements) {
9058
+ const records = [];
9059
+ const recordMap = new Map();
9060
+ for (const element of elements) {
9061
+ const parentRecord = findParentRecord(element, recordMap, this.scope);
9062
+ const { layout, layoutId } = readLayoutAttributes(element);
9063
+ const override = layoutId
9064
+ ? this.sharedTransitions.get(layoutId)
9065
+ : undefined;
9066
+ const transition = override || this.defaultOptions;
9067
+ const record = getOrCreateRecord(element, parentRecord?.projection, {
9068
+ layout,
9069
+ layoutId,
9070
+ animationType: typeof layout === "string" ? layout : "both",
9071
+ transition: transition,
9072
+ });
9073
+ recordMap.set(element, record);
9074
+ records.push(record);
9264
9075
  }
9265
- // Phase 4: Animate
9266
- context.root.didUpdate();
9267
- await new Promise((resolve) => frame.postRender(() => resolve()));
9268
- const animations = [];
9269
- for (const node of context.nodes.values()) {
9270
- if (node.currentAnimation) {
9271
- animations.push(node.currentAnimation);
9272
- }
9273
- }
9274
- const groupAnimation = new GroupAnimation(animations);
9275
- groupAnimation.finished.then(() => {
9276
- // Only clean up nodes for elements no longer in the document.
9277
- // Elements still in DOM keep their nodes so subsequent animations
9278
- // can use the stored position snapshots (A→B→A pattern).
9279
- const elementsToCleanup = new Set();
9280
- for (const element of context.nodes.keys()) {
9281
- if (!document.contains(element)) {
9282
- elementsToCleanup.add(element);
9283
- }
9076
+ return records;
9077
+ }
9078
+ handleExitingElements(beforeRecords, afterRecords, exitCandidates) {
9079
+ const afterElementsSet = new Set(afterRecords.map((record) => record.element));
9080
+ const exiting = [];
9081
+ beforeRecords.forEach((record) => {
9082
+ if (afterElementsSet.has(record.element))
9083
+ return;
9084
+ const exitRecord = exitCandidates.get(record.element);
9085
+ if (!exitRecord) {
9086
+ record.visualElement.unmount();
9087
+ visualElementStore.delete(record.element);
9088
+ return;
9089
+ }
9090
+ if (!exitRecord.element.isConnected) {
9091
+ reinstateExitElement(exitRecord);
9284
9092
  }
9285
- cleanupProjectionTree(context, elementsToCleanup);
9093
+ record.projection.isPresent = false;
9094
+ if (record.projection.options.layoutId) {
9095
+ record.projection.relegate();
9096
+ }
9097
+ exiting.push(record);
9286
9098
  });
9287
- this.notifyReady(groupAnimation);
9288
- }
9289
- getBuildOptions() {
9290
- return {
9291
- defaultTransition: this.defaultOptions || {
9292
- duration: 0.3,
9293
- ease: "easeOut",
9294
- },
9295
- sharedTransitions: this.sharedTransitions.size > 0
9296
- ? this.sharedTransitions
9297
- : undefined,
9298
- };
9099
+ return exiting;
9299
9100
  }
9300
9101
  }
9301
- /**
9302
- * Parse arguments for animateLayout overloads
9303
- */
9304
9102
  function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
9305
9103
  // animateLayout(updateDom)
9306
9104
  if (typeof scopeOrUpdateDom === "function") {
@@ -9314,11 +9112,135 @@ function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
9314
9112
  const elements = resolveElements(scopeOrUpdateDom);
9315
9113
  const scope = elements[0] || document;
9316
9114
  return {
9317
- scope: scope instanceof Document ? scope : scope,
9115
+ scope,
9318
9116
  updateDom: updateDomOrOptions,
9319
9117
  defaultOptions: options,
9320
9118
  };
9321
9119
  }
9120
+ function collectLayoutElements(scope) {
9121
+ const elements = Array.from(scope.querySelectorAll(layoutSelector));
9122
+ if (scope instanceof Element && scope.matches(layoutSelector)) {
9123
+ if (!elements.includes(scope)) {
9124
+ elements.unshift(scope);
9125
+ }
9126
+ }
9127
+ return elements;
9128
+ }
9129
+ function readLayoutAttributes(element) {
9130
+ const layoutId = element.getAttribute("data-layout-id") || undefined;
9131
+ const rawLayout = element.getAttribute("data-layout");
9132
+ let layout;
9133
+ if (rawLayout === "" || rawLayout === "true") {
9134
+ layout = true;
9135
+ }
9136
+ else if (rawLayout) {
9137
+ layout = rawLayout;
9138
+ }
9139
+ return {
9140
+ layout,
9141
+ layoutId,
9142
+ layoutExit: element.hasAttribute("data-layout-exit"),
9143
+ };
9144
+ }
9145
+ function createVisualState() {
9146
+ return {
9147
+ latestValues: {},
9148
+ renderState: {
9149
+ transform: {},
9150
+ transformOrigin: {},
9151
+ style: {},
9152
+ vars: {},
9153
+ },
9154
+ };
9155
+ }
9156
+ function seedProjectionSnapshot(projection) {
9157
+ const projectionNode = projection;
9158
+ if (!projectionNode.currentAnimation && !projectionNode.pendingAnimation) {
9159
+ return;
9160
+ }
9161
+ const snapshot = projection.measure(false);
9162
+ snapshot.latestValues =
9163
+ projectionNode.animationValues || projection.latestValues;
9164
+ projection.snapshot = snapshot;
9165
+ }
9166
+ function getOrCreateRecord(element, parentProjection, projectionOptions) {
9167
+ const existing = visualElementStore.get(element);
9168
+ const visualElement = existing ??
9169
+ new HTMLVisualElement({
9170
+ props: {},
9171
+ presenceContext: null,
9172
+ visualState: createVisualState(),
9173
+ }, { allowProjection: true });
9174
+ if (!existing || !visualElement.projection) {
9175
+ visualElement.projection = new HTMLProjectionNode(visualElement.latestValues, parentProjection);
9176
+ }
9177
+ visualElement.projection.setOptions({
9178
+ ...projectionOptions,
9179
+ visualElement,
9180
+ });
9181
+ if (!visualElement.current) {
9182
+ visualElement.mount(element);
9183
+ }
9184
+ if (!existing) {
9185
+ visualElementStore.set(element, visualElement);
9186
+ }
9187
+ return {
9188
+ element,
9189
+ visualElement,
9190
+ projection: visualElement.projection,
9191
+ };
9192
+ }
9193
+ function findParentRecord(element, recordMap, scope) {
9194
+ let parent = element.parentElement;
9195
+ while (parent) {
9196
+ const record = recordMap.get(parent);
9197
+ if (record)
9198
+ return record;
9199
+ if (parent === scope)
9200
+ break;
9201
+ parent = parent.parentElement;
9202
+ }
9203
+ return undefined;
9204
+ }
9205
+ function collectExitCandidates(records) {
9206
+ const exitCandidates = new Map();
9207
+ records.forEach((record) => {
9208
+ const { layoutExit } = readLayoutAttributes(record.element);
9209
+ if (!layoutExit)
9210
+ return;
9211
+ exitCandidates.set(record.element, {
9212
+ ...record,
9213
+ parent: record.element.parentNode,
9214
+ nextSibling: record.element.nextSibling,
9215
+ });
9216
+ });
9217
+ return exitCandidates;
9218
+ }
9219
+ function reinstateExitElement(record) {
9220
+ if (!record.parent)
9221
+ return;
9222
+ if (record.nextSibling && record.nextSibling.parentNode === record.parent) {
9223
+ record.parent.insertBefore(record.element, record.nextSibling);
9224
+ }
9225
+ else {
9226
+ record.parent.appendChild(record.element);
9227
+ }
9228
+ }
9229
+ function getProjectionRoot(afterRecords, beforeRecords) {
9230
+ const record = afterRecords[0] || beforeRecords[0];
9231
+ return record?.projection.root;
9232
+ }
9233
+ function collectAnimations(afterRecords, exitRecords) {
9234
+ const animations = new Set();
9235
+ const addAnimation = (record) => {
9236
+ const animation = record.projection.currentAnimation;
9237
+ if (animation)
9238
+ animations.add(animation);
9239
+ };
9240
+ afterRecords.forEach(addAnimation);
9241
+ exitRecords.forEach(addAnimation);
9242
+ return Array.from(animations);
9243
+ }
9322
9244
 
9323
9245
  /**
9324
9246
  * @deprecated
@@ -9429,7 +9351,7 @@ exports.createAxisDelta = createAxisDelta;
9429
9351
  exports.createBox = createBox;
9430
9352
  exports.createDelta = createDelta;
9431
9353
  exports.createGeneratorEasing = createGeneratorEasing;
9432
- exports.createProjectionNode = createProjectionNode$1;
9354
+ exports.createProjectionNode = createProjectionNode;
9433
9355
  exports.createRenderBatcher = createRenderBatcher;
9434
9356
  exports.cubicBezierAsString = cubicBezierAsString;
9435
9357
  exports.defaultEasing = defaultEasing;