@astryxdesign/core 0.7.8-canary.8a74c58 → 0.7.8-canary.8ddcfe1

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.
@@ -1,11 +1,7 @@
1
1
  /**
2
2
  * Run `reset`, which takes `root` out of rendering and back, and put every
3
3
  * CSS animation and transition of `root` and its subtree back in the state it
4
- * was in before. Recreated animations are matched to the ones they replace by
5
- * element, pseudo-element and name, in order, and then by name alone (an
6
- * effect script moved to another element or pseudo-element); a recreated
7
- * animation with no predecessor (an entry animation that had already played
8
- * and been dropped) is finished.
4
+ * was in before.
9
5
  */
10
6
  export declare function preserveAnimations(root: Element, reset: () => void): void;
11
7
  //# sourceMappingURL=preserveAnimations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"preserveAnimations.d.ts","sourceRoot":"","sources":["../../src/Layer/preserveAnimations.ts"],"names":[],"mappings":"AAoPA;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,IAAI,GAAG,IAAI,CAkDzE"}
1
+ {"version":3,"file":"preserveAnimations.d.ts","sourceRoot":"","sources":["../../src/Layer/preserveAnimations.ts"],"names":[],"mappings":"AA0BA;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,IAAI,GAAG,IAAI,CASzE"}
@@ -11,189 +11,23 @@
11
11
  *
12
12
  * Taking an element out of rendering cancels every CSS animation and
13
13
  * transition of it and its subtree. When it returns, the browser creates the
14
- * animations again from the stylesheet, at their start, and the transitions
15
- * not at all; script-created animations are untouched. `preserveAnimations`
16
- * reads every animation's state first and, afterwards, gives each recreated
17
- * CSS animation that state and resumes each cancelled transition with a
18
- * script animation at the same point.
19
- *
20
- * The state is read generically, from what the platform exposes: every
21
- * accessor with a setter on the animation's and its effect's prototype
22
- * chains, every field of the effect's timing, and its keyframes. Only values
23
- * that differ from the recreated animation's are written, so a value the
24
- * stylesheet controls stays under its control. A few members are not plain
25
- * state and are handled in their own step (`ORDERED`); the rest need no
26
- * handling (`DERIVED`). What cannot be carried over is the Animation and
27
- * effect objects themselves: the old ones are idle, their `addEventListener`
28
- * listeners and `finished`/`ready` promises stay with them, and the browser
29
- * fires `animationcancel`/`transitioncancel` for them and `animationstart`
14
+ * animations again from the stylesheet, at their start. It starts no
15
+ * transition but one from `@starting-style`, and script-created animations
16
+ * are untouched. `preserveAnimations` reads every animation's state first.
17
+ * Afterwards it gives each recreated CSS animation that state, and resumes
18
+ * each cancelled transition with a script animation at the same point. Any
19
+ * other animation the return started is finished, unless it never ends
20
+ * (motion/carryAnimations.ts). The browser fires
21
+ * `animationcancel`/`transitioncancel` for the old ones and `animationstart`
30
22
  * for the new ones.
31
23
  */
32
24
 
