magic-pill 9.1.1 → 9.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.
@@ -0,0 +1,65 @@
1
+ "use client";
2
+
3
+ import { useEffect, useLayoutEffect, useRef, useState } from "react";
4
+ import { batchSteps, isSwap, planSteps, readTimings } from "./choreography.mjs";
5
+ import { captureSwap, playSwap } from "../Pill/morph.mjs";
6
+
7
+ // src/MagicPill/useChoreography.ts
8
+ /** Where a pill starts: its target already, but a notification pops in. */
9
+ const initial = (target) => ({
10
+ scene: target.kind === "notification" ? { kind: "none" } : target,
11
+ presence: target.kind === "base" ? "compact" : target.kind === "idle" ? "rest" : "hidden"
12
+ });
13
+ /** What the pill shows, moved towards `target` one timed step at a time. */
14
+ function useChoreography(rootRef, target, options) {
15
+ const [displayed, setDisplayed] = useState(() => initial(target));
16
+ const displayedRef = useRef(displayed);
17
+ displayedRef.current = displayed;
18
+ const optionsRef = useRef(options);
19
+ optionsRef.current = options;
20
+ const followUps = useRef([]);
21
+ const capture = useRef(null);
22
+ useLayoutEffect(() => {
23
+ const swapped = capture.current;
24
+ capture.current = null;
25
+ if (swapped && rootRef.current) playSwap(rootRef.current, swapped);
26
+ optionsRef.current.onSlide?.(false);
27
+ }, [displayed]);
28
+ useEffect(() => {
29
+ const next = followUps.current.shift();
30
+ if (!next) return;
31
+ rootRef.current?.offsetWidth;
32
+ setDisplayed(next);
33
+ }, [displayed]);
34
+ useEffect(() => {
35
+ const current = displayedRef.current;
36
+ const mode = optionsRef.current.swapMode(current.scene, target);
37
+ const timers = batchSteps(current, planSteps(current, target, readTimings(rootRef.current), mode)).map(({ at, states }) => {
38
+ const apply = () => {
39
+ const from = displayedRef.current;
40
+ const { beforeApply, turns, onSlide } = optionsRef.current;
41
+ beforeApply?.(from, states[0]);
42
+ if (isSwap(from, states[0], mode)) {
43
+ const freeze = turns?.(from.scene, states[0].scene) ?? false;
44
+ capture.current = captureSwap(rootRef.current, { freeze });
45
+ if (freeze) onSlide?.(true);
46
+ }
47
+ followUps.current = states.slice(1);
48
+ setDisplayed(states[0]);
49
+ };
50
+ if (at > 0) return setTimeout(apply, at);
51
+ apply();
52
+ });
53
+ return () => {
54
+ timers.forEach(clearTimeout);
55
+ followUps.current = [];
56
+ };
57
+ }, [target]);
58
+ return {
59
+ displayed,
60
+ setDisplayed,
61
+ displayedRef
62
+ };
63
+ }
64
+
65
+ export { useChoreography };
package/dist/Pill/Pill.js CHANGED
@@ -6,6 +6,7 @@ const require_morph = require("./morph.js");
6
6
  const require_PillContext = require("./PillContext.js");
7
7
  const require_scene = require("./scene.js");
8
8
  const require_themed = require("./themed.js");
9
+ const require_travel = require("./travel.js");
9
10
  const require_zone = require("./zone.js");
10
11
  const require_react = require("../utils/react.js");
11
12
  let react_jsx_runtime = require("react/jsx-runtime");
@@ -16,13 +17,19 @@ const defaultOrientation = (placement) => placement === "left" || placement ===
16
17
  * The pill: a container with the Magic Pill design language. Put any parts
17
18
  * (or anything else) inside. Its look and animations follow its data attributes.
18
19
  */
