motion-dom 12.41.0 → 12.42.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
@@ -5334,6 +5334,29 @@ function tagClass(element, className, classed) {
5334
5334
  element.style?.setProperty("view-transition-class", className);
5335
5335
  classed.push(element);
5336
5336
  }
5337
+ /**
5338
+ * Set the element's `view-transition-group` so its layer reconstructs the DOM
5339
+ * hierarchy in the pseudo-tree (`contain`) - or stays flat (`none`). Tracked in
5340
+ * `grouped` for cleanup. When the element clips (any non-`visible` overflow) its
5341
+ * name is recorded in `clipChildren` so the caller can clip the nested children
5342
+ * (`::view-transition-group-children(name)`), mirroring the live clip through
5343
+ * the whole transition rather than only at the live-DOM handoff.
5344
+ *
5345
+ * Ignored by browsers without nested view-transition groups, where it harmlessly
5346
+ * degrades to the flat default.
5347
+ */
5348
+ function applyGroup(element, name, group, grouped, clipChildren) {
5349
+ if (!group)
5350
+ return;
5351
+ element.style?.setProperty("view-transition-group", group);
5352
+ grouped.push(element);
5353
+ if (group !== "none" && clipChildren) {
5354
+ const style = getComputedStyle(element);
5355
+ if (style.overflowX !== "visible" || style.overflowY !== "visible") {
5356
+ clipChildren.add(name);
5357
+ }
5358
+ }
5359
+ }
5337
5360
  /**
5338
5361
  * Resolve a selector/Element to elements and ensure each one carries a
5339
5362
  * `view-transition-name` we can target from script.
@@ -5347,7 +5370,7 @@ function tagClass(element, className, classed) {
5347
5370
  * across both captures (before and after the update), which is what allows a
5348
5371
  * persistent element to animate as a single `group` layer.
5349
5372
  */