33
- /**
34
- * Animation members written in a fixed order rather than generically: the
35
- * timeline before the range that is measured on it, the rate before the
36
- * time, and the start time (or, for a paused animation, the current time)
37
- * last, once the play state is set. `effect` is not
38
- * replaced, because a CSS animation whose effect script replaces leaves the
39
- * stylesheet's control; its state is copied onto the new effect instead.
40
- */
41
- const ORDERED = new Set(['effect', 'timeline', 'rangeStart', 'rangeEnd', 'playbackRate', 'startTime', 'currentTime']);
42
- /** Every accessor with a setter on `object`'s prototype chain. */
43
- function writableAccessors(object) {
44
- const names = new Set();
45
- for (let proto = Object.getPrototypeOf(object); proto != null && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
46
- for (const [name, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(proto))) {
47
- if (descriptor.set != null) {
48
- names.add(name);
49
- }
50
- }
51
- }
52
- return [...names];
53
- }
54
-
55
- /**
56
- * A textual form of a value that is compared by content rather than by
57
- * identity: a CSS numeric value (as in a timeline range offset) through its
58
- * serialization, a plain object (a range) through its members. Anything else
59
- * is compared by identity (`null`).
60
- */
61
- function contentOf(value) {
62
- if (typeof CSSNumericValue !== 'undefined' && value instanceof CSSNumericValue) {
63
- return value.toString();
64
- }
65
- if (value != null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype) {
66
- return Object.entries(value).map(([key, inner]) => `${key}:${contentOf(inner) ?? String(inner)}`).join(';');
67
- }
68
- return null;
69
- }
70
- function differs(a, b) {
71
- if (a === b) {
72
- return false;
73
- }
74
- const textA = contentOf(a);
75
- return textA == null || textA !== contentOf(b);
76
- }
77
- function save(animation) {
78
- const effect = animation.effect;
79
- const values = {};
80
- for (const name of writableAccessors(animation)) {
81
- values[name] = animation[name];
82
- }
83
- const effectValues = {};
84
- if (effect != null) {
85
- for (const name of writableAccessors(effect)) {
86
- effectValues[name] = effect[name];
87
- }
88
- }
89
- return {
90
- animation,
91
- name: animation.animationName ?? '',
92
- target: effect?.target ?? null,
93
- pseudoElement: effect?.pseudoElement ?? null,
94
- values,
95
- effectValues,
96
- timing: effect?.getTiming() ?? null,
97
- keyframes: effect?.getKeyframes() ?? null,
98
- playState: animation.playState,
99
- replaceState: animation.replaceState
100
- };
101
- }
102
- function write(object, name, value) {
103
- object[name] = value;
104
- }
105
-
106
- /** Give `next` the effect state `saved` had: every writable member. */
107
- function restoreEffect(next, saved) {
108
- for (const name of writableAccessors(next)) {
109
- if (name in saved.effectValues && differs(saved.effectValues[name], next[name])) {
110
- write(next, name, saved.effectValues[name]);
111
- }
112
- }
113
- if (saved.timing != null) {
114
- const timing = next.getTiming();
115
- const changed = {};
116
- for (const [field, value] of Object.entries(saved.timing)) {
117
- if (differs(value, timing[field])) {
118
- changed[field] = value;
119
- }
120
- }
121
- if (Object.keys(changed).length > 0) {
122
- next.updateTiming(changed);
123
- }
124
- }
125
- if (saved.keyframes != null && JSON.stringify(saved.keyframes) !== JSON.stringify(next.getKeyframes())) {
126
- next.setKeyframes(saved.keyframes);
127
- }
128
- }
129
-
130
- /** Give `next` the state `saved` had, member by member. */
131
- function restore(next, saved) {
132
- const effect = next.effect;
133
- if (effect != null) {
134
- restoreEffect(effect, saved);
135
- }
136
- const current = next;
137
- for (const name of ['timeline', 'rangeStart', 'rangeEnd']) {
138
- if (name in saved.values && differs(saved.values[name], current[name])) {
139
- write(next, name, saved.values[name]);
140
- }
141
- }
142
- for (const name of writableAccessors(next)) {
143
- if (!ORDERED.has(name) && name in saved.values && differs(saved.values[name], current[name])) {
144
- write(next, name, saved.values[name]);
145
- }
146
- }
147
- if (saved.replaceState === 'persisted' && next.replaceState !== 'persisted') {
148
- next.persist();
149
- }
150
- const rate = saved.values.playbackRate;
151
- if (typeof rate === 'number' && rate !== next.playbackRate) {
152
- next.playbackRate = rate;
153
- }
154
- if (saved.playState === 'paused' && next.playState !== 'paused') {
155
- next.pause();
156
- } else if (saved.playState !== 'paused' && next.playState === 'paused') {
157
- next.play();
158
- }
159
- // A running animation is anchored to its timeline by its start time, which
160
- // carries its current time on exactly. A paused one has none and keeps its
161
- // current time. A finished one holds its end: finishing it again holds the
162
- // same time, though its start time is then counted from now, since script
163
- // cannot set a start time and a held time together.
164
- const start = saved.values.startTime;
165
- const time = saved.values.currentTime;
166
- if (saved.playState === 'finished') {
167
- next.finish();
168
- } else if (start != null) {
169
- next.startTime = start;
170
- } else if (time != null) {
171
- next.currentTime = time;
172
- }
173
- }
174
-
175
- /** Resume a cancelled CSS transition with a script animation in its state. */
176
- function resumeTransition(saved) {
177
- if (saved.target == null || saved.keyframes == null) {
178
- return;
179
- }
180
- const replacement = saved.target.animate(saved.keyframes, {
181
- pseudoElement: saved.pseudoElement ?? undefined
182
- });
183
- restore(replacement, saved);
184
- }
185
- function isTransition(animation) {
186
- return typeof CSSTransition !== 'undefined' && animation instanceof CSSTransition;
187
- }
25
+ import { restoreAnimations, saveAnimation } from "../motion/carryAnimations.js";
188
26
 