19
- const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defaultPresence = "compact", onPresenceChange, placement = "inline", ladder = true, closeOnBackdropClick = false, backdrop, orientation, variant = "neutral", theme, scene, sceneChange = "circle", unstyled = false, children, onKeyDown, ...rest }, forwardedRef) {
20
+ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defaultPresence = "compact", onPresenceChange, placement = "inline", ladder = true, moveThroughCircle = true, closeOnBackdropClick = false, backdrop, orientation, variant = "neutral", theme, scene, sceneChange = "circle", unstyled = false, children, onKeyDown, ...rest }, forwardedRef) {
20
21
  const [uncontrolledPresence, setUncontrolledPresence] = (0, react.useState)(defaultPresence);
21
22
  const isControlled = presenceProp !== void 0;
22
23
  const askedPresence = isControlled ? presenceProp : uncontrolledPresence;
23
- const resolvedOrientation = orientation ?? defaultOrientation(placement);
24
- const isOverlay = placement === "overlay";
25
24
  const rootRef = (0, react.useRef)(null);
25
+ const moved = require_travel.useTravel(rootRef, {
26
+ placement,
27
+ orientation: orientation ?? defaultOrientation(placement),
28
+ presence: askedPresence,
29
+ enabled: moveThroughCircle && !unstyled
30
+ });
31
+ const { placement: shownPlacement, orientation: resolvedOrientation } = moved;
32
+ const isOverlay = shownPlacement === "overlay";
26
33
  const ref = (0, react.useMemo)(() => require_react.mergeRefs(rootRef, forwardedRef), [forwardedRef]);
27
34
  const panelTriggerRef = (0, react.useRef)(null);
28
35
  const panelId = (0, react.useId)();
@@ -32,7 +39,7 @@ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defau
32
39
  const shown = require_scene.useScene(rootRef, {
33
40
  scene,
34
41
  change: sceneChange,
35
- presence: askedPresence,
42
+ presence: moved.presence,
36
43
  enabled: !unstyled,
37
44
  onClosed: () => closePanelRef.current(),
38
45
  children,
@@ -42,7 +49,7 @@ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defau
42
49
  const isVisible = presence !== "hidden";
43
50
  require_modal.useModal(rootRef, isOverlay, presence, lastSize);
44
51
  require_morph.useSizeMorph(rootRef, presence, !unstyled, lastSize);
45
- require_zone.useZone(rootRef, ladder && !unstyled && !isOverlay && placement !== "inline" ? placement : void 0, presence);
52
+ require_zone.useZone(rootRef, ladder && !unstyled && !isOverlay && shownPlacement !== "inline" ? shownPlacement : void 0, presence);
46
53
  require_themed.useThemed(rootRef, unstyled ? void 0 : theme, presence, isOverlay);
47
54
  const returnPresenceRef = (0, react.useRef)("compact");
48
55
  (0, react.useEffect)(() => {
@@ -99,7 +106,7 @@ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defau
99
106
  panel,
100
107
  openPanel,
101
108
  closePanel,
102
- placement,
109
+ placement: shownPlacement,
103
110
  orientation: resolvedOrientation,
104
111
  variant: shown.variant,
105
112
  panelIdOf: (name) => name === void 0 ? panelId : `${panelId}-${name}`,
@@ -111,7 +118,7 @@ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defau
111
118
  panel,
112
119
  openPanel,
113
120
  closePanel,
114
- placement,
121
+ shownPlacement,
115
122
  resolvedOrientation,
116
123
  shown.variant,
117
124
  panelId
@@ -124,7 +131,7 @@ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defau
124
131
  ref,
125
132
  "data-mp": "pill",
126
133
  "data-presence": presence,
127
- "data-placement": placement,
134
+ "data-placement": shownPlacement,
128
135
  "data-orientation": resolvedOrientation,
129
136
  "data-variant": shown.variant,
130
137
  ...theme !== void 0 && { "data-theme": theme },
@@ -141,8 +148,12 @@ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defau
141
148
  }
142
149
  },
143
150
  children: [
151
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_morph.ExitWatch, {
152
+ presence,
153
+ target: rootRef
154
+ }),
144
155
  /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_morph.PlacementWatch, {
145
- placement: `${placement} ${resolvedOrientation}`,
156
+ placement: `${shownPlacement} ${resolvedOrientation}`,
146
157
  target: rootRef
147
158
  }),
148
159
  /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_scene.SceneWatch, {
@@ -2,10 +2,11 @@
2
2
 
3
3
  import { Fragment, forwardRef, useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
4
4
  import { isOnBackdrop, useModal } from "./modal.mjs";
5
- import { PlacementWatch, useSizeMorph } from "./morph.mjs";
5
+ import { ExitWatch, PlacementWatch, useSizeMorph } from "./morph.mjs";
6
6
  import { PillContext } from "./PillContext.mjs";
7
7
  import { SceneWatch, useScene } from "./scene.mjs";
8
8
  import { useThemed } from "./themed.mjs";
9
+ import { useTravel } from "./travel.mjs";
9
10
  import { useZone } from "./zone.mjs";
10
11
  import { inertProps, mergeRefs } from "../utils/react.mjs";
11
12
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -16,13 +17,19 @@ const defaultOrientation = (placement) => placement === "left" || placement ===
16
17
  * The pill: a container with the Magic Pill design language. Put any parts
17
18
  * (or anything else) inside. Its look and animations follow its data attributes.
18
19
  */
19
- const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence = "compact", onPresenceChange, placement = "inline", ladder = true, closeOnBackdropClick = false, backdrop, orientation, variant = "neutral", theme, scene, sceneChange = "circle", unstyled = false, children, onKeyDown, ...rest }, forwardedRef) {
20
+ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence = "compact", onPresenceChange, placement = "inline", ladder = true, moveThroughCircle = true, closeOnBackdropClick = false, backdrop, orientation, variant = "neutral", theme, scene, sceneChange = "circle", unstyled = false, children, onKeyDown, ...rest }, forwardedRef) {
20
21
  const [uncontrolledPresence, setUncontrolledPresence] = useState(defaultPresence);
21
22
  const isControlled = presenceProp !== void 0;
22
23
  const askedPresence = isControlled ? presenceProp : uncontrolledPresence;
23
- const resolvedOrientation = orientation ?? defaultOrientation(placement);
24
- const isOverlay = placement === "overlay";
25
24
  const rootRef = useRef(null);
25
+ const moved = useTravel(rootRef, {
26
+ placement,
27
+ orientation: orientation ?? defaultOrientation(placement),
28
+ presence: askedPresence,
29
+ enabled: moveThroughCircle && !unstyled
30
+ });
31
+ const { placement: shownPlacement, orientation: resolvedOrientation } = moved;
32
+ const isOverlay = shownPlacement === "overlay";
26
33
  const ref = useMemo(() => mergeRefs(rootRef, forwardedRef), [forwardedRef]);
27
34
  const panelTriggerRef = useRef(null);
28
35
  const panelId = useId();
@@ -32,7 +39,7 @@ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence
32
39
  const shown = useScene(rootRef, {
33
40
  scene,
34
41
  change: sceneChange,
35
- presence: askedPresence,
42
+ presence: moved.presence,
36
43
  enabled: !unstyled,
37
44
  onClosed: () => closePanelRef.current(),
38
45
  children,
@@ -42,7 +49,7 @@ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence
42
49
  const isVisible = presence !== "hidden";
43
50
  useModal(rootRef, isOverlay, presence, lastSize);
44
51
  useSizeMorph(rootRef, presence, !unstyled, lastSize);
45
- useZone(rootRef, ladder && !unstyled && !isOverlay && placement !== "inline" ? placement : void 0, presence);
52
+ useZone(rootRef, ladder && !unstyled && !isOverlay && shownPlacement !== "inline" ? shownPlacement : void 0, presence);
46
53
  useThemed(rootRef, unstyled ? void 0 : theme, presence, isOverlay);
47
54
  const returnPresenceRef = useRef("compact");
48
55
  useEffect(() => {
@@ -99,7 +106,7 @@ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence
99
106
  panel,
100
107
  openPanel,
101
108
  closePanel,
102
- placement,
109
+ placement: shownPlacement,
103
110
  orientation: resolvedOrientation,
104
111
  variant: shown.variant,
105
112
  panelIdOf: (name) => name === void 0 ? panelId : `${panelId}-${name}`,
@@ -111,7 +118,7 @@ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence
111
118
  panel,
112
119
  openPanel,
113
120
  closePanel,
114
- placement,
121
+ shownPlacement,
115
122
  resolvedOrientation,
116
123
  shown.variant,
117
124
  panelId
@@ -124,7 +131,7 @@ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence
124
131
  ref,
125
132
  "data-mp": "pill",
126
133
  "data-presence": presence,
127
- "data-placement": placement,
134
+ "data-placement": shownPlacement,
128
135
  "data-orientation": resolvedOrientation,
129
136
  "data-variant": shown.variant,
130
137
  ...theme !== void 0 && { "data-theme": theme },
@@ -141,8 +148,12 @@ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence
141
148
  }
142
149
  },
143
150
  children: [
151
+ /* @__PURE__ */ jsx(ExitWatch, {
152
+ presence,
153
+ target: rootRef
154
+ }),
144
155
  /* @__PURE__ */ jsx(PlacementWatch, {
145
- placement: `${placement} ${resolvedOrientation}`,
156
+ placement: `${shownPlacement} ${resolvedOrientation}`,
146
157
  target: rootRef
147
158
  }),
148
159
  /* @__PURE__ */ jsx(SceneWatch, {
@@ -7,6 +7,8 @@ const require_timing = require("../utils/timing.js");
7
7
  const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
8
8
  /** Tags this module's animations, so a size change can stop the one before it. */
9
9
  const MORPH = "mp-morph";
10
+ /** Tags the placement animation, which runs alongside the size ones. */
11
+ const MOVE = "mp-move";
10
12
  /** The length of the fading edge over the panel's clipped content. */
11
13
  const EDGE = 48;
12
14
  const EASING = "ease-in-out";
@@ -38,7 +40,7 @@ const held = (width) => ({
38
40
  * own animation starts), to hold them there during an animation.
39
41
  */
40
42
  function holdMessages(element) {
41
- const messages = [...element.querySelectorAll(":scope > [data-mp=\"message\"]")].map((message) => [message, held(message.getBoundingClientRect().width)]);
43
+ const messages = [...element.querySelectorAll(":scope > :is([data-mp=\"message\"], [data-mp=\"body\"], [data-mp=\"card\"])")].map((message) => [message, held(message.getBoundingClientRect().width)]);
42
44
  return (options) => messages.map(([message, frame]) => message.animate([frame, frame], options));
43
45
  }
44
46
  const sameSize = (a, b) => Math.abs(a.width - b.width) < 1 && Math.abs(a.height - b.height) < 1;
@@ -103,6 +105,78 @@ function planMorph(from, to, previous, next, timings, panel) {
103
105
  }
104
106
  return plan;
105
107
  }
108
+ /** Tags the exit played by script (see `planExit`). */
109
+ const EXIT = "mp-exit";
110
+ /**
111
+ * The pill leaving, as the stylesheet times it: its content fades out, it
112
+ * shrinks to the circle (the size morph), scales out, then stops being
113
+ * visible. The stylesheet does it with delayed transitions, which a restyle
114
+ * of the whole page can cut short: on iOS, the toolbar folding away as the
115
+ * page scrolls resizes the viewport, and the pill vanished at once instead.
116
+ * Played by script on top, the exit keeps its course. Pure, so it can be tested.
117
+ */
118
+ function planExit(start, timings, exit) {
119
+ const wait = timings.fade + timings.change;
120
+ const total = wait + exit;
121
+ if (exit <= 0 || total <= 0) return null;
122
+ const plan = { pill: {
123
+ keyframes: [
124
+ {
125
+ scale: start.scale,
126
+ visibility: "visible"
127
+ },
128
+ {
129
+ scale: start.scale,
130
+ visibility: "visible",
131
+ offset: wait / total,
132
+ easing: "ease-in"
133
+ },
134
+ {
135
+ scale: "0",
136
+ visibility: "visible"
137
+ }
138
+ ],
139
+ options: {
140
+ id: EXIT,
141
+ duration: total
142
+ }
143
+ } };
144
+ if (start.content !== void 0 && timings.fade > 0) plan.content = {
145
+ keyframes: [{ opacity: start.content }, { opacity: "0" }],
146
+ options: {
147
+ id: EXIT,
148
+ duration: timings.fade,
149
+ easing: "ease-in"
150
+ }
151
+ };
152
+ return plan;
153
+ }
154
+ /** The pill's own content: what fades out as it closes (not its icon, panels or a swap's copy). */
155
+ const fadingContent = (element) => [...element.children].filter((child) => child instanceof HTMLElement && ![
156
+ "icon",
157
+ "panel",
158
+ "outgoing"
159
+ ].includes(child.getAttribute("data-mp") ?? ""));
160
+ /** Plays the exit on the pill that has just been hidden. */
161
+ function playExit(element, timings) {
162
+ const style = getComputedStyle(element);
163
+ const scale = style.scale && style.scale !== "none" ? style.scale : "1";
164
+ cancel(element, EXIT, true);
165
+ const content = fadingContent(element);
166
+ const opacity = content.length > 0 ? getComputedStyle(content[0]).opacity : "0";
167
+ const exit = require_timing.parseDuration(style.getPropertyValue("--_mp-exit"), 300);
168
+ const plan = planExit({
169
+ scale,
170
+ content: Number(opacity) > 0 ? opacity : void 0
171
+ }, timings, exit);
172
+ if (!plan) return [];
173
+ const animations = [element.animate(plan.pill.keyframes, plan.pill.options)];
174
+ if (plan.content) {
175
+ const { keyframes, options } = plan.content;
176
+ for (const child of content) animations.push(child.animate(keyframes, options));
177
+ }
178
+ return animations;
179
+ }
106
180
  const readSize = (element) => ({
107
181
  width: element.offsetWidth,
108
182
  height: element.offsetHeight
@@ -202,6 +276,39 @@ function planSwap(from, to, timings, anchor, down = false) {
202
276
  };
203
277
  }
204
278
  const CONTROLS = "input, textarea, select";
279
+ /** What lays a copy out the way the original is on screen, whatever the pill turns into. */
280
+ const FROZEN = [
281
+ "flex-direction",
282
+ "flex-wrap",
283
+ "align-items",
284
+ "justify-content",
285
+ "align-self",
286
+ "gap",
287
+ "direction"
288
+ ];
289
+ /**
290
+ * The copy laid out as the original is now, element by element: its sizes
291
+ * and the directions its content runs in. For a pill that turns as the new
292
+ * content comes in (a sidebar's notification, from a column to a row): the
293
+ * stylesheet would lay the copy out again for the new orientation.
294
+ */
295
+ function freezeLayout(from, to) {
296
+ if (!(from instanceof HTMLElement || from instanceof SVGElement) || !("style" in to)) return;
297
+ const computed = getComputedStyle(from);
298
+ const style = to.style;
299
+ for (const property of FROZEN) style.setProperty(property, computed.getPropertyValue(property));
300
+ const { width, height } = from.getBoundingClientRect();
301
+ if (computed.display !== "inline") {
302
+ style.boxSizing = "border-box";
303
+ style.width = px(width);
304
+ style.height = px(height);
305
+ style.flexShrink = "0";
306
+ }
307
+ [...from.children].forEach((child, index) => {
308
+ const copy = to.children[index];
309
+ if (copy) freezeLayout(child, copy);
310
+ });
311
+ }
205
312
  /** What was typed or chosen: a copied control starts from its default value. */
206
313
  function copyValues(from, to) {
207
314
  const copies = to.querySelectorAll(CONTROLS);
@@ -217,11 +324,16 @@ function copyValues(from, to) {
217
324
  * laid over it, to slide out once the new content is in. Null when there's
218
325
  * nothing to animate (unstyled, reduced motion, no Web Animations API).
219
326
  */
220
- function captureSwap(element) {
327
+ function captureSwap(element, { freeze = false } = {}) {
221
328
  if (!element || element.hasAttribute("data-unstyled") || !element.animate || require_timing.prefersReducedMotion()) return null;
222
329
  const from = readExactSize(element);
223
330
  const outgoing = document.createElement("div");
224
- for (const child of element.children) if (child.getAttribute("data-mp") !== "outgoing") outgoing.append(child.cloneNode(true));
331
+ for (const child of element.children) {
332
+ if (child.getAttribute("data-mp") === "outgoing") continue;
333
+ const copy = child.cloneNode(true);
334
+ if (freeze) freezeLayout(child, copy);
335
+ outgoing.append(copy);
336
+ }
225
337
  stopMorphs(element);
226
338
  copyValues(element, outgoing);
227
339
  for (const node of outgoing.querySelectorAll("[id], [name]")) {
@@ -248,6 +360,7 @@ function captureSwap(element) {
248
360
  /** Once the new content is in: the swap prepared by `captureSwap`, played. */
249
361
  function playSwap(element, { from, outgoing, anchor, down }) {
250
362
  stopMorphs(element, outgoing);
363
+ cancel(element, MOVE);
251
364
  const plan = planSwap(from, readExactSize(element), readMorphTimings(element), anchor, down);
252
365
  const done = () => outgoing.remove();
253
366
  if (!plan) return done();
@@ -329,27 +442,32 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
329
442
  const panel = readPanel(element);
330
443
  if (presence === "panel") openPanel.current = panel?.box;
331
444
  const panelBox = previous === "panel" ? openPanel.current : panel?.box;
332
- const plan = planMorph(from, to, previous, presence, readMorphTimings(element), panelBox);
333
- if (!plan) return;
334
- const hold = holdMessages(element);
335
- const options = {
336
- ...plan.options,
337
- id: MORPH
338
- };
339
- const animations = [element.animate(plan.pill, options)];
340
- if (plan.panel && panel) {
341
- animations.push(panel.element.animate(plan.panel, options));
342
- const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
343
- if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
344
- }
345
- if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
346
- if (plan.content) {
347
- const { keyframes, options } = plan.content;
348
- for (const child of element.children) if (child !== panel?.element) animations.push(child.animate(keyframes, {
349
- ...options,
445
+ const timings = readMorphTimings(element);
446
+ const plan = planMorph(from, to, previous, presence, timings, panelBox);
447
+ const animations = [];
448
+ if (plan) {
449
+ const hold = holdMessages(element);
450
+ const options = {
451
+ ...plan.options,
350
452
  id: MORPH
351
- }));
453
+ };
454
+ animations.push(element.animate(plan.pill, options));
455
+ if (plan.panel && panel) {
456
+ animations.push(panel.element.animate(plan.panel, options));
457
+ const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
458
+ if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
459
+ }
460
+ if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
461
+ if (plan.content) {
462
+ const { keyframes, options } = plan.content;
463
+ for (const child of element.children) if (child !== panel?.element) animations.push(child.animate(keyframes, {
464
+ ...options,
465
+ id: MORPH
466
+ }));
467
+ }
352
468
  }
469
+ if (presence === "hidden") animations.push(...playExit(element, timings));
470
+ if (animations.length === 0) return;
353
471
  running.current = animations;
354
472
  animations[0].finished.then(() => {
355
473
  if (running.current === animations) running.current = [];
@@ -361,8 +479,39 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
361
479
  lastSize
362
480
  ]);
363
481
  }
364
- /** Tags the placement animation, which runs alongside the size ones. */
365
- const MOVE = "mp-move";
482
+ /**
483
+ * When the pill is asked back before its exit is over: the exit played by
484
+ * script stops, and the pill grows back from the scale it had got to, as
485
+ * the stylesheet's entry does. The stylesheet can't do it here: browsers
486
+ * don't turn a transition back once a script has animated the same property,
487
+ * so it would carry on scaling out first. Its leftover transition is dropped.
488
+ */
489
+ var ExitWatch = class extends react.Component {
490
+ getSnapshotBeforeUpdate(previous) {
491
+ const element = this.props.target.current;
492
+ if (!element || previous.presence !== "hidden" || this.props.presence === "hidden") return null;
493
+ if ((element.getAnimations?.() ?? []).filter(({ id }) => id === EXIT).length === 0) return null;
494
+ const { scale } = getComputedStyle(element);
495
+ cancel(element, EXIT, true);
496
+ return scale && scale !== "none" ? scale : "1";
497
+ }
498
+ componentDidUpdate(_props, _state, scale) {
499
+ const element = this.props.target.current;
500
+ if (scale === null || !element) return;
501
+ for (const animation of element.getAnimations()) if (animation.transitionProperty === "scale") animation.cancel();
502
+ if (Number(scale) >= 1) return;
503
+ const style = getComputedStyle(element);
504
+ const enter = require_timing.parseDuration(style.getPropertyValue("--_mp-enter"), 500);
505
+ element.animate([{ scale }, { scale: "1" }], {
506
+ id: EXIT,
507
+ duration: enter * Math.min(1, 1 - Number(scale)),
508
+ easing: style.getPropertyValue("--_mp-ease-enter").trim() || "ease-out"
509
+ });
510
+ }
511
+ render() {
512
+ return null;
513
+ }
514
+ };
366
515
  /**
367
516
  * The pill moved to another placement (or orientation): it travels there
368
517
  * from where it was (`from`), resizing on the way if its size changed.
@@ -411,11 +560,13 @@ var PlacementWatch = class extends react.Component {
411
560
  };
412
561
 
413
562
  exports.EASING = EASING;
563
+ exports.ExitWatch = ExitWatch;
414
564
  exports.MORPH = MORPH;
415
565
  exports.PlacementWatch = PlacementWatch;
416
566
  exports.animateMove = animateMove;
417
567
  exports.cancel = cancel;
418
568
  exports.captureSwap = captureSwap;
569
+ exports.planExit = planExit;
419
570
  exports.planMorph = planMorph;
420
571
  exports.planSwap = planSwap;
421
572
  exports.playSwap = playSwap;