@mun-ui/web 0.0.0-stage → 0.2.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 (83) hide show
  1. package/LICENSE +21 -0
  2. package/dist/content-transition.d.ts +23 -0
  3. package/dist/content-transition.d.ts.map +1 -0
  4. package/dist/content-transition.js +780 -0
  5. package/dist/content-transition.js.map +1 -0
  6. package/dist/continuous-corners.d.ts +3 -0
  7. package/dist/continuous-corners.d.ts.map +1 -0
  8. package/dist/continuous-corners.js +71 -0
  9. package/dist/continuous-corners.js.map +1 -0
  10. package/dist/devtools.d.ts +50 -0
  11. package/dist/devtools.d.ts.map +1 -0
  12. package/dist/devtools.js +118 -0
  13. package/dist/devtools.js.map +1 -0
  14. package/dist/dom.d.ts +12 -0
  15. package/dist/dom.d.ts.map +1 -0
  16. package/dist/dom.js +5375 -0
  17. package/dist/dom.js.map +1 -0
  18. package/dist/element-motion.d.ts +29 -0
  19. package/dist/element-motion.d.ts.map +1 -0
  20. package/dist/element-motion.js +463 -0
  21. package/dist/element-motion.js.map +1 -0
  22. package/dist/focus.d.ts +3 -0
  23. package/dist/focus.d.ts.map +1 -0
  24. package/dist/focus.js +70 -0
  25. package/dist/focus.js.map +1 -0
  26. package/dist/gpu-island.d.ts +123 -0
  27. package/dist/gpu-island.d.ts.map +1 -0
  28. package/dist/gpu-island.js +1093 -0
  29. package/dist/gpu-island.js.map +1 -0
  30. package/dist/hydration.d.ts +18 -0
  31. package/dist/hydration.d.ts.map +1 -0
  32. package/dist/hydration.js +223 -0
  33. package/dist/hydration.js.map +1 -0
  34. package/dist/index.d.ts +7 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +5 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/lazy-index.d.ts +42 -0
  39. package/dist/lazy-index.d.ts.map +1 -0
  40. package/dist/lazy-index.js +136 -0
  41. package/dist/lazy-index.js.map +1 -0
  42. package/dist/motion.d.ts +49 -0
  43. package/dist/motion.d.ts.map +1 -0
  44. package/dist/motion.js +745 -0
  45. package/dist/motion.js.map +1 -0
  46. package/dist/patch-ir.d.ts +44 -0
  47. package/dist/patch-ir.d.ts.map +1 -0
  48. package/dist/patch-ir.js +221 -0
  49. package/dist/patch-ir.js.map +1 -0
  50. package/dist/path-interpolation.d.ts +29 -0
  51. package/dist/path-interpolation.d.ts.map +1 -0
  52. package/dist/path-interpolation.js +318 -0
  53. package/dist/path-interpolation.js.map +1 -0
  54. package/dist/presentation.d.ts +4 -0
  55. package/dist/presentation.d.ts.map +1 -0
  56. package/dist/presentation.js +198 -0
  57. package/dist/presentation.js.map +1 -0
  58. package/dist/props.d.ts +22 -0
  59. package/dist/props.d.ts.map +1 -0
  60. package/dist/props.js +663 -0
  61. package/dist/props.js.map +1 -0
  62. package/dist/shared.d.ts +73 -0
  63. package/dist/shared.d.ts.map +1 -0
  64. package/dist/shared.js +704 -0
  65. package/dist/shared.js.map +1 -0
  66. package/dist/ssr.d.ts +3 -0
  67. package/dist/ssr.d.ts.map +1 -0
  68. package/dist/ssr.js +287 -0
  69. package/dist/ssr.js.map +1 -0
  70. package/dist/transition.d.ts +12 -0
  71. package/dist/transition.d.ts.map +1 -0
  72. package/dist/transition.js +172 -0
  73. package/dist/transition.js.map +1 -0
  74. package/dist/ui-ir.d.ts +12 -0
  75. package/dist/ui-ir.d.ts.map +1 -0
  76. package/dist/ui-ir.js +324 -0
  77. package/dist/ui-ir.js.map +1 -0
  78. package/dist/url-policy.d.ts +2 -0
  79. package/dist/url-policy.d.ts.map +1 -0
  80. package/dist/url-policy.js +27 -0
  81. package/dist/url-policy.js.map +1 -0
  82. package/package.json +47 -3
  83. package/README.md +0 -3