5350
- function assignViewTransitionNames(definition, registry, assigned, forcedNames, className, classed = []) {
5373
+ function assignViewTransitionNames(definition, registry, assigned, forcedNames, className, classed = [], group, grouped = [], clipChildren) {
5351
5374
  const elements = resolveElements(definition);
5352
5375
  /**
5353
5376
  * The new end of a paired morph: give each element the matching name from
@@ -5368,6 +5391,7 @@ function assignViewTransitionNames(definition, registry, assigned, forcedNames,
5368
5391
  assigned.push(element);
5369
5392
  registry.set(element, name);
5370
5393
  tagClass(element, className, classed);
5394
+ applyGroup(element, name, group, grouped, clipChildren);
5371
5395
  return name;
5372
5396
  });
5373
5397
  }
@@ -5408,6 +5432,7 @@ function assignViewTransitionNames(definition, registry, assigned, forcedNames,
5408
5432
  }
5409
5433
  registry.set(element, name);
5410
5434
  tagClass(element, className, classed);
5435
+ applyGroup(element, name, group, grouped, clipChildren);
5411
5436
  return name;
5412
5437
  });
5413
5438
  }
@@ -5417,13 +5442,16 @@ function assignViewTransitionNames(definition, registry, assigned, forcedNames,
5417
5442
  * (they're not in `assigned`). Safe to call more than once (e.g. on both a
5418
5443
  * finished and an interrupted transition).
5419
5444
  */
5420
- function releaseViewTransitionNames(assigned, classed = []) {
5445
+ function releaseViewTransitionNames(assigned, classed = [], grouped = []) {
5421
5446
  for (const element of assigned) {
5422
5447
  element.style?.removeProperty("view-transition-name");
5423
5448
  }
5424
5449
  for (const element of classed) {
5425
5450
  element.style?.removeProperty("view-transition-class");
5426
5451
  }
5452
+ for (const element of grouped) {
5453
+ element.style?.removeProperty("view-transition-group");
5454
+ }
5427
5455
  }
5428
5456
 
5429
5457
  function chooseLayerType(valueName) {
@@ -5508,14 +5536,14 @@ const ORIGIN_DEFAULTS = {
5508
5536
  new: { opacity: 0, scale: 0.85 },
5509
5537
  old: { opacity: 1, scale: 1 },
5510
5538
  };
5511
- const cornerProps = [
5512
- "borderTopLeftRadius",
5513
- "borderTopRightRadius",
5514
- "borderBottomRightRadius",
5515
- "borderBottomLeftRadius",
5516
- ];
5539
+ /**
5540
+ * How much two box aspect ratios must differ before a morph is treated as
5541
+ * aspect-changing (and so worth cropping). Matches the projection engine's
5542
+ * `preserve-aspect` threshold, so small layout jitter doesn't trigger a crop.
5543
+ */
5544
+ const ASPECT_TOLERANCE = 0.2;
5517
5545
  function startViewAnimation(builder) {
5518
- const { update, targets, resolveDefs, noCrop, pairs, classNames, options: defaultOptions, } = builder;
5546
+ const { update, targets, resolveDefs, cropOverride, pairs, classNames, flatGroups, options: defaultOptions, } = builder;
5519
5547
  if (!document.startViewTransition) {
5520
5548
  // An async IIFE (not `new Promise(async …)`) so a throwing/rejecting
5521
5549
  // update rejects this promise rather than leaving it unsettled.
@@ -5539,8 +5567,21 @@ function startViewAnimation(builder) {
5539
5567
  * ever stripping an author's own inline `view-transition-name`.
5540
5568
  */
5541
5569
  const classed = [];
5570
+ /**
5571
+ * Elements we set a `view-transition-group` on (for nesting), tracked for
5572
+ * cleanup. `clipChildren` collects the names of nested parents that clip in
5573
+ * the live layout, so their `::view-transition-group-children` is clipped
5574
+ * through the transition - not just once the live DOM takes back over.
5575
+ */
5576
+ const grouped = [];
5577
+ const clipChildren = new Set();
5542
5578
  const layerTargets = new Map();
5543
5579
  const croppedNames = new Set();
5580
+ /**
5581
+ * Each layer's explicit `.crop(true | false)` override (by resolved name),
5582
+ * so `finalizeCrop` can let an author's choice win over the morph default.
5583
+ */
5584
+ const cropForName = new Map();
5544
5585
  /**
5545
5586
  * Each layer's stagger position (index + total) within its subject, per
5546
5587
  * snapshot. Resolving against the snapshot the layer belongs to keeps
@@ -5562,6 +5603,18 @@ function startViewAnimation(builder) {
5562
5603
  const resolveLayers = (phase) => {
5563
5604
  targets.forEach((target, definition) => {
5564
5605
  const className = classNames.get(definition);
5606
+ /**
5607
+ * Nest each resolved layer under its DOM-ancestor layer by default
5608
+ * (`contain`), so an ancestor's clip/transform/opacity reach it
5609
+ * through the transition; `.group(false)` opts a subject out (`none`)
5610
+ * to stay flat and escape. Skipped for root / pre-named layers, which
5611
+ * aren't elements we resolve and style.
5612
+ */
5613
+ const group = definition === "root" || !resolveDefs.has(definition)
5614
+ ? undefined
5615
+ : flatGroups.has(definition)
5616
+ ? "none"
5617
+ : "contain";
5565
5618
  let names;
5566
5619
  if (definition === "root" || !resolveDefs.has(definition)) {
5567
5620
  names = [definition];
@@ -5574,7 +5627,7 @@ function startViewAnimation(builder) {
5574
5627
  */
5575
5628
  if (phase === "old") {
5576
5629
  pairFrom.set(definition, resolveElements(definition));
5577
- names = assignViewTransitionNames(definition, nameRegistry, assigned, undefined, className, classed);
5630
+ names = assignViewTransitionNames(definition, nameRegistry, assigned, undefined, className, classed, group, grouped, clipChildren);
5578
5631
  pairNames.set(definition, names);
5579
5632
  }
5580
5633
  else {
@@ -5594,13 +5647,16 @@ function startViewAnimation(builder) {
5594
5647
  */
5595
5648
  nameRegistry.delete(el);
5596
5649
  }
5597
- names = assignViewTransitionNames(pairs.get(definition), nameRegistry, assigned, pairNames.get(definition), className, classed);
5650
+ names = assignViewTransitionNames(pairs.get(definition), nameRegistry, assigned, pairNames.get(definition), className, classed, group, grouped, clipChildren);
5598
5651
  }
5599
5652
  }
5600
5653
  else {
5601
- names = assignViewTransitionNames(definition, nameRegistry, assigned, undefined, className, classed);
5654
+ names = assignViewTransitionNames(definition, nameRegistry, assigned, undefined, className, classed, group, grouped, clipChildren);
5602
5655
  }
5603
- const cropped = definition !== "root" && !noCrop.has(definition);
5656
+ // Record any explicit `.crop(true | false)` per resolved name; the
5657
+ // crop set itself is computed later by `finalizeCrop` (it needs both
5658
+ // snapshots to know which morphs change aspect ratio).
5659
+ const override = cropOverride.get(definition);
5604
5660
  names.forEach((name, index) => {
5605
5661
  /**
5606
5662
  * If two subjects resolve to the same element, merge their
@@ -5610,8 +5666,8 @@ function startViewAnimation(builder) {
5610
5666
  layerTargets.set(name, existing && existing !== target
5611
5667
  ? { ...existing, ...target }
5612
5668
  : target);
5613
- if (cropped)
5614
- croppedNames.add(name);
5669
+ if (override !== undefined)
5670
+ cropForName.set(name, override);
5615
5671
  const stagger = layerStagger.get(name) ?? {};
5616
5672
  stagger[phase] = [index, names.length];
5617
5673
  layerStagger.set(name, stagger);
@@ -5646,44 +5702,62 @@ function startViewAnimation(builder) {
5646
5702
  return transition;
5647
5703
  };
5648
5704
  /**
5649
- * Measured corner radii per cropped layer, so the clip can animate each
5650
- * corner between the old and new elements. Per-corner (rather than the
5651
- * shorthand) so mismatched/individual radii interpolate cleanly.
5705
+ * Each layer's box size per snapshot, so `finalizeCrop` can tell whether a
5706
+ * morph's aspect ratio changed (the only case worth cropping).
5652
5707
  */
5653
- const cropRadii = new Map();
5654
- const recordRadii = (style, name, phase) => {
5655
- const corners = {};
5656
- for (const corner of cornerProps)
5657
- corners[corner] = style[corner];
5658
- const entry = cropRadii.get(name) ?? {};
5659
- entry[phase] = corners;
5660
- cropRadii.set(name, entry);
5661
- };
5708
+ const cropBox = new Map();
5709
+ const measureLayers = (phase) => nameRegistry.forEach((name, element) => {
5710
+ const rect = element.getBoundingClientRect?.();
5711
+ if (rect && rect.height) {
5712
+ const entry = cropBox.get(name) ?? {};
5713
+ entry[phase] = { width: rect.width, height: rect.height };
5714
+ cropBox.set(name, entry);
5715
+ }
5716
+ });
5662
5717
  /**
5663
- * Cropped layers all come from `.add()`, so their elements are in the
5664
- * registry - read each one's corner radii directly. For a paired morph both
5665
- * ends share a name; the new-snapshot element is registered last, so it
5666
- * wins the `new` reading (and the old end the `old` reading).
5718
+ * With both snapshots measured, settle which layers crop. The default crops
5719
+ * only a morph whose aspect ratio *changes* between snapshots - the one case
5720
+ * where `object-fit: cover` does real work. A same-aspect morph or a
5721
+ * fade-only layer is left uncropped: its corners scale naturally (no flash
5722
+ * from squaring, no `overflow: clip` eating its shadow) and a backdrop can't
5723
+ * be clipped to nothing. An explicit `.crop(true | false)` overrides either
5724
+ * way. Runs after both snapshots are measured, since aspect needs both.
5667
5725
  */
5668
- const measureCrop = (phase) => {
5669
- if (!croppedNames.size)
5670
- return;
5671
- nameRegistry.forEach((name, element) => {
5672
- if (croppedNames.has(name)) {
5673
- recordRadii(getComputedStyle(element), name, phase);
5726
+ const finalizeCrop = () => {
5727
+ croppedNames.clear();
5728
+ for (const name of layerStagger.keys()) {
5729
+ if (name === "root")
5730
+ continue;
5731
+ // An explicit `.crop(true | false)` wins; otherwise crop a morph
5732
+ // whose aspect ratio changed.
5733
+ if (cropForName.get(name) ?? aspectChanged(name)) {
5734
+ croppedNames.add(name);
5674
5735
  }
5675
- });
5736
+ }
5737
+ };
5738
+ /**
5739
+ * Whether a layer is a morph whose box aspect ratio changed between
5740
+ * snapshots (beyond a small tolerance). Fade-only layers (one snapshot) are
5741
+ * never "changed".
5742
+ */
5743
+ const aspectChanged = (name) => {
5744
+ const box = cropBox.get(name);
5745
+ if (!box?.old || !box?.new || !box.old.height || !box.new.height) {
5746
+ return false;
5747
+ }
5748
+ return (Math.abs(box.old.width / box.old.height -
5749
+ box.new.width / box.new.height) > ASPECT_TOLERANCE);
5676
5750
  };
5677
5751
  /**
5678
5752
  * Write the persistent view-transition CSS: suppress root capture when the
5679
5753
  * root has no animations of its own; force linear timing (baked into the
5680
5754
  * keyframes, so we can retime later via updateTiming); and clip +
5681
5755
  * object-fit: cover every cropped morph (the UA default overflows on
5682
- * aspect-ratio change), with an animated border-radius added below.
5756
+ * aspect-ratio change).
5683
5757
  *
5684
5758
  * `css.commit` replaces rather than appends, so we re-set the full rule set
5685
- * each call - the second call (in the update callback) then picks up cropped
5686
- * layers that only exist in the new snapshot.
5759
+ * each call - the crop rules are only known after `finalizeCrop` runs in the
5760
+ * update callback, so the second call writes them.
5687
5761
  */
5688
5762
  const commitViewCSS = () => {
5689
5763
  if (!hasTarget("root", targets)) {
@@ -5694,33 +5768,49 @@ function startViewAnimation(builder) {
5694
5768
  css.set(`::view-transition-group(${name})`, { overflow: "clip" });
5695
5769
  css.set(`::view-transition-old(${name}), ::view-transition-new(${name})`, { width: "100%", height: "100%", "object-fit": "cover" });
5696
5770
  });
5771
+ /**
5772
+ * Clip the nested children of any layer that clips in the live layout,
5773
+ * so a wrapper crops its child for the whole morph (mirroring the DOM)
5774
+ * rather than only at the live-DOM handoff. No-op on browsers without
5775
+ * nested view-transition groups.
5776
+ */
5777
+ clipChildren.forEach((name) => {
5778
+ css.set(`::view-transition-group-children(${name})`, {
5779
+ overflow: "clip",
5780
+ });
5781
+ });
5697
5782
  css.commit(); // Write
5698
5783
  };
5699
5784
  const cleanup = () => {
5700
- releaseViewTransitionNames(assigned, classed);
5785
+ releaseViewTransitionNames(assigned, classed, grouped);
5701
5786
  css.remove(); // Write
5702
5787
  };
5703
5788
  const callback = async () => {
5704
5789
  await update();
5705
5790
  /**
5706
5791
  * Re-resolve so elements created by the update are named for the new
5707
- * snapshot, then measure the cropped layers' new border-radius.
5792
+ * snapshot, then measure them. With both snapshots measured we can
5793
+ * settle the crop set (aspect-changing morphs + forced).
5708
5794
  */
5709
- const croppedBefore = croppedNames.size;
5710
5795
  resolveLayers("new");
5711
- measureCrop("new");
5796
+ measureLayers("new");
5797
+ finalizeCrop();
5712
5798
  /**
5713
- * Re-commit the crop CSS only if the new snapshot introduced cropped
5714
- * layers, so a layer that exists only in the new snapshot is clipped
5715
- * too - without forcing a redundant style write on the common path.
5799
+ * Re-commit the crop CSS unconditionally: `finalizeCrop` is computed
5800
+ * here (after both snapshots are measured), so the clip rules must be
5801
+ * (re)written to match the settled set.
5716
5802
  */
5717
- if (croppedNames.size > croppedBefore)
5718
- commitViewCSS();
5803
+ commitViewCSS();
5719
5804
  };
5720
5805
  let transition;
5721
5806
  try {
5722
5807
  resolveLayers("old");
5723
- measureCrop("old");
5808
+ /**
5809
+ * Measure the old snapshot against the optimistic crop set (the new
5810
+ * snapshot doesn't exist yet, so aspect change can't be known here;
5811
+ * `finalizeCrop` settles it post-update).
5812
+ */
5813
+ measureLayers("old");
5724
5814
  commitViewCSS();
5725
5815
  transition = document.startViewTransition(callback);
5726
5816
  }
@@ -5949,38 +6039,6 @@ function startViewAnimation(builder) {
5949
6039
  });
5950
6040
  animations.push(new NativeAnimationWrapper(animation));
5951
6041
  }
5952
- /**
5953
- * Animate each cropped layer's clip corners between the old and
5954
- * new elements, so a cropped morph keeps rounded corners
5955
- * (handling individual per-corner radii).
5956
- */
5957
- cropRadii.forEach((radii, name) => {
5958
- if (!radii.old && !radii.new)
5959
- return;
5960
- const target = layerTargets.get(name);
5961
- const [index, total] = staggerPosition(name, "group");
5962
- const radiusOptions = resolveLayerTransition(target, "group", "layout", index === -1 ? 0 : index, total);
5963
- radiusOptions.duration && (radiusOptions.duration = motionUtils.secondsToMilliseconds(radiusOptions.duration));
5964
- radiusOptions.delay && (radiusOptions.delay = motionUtils.secondsToMilliseconds(radiusOptions.delay));
5965
- for (const corner of cornerProps) {
5966
- // `||` (not `??`) so an empty measurement (e.g. an
5967
- // un-rendered element) falls back rather than producing
5968
- // an invalid keyframe.
5969
- const from = radii.old?.[corner] || radii.new?.[corner] || "0px";
5970
- const to = radii.new?.[corner] || radii.old?.[corner] || "0px";
5971
- // Skip square corners - nothing to round.
5972
- if (parseFloat(from) === 0 && parseFloat(to) === 0) {
5973
- continue;
5974
- }
5975
- animations.push(new NativeAnimation({
5976
- ...radiusOptions,
5977
- element: document.documentElement,
5978
- name: corner,
5979
- pseudoElement: `::view-transition-group(${name})`,
5980
- keyframes: [from, to],
5981
- }));
5982
- }
5983
- });
5984
6042
  resolve(new GroupAnimation(animations));
5985
6043
  })
5986
6044
  .catch(() =>
@@ -6090,10 +6148,12 @@ class ViewTransitionBuilder {
6090
6148
  */
6091
6149
  this.resolveDefs = new Set();
6092
6150
  /**
6093
- * Subjects opted out of the default crop (clip + object-fit: cover +
6094
- * animated corner radii) via `.crop(false)`.
6151
+ * Per-subject crop override: `true` forces the crop (clip + object-fit:
6152
+ * cover + animated corner radii) on, `false` forces it off. A subject with
6153
+ * no entry uses the default - crop only a genuine morph (a layer present in
6154
+ * both snapshots), so a fade-only enter/exit isn't clipped to nothing.
6095
6155
  */
6096
- this.noCrop = new Set();
6156
+ this.cropOverride = new Map();
6097
6157
  /**
6098
6158
  * Subjects paired with a different new-snapshot target (the second `.add()`
6099
6159
  * argument), so two distinct elements share one name and morph into each
@@ -6106,6 +6166,14 @@ class ViewTransitionBuilder {
6106
6166
  * the opaque generated name.
6107
6167
  */
6108
6168
  this.classNames = new Map();
6169
+ /**
6170
+ * Subjects opted out of automatic group nesting via `.group(false)`. Their
6171
+ * layer stays a flat top-level group (`view-transition-group: none`) instead
6172
+ * of nesting under its DOM-ancestor layer - so it animates independently and
6173
+ * escapes an ancestor's clip/transform (e.g. an element that lifts out of a
6174
+ * card and flies across, which nesting would clip to the card).
6175
+ */
6176
+ this.flatGroups = new Set();
6109
6177
  this.notifyReady = motionUtils.noop;
6110
6178
  this.notifyReject = motionUtils.noop;
6111
6179
  this.readyPromise = new Promise((resolve, reject) => {
@@ -6143,14 +6211,37 @@ class ViewTransitionBuilder {
6143
6211
  return this;
6144
6212
  }
6145
6213
  /**
6146
- * Morphs are clipped + `object-fit: cover` (and their corners animate)
6147
- * by default. Call `.crop(false)` to opt this subject out and fall back
6148
- * to the browser default (which overflows on aspect-ratio change).
6214
+ * Control this subject's crop (clip + `object-fit: cover` + animated
6215
+ * corners). By default a subject auto-crops only when it actually morphs -
6216
+ * present in both snapshots (a survivor, or an `.add(a, b)` pair). A
6217
+ * fade-only enter/exit has no second box to crop against, so it's left to
6218
+ * the browser default; in particular the `overflow: clip` a crop adds would
6219
+ * otherwise clip a mis-sized enter/exit layer to nothing.
6220
+ *
6221
+ * `.crop(false)` forces the crop off (e.g. a text morph, where
6222
+ * `object-fit: cover` clips glyphs as the box grows); `.crop(true)` forces
6223
+ * it on for a non-morph the default wouldn't otherwise crop.
6149
6224
  */
6150
6225
  crop(enabled = true) {
6226
+ this.cropOverride.set(this.currentSubject, enabled);
6227
+ return this;
6228
+ }
6229
+ /**
6230
+ * By default a subject's layer nests under its nearest DOM-ancestor layer
6231
+ * (`view-transition-group: contain`), so the ancestor's clip/transform/opacity
6232
+ * apply to it through the transition - mirroring how the DOM actually paints,
6233
+ * and letting a wrapper crop its child for the whole morph rather than only
6234
+ * once the live DOM takes back over. (Needs a browser that supports nested
6235
+ * view-transition groups; elsewhere it degrades to the flat default.)
6236
+ *
6237
+ * Call `.group(false)` to opt out: the layer stays flat and top-level, so it
6238
+ * animates independently and escapes an ancestor's clip - e.g. an element
6239
+ * that should lift out of a card and fly across, which nesting would clip.
6240
+ */
6241
+ group(enabled = true) {
6151
6242
  enabled
6152
- ? this.noCrop.delete(this.currentSubject)
6153
- : this.noCrop.add(this.currentSubject);
6243
+ ? this.flatGroups.delete(this.currentSubject)
6244
+ : this.flatGroups.add(this.currentSubject);
6154
6245
  return this;
6155
6246
  }
6156
6247
  /**