189
27
  /**
190
28
  * Run `reset`, which takes `root` out of rendering and back, and put every
191
29
  * CSS animation and transition of `root` and its subtree back in the state it
192
- * was in before. Recreated animations are matched to the ones they replace by
193
- * element, pseudo-element and name, in order, and then by name alone (an
194
- * effect script moved to another element or pseudo-element); a recreated
195
- * animation with no predecessor (an entry animation that had already played
196
- * and been dropped) is finished.
30
+ * was in before.
197
31
  */
198
32
  export function preserveAnimations(root, reset) {
199
33
  if (typeof root.getAnimations !== 'function') {
@@ -203,43 +37,7 @@ export function preserveAnimations(root, reset) {
203
37
  const before = root.getAnimations({
204
38
  subtree: true
205
39
  });
206
- const saved = before.map(save);
40
+ const saved = before.map(saveAnimation);
207
41
  reset();
208
- const after = root.getAnimations({
209
- subtree: true
210
- });
211
- const current = new Set(after);
212
- const survivors = new Set(before);
213
- const cancelled = [];
214
- for (const state of saved) {
215
- if (current.has(state.animation)) {
216
- continue;
217
- }
218
- if (isTransition(state.animation)) {
219
- resumeTransition(state);
220
- } else {
221
- cancelled.push(state);
222
- }
223
- }
224
- const recreated = after.filter(animation => !survivors.has(animation));
225
- const unmatched = [];
226
- for (const animation of recreated) {
227
- const effect = animation.effect;
228
- const name = animation.animationName ?? '';
229
- const index = cancelled.findIndex(state => state.name === name && state.target === (effect?.target ?? null) && state.pseudoElement === (effect?.pseudoElement ?? null));
230
- if (index === -1) {
231
- unmatched.push(animation);
232
- } else {
233
- restore(animation, cancelled.splice(index, 1)[0]);
234
- }
235
- }
236
- for (const animation of unmatched) {
237
- const name = animation.animationName ?? '';
238
- const index = cancelled.findIndex(state => state.name === name);
239
- if (index === -1) {
240
- animation.finish();
241
- } else {
242
- restore(animation, cancelled.splice(index, 1)[0]);
243
- }
244
- }
42
+ restoreAnimations(root, saved, new Set(before));
245
43
  }
@@ -0,0 +1,58 @@
1
+ type Writable = Record<string, unknown>;
2
+ /** One animation's state, read before the browser starts it afresh. */
3
+ export interface SavedAnimation {
4
+ animation: Animation;
5
+ name: string;
6
+ target: Element | null;
7
+ pseudoElement: string | null;
8
+ values: Writable;
9
+ effectValues: Writable;
10
+ timing: EffectTiming | null;
11
+ keyframes: ComputedKeyframe[] | null;
12
+ playState: AnimationPlayState;
13
+ replaceState: AnimationReplaceState;
14
+ }
15
+ /**
16
+ * Reads one animation's state generically, from what the platform exposes.
17
+ * That is every accessor with a setter on the animation's and its effect's
18
+ * prototype chains, every field of the effect's timing, and its keyframes. Only values
19
+ * that differ from the recreated animation's are written back, so a value the
20
+ * stylesheet controls stays under its control. A few members are not plain
21
+ * state and are written in their own step (`ORDERED`). The Animation and
22
+ * effect objects themselves cannot be carried over: the old ones keep their
23
+ * listeners and promises.
24
+ */
25
+ export declare function saveAnimation(animation: Animation): SavedAnimation;
26
+ /**
27
+ * Puts each state `saved` holds on the CSS animations and transitions of
28
+ * `root` and its subtree that are not among `survivors`. Each CSS animation
29
+ * the browser started afresh takes the state of the one it replaces. It is
30
+ * matched by element, pseudo-element and name, in order, and then by name
31
+ * alone (an effect script moved to another element or pseudo-element). Each
32
+ * transition whose animation no longer runs resumes as a script animation in
33
+ * its state. One started afresh that replaces none is finished: an entrance
34
+ * that had played and been dropped, or a transition from `@starting-style`.
35
+ * Script animations are left as they are.
36
+ */
37
+ export declare function restoreAnimations(root: Element, saved: ReadonlyArray<SavedAnimation>, survivors: ReadonlySet<Animation>): void;
38
+ /** The CSS animations and transitions `saveAnimations` read. */
39
+ export type SavedAnimations = ReadonlyArray<SavedAnimation>;
40
+ /**
41
+ * Reads the state of every CSS animation and transition of `root` and its
42
+ * subtree, for `carryAnimations` to give a copy of it.
43
+ */
44
+ export declare function saveAnimations(root: Element): SavedAnimations;
45
+ /**
46
+ * Carries on in `copy`, a clone of the root `saved` was read from, now laid
47
+ * out, the CSS animations and transitions its element ran. A clone starts
48
+ * each CSS animation again and runs no transition but a `@starting-style`
49
+ * one. So each CSS animation the clone started takes the state of its
50
+ * element's, and each transition runs on as a script animation. One the
51
+ * clone started that its element no longer ran is finished: an entrance that
52
+ * had played, or a `@starting-style` transition. `copyOf` maps each element
53
+ * of the cloned subtree to its copy. The element's own animations keep
54
+ * running, so the copy's take no id or event handler.
55
+ */
56
+ export declare function carryAnimations(saved: SavedAnimations, copy: Element, copyOf: (element: Element) => Element | null): void;
57
+ export {};
58
+ //# sourceMappingURL=carryAnimations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"carryAnimations.d.ts","sourceRoot":"","sources":["../../src/motion/carryAnimations.ts"],"names":[],"mappings":"AA+BA,KAAK,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAsDxC,uEAAuE;AACvE,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,SAAS,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IACvB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,MAAM,EAAE,QAAQ,CAAC;IACjB,YAAY,EAAE,QAAQ,CAAC;IACvB,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,SAAS,EAAE,gBAAgB,EAAE,GAAG,IAAI,CAAC;IACrC,SAAS,EAAE,kBAAkB,CAAC;IAC9B,YAAY,EAAE,qBAAqB,CAAC;CACrC;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,cAAc,CAyBlE;AAsJD;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,OAAO,EACb,KAAK,EAAE,aAAa,CAAC,cAAc,CAAC,EACpC,SAAS,EAAE,WAAW,CAAC,SAAS,CAAC,GAChC,IAAI,CA8CN;AAED,gEAAgE;AAChE,MAAM,MAAM,eAAe,GAAG,aAAa,CAAC,cAAc,CAAC,CAAC;AAE5D;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe,CAQ7D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,eAAe,EACtB,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,GAAG,IAAI,GAC3C,IAAI,CASN"}
@@ -0,0 +1,305 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file carryAnimations.ts
5
+ * @input The CSS animations and transitions of an element and its subtree,
6
+ * read before the browser starts them afresh
7
+ * @output Exports `saveAnimations` and `carryAnimations`, which carry them on
8
+ * in a clone of the element. Also `saveAnimation` and `restoreAnimations`,
9
+ * which put them back after a display reset
10
+ * @position Motion foundation; the transient copy (transientCopy.ts) and the
11
+ * layer runtime's re-placement (Layer/preserveAnimations.ts) use it
12
+ */
13
+
14
+ /**
15
+ * Animation members written in a fixed order rather than generically: the
16
+ * timeline before the range that is measured on it, the rate before the
17
+ * time, and the start time (or, for a paused animation, the current time)
18
+ * last, once the play state is set. `effect` is not
19
+ * replaced, because a CSS animation whose effect script replaces leaves the
20
+ * stylesheet's control; its state is copied onto the new effect instead.
21
+ */
22
+ const ORDERED = new Set(['effect', 'timeline', 'rangeStart', 'rangeEnd', 'playbackRate', 'startTime', 'currentTime']);
23
+ /** Every accessor with a setter on `object`'s prototype chain. */
24
+ function writableAccessors(object) {
25
+ const names = new Set();
26
+ for (let proto = Object.getPrototypeOf(object); proto != null && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
27
+ for (const [name, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(proto))) {
28
+ if (descriptor.set != null) {
29
+ names.add(name);
30
+ }
31
+ }
32
+ }
33
+ return [...names];
34
+ }
35
+
36
+ /**
37
+ * A textual form of a value that is compared by content rather than by
38
+ * identity: a CSS numeric value (as in a timeline range offset) through its
39
+ * serialization, a plain object (a range) through its members. Anything else
40
+ * is compared by identity (`null`).
41
+ */
42
+ function contentOf(value) {
43
+ if (typeof CSSNumericValue !== 'undefined' && value instanceof CSSNumericValue) {
44
+ return value.toString();
45
+ }
46
+ if (value != null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype) {
47
+ return Object.entries(value).map(([key, inner]) => `${key}:${contentOf(inner) ?? String(inner)}`).join(';');
48
+ }
49
+ return null;
50
+ }
51
+ function differs(a, b) {
52
+ if (a === b) {
53
+ return false;
54
+ }
55
+ const textA = contentOf(a);
56
+ return textA == null || textA !== contentOf(b);
57
+ }
58
+
59
+ /** One animation's state, read before the browser starts it afresh. */
60
+
61
+ /**
62
+ * Reads one animation's state generically, from what the platform exposes.
63
+ * That is every accessor with a setter on the animation's and its effect's
64
+ * prototype chains, every field of the effect's timing, and its keyframes. Only values
65
+ * that differ from the recreated animation's are written back, so a value the
66
+ * stylesheet controls stays under its control. A few members are not plain
67
+ * state and are written in their own step (`ORDERED`). The Animation and
68
+ * effect objects themselves cannot be carried over: the old ones keep their
69
+ * listeners and promises.
70
+ */
71
+ export function saveAnimation(animation) {
72
+ const effect = animation.effect;
73
+ const values = {};
74
+ for (const name of writableAccessors(animation)) {
75
+ values[name] = animation[name];
76
+ }
77
+ const effectValues = {};
78
+ if (effect != null) {
79
+ for (const name of writableAccessors(effect)) {
80
+ effectValues[name] = effect[name];
81
+ }
82
+ }
83
+ return {
84
+ animation,
85
+ name: animation.animationName ?? '',
86
+ target: effect?.target ?? null,
87
+ pseudoElement: effect?.pseudoElement ?? null,
88
+ values,
89
+ effectValues,
90
+ timing: effect?.getTiming() ?? null,
91
+ keyframes: effect?.getKeyframes() ?? null,
92
+ playState: animation.playState,
93
+ replaceState: animation.replaceState
94
+ };
95
+ }
96
+ function write(object, name, value) {
97
+ object[name] = value;
98
+ }
99
+
100
+ /** Give `next` the effect state `saved` had: every writable member. */
101
+ function restoreEffect(next, saved) {
102
+ for (const name of writableAccessors(next)) {
103
+ if (name in saved.effectValues && differs(saved.effectValues[name], next[name])) {
104
+ write(next, name, saved.effectValues[name]);
105
+ }
106
+ }
107
+ if (saved.timing != null) {
108
+ const timing = next.getTiming();
109
+ const changed = {};
110
+ for (const [field, value] of Object.entries(saved.timing)) {
111
+ if (differs(value, timing[field])) {
112
+ changed[field] = value;
113
+ }
114
+ }
115
+ if (Object.keys(changed).length > 0) {
116
+ next.updateTiming(changed);
117
+ }
118
+ }
119
+ if (saved.keyframes != null && JSON.stringify(saved.keyframes) !== JSON.stringify(next.getKeyframes())) {
120
+ next.setKeyframes(saved.keyframes);
121
+ }
122
+ }
123
+
124
+ /** Give `next` the state `saved` had, member by member. */
125
+ function restore(next, saved) {
126
+ const effect = next.effect;
127
+ if (effect != null) {
128
+ restoreEffect(effect, saved);
129
+ }
130
+ const current = next;
131
+ for (const name of ['timeline', 'rangeStart', 'rangeEnd']) {
132
+ if (name in saved.values && differs(saved.values[name], current[name])) {
133
+ write(next, name, saved.values[name]);
134
+ }
135
+ }
136
+ for (const name of writableAccessors(next)) {
137
+ if (!ORDERED.has(name) && name in saved.values && differs(saved.values[name], current[name])) {
138
+ write(next, name, saved.values[name]);
139
+ }
140
+ }
141
+ if (saved.replaceState === 'persisted' && next.replaceState !== 'persisted') {
142
+ next.persist();
143
+ }
144
+ const rate = saved.values.playbackRate;
145
+ if (typeof rate === 'number' && rate !== next.playbackRate) {
146
+ next.playbackRate = rate;
147
+ }
148
+ if (saved.playState === 'paused' && next.playState !== 'paused') {
149
+ next.pause();
150
+ } else if (saved.playState !== 'paused' && next.playState === 'paused') {
151
+ next.play();
152
+ }
153
+ // A running animation is anchored to its timeline by its start time, which
154
+ // carries its current time on exactly. A paused one has none and keeps its
155
+ // current time. A finished one holds its end: finishing it again holds the
156
+ // same time, though its start time is then counted from now, since script
157
+ // cannot set a start time and a held time together.
158
+ const start = saved.values.startTime;
159
+ const time = saved.values.currentTime;
160
+ if (saved.playState === 'finished') {
161
+ next.finish();
162
+ } else if (start != null) {
163
+ next.startTime = start;
164
+ } else if (time != null) {
165
+ next.currentTime = time;
166
+ }
167
+ }
168
+
169
+ /** Resume a cancelled CSS transition with a script animation in its state. */
170
+ function resumeTransition(saved) {
171
+ if (saved.target == null || saved.keyframes == null) {
172
+ return;
173
+ }
174
+ const replacement = saved.target.animate(saved.keyframes, {
175
+ pseudoElement: saved.pseudoElement ?? undefined
176
+ });
177
+ restore(replacement, saved);
178
+ }
179
+ function isTransition(animation) {
180
+ return typeof CSSTransition !== 'undefined' && animation instanceof CSSTransition;
181
+ }
182
+ function isCssAnimation(animation) {
183
+ return typeof CSSAnimation !== 'undefined' && animation instanceof CSSAnimation;
184
+ }
185
+
186
+ /**
187
+ * Finishes an animation that ends: an infinite one cannot be finished, and is
188
+ * left as it runs.
189
+ */
190
+ function finishIfItEnds(animation) {
191
+ const end = animation.effect?.getComputedTiming().endTime;
192
+ if (end != null && Number.isFinite(Number(end)) && animation.playbackRate !== 0) {
193
+ animation.finish();
194
+ }
195
+ }
196
+
197
+ /** The members a copy's animation does not take: its element's keep them. */
198
+ const IDENTITY_MEMBERS = new Set(['id', 'onfinish', 'oncancel', 'onremove']);
199
+
200
+ /**
201
+ * `saved` for an animation that carries on on the copy of its element:
202
+ * aimed at the copy, without the element's id and event handlers.
203
+ */
204
+ function retarget(saved, copyOf) {
205
+ const target = saved.target == null ? null : copyOf(saved.target);
206
+ if (target == null) {
207
+ return null;
208
+ }
209
+ const values = Object.fromEntries(Object.entries(saved.values).filter(([name]) => !IDENTITY_MEMBERS.has(name)));
210
+ const effectValues = {
211
+ ...saved.effectValues
212
+ };
213
+ if ('target' in effectValues) {
214
+ effectValues.target = target;
215
+ }
216
+ return {
217
+ ...saved,
218
+ target,
219
+ values,
220
+ effectValues
221
+ };
222
+ }
223
+
224
+ /**
225
+ * Puts each state `saved` holds on the CSS animations and transitions of
226
+ * `root` and its subtree that are not among `survivors`. Each CSS animation
227
+ * the browser started afresh takes the state of the one it replaces. It is
228
+ * matched by element, pseudo-element and name, in order, and then by name
229
+ * alone (an effect script moved to another element or pseudo-element). Each
230
+ * transition whose animation no longer runs resumes as a script animation in
231
+ * its state. One started afresh that replaces none is finished: an entrance
232
+ * that had played and been dropped, or a transition from `@starting-style`.
233
+ * Script animations are left as they are.
234
+ */
235
+ export function restoreAnimations(root, saved, survivors) {
236
+ const after = root.getAnimations({
237
+ subtree: true
238
+ });
239
+ const current = new Set(after);
240
+ const cancelled = [];
241
+ for (const state of saved) {
242
+ if (current.has(state.animation)) {
243
+ continue;
244
+ }
245
+ if (isTransition(state.animation)) {
246
+ resumeTransition(state);
247
+ } else {
248
+ cancelled.push(state);
249
+ }
250
+ }
251
+ const recreated = after.filter(animation => (isCssAnimation(animation) || isTransition(animation)) && !survivors.has(animation));
252
+ const unmatched = [];
253
+ for (const animation of recreated) {
254
+ const effect = animation.effect;
255
+ const name = animation.animationName ?? '';
256
+ const index = cancelled.findIndex(state => state.name === name && state.target === (effect?.target ?? null) && state.pseudoElement === (effect?.pseudoElement ?? null));
257
+ if (index === -1) {
258
+ unmatched.push(animation);
259
+ } else {
260
+ restore(animation, cancelled.splice(index, 1)[0]);
261
+ }
262
+ }
263
+ for (const animation of unmatched) {
264
+ const name = animation.animationName ?? '';
265
+ const index = cancelled.findIndex(state => state.name === name);
266
+ if (index === -1) {
267
+ finishIfItEnds(animation);
268
+ } else {
269
+ restore(animation, cancelled.splice(index, 1)[0]);
270
+ }
271
+ }
272
+ }
273
+
274
+ /** The CSS animations and transitions `saveAnimations` read. */
275
+
276
+ /**
277
+ * Reads the state of every CSS animation and transition of `root` and its
278
+ * subtree, for `carryAnimations` to give a copy of it.
279
+ */
280
+ export function saveAnimations(root) {
281
+ if (typeof root.getAnimations !== 'function') {
282
+ return [];
283
+ }
284
+ return root.getAnimations({
285
+ subtree: true
286
+ }).filter(animation => isCssAnimation(animation) || isTransition(animation)).map(saveAnimation);
287
+ }
288
+
289
+ /**
290
+ * Carries on in `copy`, a clone of the root `saved` was read from, now laid
291
+ * out, the CSS animations and transitions its element ran. A clone starts
292
+ * each CSS animation again and runs no transition but a `@starting-style`
293
+ * one. So each CSS animation the clone started takes the state of its
294
+ * element's, and each transition runs on as a script animation. One the
295
+ * clone started that its element no longer ran is finished: an entrance that
296
+ * had played, or a `@starting-style` transition. `copyOf` maps each element
297
+ * of the cloned subtree to its copy. The element's own animations keep
298
+ * running, so the copy's take no id or event handler.
299
+ */
300
+ export function carryAnimations(saved, copy, copyOf) {
301
+ if (typeof copy.getAnimations !== 'function') {
302
+ return;
303
+ }
304
+ restoreAnimations(copy, saved.flatMap(state => retarget(state, copyOf) ?? []), new Set());
305
+ }
@@ -4,7 +4,8 @@ export declare function isCopyable(element: Element): boolean;
4
4
  * A copy that takes no part: no ids or test ids, no form or radio group (a
5
5
  * copied checked radio would uncheck the live one), no label for a live
6
6
  * control, and copied frames and media load nothing. Scrolled descendants
7
- * keep their offsets once the copy is laid out (`placeOver`).
7
+ * keep their offsets. The CSS animations and transitions running in it carry
8
+ * on from where they were, once the copy is laid out (`placeOver`).
8
9
  */
9
10
  export declare function inertCopy(element: HTMLElement): HTMLElement | null;
10
11
  /** The background `element` shows: its own, or the nearest ancestor's. */
@@ -12,7 +13,8 @@ export declare function paintedBackground(element: Element): string;
12
13
  /**
13
14
  * Lay `element` over `rect` (viewport coordinates), absolutely, as the last
14
15
  * child of `host`, wherever its containing block is: no ancestor's position
15
- * changes.
16
+ * changes. A copy's scrolled descendants then restore their offsets, and the
17
+ * CSS animations and transitions its element ran carry on in it.
16
18
  */
17
19
  export declare function placeOver(element: HTMLElement, host: Element, rect: DOMRect): void;
18
20
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"transientCopy.d.ts","sourceRoot":"","sources":["../../src/motion/transientCopy.ts"],"names":[],"mappings":"AAiCA,iDAAiD;AACjD,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAEpD;AAqHD;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,IAAI,CA0ClE;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQ1D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,OAAO,GACZ,IAAI,CAwDN;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG;IACrD,IAAI,EAAE,WAAW,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;CACf,GAAG,IAAI,CA8CP;AAID;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,EAAE,CA6BrD"}
1
+ {"version":3,"file":"transientCopy.d.ts","sourceRoot":"","sources":["../../src/motion/transientCopy.ts"],"names":[],"mappings":"AA0CA,iDAAiD;AACjD,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAEpD;AA+HD;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,IAAI,CA8ClE;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQ1D;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,OAAO,GACZ,IAAI,CAiEN;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,GAAG;IACrD,IAAI,EAAE,WAAW,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;CACf,GAAG,IAAI,CAoDP;AAID;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,EAAE,CA6BrD"}