package/dist/motion.js ADDED
@@ -0,0 +1,745 @@
1
+ import { animate, curves, createInterpolator, motionValue, smooth, spring, timing, } from "@mun-ui/animation";
2
+ import { compositorMotionPropertyMask, layoutMotionPropertyMask, motionPropertyBit, paintMotionPropertyMask } from "@mun-ui/core/internal/motion-abi";
3
+ import { svgPathInterpolatorOptions } from "./path-interpolation.js";
4
+ const paintMotionPropertyFallback = new Set(["filter", "box-shadow", "text-shadow"]);
5
+ const layoutMotionPropertyFallback = new Set([
6
+ "word-spacing", "border-width", "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
7
+ "flex-basis", "grid-template-columns", "grid-template-rows",
8
+ ]);
9
+ /** Stable launch lane used by automatic motion batches: compositor -> paint -> layout -> other. */
10
+ function motionPropertyPriority(property) {
11
+ const bit = motionPropertyBit(property);
12
+ if (bit !== 0 && (bit & compositorMotionPropertyMask) !== 0)
13
+ return 0;
14
+ if ((bit !== 0 && (bit & paintMotionPropertyMask) !== 0) || paintMotionPropertyFallback.has(property) || property.endsWith("-color"))
15
+ return 1;
16
+ if ((bit !== 0 && (bit & layoutMotionPropertyMask) !== 0) || layoutMotionPropertyFallback.has(property))
17
+ return 2;
18
+ return 3;
19
+ }
20
+ const compiledMotionPlans = new WeakMap();
21
+ const compiledDescriptorPlans = new Map();
22
+ const maximumCompiledDescriptorPlans = 128;
23
+ const activeChannels = new WeakMap();
24
+ const activeAttributeChannels = new WeakMap();
25
+ const pendingStyleWrites = new Map();
26
+ const pendingAttributeWrites = new Map();
27
+ let styleWriteScheduled = false;
28
+ let attributeWriteScheduled = false;
29
+ function queueStyleWrite(element, property, value) {
30
+ let writes = pendingStyleWrites.get(element);
31
+ if (!writes) {
32
+ writes = new Map();
33
+ pendingStyleWrites.set(element, writes);
34
+ }
35
+ writes.set(property, value);
36
+ if (styleWriteScheduled)
37
+ return;
38
+ styleWriteScheduled = true;
39
+ queueMicrotask(() => {
40
+ styleWriteScheduled = false;
41
+ const batch = [...pendingStyleWrites];
42
+ pendingStyleWrites.clear();
43
+ for (const [target, properties] of batch) {
44
+ const style = target.style;
45
+ if (!style)
46
+ continue;
47
+ for (const [name, next] of properties)
48
+ style.setProperty(name, next);
49
+ }
50
+ });
51
+ }
52
+ function removePendingStyleWrite(element, property) {
53
+ const writes = pendingStyleWrites.get(element);
54
+ if (!writes)
55
+ return;
56
+ if (property === undefined) {
57
+ pendingStyleWrites.delete(element);
58
+ return;
59
+ }
60
+ writes.delete(property);
61
+ if (writes.size === 0)
62
+ pendingStyleWrites.delete(element);
63
+ }
64
+ function queueAttributeWrite(element, name, value) {
65
+ let writes = pendingAttributeWrites.get(element);
66
+ if (!writes) {
67
+ writes = new Map();
68
+ pendingAttributeWrites.set(element, writes);
69
+ }
70
+ writes.set(name, value);
71
+ if (attributeWriteScheduled)
72
+ return;
73
+ attributeWriteScheduled = true;
74
+ queueMicrotask(() => {
75
+ attributeWriteScheduled = false;
76
+ const batch = [...pendingAttributeWrites];
77
+ pendingAttributeWrites.clear();
78
+ for (const [target, attributes] of batch) {
79
+ for (const [name, next] of attributes)
80
+ target.setAttribute(name, next);
81
+ }
82
+ });
83
+ }
84
+ function removePendingAttributeWrite(element, name) {
85
+ const writes = pendingAttributeWrites.get(element);
86
+ if (!writes)
87
+ return;
88
+ if (name === undefined) {
89
+ pendingAttributeWrites.delete(element);
90
+ return;
91
+ }
92
+ writes.delete(name);
93
+ if (writes.size === 0)
94
+ pendingAttributeWrites.delete(element);
95
+ }
96
+ function channelsFor(element) {
97
+ let channels = activeChannels.get(element);
98
+ if (!channels) {
99
+ channels = new Map();
100
+ activeChannels.set(element, channels);
101
+ }
102
+ return channels;
103
+ }
104
+ function curveFor(kind) {
105
+ switch (kind) {
106
+ case "linear": return timing({ duration: 0.35, curve: curves.linear });
107
+ case "easeIn": return timing({ duration: 0.35, curve: curves.easeIn });
108
+ case "easeOut": return timing({ duration: 0.35, curve: curves.easeOut });
109
+ case "easeInOut": return timing({ duration: 0.35, curve: curves.easeInOut });
110
+ default: return smooth();
111
+ }
112
+ }
113
+ function compileMotionPlan(animation) {
114
+ const cached = compiledMotionPlans.get(animation);
115
+ if (cached)
116
+ return cached;
117
+ const descriptor = animation.descriptor;
118
+ const speed = Number.isFinite(descriptor.speed) && descriptor.speed > 0 ? descriptor.speed : 1;
119
+ const duration = Number.isFinite(descriptor.duration) ? Math.max(0, descriptor.duration) / speed : 0.35;
120
+ const response = Math.max(0.001, (descriptor.response ?? descriptor.duration) / speed);
121
+ const dampingRatio = descriptor.dampingFraction ?? 0.825;
122
+ const blendDuration = Math.max(0, Number.isFinite(descriptor.blendDuration) ? descriptor.blendDuration ?? 0 : 0) / speed;
123
+ const delayMs = Math.max(0, Number.isFinite(descriptor.delay) ? descriptor.delay : 0) * 1000 / speed;
124
+ const repeatCount = descriptor.repeatCount === Number.POSITIVE_INFINITY
125
+ ? Number.POSITIVE_INFINITY
126
+ : Math.max(1, Math.floor(Number.isFinite(descriptor.repeatCount) ? descriptor.repeatCount : 1));
127
+ const autoreverses = descriptor.autoreverses ?? true;
128
+ // Compiler-hoisted Animation values hit the WeakMap above. This bounded
129
+ // structural cache covers dynamic code that recreates an equivalent immutable
130
+ // descriptor, so easing LUT/spring coefficient compilation is still paid once.
131
+ const descriptorKey = [
132
+ descriptor.kind, duration, response, dampingRatio, blendDuration, delayMs,
133
+ repeatCount, autoreverses ? 1 : 0,
134
+ ].join("|");
135
+ const shared = compiledDescriptorPlans.get(descriptorKey);
136
+ if (shared) {
137
+ compiledDescriptorPlans.delete(descriptorKey);
138
+ compiledDescriptorPlans.set(descriptorKey, shared);
139
+ compiledMotionPlans.set(animation, shared);
140
+ return shared;
141
+ }
142
+ const spec = descriptor.kind === "spring"
143
+ ? Object.freeze({ ...spring({ response, dampingRatio }), blendDuration })
144
+ : (() => {
145
+ const base = curveFor(descriptor.kind);
146
+ return base.kind === "timing" ? timing({ duration, curve: base.curve }) : base;
147
+ })();
148
+ const plan = Object.freeze({
149
+ spec,
150
+ delayMs,
151
+ repeatCount,
152
+ autoreverses,
153
+ });
154
+ compiledMotionPlans.set(animation, plan);
155
+ compiledDescriptorPlans.set(descriptorKey, plan);
156
+ while (compiledDescriptorPlans.size > maximumCompiledDescriptorPlans) {
157
+ const oldest = compiledDescriptorPlans.keys().next().value;
158
+ if (oldest === undefined)
159
+ break;
160
+ compiledDescriptorPlans.delete(oldest);
161
+ }
162
+ return plan;
163
+ }
164
+ /** Convert and memoize Mun's SwiftUI-shaped value into a public @mun-ui/animation spec. */
165
+ export function motionSpecForAnimation(animation) {
166
+ return compileMotionPlan(animation).spec;
167
+ }
168
+ function isColorProperty(property) {
169
+ return property === "color" || property === "background" || property === "background-color" || property === "border-color" || property.endsWith("-color");
170
+ }
171
+ function parseScalar(value) {
172
+ if (typeof value === "number")
173
+ return Number.isFinite(value) ? { value, unit: "" } : undefined;
174
+ if (typeof value !== "string")
175
+ return undefined;
176
+ // A scalar CSS value is one number plus one simple unit. Do not classify
177
+ // compound values such as `17.6px 0px` as a scalar whose unit happens to be
178
+ // `px 0px`: that route carries spring velocity through the first numeric token
179
+ // and can make a rapidly reversed switch thumb keep travelling the old way.
180
+ // Compound numeric CSS belongs to the template interpolator below instead.
181
+ const match = value.trim().match(/^(-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?)([A-Za-z%]*)$/i);
182
+ if (!match)
183
+ return undefined;
184
+ const scalar = Number(match[1]);
185
+ return Number.isFinite(scalar) ? { value: scalar, unit: match[2] } : undefined;
186
+ }
187
+ function numericCssTemplate(value) {
188
+ const pattern = /-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi;
189
+ const parts = [];
190
+ const values = [];
191
+ let cursor = 0;
192
+ let match;
193
+ while ((match = pattern.exec(value))) {
194
+ parts.push(value.slice(cursor, match.index));
195
+ const number = Number(match[0]);
196
+ if (!Number.isFinite(number))
197
+ return undefined;
198
+ values.push(number);
199
+ cursor = match.index + match[0].length;
200
+ }
201
+ if (values.length === 0)
202
+ return undefined;
203
+ parts.push(value.slice(cursor));
204
+ return { parts, values };
205
+ }
206
+ function numericCssInterpolatorOptions(from, to) {
207
+ const left = numericCssTemplate(from);
208
+ const right = numericCssTemplate(to);
209
+ if (!left || !right || left.values.length !== right.values.length || left.parts.length !== right.parts.length)
210
+ return undefined;
211
+ for (let index = 0; index < left.parts.length; index += 1) {
212
+ if (left.parts[index] !== right.parts[index])
213
+ return undefined;
214
+ }
215
+ return {
216
+ interpolate(_from, _to, progress) {
217
+ let output = left.parts[0];
218
+ for (let index = 0; index < left.values.length; index += 1) {
219
+ const value = left.values[index] + (right.values[index] - left.values[index]) * progress;
220
+ const normalized = Math.abs(value) < 1e-10 ? 0 : Math.round(value * 100000) / 100000;
221
+ output += `${normalized}${left.parts[index + 1]}`;
222
+ }
223
+ return output;
224
+ },
225
+ };
226
+ }
227
+ function interpolatorOptions(property, from, to) {
228
+ if (property === "transform" && typeof from === "string" && typeof to === "string")
229
+ return { type: "transform" };
230
+ if (isColorProperty(property) && typeof from === "string" && typeof to === "string")
231
+ return { type: "color", color: { space: "oklab" } };
232
+ // CSS has many numerically-interpolable compound values (translate/scale,
233
+ // border radii, filters, shadows without color changes, background position,
234
+ // etc.). If the token skeleton is stable, interpolate every numeric token in
235
+ // one channel rather than falling back to a discrete jump.
236
+ if (typeof from === "string" && typeof to === "string")
237
+ return numericCssInterpolatorOptions(from, to);
238
+ return undefined;
239
+ }
240
+ function routeFor(property, from, to) {
241
+ const left = parseScalar(from);
242
+ const right = parseScalar(to);
243
+ if (left && right && left.unit === right.unit)
244
+ return { kind: "scalar", value: right.value, unit: right.unit };
245
+ const options = interpolatorOptions(property, from, to);
246
+ return options ? { kind: "interpolated", options } : undefined;
247
+ }
248
+ function startAfterDelay(delayMs, start) {
249
+ if (!(delayMs > 0)) {
250
+ start();
251
+ return undefined;
252
+ }
253
+ return setTimeout(start, delayMs);
254
+ }
255
+ class ScalarMotionChannel {
256
+ element;
257
+ property;
258
+ route = "scalar";
259
+ #value;
260
+ #unsubscribe;
261
+ #control;
262
+ #timer;
263
+ #generation = 0;
264
+ #active = false;
265
+ #unit;
266
+ #currentStyle;
267
+ constructor(element, property, start, unit) {
268
+ this.element = element;
269
+ this.property = property;
270
+ this.#unit = unit;
271
+ this.#currentStyle = `${start}${unit}`;
272
+ this.#value = motionValue(start);
273
+ this.#unsubscribe = this.#value.subscribeValue(value => {
274
+ this.#currentStyle = `${value}${this.#unit}`;
275
+ queueStyleWrite(this.element, this.property, this.#currentStyle);
276
+ }, { emitCurrent: false });
277
+ }
278
+ currentStyleValue() { return this.#currentStyle; }
279
+ isActive() { return this.#active; }
280
+ retarget(to, plan) {
281
+ const target = parseScalar(to);
282
+ if (!target || target.unit !== this.#unit)
283
+ return false;
284
+ const generation = ++this.#generation;
285
+ this.#active = true;
286
+ if (this.#timer !== undefined)
287
+ clearTimeout(this.#timer);
288
+ this.#timer = undefined;
289
+ const run = async () => {
290
+ if (generation !== this.#generation)
291
+ return;
292
+ const origin = this.#value.get();
293
+ let cycle = 0;
294
+ try {
295
+ while (generation === this.#generation && (plan.repeatCount === Number.POSITIVE_INFINITY || cycle < plan.repeatCount)) {
296
+ const reverse = plan.autoreverses && cycle % 2 === 1;
297
+ // Calling animate() on the same MotionValue deliberately avoids a
298
+ // pre-cancel here: @mun-ui/animation retargets a live spring in-place and carries
299
+ // its velocity into the new target.
300
+ this.#control = animate(this.#value, reverse ? origin : target.value, plan.spec);
301
+ const result = await this.#control.finished;
302
+ if (generation !== this.#generation || result.status !== "finished")
303
+ return;
304
+ cycle += 1;
305
+ if (!plan.autoreverses
306
+ && (plan.repeatCount === Number.POSITIVE_INFINITY || cycle < plan.repeatCount)) {
307
+ this.#value.set(origin, 0);
308
+ }
309
+ }
310
+ }
311
+ finally {
312
+ if (generation === this.#generation && plan.repeatCount !== Number.POSITIVE_INFINITY)
313
+ this.#active = false;
314
+ }
315
+ };
316
+ // A delayed retarget intentionally leaves the current engine animation
317
+ // running. When the timer fires animate() retargets the same MotionValue
318
+ // from its then-current presentation value/velocity instead of freezing it
319
+ // at request time and later reviving stale velocity.
320
+ this.#timer = startAfterDelay(plan.delayMs, () => {
321
+ this.#timer = undefined;
322
+ void run();
323
+ });
324
+ return true;
325
+ }
326
+ dispose() {
327
+ this.#generation += 1;
328
+ this.#active = false;
329
+ if (this.#timer !== undefined)
330
+ clearTimeout(this.#timer);
331
+ this.#timer = undefined;
332
+ this.#control?.cancel();
333
+ this.#control = undefined;
334
+ this.#unsubscribe();
335
+ removePendingStyleWrite(this.element, this.property);
336
+ }
337
+ }
338
+ class InterpolatedMotionChannel {
339
+ element;
340
+ property;
341
+ route = "interpolated";
342
+ #progress = motionValue(0);
343
+ #unsubscribe;
344
+ #control;
345
+ #timer;
346
+ #generation = 0;
347
+ #active = false;
348
+ #interpolate;
349
+ #currentStyle;
350
+ #origin;
351
+ #target;
352
+ #options;
353
+ constructor(element, property, from, to, options) {
354
+ this.element = element;
355
+ this.property = property;
356
+ this.#origin = from;
357
+ this.#target = to;
358
+ this.#options = options;
359
+ this.#interpolate = createInterpolator(from, to, options);
360
+ this.#currentStyle = String(from);
361
+ this.#unsubscribe = this.#progress.subscribeValue(progress => {
362
+ const value = this.#interpolate(progress);
363
+ this.#currentStyle = String(value);
364
+ queueStyleWrite(this.element, this.property, this.#currentStyle);
365
+ }, { emitCurrent: false });
366
+ }
367
+ currentStyleValue() { return this.#currentStyle; }
368
+ isActive() { return this.#active; }
369
+ retarget(to, plan) {
370
+ const options = interpolatorOptions(this.property, this.#currentStyle, to);
371
+ if (!options)
372
+ return false;
373
+ const generation = ++this.#generation;
374
+ this.#active = true;
375
+ if (this.#timer !== undefined)
376
+ clearTimeout(this.#timer);
377
+ this.#timer = undefined;
378
+ const run = async () => {
379
+ if (generation !== this.#generation)
380
+ return;
381
+ this.#control?.cancel();
382
+ this.#origin = this.#currentStyle;
383
+ this.#target = to;
384
+ this.#options = options;
385
+ try {
386
+ this.#interpolate = createInterpolator(this.#origin, this.#target, this.#options);
387
+ }
388
+ catch {
389
+ if (generation === this.#generation)
390
+ this.#active = false;
391
+ return;
392
+ }
393
+ this.#progress.set(0, 0);
394
+ let cycle = 0;
395
+ try {
396
+ while (generation === this.#generation && (plan.repeatCount === Number.POSITIVE_INFINITY || cycle < plan.repeatCount)) {
397
+ const reverse = plan.autoreverses && cycle % 2 === 1;
398
+ this.#progress.set(reverse ? 1 : 0, 0);
399
+ this.#control = animate(this.#progress, reverse ? 0 : 1, plan.spec);
400
+ const result = await this.#control.finished;
401
+ if (generation !== this.#generation || result.status !== "finished")
402
+ return;
403
+ cycle += 1;
404
+ }
405
+ }
406
+ finally {
407
+ if (generation === this.#generation && plan.repeatCount !== Number.POSITIVE_INFINITY)
408
+ this.#active = false;
409
+ }
410
+ };
411
+ this.#timer = startAfterDelay(plan.delayMs, () => {
412
+ this.#timer = undefined;
413
+ void run();
414
+ });
415
+ return true;
416
+ }
417
+ dispose() {
418
+ this.#generation += 1;
419
+ this.#active = false;
420
+ if (this.#timer !== undefined)
421
+ clearTimeout(this.#timer);
422
+ this.#timer = undefined;
423
+ this.#control?.cancel();
424
+ this.#control = undefined;
425
+ this.#unsubscribe();
426
+ removePendingStyleWrite(this.element, this.property);
427
+ }
428
+ }
429
+ class InterpolatedAttributeMotionChannel {
430
+ element;
431
+ name;
432
+ #progress = motionValue(0);
433
+ #unsubscribe;
434
+ #control;
435
+ #timer;
436
+ #generation = 0;
437
+ #active = false;
438
+ #interpolate;
439
+ #current;
440
+ constructor(element, name, from, to, options) {
441
+ this.element = element;
442
+ this.name = name;
443
+ this.#interpolate = createInterpolator(from, to, options);
444
+ this.#current = String(from);
445
+ this.#unsubscribe = this.#progress.subscribeValue(progress => {
446
+ this.#current = String(this.#interpolate(progress));
447
+ queueAttributeWrite(this.element, this.name, this.#current);
448
+ }, { emitCurrent: false });
449
+ }
450
+ currentValue() { return this.#current; }
451
+ isActive() { return this.#active; }
452
+ retarget(to, plan, options) {
453
+ const generation = ++this.#generation;
454
+ this.#active = true;
455
+ if (this.#timer !== undefined)
456
+ clearTimeout(this.#timer);
457
+ this.#timer = undefined;
458
+ const origin = this.#current;
459
+ const target = String(to);
460
+ let interpolate;
461
+ try {
462
+ interpolate = createInterpolator(this.#current, to, options);
463
+ }
464
+ catch {
465
+ this.#active = false;
466
+ return false;
467
+ }
468
+ const run = async () => {
469
+ if (generation !== this.#generation)
470
+ return;
471
+ this.#control?.cancel();
472
+ this.#interpolate = interpolate;
473
+ this.#progress.set(0, 0);
474
+ let cycle = 0;
475
+ try {
476
+ while (generation === this.#generation && (plan.repeatCount === Number.POSITIVE_INFINITY || cycle < plan.repeatCount)) {
477
+ const reverse = plan.autoreverses && cycle % 2 === 1;
478
+ this.#progress.set(reverse ? 1 : 0, 0);
479
+ this.#control = animate(this.#progress, reverse ? 0 : 1, plan.spec);
480
+ const result = await this.#control.finished;
481
+ if (generation !== this.#generation || result.status !== "finished")
482
+ return;
483
+ cycle += 1;
484
+ }
485
+ }
486
+ finally {
487
+ if (generation === this.#generation && plan.repeatCount !== Number.POSITIVE_INFINITY) {
488
+ this.#active = false;
489
+ const finishesAtOrigin = plan.autoreverses && plan.repeatCount > 1 && plan.repeatCount % 2 === 0;
490
+ this.#current = finishesAtOrigin ? origin : target;
491
+ queueAttributeWrite(this.element, this.name, this.#current);
492
+ }
493
+ }
494
+ };
495
+ this.#timer = startAfterDelay(plan.delayMs, () => {
496
+ this.#timer = undefined;
497
+ void run();
498
+ });
499
+ return true;
500
+ }
501
+ dispose() {
502
+ this.#generation += 1;
503
+ this.#active = false;
504
+ if (this.#timer !== undefined)
505
+ clearTimeout(this.#timer);
506
+ this.#timer = undefined;
507
+ this.#control?.cancel();
508
+ this.#control = undefined;
509
+ this.#unsubscribe();
510
+ removePendingAttributeWrite(this.element, this.name);
511
+ }
512
+ }
513
+ function attributeInterpolatorOptions(name, from, to) {
514
+ if (name === "d" && typeof from === "string" && typeof to === "string") {
515
+ try {
516
+ return svgPathInterpolatorOptions(from, to);
517
+ }
518
+ catch {
519
+ return undefined;
520
+ }
521
+ }
522
+ if ((name === "fill" || name === "stroke" || name.endsWith("color")) && typeof from === "string" && typeof to === "string") {
523
+ return { type: "color", color: { space: "oklab" } };
524
+ }
525
+ if (typeof from === "string" || typeof from === "number") {
526
+ if (typeof to === "string" || typeof to === "number")
527
+ return numericCssInterpolatorOptions(String(from), String(to));
528
+ }
529
+ return undefined;
530
+ }
531
+ function dropAttributeChannel(element, name) {
532
+ const channels = activeAttributeChannels.get(element);
533
+ const channel = channels?.get(name);
534
+ channel?.dispose();
535
+ channels?.delete(name);
536
+ if (channels?.size === 0)
537
+ activeAttributeChannels.delete(element);
538
+ }
539
+ /** Stop one animated DOM attribute before a direct patch. */
540
+ export function cancelDomAttributeAnimation(element, name) {
541
+ dropAttributeChannel(element, name);
542
+ }
543
+ /**
544
+ * Animate a DOM attribute through the same Mun animation clock as style motion.
545
+ * SVG path data uses @mun-ui/animation's cubic-normalizing path interpolator and keeps its
546
+ * current presentation value when interrupted and retargeted.
547
+ */
548
+ export function animateDomAttribute(element, name, from, to, animation) {
549
+ const options = attributeInterpolatorOptions(name, from, to);
550
+ if (!options) {
551
+ dropAttributeChannel(element, name);
552
+ return false;
553
+ }
554
+ let channels = activeAttributeChannels.get(element);
555
+ if (!channels) {
556
+ channels = new Map();
557
+ activeAttributeChannels.set(element, channels);
558
+ }
559
+ let existing = channels.get(name);
560
+ if (existing && !existing.isActive() && String(from) !== existing.currentValue()) {
561
+ existing.dispose();
562
+ channels.delete(name);
563
+ existing = undefined;
564
+ }
565
+ if (existing?.retarget(to, compileMotionPlan(animation), options))
566
+ return true;
567
+ existing?.dispose();
568
+ try {
569
+ const channel = new InterpolatedAttributeMotionChannel(element, name, existing?.currentValue() ?? from, to, options);
570
+ channels.set(name, channel);
571
+ return channel.retarget(to, compileMotionPlan(animation), options);
572
+ }
573
+ catch {
574
+ channels.delete(name);
575
+ return false;
576
+ }
577
+ }
578
+ function dropChannel(element, property) {
579
+ const channels = activeChannels.get(element);
580
+ const channel = channels?.get(property);
581
+ channel?.dispose();
582
+ channels?.delete(property);
583
+ if (channels?.size === 0)
584
+ activeChannels.delete(element);
585
+ }
586
+ /** Stop one property channel before a direct/remove style patch. */
587
+ export function cancelDomStyleAnimation(element, property) {
588
+ dropChannel(element, property);
589
+ }
590
+ /**
591
+ * Animate one DOM style value with a precompiled motion plan and a persistent
592
+ * per-property channel. Returns false when the value cannot be interpolated
593
+ * safely, allowing the normal DOM patcher to apply it directly.
594
+ */
595
+ function animateDomStyleWithPlan(element, property, from, to, plan) {
596
+ const channels = channelsFor(element);
597
+ let existing = channels.get(property);
598
+ // While a channel is running, its presentation value is the authoritative
599
+ // starting point for retargeting. Once it settles, the renderer-provided
600
+ // `from` value wins again so external/direct style patches can resynchronize
601
+ // the persistent channel without an artificial jump.
602
+ if (existing && !existing.isActive() && String(from) !== existing.currentStyleValue()) {
603
+ existing.dispose();
604
+ channels.delete(property);
605
+ existing = undefined;
606
+ }
607
+ const effectiveFrom = existing?.isActive() ? existing.currentStyleValue() : from;
608
+ const route = routeFor(property, effectiveFrom, to);
609
+ if (!route) {
610
+ dropChannel(element, property);
611
+ return false;
612
+ }
613
+ if (route.kind === "scalar") {
614
+ if (existing?.route === "scalar" && existing.retarget(to, plan))
615
+ return true;
616
+ existing?.dispose();
617
+ const start = parseScalar(effectiveFrom);
618
+ if (!start || start.unit !== route.unit) {
619
+ channels.delete(property);
620
+ return false;
621
+ }
622
+ const channel = new ScalarMotionChannel(element, property, start.value, start.unit);
623
+ channels.set(property, channel);
624
+ return channel.retarget(to, plan);
625
+ }
626
+ if (existing?.route === "interpolated" && existing.retarget(to, plan))
627
+ return true;
628
+ existing?.dispose();
629
+ try {
630
+ const channel = new InterpolatedMotionChannel(element, property, effectiveFrom, to, route.options);
631
+ channels.set(property, channel);
632
+ return channel.retarget(to, plan);
633
+ }
634
+ catch {
635
+ channels.delete(property);
636
+ return false;
637
+ }
638
+ }
639
+ /** Animate one style property. Kept as the small compatibility surface. */
640
+ export function animateDomStyle(element, property, from, to, animation) {
641
+ return animateDomStyleWithPlan(element, property, from, to, compileMotionPlan(animation));
642
+ }
643
+ /**
644
+ * Launch a whole style diff as one motion batch. Every property keeps its own
645
+ * persistent channel and execution plan, while @mun-ui/animation shares the frame loop and
646
+ * Mun coalesces the resulting DOM writes into one commit batch.
647
+ */
648
+ export function animateDomStyles(element, changes) {
649
+ const animated = new Set();
650
+ if (changes.length === 0)
651
+ return animated;
652
+ const plans = new Map();
653
+ const ordered = changes.length < 2
654
+ ? changes
655
+ : changes.map((change, index) => ({ change, index }))
656
+ .sort((left, right) => motionPropertyPriority(left.change.property) - motionPropertyPriority(right.change.property) || left.index - right.index)
657
+ .map(item => item.change);
658
+ for (const change of ordered) {
659
+ let plan = plans.get(change.animation);
660
+ if (!plan) {
661
+ plan = compileMotionPlan(change.animation);
662
+ plans.set(change.animation, plan);
663
+ }
664
+ if (animateDomStyleWithPlan(element, change.property, change.from, change.to, plan))
665
+ animated.add(change.property);
666
+ }
667
+ return animated;
668
+ }
669
+ /**
670
+ * FLIP-style geometry animation for intrinsic layout changes. Built-in offset,
671
+ * rotation and scale use the independent CSS translate/rotate/scale channels,
672
+ * leaving `transform` available as an internal layout channel. If user code is
673
+ * already occupying transform, the conservative choice is to skip FLIP rather
674
+ * than interfere with it.
675
+ */
676
+ export function animateDomLayout(element, before, after, animation) {
677
+ if (![before.left, before.top, before.width, before.height, after.left, after.top, after.width, after.height].every(Number.isFinite))
678
+ return false;
679
+ if (!(after.width > 0) || !(after.height > 0) || !(before.width > 0) || !(before.height > 0))
680
+ return false;
681
+ const dx = before.left + before.width / 2 - (after.left + after.width / 2);
682
+ const dy = before.top + before.height / 2 - (after.top + after.height / 2);
683
+ const sx = before.width / after.width;
684
+ const sy = before.height / after.height;
685
+ const moved = Math.abs(dx) >= 0.01 || Math.abs(dy) >= 0.01;
686
+ const resized = Math.abs(sx - 1) >= 0.0001 || Math.abs(sy - 1) >= 0.0001;
687
+ if (!moved && !resized)
688
+ return false;
689
+ const style = element.style;
690
+ if (!style)
691
+ return false;
692
+ const channels = activeChannels.get(element);
693
+ const changes = [];
694
+ // Use the independent CSS translate/scale channels for FLIP. This keeps the
695
+ // user's transform property entirely free and lets an opacity/rotation/scale
696
+ // animation continue with its own curve while layout converges in parallel.
697
+ if (moved) {
698
+ const currentTranslate = style.getPropertyValue("translate").trim();
699
+ if (!channels?.get("translate") && (!currentTranslate || currentTranslate === "none" || currentTranslate === "0px" || currentTranslate === "0px 0px")) {
700
+ const inverse = `${dx}px ${dy}px`;
701
+ dropChannel(element, "translate");
702
+ style.setProperty("translate", inverse);
703
+ changes.push({ property: "translate", from: inverse, to: "0px 0px", animation });
704
+ }
705
+ }
706
+ if (resized) {
707
+ const currentScale = style.getPropertyValue("scale").trim();
708
+ if (!channels?.get("scale") && (!currentScale || currentScale === "none" || currentScale === "1" || currentScale === "1 1")) {
709
+ const inverse = `${sx} ${sy}`;
710
+ dropChannel(element, "scale");
711
+ style.setProperty("scale", inverse);
712
+ changes.push({ property: "scale", from: inverse, to: "1 1", animation });
713
+ }
714
+ }
715
+ if (changes.length === 0) {
716
+ // Older browsers or applications already owning both independent channels
717
+ // still receive the conservative transform fallback when it is unused.
718
+ const transformChannel = channels?.get("transform");
719
+ const current = style.getPropertyValue("transform").trim();
720
+ if (current && current !== "none" && !transformChannel)
721
+ return false;
722
+ const inverse = `translate(${dx}px, ${dy}px) scale(${sx}, ${sy})`;
723
+ dropChannel(element, "transform");
724
+ style.setProperty("transform", inverse);
725
+ return animateDomStyleWithPlan(element, "transform", inverse, "none", compileMotionPlan(animation));
726
+ }
727
+ return animateDomStyles(element, changes).size > 0;
728
+ }
729
+ export function cancelDomAnimations(element) {
730
+ const channels = activeChannels.get(element);
731
+ if (channels) {
732
+ channels.forEach(channel => channel.dispose());
733
+ channels.clear();
734
+ activeChannels.delete(element);
735
+ }
736
+ removePendingStyleWrite(element);
737
+ const attributeChannels = activeAttributeChannels.get(element);
738
+ if (attributeChannels) {
739
+ attributeChannels.forEach(channel => channel.dispose());
740
+ attributeChannels.clear();
741
+ activeAttributeChannels.delete(element);
742
+ }
743
+ removePendingAttributeWrite(element);
744
+ }
745
+ //# sourceMappingURL=motion.js.map