motion-dom 12.40.0 → 12.41.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.
Files changed (45) hide show
  1. package/dist/cjs/index.js +1023 -458
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/JSAnimation.mjs +0 -3
  4. package/dist/es/animation/JSAnimation.mjs.map +1 -1
  5. package/dist/es/animation/waapi/start-waapi-animation.mjs +1 -12
  6. package/dist/es/animation/waapi/start-waapi-animation.mjs.map +1 -1
  7. package/dist/es/events/add-dom-event.mjs +1 -1
  8. package/dist/es/events/add-dom-event.mjs.map +1 -1
  9. package/dist/es/frameloop/batcher.mjs +1 -1
  10. package/dist/es/frameloop/batcher.mjs.map +1 -1
  11. package/dist/es/frameloop/render-step.mjs +1 -12
  12. package/dist/es/frameloop/render-step.mjs.map +1 -1
  13. package/dist/es/gestures/press/index.mjs +11 -4
  14. package/dist/es/gestures/press/index.mjs.map +1 -1
  15. package/dist/es/index.mjs +0 -1
  16. package/dist/es/index.mjs.map +1 -1
  17. package/dist/es/layout/LayoutAnimationBuilder.mjs +299 -214
  18. package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
  19. package/dist/es/projection/node/create-projection-node.mjs +0 -6
  20. package/dist/es/projection/node/create-projection-node.mjs.map +1 -1
  21. package/dist/es/stats/buffer.mjs.map +1 -1
  22. package/dist/es/stats/index.mjs +4 -93
  23. package/dist/es/stats/index.mjs.map +1 -1
  24. package/dist/es/view/index.mjs +90 -13
  25. package/dist/es/view/index.mjs.map +1 -1
  26. package/dist/es/view/queue.mjs +9 -3
  27. package/dist/es/view/queue.mjs.map +1 -1
  28. package/dist/es/view/start.mjs +484 -83
  29. package/dist/es/view/start.mjs.map +1 -1
  30. package/dist/es/view/utils/assign-names.mjs +118 -0
  31. package/dist/es/view/utils/assign-names.mjs.map +1 -0
  32. package/dist/es/view/utils/choose-layer-type.mjs +1 -3
  33. package/dist/es/view/utils/choose-layer-type.mjs.map +1 -1
  34. package/dist/es/view/utils/get-layer-info.mjs +3 -1
  35. package/dist/es/view/utils/get-layer-info.mjs.map +1 -1
  36. package/dist/es/view/utils/get-view-animations.mjs +6 -8
  37. package/dist/es/view/utils/get-view-animations.mjs.map +1 -1
  38. package/dist/index.d.ts +105 -52
  39. package/dist/motion-dom.dev.js +1023 -458
  40. package/dist/motion-dom.js +1 -1
  41. package/dist/size-rollup-motion-value.js.map +1 -1
  42. package/dist/size-rollup-style-effect.js.map +1 -1
  43. package/package.json +2 -2
  44. package/dist/es/stats/animation-count.mjs +0 -8
  45. package/dist/es/stats/animation-count.mjs.map +0 -1
@@ -1,10 +1,12 @@
1
- import { secondsToMilliseconds } from 'motion-utils';
1
+ import { warnOnce, secondsToMilliseconds } from 'motion-utils';
2
2
  import { GroupAnimation } from '../animation/GroupAnimation.mjs';
3
3
  import { NativeAnimation } from '../animation/NativeAnimation.mjs';
4
4
  import { NativeAnimationWrapper } from '../animation/NativeAnimationWrapper.mjs';
5
5
  import { getValueTransition } from '../animation/utils/get-value-transition.mjs';
6
6
  import { mapEasingToNativeEasing } from '../animation/waapi/easing/map-easing.mjs';
7
7
  import { applyGeneratorOptions } from '../animation/waapi/utils/apply-generator.mjs';
8
+ import { resolveElements } from '../utils/resolve-elements.mjs';
9
+ import { assignViewTransitionNames, releaseViewTransitionNames } from './utils/assign-names.mjs';
8
10
  import { chooseLayerType } from './utils/choose-layer-type.mjs';
