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