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