9
11
  import { css } from './utils/css.mjs';
10
12
  import { getViewAnimationLayerInfo } from './utils/get-layer-info.mjs';
@@ -12,87 +14,370 @@ import { getViewAnimations } from './utils/get-view-animations.mjs';
12
14
  import { hasTarget } from './utils/has-target.mjs';
13
15
 
14
16
  const definitionNames = ["layout", "enter", "exit", "new", "old"];
17
+ /**
18
+ * The `ViewTransitionTarget` buckets driving each generated layer type, in
19
+ * priority order - the inverse of `chooseLayerType`. The new view is driven by
20
+ * `new`/`enter`, the old by `old`/`exit`. `group-children`/`image-pair` have no
21
+ * bucket; they follow the default layout timing.
22
+ */
23
+ const typeBuckets = {
24
+ group: ["layout"],
25
+ new: ["new", "enter"],
26
+ old: ["old", "exit"],
27
+ };
28
+ /**
29
+ * Default "absent" origin for a single-value keyframe, by pseudo type, so e.g.
30
+ * `enter({ scale: 1 })` grows in from 0.85 and `exit({ opacity: 0 })` fades
31
+ * from 1. `enter` prefers the matching `exit` value over these (see below).
32
+ */
33
+ const ORIGIN_DEFAULTS = {
34
+ new: { opacity: 0, scale: 0.85 },
35
+ old: { opacity: 1, scale: 1 },
36
+ };
37
+ const cornerProps = [
38
+ "borderTopLeftRadius",
39
+ "borderTopRightRadius",
40
+ "borderBottomRightRadius",
41
+ "borderBottomLeftRadius",
42
+ ];
15
43
  function startViewAnimation(builder) {
16
- const { update, targets, options: defaultOptions } = builder;
44
+ const { update, targets, resolveDefs, noCrop, pairs, classNames, options: defaultOptions, } = builder;
17
45
  if (!document.startViewTransition) {
18
- return new Promise(async (resolve) => {
46
+ // An async IIFE (not `new Promise(async …)`) so a throwing/rejecting
47
+ // update rejects this promise rather than leaving it unsettled.
48
+ return (async () => {
19
49
  await update();
20
- resolve(new GroupAnimation([]));
21
- });
50
+ return new GroupAnimation([]);
51
+ })();
22
52
  }
23
- // TODO: Go over existing targets and ensure they all have ids
24
53
  /**
25
- * If we don't have any animations defined for the root target,
26
- * remove it from being captured.
54
+ * Resolve any selector/Element targets to layer names, assigning a
55
+ * `view-transition-name` to each element as we go. We run this before the
56
+ * update (so the elements are captured in the old snapshot) and again
57
+ * after it (for the new snapshot). An element present in both keeps the
58
+ * same name and animates as a single `group` layer.
59
+ */
60
+ const nameRegistry = new Map();
61
+ const assigned = [];
62
+ /**
63
+ * Elements we tagged with a `view-transition-class` (via `.class()`),
64
+ * tracked separately from `assigned` so cleanup removes the class without
65
+ * ever stripping an author's own inline `view-transition-name`.
66
+ */
67
+ const classed = [];
68
+ const layerTargets = new Map();
69
+ const croppedNames = new Set();
70
+ /**
71
+ * Each layer's stagger position (index + total) within its subject, per
72
+ * snapshot. Resolving against the snapshot the layer belongs to keeps
73
+ * stagger correct when `update()` replaces the matched elements, and lets
74
+ * us skip a layer that's absent from a snapshot (e.g. an exited element
75
+ * has no `new` pseudo-element).
76
+ */
77
+ const layerStagger = new Map();
78
+ /**
79
+ * Names allocated for a paired subject in the old snapshot, replayed onto
80
+ * its new-snapshot target so both ends share a layer and morph.
81
+ */
82
+ const pairNames = new Map();
83
+ /**
84
+ * The old (`from`) elements of each paired subject, so their names can be
85
+ * transferred off before the new (`to`) elements inherit them.
86
+ */
87
+ const pairFrom = new Map();
88
+ const resolveLayers = (phase) => {
89
+ targets.forEach((target, definition) => {
90
+ const className = classNames.get(definition);
91
+ let names;
92
+ if (definition === "root" || !resolveDefs.has(definition)) {
93
+ names = [definition];
94
+ }
95
+ else if (pairs.has(definition)) {
96
+ /**
97
+ * Paired morph: name the old target in the old snapshot, then
98
+ * force the same name(s) onto the new target in the new one, so
99
+ * two different elements morph as a single layer.
100
+ */
101
+ if (phase === "old") {
102
+ pairFrom.set(definition, resolveElements(definition));
103
+ names = assignViewTransitionNames(definition, nameRegistry, assigned, undefined, className, classed);
104
+ pairNames.set(definition, names);
105
+ }
106
+ else {
107
+ /**
108
+ * Transfer the name(s) off the `from` elements before the
109
+ * `to` elements inherit them. A `from` that survives into
110
+ * the new snapshot (e.g. hidden with `visibility: hidden`
111
+ * rather than removed) would otherwise keep the name and
112
+ * collide - "duplicate view-transition-name".
113
+ */
114
+ for (const el of pairFrom.get(definition) ?? []) {
115
+ el.style?.removeProperty("view-transition-name");
116
+ /**
117
+ * Drop the old end from the registry too, so the new
118
+ * end alone supplies this name's `new` crop radii - we
119
+ * neither re-measure nor get ordered by a stale element.
120
+ */
121
+ nameRegistry.delete(el);
122
+ }
123
+ names = assignViewTransitionNames(pairs.get(definition), nameRegistry, assigned, pairNames.get(definition), className, classed);
124
+ }
125
+ }
126
+ else {
127
+ names = assignViewTransitionNames(definition, nameRegistry, assigned, undefined, className, classed);
128
+ }
129
+ const cropped = definition !== "root" && !noCrop.has(definition);
130
+ names.forEach((name, index) => {
131
+ /**
132
+ * If two subjects resolve to the same element, merge their
133
+ * definitions so neither subject's animations are dropped.
134
+ */
135
+ const existing = layerTargets.get(name);
136
+ layerTargets.set(name, existing && existing !== target
137
+ ? { ...existing, ...target }
138
+ : target);
139
+ if (cropped)
140
+ croppedNames.add(name);
141
+ const stagger = layerStagger.get(name) ?? {};
142
+ stagger[phase] = [index, names.length];
143
+ layerStagger.set(name, stagger);
144
+ });
145
+ });
146
+ };
147
+ /**
148
+ * The stagger index/total for a layer, resolved against the snapshot it
149
+ * belongs to. Returns index -1 when the layer is absent from that snapshot
150
+ * so the caller can skip a pseudo-element that doesn't exist.
151
+ */
152
+ const staggerPosition = (name, type) => {
153
+ const stagger = layerStagger.get(name);
154
+ const position = type === "old"
155
+ ? stagger?.old
156
+ : type === "new"
157
+ ? stagger?.new
158
+ : // group / group-children / image-pair persist across both.
159
+ stagger?.new ?? stagger?.old;
160
+ return position ?? [-1, 1];
161
+ };
162
+ /**
163
+ * Merge default + per-layer transition options for a generated layer and
164
+ * resolve any stagger/delay function against this element's position. Used
165
+ * by both the morph-retiming and crop corner-radius passes.
166
+ */
167
+ const resolveLayerTransition = (target, type, transitionName, index, total) => {
168
+ const transition = mergeTransition(getValueTransition(defaultOptions, transitionName), getValueTransition((layerOptions(target, type) ?? {}), transitionName));
169
+ if (typeof transition.delay === "function") {
170
+ transition.delay = transition.delay(index, total);
171
+ }
172
+ return transition;
173
+ };
174
+ /**
175
+ * Measured corner radii per cropped layer, so the clip can animate each
176
+ * corner between the old and new elements. Per-corner (rather than the
177
+ * shorthand) so mismatched/individual radii interpolate cleanly.
178
+ */
179
+ const cropRadii = new Map();
180
+ const recordRadii = (style, name, phase) => {
181
+ const corners = {};
182
+ for (const corner of cornerProps)
183
+ corners[corner] = style[corner];
184
+ const entry = cropRadii.get(name) ?? {};
185
+ entry[phase] = corners;
186
+ cropRadii.set(name, entry);
187
+ };
188
+ /**
189
+ * Cropped layers all come from `.add()`, so their elements are in the
190
+ * registry - read each one's corner radii directly. For a paired morph both
191
+ * ends share a name; the new-snapshot element is registered last, so it
192
+ * wins the `new` reading (and the old end the `old` reading).
27
193
  */
28
- if (!hasTarget("root", targets)) {
29
- css.set(":root", {
30
- "view-transition-name": "none",
194
+ const measureCrop = (phase) => {
195
+ if (!croppedNames.size)
196
+ return;
197
+ nameRegistry.forEach((name, element) => {
198
+ if (croppedNames.has(name)) {
199
+ recordRadii(getComputedStyle(element), name, phase);
200
+ }
31
201
  });
32
- }
202
+ };
33
203
  /**
34
- * Set the timing curve to linear for all view transition layers.
35
- * This gets baked into the keyframes, which can't be changed
36
- * without breaking the generated animation.
204
+ * Write the persistent view-transition CSS: suppress root capture when the
205
+ * root has no animations of its own; force linear timing (baked into the
206
+ * keyframes, so we can retime later via updateTiming); and clip +
207
+ * object-fit: cover every cropped morph (the UA default overflows on
208
+ * aspect-ratio change), with an animated border-radius added below.
37
209
  *
38
- * This allows us to set easing via updateTiming - which can be changed.
210
+ * `css.commit` replaces rather than appends, so we re-set the full rule set
211
+ * each call - the second call (in the update callback) then picks up cropped
212
+ * layers that only exist in the new snapshot.
39
213
  */
40
- css.set("::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*)", { "animation-timing-function": "linear !important" });
41
- css.commit(); // Write
42
- const transition = document.startViewTransition(async () => {
43
- await update();
44
- // TODO: Go over new targets and ensure they all have ids
45
- });
46
- transition.finished.finally(() => {
214
+ const commitViewCSS = () => {
215
+ if (!hasTarget("root", targets)) {
216
+ css.set(":root", { "view-transition-name": "none" });
217
+ }
218
+ css.set("::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*)", { "animation-timing-function": "linear !important" });
219
+ croppedNames.forEach((name) => {
220
+ css.set(`::view-transition-group(${name})`, { overflow: "clip" });
221
+ css.set(`::view-transition-old(${name}), ::view-transition-new(${name})`, { width: "100%", height: "100%", "object-fit": "cover" });
222
+ });
223
+ css.commit(); // Write
224
+ };
225
+ const cleanup = () => {
226
+ releaseViewTransitionNames(assigned, classed);
47
227
  css.remove(); // Write
48
- });
49
- return new Promise((resolve) => {
50
- transition.ready.then(() => {
228
+ };
229
+ const callback = async () => {
230
+ await update();
231
+ /**
232
+ * Re-resolve so elements created by the update are named for the new
233
+ * snapshot, then measure the cropped layers' new border-radius.
234
+ */
235
+ const croppedBefore = croppedNames.size;
236
+ resolveLayers("new");
237
+ measureCrop("new");
238
+ /**
239
+ * Re-commit the crop CSS only if the new snapshot introduced cropped
240
+ * layers, so a layer that exists only in the new snapshot is clipped
241
+ * too - without forcing a redundant style write on the common path.
242
+ */
243
+ if (croppedNames.size > croppedBefore)
244
+ commitViewCSS();
245
+ };
246
+ let transition;
247
+ try {
248
+ resolveLayers("old");
249
+ measureCrop("old");
250
+ commitViewCSS();
251
+ transition = document.startViewTransition(callback);
252
+ }
253
+ catch (error) {
254
+ /**
255
+ * The prelude writes inline names before the transition exists. If it
256
+ * throws (e.g. startViewTransition rejects in a bad UA state), release
257
+ * them so we neither leak DOM state nor stall the queue on a promise
258
+ * that never settles - hand back a rejection it can advance past.
259
+ */
260
+ cleanup();
261
+ return Promise.reject(error);
262
+ }
263
+ transition.finished.finally(cleanup);
264
+ return new Promise((resolve, reject) => {
265
+ transition.ready
266
+ .then(() => {
51
267
  const generatedViewAnimations = getViewAnimations();
52
268
  const animations = [];
53
269
  /**
54
270
  * Create animations for each of our explicitly-defined subjects.
271
+ * `opacityAnimated` additionally tracks which `${name}:${type}`
272
+ * we faded, so we can keep the UA `plus-lighter` blend only for a
273
+ * genuine opacity crossfade (both sides fading) and drop it for a
274
+ * slide/transform, where additive compositing would flash bright.
55
275
  */
56
- targets.forEach((definition, target) => {
57
- // TODO: If target is not "root", resolve elements
58
- // and iterate over each
276
+ const explicitlyAnimated = new Set();
277
+ const opacityAnimated = new Set();
278
+ layerTargets.forEach((target, name) => {
279
+ const stagger = layerStagger.get(name);
280
+ /**
281
+ * Presence: `enter` only fires for a pure newcomer (a new
282
+ * view with no old), `exit` only for a pure leaver. A
283
+ * survivor (both) gets neither - it just morphs.
284
+ */
285
+ const enterApplies = !!stagger?.new && !stagger?.old;
286
+ const exitApplies = !!stagger?.old && !stagger?.new;
59
287
  for (const key of definitionNames) {
60
- if (!definition[key])
288
+ if (!target[key])
289
+ continue;
290
+ if (key === "enter" && !enterApplies)
291
+ continue;
292
+ if (key === "exit" && !exitApplies)
61
293
  continue;
62
- const { keyframes, options } = definition[key];
294
+ const type = chooseLayerType(key);
295
+ const [index, total] = staggerPosition(name, type);
296
+ // Skip a layer absent from its snapshot.
297
+ if (index === -1)
298
+ continue;
299
+ const { keyframes, options } = target[key];
63
300
  for (let [valueName, valueKeyframes] of Object.entries(keyframes)) {
64
- if (!valueKeyframes)
301
+ // Skip only missing values - `0` (e.g. opacity: 0)
302
+ // is valid and must reach the from-value inference.
303
+ if (valueKeyframes == null)
304
+ continue;
305
+ /**
306
+ * The view path hands keyframes straight to WAAPI,
307
+ * so Motion's `x`/`y` shorthands (compiled to
308
+ * `transform` only via the value pipeline) have no
309
+ * effect. Warn and skip - use `transform`/`translate`.
310
+ */
311
+ if (valueName === "x" || valueName === "y") {
312
+ warnOnce(false, `animateView() animates view-transition layers with CSS properties; the "${valueName}" shorthand has no effect - use transform, e.g. { transform: "translateX(40px)" }.`);
65
313
  continue;
66
- const valueOptions = {
67
- ...getValueTransition(defaultOptions, valueName),
68
- ...getValueTransition(options, valueName),
69
- };
70
- const type = chooseLayerType(key);
314
+ }
71
315
  /**
72
- * If this is an opacity animation, and keyframes are not an array,
73
- * we need to convert them into an array and set an initial value.
316
+ * enter/exit win over new/old on a shared property -
317
+ * skip it here when the gated bucket also defines it.
74
318
  */
75
- if (valueName === "opacity" &&
76
- !Array.isArray(valueKeyframes)) {
77
- const initialValue = type === "new" ? 0 : 1;
78
- valueKeyframes = [initialValue, valueKeyframes];
319
+ if (key === "new" &&
320
+ enterApplies &&
321
+ target.enter?.keyframes[valueName] != null) {
322
+ continue;
79
323
  }
324
+ if (key === "old" &&
325
+ exitApplies &&
326
+ target.exit?.keyframes[valueName] != null) {
327
+ continue;
328
+ }
329
+ const valueOptions = mergeTransition(getValueTransition(defaultOptions, valueName), getValueTransition(options, valueName));
80
330
  /**
81
- * Resolve stagger function if provided.
331
+ * Infer an origin for a single-value keyframe. An
332
+ * `enter` mirrors the matching `exit` value (a
333
+ * defined exit reverses into the enter for free);
334
+ * otherwise the per-type default (opacity 0/1, scale
335
+ * 0.85). No default -> left as-is (animates from the
336
+ * live value).
337
+ *
338
+ * `new`/`old` fire for survivors too, where only the
339
+ * opacity crossfade default applies - a transform
340
+ * default like scale 0.85 would pop a persisting
341
+ * element, so gate it on the layer actually
342
+ * entering/leaving.
343
+ */
344
+ if (!Array.isArray(valueKeyframes)) {
345
+ const exitValue = key === "enter"
346
+ ? target.exit?.keyframes[valueName]
347
+ : undefined;
348
+ const allowDefault = valueName === "opacity" ||
349
+ (type === "new" ? enterApplies : exitApplies);
350
+ const from = exitValue != null
351
+ ? Array.isArray(exitValue)
352
+ ? exitValue[exitValue.length - 1]
353
+ : exitValue
354
+ : allowDefault
355
+ ? ORIGIN_DEFAULTS[type]?.[valueName]
356
+ : undefined;
357
+ if (from !== undefined) {
358
+ valueKeyframes = [from, valueKeyframes];
359
+ }
360
+ }
361
+ /**
362
+ * Resolve stagger function if provided, per element
363
+ * across this subject's resolved layers.
82
364
  */
83
365
  if (typeof valueOptions.delay === "function") {
84
- valueOptions.delay = valueOptions.delay(0, 1);
366
+ valueOptions.delay = valueOptions.delay(index, total);
85
367
  }
86
368
  valueOptions.duration && (valueOptions.duration = secondsToMilliseconds(valueOptions.duration));
87
369
  valueOptions.delay && (valueOptions.delay = secondsToMilliseconds(valueOptions.delay));
88
- const animation = new NativeAnimation({
370
+ animations.push(new NativeAnimation({
89
371
  ...valueOptions,
90
372
  element: document.documentElement,
91
373
  name: valueName,
92
- pseudoElement: `::view-transition-${type}(${target})`,
374
+ pseudoElement: `::view-transition-${type}(${name})`,
93
375
  keyframes: valueKeyframes,
94
- });
95
- animations.push(animation);
376
+ }));
377
+ explicitlyAnimated.add(`${name}:${type}`);
378
+ if (valueName === "opacity") {
379
+ opacityAnimated.add(`${name}:${type}`);
380
+ }
96
381
  }
97
382
  }
98
383
  });
@@ -111,45 +396,161 @@ function startViewAnimation(builder) {
111
396
  const name = getViewAnimationLayerInfo(pseudoElement);
112
397
  if (!name)
113
398
  continue;
114
- const targetDefinition = targets.get(name.layer);
115
- if (!targetDefinition) {
116
- /**
117
- * If transition name is group then update the timing of the animation
118
- * whereas if it's old or new then we could possibly replace it using
119
- * the above method.
120
- */
121
- const transitionName = name.type === "group" ? "layout" : "";
122
- let animationTransition = {
123
- ...getValueTransition(defaultOptions, transitionName),
124
- };
125
- animationTransition.duration && (animationTransition.duration = secondsToMilliseconds(animationTransition.duration));
126
- animationTransition =
127
- applyGeneratorOptions(animationTransition);
128
- const easing = mapEasingToNativeEasing(animationTransition.ease, animationTransition.duration);
129
- effect.updateTiming({
130
- delay: secondsToMilliseconds(animationTransition.delay ?? 0),
131
- duration: animationTransition.duration,
132
- easing,
133
- });
134
- animations.push(new NativeAnimationWrapper(animation));
135
- }
136
- else if (hasOpacity(targetDefinition, "enter") &&
137
- hasOpacity(targetDefinition, "exit") &&
138
- effect
139
- .getKeyframes()
140
- .some((keyframe) => keyframe.mixBlendMode)) {
141
- animations.push(new NativeAnimationWrapper(animation));
142
- }
143
- else {
144
- animation.cancel();
399
+ const targetDefinition = layerTargets.get(name.layer);
400
+ /**
401
+ * We built our own animation for this layer, so drop the
402
+ * browser-generated fade we're replacing. The UA
403
+ * `plus-lighter` blend is a *separate* generated animation on
404
+ * the same pseudo (it sets `mix-blend-mode` in its keyframes):
405
+ * keep it *only* for a true opacity crossfade - both sides
406
+ * fading - so a symmetric crossfade composites without
407
+ * darkening, but a slide/transform (where both layers stay
408
+ * opaque and overlap) doesn't flash bright from the addition.
409
+ */
410
+ if (explicitlyAnimated.has(`${name.layer}:${name.type}`)) {
411
+ const isCrossfade = opacityAnimated.has(`${name.layer}:new`) &&
412
+ opacityAnimated.has(`${name.layer}:old`);
413
+ if (isCrossfade &&
414
+ effect
415
+ .getKeyframes()
416
+ .some((keyframe) => keyframe.mixBlendMode)) {
417
+ animations.push(new NativeAnimationWrapper(animation));
418
+ }
419
+ else {
420
+ animation.cancel();
421
+ }
422
+ continue;
145
423
  }
424
+ /**
425
+ * Otherwise retime the browser-generated animation to
426
+ * Motion's timing. This auto-enables the layout (group)
427
+ * morph for any resolved/named target, and applies the
428
+ * default timing to old/new layers we haven't explicitly
429
+ * overridden.
430
+ *
431
+ * group + group-children both follow the layout timing so
432
+ * the nesting container stays in sync with the morph.
433
+ */
434
+ /**
435
+ * A survivor's old + new are the two halves of one
436
+ * `plus-lighter` crossfade. They must share identical timing
437
+ * (so their opacities stay mirrored and sum to 1 - else the
438
+ * additive blend flashes bright wherever both are partly
439
+ * visible) and fade linearly (the bounce belongs on the
440
+ * group's geometry, not the opacity). So time them as the
441
+ * group, rather than via their own - potentially staggered,
442
+ * or enter/exit-derived - old/new options.
443
+ */
444
+ const stagger = layerStagger.get(name.layer);
445
+ const isMorphCrossfade = (name.type === "old" || name.type === "new") &&
446
+ !!stagger?.old &&
447
+ !!stagger?.new;
448
+ const timingType = name.type.startsWith("group") || isMorphCrossfade
449
+ ? "group"
450
+ : name.type;
451
+ const [index, total] = staggerPosition(name.layer, timingType);
452
+ const transitionName = timingType === "group" ? "layout" : "";
453
+ let animationTransition = resolveLayerTransition(targetDefinition, timingType, transitionName, index === -1 ? 0 : index, total);
454
+ /**
455
+ * The crossfade should resolve at the spring's *perceptual*
456
+ * (visual) duration - the geometry can keep bouncing, but the
457
+ * opacity shouldn't drag through the settle. So capture
458
+ * `visualDuration` before `applyGeneratorOptions` replaces it
459
+ * with the full overshoot duration, and use it for the fade.
460
+ */
461
+ const visualDuration = animationTransition.visualDuration;
462
+ animationTransition.duration && (animationTransition.duration = secondsToMilliseconds(animationTransition.duration));
463
+ animationTransition =
464
+ applyGeneratorOptions(animationTransition);
465
+ const duration = isMorphCrossfade && visualDuration !== undefined
466
+ ? secondsToMilliseconds(visualDuration)
467
+ : animationTransition.duration;
468
+ const easing = isMorphCrossfade
469
+ ? "linear"
470
+ : mapEasingToNativeEasing(animationTransition.ease, animationTransition.duration);
471
+ effect.updateTiming({
472
+ delay: secondsToMilliseconds(animationTransition.delay ?? 0),
473
+ duration,
474
+ easing,
475
+ });
476
+ animations.push(new NativeAnimationWrapper(animation));
146
477
  }
478
+ /**
479
+ * Animate each cropped layer's clip corners between the old and
480
+ * new elements, so a cropped morph keeps rounded corners
481
+ * (handling individual per-corner radii).
482
+ */
483
+ cropRadii.forEach((radii, name) => {
484
+ if (!radii.old && !radii.new)
485
+ return;
486
+ const target = layerTargets.get(name);
487
+ const [index, total] = staggerPosition(name, "group");
488
+ const radiusOptions = resolveLayerTransition(target, "group", "layout", index === -1 ? 0 : index, total);
489
+ radiusOptions.duration && (radiusOptions.duration = secondsToMilliseconds(radiusOptions.duration));
490
+ radiusOptions.delay && (radiusOptions.delay = secondsToMilliseconds(radiusOptions.delay));
491
+ for (const corner of cornerProps) {
492
+ // `||` (not `??`) so an empty measurement (e.g. an
493
+ // un-rendered element) falls back rather than producing
494
+ // an invalid keyframe.
495
+ const from = radii.old?.[corner] || radii.new?.[corner] || "0px";
496
+ const to = radii.new?.[corner] || radii.old?.[corner] || "0px";
497
+ // Skip square corners - nothing to round.
498
+ if (parseFloat(from) === 0 && parseFloat(to) === 0) {
499
+ continue;
500
+ }
501
+ animations.push(new NativeAnimation({
502
+ ...radiusOptions,
503
+ element: document.documentElement,
504
+ name: corner,
505
+ pseudoElement: `::view-transition-group(${name})`,
506
+ keyframes: [from, to],
507
+ }));
508
+ }
509
+ });
147
510
  resolve(new GroupAnimation(animations));
148
- });
511
+ })
512
+ .catch(() =>
513
+ /**
514
+ * `ready` rejects when the transition is skipped - no visual
515
+ * change, or superseded by an interrupting transition. The DOM
516
+ * update still applied, so settle with no animations rather than
517
+ * surfacing it as an error to an awaiting caller. A genuine
518
+ * failure in `update()` rejects `updateCallbackDone` (already
519
+ * settled by now), so propagate that instead.
520
+ */
521
+ transition.updateCallbackDone.then(() => resolve(new GroupAnimation([])), reject));
149
522
  });
150
523
  }
151
- function hasOpacity(target, key) {
152
- return target?.[key]?.keyframes.opacity;
524
+ /**
525
+ * The options that should time a given generated layer type, so a retimed
526
+ * group/old/new picks up any per-target transition the user provided. Checks
527
+ * the type's buckets in priority order (e.g. `new` before `enter`).
528
+ */
529
+ function layerOptions(target, type) {
530
+ for (const bucket of typeBuckets[type] ?? []) {
531
+ const options = target?.[bucket]?.options;
532
+ if (options)
533
+ return options;
534
+ }
535
+ }
536
+ /**
537
+ * Merge a base transition (e.g. the default `options`) with a per-layer/value
538
+ * override. An explicit `duration` on the override must win over an inherited
539
+ * generator's own timing: a spring prefers `visualDuration`, and
540
+ * `spring.applyToOptions` overwrites `duration` with the computed settle time -
541
+ * so without this the override is silently discarded. Dropping the inherited
542
+ * `type`/`visualDuration` makes the layer a plain tween of that duration, unless
543
+ * it asked for its own generator `type`/`visualDuration`.
544
+ */
545
+ function mergeTransition(base, override) {
546
+ const merged = { ...base, ...override };
547
+ if (override.duration !== undefined) {
548
+ if (override.visualDuration === undefined)
549
+ delete merged.visualDuration;
550
+ if (override.type === undefined)
551
+ delete merged.type;
552
+ }
553
+ return merged;
153
554
  }
154
555
 
155
556
  export { startViewAnimation };