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
@@ -1,16 +1,18 @@
1
1
  "use client";
2
2
 
3
- import { Fragment, forwardRef, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
3
+ import { Fragment, forwardRef, useEffect, useMemo, useRef, useState } from "react";
4
4
  import Pill from "../Pill/Pill.mjs";
5
- import { captureSwap, playSwap } from "../Pill/morph.mjs";
6
5
  import { PillIcon } from "../Pill/parts.mjs";
7
6
  import { NotificationContent } from "../PillNotification/PillNotification.mjs";
8
7
  import { getStore } from "../store/store.mjs";
9
8
  import { getInputModality, trackInputModality } from "../utils/inputModality.mjs";
10
9
  import { usePillQueue } from "../store/usePillQueue.mjs";
11
- import { mergeRefs } from "../utils/react.mjs";
10
+ import { mergeRefs, useIsomorphicLayoutEffect } from "../utils/react.mjs";
11
+ import { prefersReducedMotion } from "../utils/timing.mjs";
12
12
  import { VISUALLY_HIDDEN } from "../utils/visuallyHidden.mjs";
13
- import { BASE, IDLE, NONE, batchSteps, isOpen, isSwap, planSteps, readTimings } from "./choreography.mjs";
13
+ import { BASE, IDLE, NONE, isOpen, readTimings } from "./choreography.mjs";
14
+ import { flagStyle, measureFlagSpot, playBridge } from "./flag.mjs";
15
+ import { useChoreography } from "./useChoreography.mjs";
14
16
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
15
17
 
16
18
  // src/MagicPill/MagicPill.tsx
@@ -33,14 +35,59 @@ function useDocumentHidden() {
33
35
  return hidden;
34
36
  }
35
37
  /**
38
+ * Pauses a notification while it's pointed at or has the keyboard focus
39
+ * (Y3, L22). The handlers go on the pill that shows it.
40
+ */
41
+ function usePause(rootRef, presence) {
42
+ const [hovered, setHovered] = useState(false);
43
+ const [focused, setFocused] = useState(false);
44
+ useEffect(() => {
45
+ if (presence === "hidden") {
46
+ setHovered(false);
47
+ setFocused(false);
48
+ }
49
+ }, [presence]);
50
+ useEffect(() => {
51
+ const root = rootRef.current;
52
+ if (!focused || !root) return;
53
+ const update = () => {
54
+ if (!root.contains(document.activeElement)) setFocused(false);
55
+ };
56
+ update();
57
+ const observer = new MutationObserver(update);
58
+ observer.observe(root, {
59
+ childList: true,
60
+ subtree: true
61
+ });
62
+ return () => observer.disconnect();
63
+ }, [rootRef, focused]);
64
+ return {
65
+ active: hovered || focused,
66
+ handlers: {
67
+ onPointerEnter: (event) => {
68
+ if (event.pointerType !== "touch") setHovered(true);
69
+ },
70
+ onPointerLeave: () => setHovered(false),
71
+ onFocus: () => {
72
+ if (getInputModality() === "keyboard") setFocused(true);
73
+ },
74
+ onBlur: (event) => {
75
+ if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
76
+ }
77
+ }
78
+ };
79
+ }
80
+ const notificationIn = ({ scene, presence }) => scene.kind === "notification" && isOpen(presence) ? scene.item : null;
81
+ /**
36
82
  * The batteries-included pill: a <Pill> with a base layer, a queue of
37
83
  * notifications fed by pill(name), and a live region that announces them.
38
84
  */
39
- const MagicPill = forwardRef(function MagicPill({ name = "main", children, scene, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, swap = "queue", linkComponent, classNames, placement = "bottom", variant, onPointerEnter, onPointerLeave, onFocus, onBlur, ...rest }, forwardedRef) {
85
+ const MagicPill = forwardRef(function MagicPill({ name = "main", children, scene, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, swap = "queue", notificationLayout = "pill", linkComponent, classNames, placement = "bottom", orientation, moveThroughCircle, variant, onPointerEnter, onPointerLeave, onFocus, onBlur, ...rest }, forwardedRef) {
40
86
  const store = getStore(name);
41
87
  const { current } = usePillQueue(name);
42
88
  const rootRef = useRef(null);
43
89
  const ref = useMemo(() => mergeRefs(rootRef, forwardedRef), [forwardedRef]);
90
+ const flagRef = useRef(null);
44
91
  useEffect(() => store.registerHost(), [store]);
45
92
  useEffect(trackInputModality, []);
46
93
  const hasBase = hasContent(children);
@@ -57,82 +104,103 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, scene
57
104
  });
58
105
  const [isFillingIn, setIsFillingIn] = useState(false);
59
106
  const isWaiting = isFillingIn && hasBase;
60
- const target = useMemo(() => current && !isWaiting ? {
107
+ const onSide = placement === "left" || placement === "right";
108
+ const isVertical = orientation === "vertical" || orientation === void 0 && onSide;
109
+ const layoutOf = (item) => {
110
+ const layout = item.input.layout ?? notificationLayout;
111
+ if (layout === "card" && !isVertical) return "pill";
112
+ if (layout === "flag" && !(isVertical && onSide && hasBase)) return "pill";
113
+ return layout;
114
+ };
115
+ const sceneLayout = (shown) => shown.kind === "notification" ? layoutOf(shown.item) : void 0;
116
+ const isFlag = current !== null && current !== void 0 && layoutOf(current) === "flag";
117
+ const target = useMemo(() => current && !isWaiting && !isFlag ? {
61
118
  kind: "notification",
62
119
  item: current
63
120
  } : hasBase ? base : idle === "rest" ? IDLE : NONE, [
64
121
  current,
65
122
  isWaiting,
123
+ isFlag,
66
124
  hasBase,
67
125
  base,
68
126
  idle
69
127
  ]);
70
- const [displayed, setDisplayed] = useState(() => ({
71
- scene: target.kind === "notification" ? NONE : target,
72
- presence: target.kind === "base" ? "compact" : target.kind === "idle" ? "rest" : "hidden"
73
- }));
74
- const displayedRef = useRef(displayed);
75
- displayedRef.current = displayed;
76
- const followUps = useRef([]);
77
- const capture = useRef(null);
78
- const swapMode = useRef(swap);
79
- swapMode.current = swap;
80
- useLayoutEffect(() => {
81
- const swapped = capture.current;
82
- capture.current = null;
83
- if (swapped && rootRef.current) playSwap(rootRef.current, swapped);
84
- }, [displayed]);
128
+ const flagTarget = useMemo(() => current && !isWaiting && isFlag ? {
129
+ kind: "notification",
130
+ item: current
131
+ } : NONE, [
132
+ current,
133
+ isWaiting,
134
+ isFlag
135
+ ]);
136
+ const orientationOf = (shown) => isVertical && shown.kind === "notification" && sceneLayout(shown) !== "card" ? "horizontal" : orientation;
137
+ const sidebarWidth = useRef(void 0);
138
+ const [isSliding, setIsSliding] = useState(false);
139
+ const { displayed, setDisplayed, displayedRef } = useChoreography(rootRef, target, {
140
+ swapMode: (from, to) => {
141
+ if (!isVertical || swap !== "always") return swap;
142
+ const layout = sceneLayout(to.kind === "notification" ? to : from);
143
+ return layout === "rows" || layout === "card" ? "always" : "queue";
144
+ },
145
+ turns: (from, to) => (orientationOf(from) ?? "") !== (orientationOf(to) ?? ""),
146
+ beforeApply: (from) => {
147
+ if (from.scene.kind === "base" && isOpen(from.presence) && rootRef.current) sidebarWidth.current = rootRef.current.getBoundingClientRect().width;
148
+ },
149
+ onSlide: setIsSliding
150
+ });
151
+ const [flagSpot, setFlagSpot] = useState({ shift: 0 });
152
+ const flagFrom = useRef(null);
153
+ const { displayed: flagDisplayed, setDisplayed: setFlagDisplayed, displayedRef: flagDisplayedRef } = useChoreography(flagRef, flagTarget, {
154
+ swapMode: () => swap === "never" ? "never" : "queue",
155
+ beforeApply: (from, next) => {
156
+ if (next.scene.kind !== "notification" || next.scene === from.scene || !rootRef.current) return;
157
+ if (flagRef.current && isOpen(from.presence)) flagFrom.current = flagRef.current.getBoundingClientRect();
158
+ setFlagSpot(measureFlagSpot(rootRef.current, next.scene.item.input.at));
159
+ }
160
+ });
161
+ const flagY = flagDisplayed.presence === "panel" ? void 0 : flagSpot.y;
162
+ useIsomorphicLayoutEffect(() => {
163
+ const from = flagFrom.current;
164
+ flagFrom.current = null;
165
+ const flag = flagRef.current;
166
+ if (!from || !flag || typeof flag.animate !== "function" || prefersReducedMotion()) return;
167
+ const to = flag.getBoundingClientRect();
168
+ const shift = from.top + from.height / 2 - (to.top + to.height / 2);
169
+ if (Math.abs(shift) < 1) return;
170
+ const { fade, change } = readTimings(flag);
171
+ flag.animate([{ translate: `0px ${shift}px` }, { translate: "0px 0px" }], {
172
+ duration: fade + change,
173
+ easing: "ease-in-out",
174
+ composite: "add"
175
+ });
176
+ }, [flagSpot, flagY]);
177
+ const flagPresence = useRef(flagDisplayed.presence);
178
+ useIsomorphicLayoutEffect(() => {
179
+ const previous = flagPresence.current;
180
+ flagPresence.current = flagDisplayed.presence;
181
+ const sidebar = rootRef.current;
182
+ const flag = flagRef.current;
183
+ if (!sidebar || !flag || previous === flagDisplayed.presence) return;
184
+ const { enter, fade, change, exit } = readTimings(flag);
185
+ if (previous === "hidden" && flagDisplayed.presence === "rest") playBridge(sidebar, flag, flagSpot.y, "split", {
186
+ delay: 0,
187
+ duration: enter
188
+ });
189
+ else if (flagDisplayed.presence === "hidden") playBridge(sidebar, flag, flagSpot.y, "join", {
190
+ delay: fade + change,
191
+ duration: exit
192
+ });
193
+ }, [flagDisplayed.presence]);
85
194
  useEffect(() => {
86
195
  const hasForm = () => !!rootRef.current?.querySelector("[data-mp=\"panel\"][data-open] form");
87
196
  setIsFillingIn(displayed.scene.kind === "base" && displayed.presence === "panel" && hasForm());
88
197
  }, [displayed]);
89
- useEffect(() => {
90
- const next = followUps.current.shift();
91
- if (!next) return;
92
- rootRef.current?.offsetWidth;
93
- setDisplayed(next);
94
- }, [displayed]);
95
- useEffect(() => {
96
- const current = displayedRef.current;
97
- const timers = batchSteps(current, planSteps(current, target, readTimings(rootRef.current), swapMode.current)).map(({ at, states }) => {
98
- const apply = () => {
99
- if (isSwap(displayedRef.current, states[0], swapMode.current)) capture.current = captureSwap(rootRef.current);
100
- followUps.current = states.slice(1);
101
- setDisplayed(states[0]);
102
- };
103
- if (at > 0) return setTimeout(apply, at);
104
- apply();
105
- });
106
- return () => {
107
- timers.forEach(clearTimeout);
108
- followUps.current = [];
109
- };
110
- }, [target]);
111
- const [hovered, setHovered] = useState(false);
112
- const [focused, setFocused] = useState(false);
198
+ const mainPause = usePause(rootRef, displayed.presence);
199
+ const flagPause = usePause(flagRef, flagDisplayed.presence);
113
200
  const documentHidden = useDocumentHidden();
114
- useEffect(() => {
115
- if (displayed.presence === "hidden") {
116
- setHovered(false);
117
- setFocused(false);
118
- }
119
- }, [displayed.presence]);
120
- useEffect(() => {
121
- const root = rootRef.current;
122
- if (!focused || !root) return;
123
- const update = () => {
124
- if (!root.contains(document.activeElement)) setFocused(false);
125
- };
126
- update();
127
- const observer = new MutationObserver(update);
128
- observer.observe(root, {
129
- childList: true,
130
- subtree: true
131
- });
132
- return () => observer.disconnect();
133
- }, [focused]);
134
- const shown = displayed.scene.kind === "notification" && isOpen(displayed.presence) ? displayed.scene.item : null;
135
- const paused = hovered || focused || documentHidden || displayed.presence === "panel";
201
+ const inFlag = notificationIn(flagDisplayed);
202
+ const shown = notificationIn(displayed) ?? inFlag;
203
+ const paused = documentHidden || (shown !== null && shown === inFlag ? flagPause.active || flagDisplayed.presence === "panel" : mainPause.active || displayed.presence === "panel");
136
204
  useEffect(() => {
137
205
  if (!shown || paused) return;
138
206
  const ms = shown.input.duration ?? duration;
@@ -150,29 +218,29 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, scene
150
218
  if (!shown) return;
151
219
  const { announce } = shown.input;
152
220
  if (announce === false) return;
153
- const text = typeof announce === "string" ? announce : readMessage(rootRef.current);
221
+ const text = typeof announce === "string" ? announce : readMessage(shown === inFlag ? flagRef.current : rootRef.current);
154
222
  if (text) setAnnouncement((previous) => previous?.key === shown ? previous : {
155
223
  key: shown,
156
224
  text
157
225
  });
158
- }, [shown]);
159
- const handlePresenceChange = (next) => {
160
- const { scene, presence } = displayedRef.current;
226
+ }, [shown, inFlag]);
227
+ const presenceHandler = (stateRef, setState) => (next) => {
228
+ const { scene: shownScene, presence } = stateRef.current;
161
229
  if (next === "hidden") {
162
- if (scene.kind === "notification") store.dismiss(scene.item.id, "dismissed");
230
+ if (shownScene.kind === "notification") store.dismiss(shownScene.item.id, "dismissed");
163
231
  return;
164
232
  }
165
- if (isOpen(next) && isOpen(presence)) setDisplayed({
166
- scene,
233
+ if (isOpen(next) && isOpen(presence)) setState({
234
+ scene: shownScene,
167
235
  presence: next
168
236
  });
169
237
  };
170
- const renderScene = (scene) => {
171
- switch (scene.kind) {
172
- case "base": return /* @__PURE__ */ jsx(Fragment, { children: lastScenes.current.get(scene.key)?.children ?? lastBase.current }, scene.key);
238
+ const renderScene = (shownScene) => {
239
+ switch (shownScene.kind) {
240
+ case "base": return /* @__PURE__ */ jsx(Fragment, { children: lastScenes.current.get(shownScene.key)?.children ?? lastBase.current }, shownScene.key);
173
241
  case "idle": return /* @__PURE__ */ jsx(PillIcon, { children: idleIcon });
174
242
  case "notification": {
175
- const { id, input } = scene.item;
243
+ const { id, input } = shownScene.item;
176
244
  if (input.render) return input.render({
177
245
  id,
178
246
  dismiss: () => store.dismiss(id, "dismissed")
@@ -182,6 +250,7 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, scene
182
250
  message: input.message,
183
251
  action: input.action,
184
252
  panel: input.panel,
253
+ layout: layoutOf(shownScene.item),
185
254
  linkComponent,
186
255
  classNames,
187
256
  onAction: (event) => {
@@ -195,38 +264,70 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, scene
195
264
  const displayedKey = displayed.scene.kind === "base" ? displayed.scene.key : scene;
196
265
  for (const key of lastScenes.current.keys()) if (key !== scene && key !== displayedKey) lastScenes.current.delete(key);
197
266
  const sceneVariant = displayed.scene.kind === "notification" ? displayed.scene.item.input.variant ?? variant : (lastScenes.current.get(displayedKey) ?? { variant }).variant;
198
- return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Pill, {
199
- ...rest,
200
- ref,
201
- placement,
202
- variant: sceneVariant,
203
- presence: displayed.presence,
204
- onPresenceChange: handlePresenceChange,
205
- onPointerEnter: (event) => {
206
- onPointerEnter?.(event);
207
- if (event.pointerType !== "touch") setHovered(true);
208
- },
209
- onPointerLeave: (event) => {
210
- onPointerLeave?.(event);
211
- setHovered(false);
212
- },
213
- onFocus: (event) => {
214
- onFocus?.(event);
215
- if (getInputModality() === "keyboard") setFocused(true);
216
- },
217
- onBlur: (event) => {
218
- onBlur?.(event);
219
- if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
220
- },
221
- children: renderScene(displayed.scene)
222
- }), /* @__PURE__ */ jsx("div", {
223
- role: "status",
224
- "aria-live": "polite",
225
- "aria-atomic": "true",
226
- "data-mp": "announcer",
227
- style: VISUALLY_HIDDEN,
228
- children: announcement && /* @__PURE__ */ jsx("span", { children: announcement.text }, announcement.key.id + announcement.text)
229
- })] });
267
+ const mainLayout = sceneLayout(displayed.scene);
268
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
269
+ /* @__PURE__ */ jsx(Pill, {
270
+ ...rest,
271
+ ref,
272
+ placement,
273
+ orientation: orientationOf(displayed.scene),
274
+ moveThroughCircle: moveThroughCircle !== false && !isSliding,
275
+ "data-layout": mainLayout,
276
+ style: mainLayout === "card" && sidebarWidth.current ? {
277
+ ...rest.style,
278
+ "--_mp-sidebar-width": `${sidebarWidth.current}px`
279
+ } : rest.style,
280
+ variant: sceneVariant,
281
+ presence: displayed.presence,
282
+ onPresenceChange: presenceHandler(displayedRef, setDisplayed),
283
+ onPointerEnter: (event) => {
284
+ onPointerEnter?.(event);
285
+ mainPause.handlers.onPointerEnter(event);
286
+ },
287
+ onPointerLeave: (event) => {
288
+ onPointerLeave?.(event);
289
+ mainPause.handlers.onPointerLeave();
290
+ },
291
+ onFocus: (event) => {
292
+ onFocus?.(event);
293
+ mainPause.handlers.onFocus();
294
+ },
295
+ onBlur: (event) => {
296
+ onBlur?.(event);
297
+ mainPause.handlers.onBlur(event);
298
+ },
299
+ children: renderScene(displayed.scene)
300
+ }),
301
+ (isVertical && onSide && hasBase || flagDisplayed.presence !== "hidden") && /* @__PURE__ */ jsx(Pill, {
302
+ ref: flagRef,
303
+ placement,
304
+ orientation: "horizontal",
305
+ ladder: false,
306
+ moveThroughCircle: false,
307
+ theme: rest.theme,
308
+ variant: flagDisplayed.scene.kind === "notification" ? flagDisplayed.scene.item.input.variant ?? variant : variant,
309
+ presence: flagDisplayed.presence,
310
+ onPresenceChange: (next) => {
311
+ if (flagRef.current && isOpen(next)) flagFrom.current = flagRef.current.getBoundingClientRect();
312
+ presenceHandler(flagDisplayedRef, setFlagDisplayed)(next);
313
+ },
314
+ "data-layout": "flag",
315
+ style: flagStyle({
316
+ shift: flagSpot.shift,
317
+ y: flagY
318
+ }),
319
+ ...flagPause.handlers,
320
+ children: renderScene(flagDisplayed.scene)
321
+ }),
322
+ /* @__PURE__ */ jsx("div", {
323
+ role: "status",
324
+ "aria-live": "polite",
325
+ "aria-atomic": "true",
326
+ "data-mp": "announcer",
327
+ style: VISUALLY_HIDDEN,
328
+ children: announcement && /* @__PURE__ */ jsx("span", { children: announcement.text }, announcement.key.id + announcement.text)
329
+ })
330
+ ] });
230
331
  });
231
332
 
232
333
  export { MagicPill as default };
@@ -0,0 +1,93 @@
1
+ const require_matchPath = require("../PillNavbar/matchPath.js");
2
+ const require_timing = require("../utils/timing.js");
3
+
4
+ // src/MagicPill/flag.ts
5
+ /** Measures the sidebar, and the navbar item whose `href` matches `at`, if any. */
6
+ function measureFlagSpot(sidebar, at) {
7
+ const ownShift = Number.parseFloat(getComputedStyle(sidebar).getPropertyValue("--_mp-shift-x")) || 0;
8
+ const spot = { shift: sidebar.offsetWidth + ownShift };
9
+ if (at === void 0) return spot;
10
+ const links = [...sidebar.querySelectorAll("[data-mp=\"navbar\"] [data-href]")];
11
+ const index = require_matchPath.findActiveIndex(at, links.map((link) => ({ href: link.dataset.href ?? "" })));
12
+ if (index >= 0) {
13
+ const { top, height } = links[index].getBoundingClientRect();
14
+ spot.y = top + height / 2;
15
+ }
16
+ return spot;
17
+ }
18
+ /**
19
+ * The flag's style: past the sidebar by a short gap, and centred on its
20
+ * section's item if it has one (between two edges as far from it on either
21
+ * side, inside the screen's offset), in the middle otherwise, like the
22
+ * sidebar. An open panel goes in the middle (`y` left out): it may be too
23
+ * tall to centre on an item near an edge.
24
+ */
25
+ function flagStyle({ shift, y }) {
26
+ const style = { "--_mp-shift-x": `calc(${shift}px + var(--_mp-gap) * 0.75)` };
27
+ if (y !== void 0) {
28
+ const half = `max(0px, min(${y}px - var(--_mp-offset), 100dvh - var(--_mp-offset) - ${y}px))`;
29
+ style.insetBlockStart = `calc(${y}px - ${half})`;
30
+ style.insetBlockEnd = `calc(100dvh - ${y}px - ${half})`;
31
+ }
32
+ return style;
33
+ }
34
+ /**
35
+ * The bridge between the sidebar and its flag: it stretches out of the
36
+ * sidebar as the flag pops out of it, then thins away as they part
37
+ * (`split`), or the other way round as the flag goes back (`join`).
38
+ */
39
+ function playBridge(sidebar, flag, y, kind, timing) {
40
+ if (timing.duration <= 0 || require_timing.prefersReducedMotion() || typeof document.body.animate !== "function") return;
41
+ const rect = sidebar.getBoundingClientRect();
42
+ const flagStyles = getComputedStyle(flag);
43
+ const gap = (Number.parseFloat(flagStyles.columnGap) || 16) * .75;
44
+ const onRight = flag.dataset.placement === "right";
45
+ const overlap = 8;
46
+ const thickness = 22;
47
+ const centre = y ?? rect.top + rect.height / 2;
48
+ const bridge = document.createElement("div");
49
+ bridge.setAttribute("data-mp", "flag-bridge");
50
+ bridge.setAttribute("aria-hidden", "true");
51
+ const from = getComputedStyle(sidebar).backgroundColor;
52
+ const to = flagStyles.backgroundColor;
53
+ bridge.style.cssText = [
54
+ "position:fixed",
55
+ "pointer-events:none",
56
+ `z-index:${(Number.parseInt(flagStyles.zIndex, 10) || 50) - 1}`,
57
+ `top:${centre - thickness / 2}px`,
58
+ `left:${onRight ? rect.left - gap - overlap : rect.right - overlap}px`,
59
+ `width:${gap + 16}px`,
60
+ `height:${thickness}px`,
61
+ `border-radius:${thickness / 2}px`,
62
+ `background:linear-gradient(to ${onRight ? "left" : "right"}, ${from}, ${to})`,
63
+ `transform-origin:${onRight ? "right" : "left"} center`,
64
+ "transform:scaleX(0)"
65
+ ].join(";");
66
+ document.body.append(bridge);
67
+ const out = [
68
+ { transform: "scaleX(0)" },
69
+ {
70
+ transform: "scaleX(1)",
71
+ offset: .35
72
+ },
73
+ {
74
+ transform: "scaleX(1) scaleY(0.35)",
75
+ offset: .7
76
+ },
77
+ { transform: "scaleX(1) scaleY(0)" }
78
+ ];
79
+ const remove = () => bridge.remove();
80
+ bridge.animate(kind === "split" ? out : [...out].reverse().map((frame, index, all) => ({
81
+ ...frame,
82
+ offset: index / (all.length - 1)
83
+ })), {
84
+ delay: timing.delay,
85
+ duration: timing.duration,
86
+ easing: "ease-out",
87
+ fill: "both"
88
+ }).finished.then(remove, remove);
89
+ }
90
+
91
+ exports.flagStyle = flagStyle;
92
+ exports.measureFlagSpot = measureFlagSpot;
93
+ exports.playBridge = playBridge;
@@ -0,0 +1,91 @@
1
+ import { findActiveIndex } from "../PillNavbar/matchPath.mjs";
2
+ import { prefersReducedMotion } from "../utils/timing.mjs";
3
+
4
+ // src/MagicPill/flag.ts
5
+ /** Measures the sidebar, and the navbar item whose `href` matches `at`, if any. */
6
+ function measureFlagSpot(sidebar, at) {
7
+ const ownShift = Number.parseFloat(getComputedStyle(sidebar).getPropertyValue("--_mp-shift-x")) || 0;
8
+ const spot = { shift: sidebar.offsetWidth + ownShift };
9
+ if (at === void 0) return spot;
10
+ const links = [...sidebar.querySelectorAll("[data-mp=\"navbar\"] [data-href]")];
11
+ const index = findActiveIndex(at, links.map((link) => ({ href: link.dataset.href ?? "" })));
12
+ if (index >= 0) {
13
+ const { top, height } = links[index].getBoundingClientRect();
14
+ spot.y = top + height / 2;
15
+ }
16
+ return spot;
17
+ }
18
+ /**
19
+ * The flag's style: past the sidebar by a short gap, and centred on its
20
+ * section's item if it has one (between two edges as far from it on either
21
+ * side, inside the screen's offset), in the middle otherwise, like the
22
+ * sidebar. An open panel goes in the middle (`y` left out): it may be too
23
+ * tall to centre on an item near an edge.
24
+ */
25
+ function flagStyle({ shift, y }) {
26
+ const style = { "--_mp-shift-x": `calc(${shift}px + var(--_mp-gap) * 0.75)` };
27
+ if (y !== void 0) {
28
+ const half = `max(0px, min(${y}px - var(--_mp-offset), 100dvh - var(--_mp-offset) - ${y}px))`;
29
+ style.insetBlockStart = `calc(${y}px - ${half})`;
30
+ style.insetBlockEnd = `calc(100dvh - ${y}px - ${half})`;
31
+ }
32
+ return style;
33
+ }
34
+ /**
35
+ * The bridge between the sidebar and its flag: it stretches out of the
36
+ * sidebar as the flag pops out of it, then thins away as they part
37
+ * (`split`), or the other way round as the flag goes back (`join`).
38
+ */
39
+ function playBridge(sidebar, flag, y, kind, timing) {
40
+ if (timing.duration <= 0 || prefersReducedMotion() || typeof document.body.animate !== "function") return;
41
+ const rect = sidebar.getBoundingClientRect();
42
+ const flagStyles = getComputedStyle(flag);
43
+ const gap = (Number.parseFloat(flagStyles.columnGap) || 16) * .75;
44
+ const onRight = flag.dataset.placement === "right";
45
+ const overlap = 8;
46
+ const thickness = 22;
47
+ const centre = y ?? rect.top + rect.height / 2;
48
+ const bridge = document.createElement("div");
49
+ bridge.setAttribute("data-mp", "flag-bridge");
50
+ bridge.setAttribute("aria-hidden", "true");
51
+ const from = getComputedStyle(sidebar).backgroundColor;
52
+ const to = flagStyles.backgroundColor;
53
+ bridge.style.cssText = [
54
+ "position:fixed",
55
+ "pointer-events:none",
56
+ `z-index:${(Number.parseInt(flagStyles.zIndex, 10) || 50) - 1}`,
57
+ `top:${centre - thickness / 2}px`,
58
+ `left:${onRight ? rect.left - gap - overlap : rect.right - overlap}px`,
59
+ `width:${gap + 16}px`,
60
+ `height:${thickness}px`,
61
+ `border-radius:${thickness / 2}px`,
62
+ `background:linear-gradient(to ${onRight ? "left" : "right"}, ${from}, ${to})`,
63
+ `transform-origin:${onRight ? "right" : "left"} center`,
64
+ "transform:scaleX(0)"
65
+ ].join(";");
66
+ document.body.append(bridge);
67
+ const out = [
68
+ { transform: "scaleX(0)" },
69
+ {
70
+ transform: "scaleX(1)",
71
+ offset: .35
72
+ },
73
+ {
74
+ transform: "scaleX(1) scaleY(0.35)",
75
+ offset: .7
76
+ },
77
+ { transform: "scaleX(1) scaleY(0)" }
78
+ ];
79
+ const remove = () => bridge.remove();
80
+ bridge.animate(kind === "split" ? out : [...out].reverse().map((frame, index, all) => ({
81
+ ...frame,
82
+ offset: index / (all.length - 1)
83
+ })), {
84
+ delay: timing.delay,
85
+ duration: timing.duration,
86
+ easing: "ease-out",
87
+ fill: "both"
88
+ }).finished.then(remove, remove);
89
+ }
90
+
91
+ export { flagStyle, measureFlagSpot, playBridge };
@@ -0,0 +1,66 @@
1
+ "use client";
2
+
3
+ let react = require("react");
4
+ const require_react = require("../utils/react.js");
5
+ const require_choreography = require("./choreography.js");
6
+ const require_morph = require("../Pill/morph.js");
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] = (0, react.useState)(() => initial(target));
17
+ const displayedRef = (0, react.useRef)(displayed);
18
+ displayedRef.current = displayed;
19
+ const optionsRef = (0, react.useRef)(options);
20
+ optionsRef.current = options;
21
+ const followUps = (0, react.useRef)([]);
22
+ const capture = (0, react.useRef)(null);
23
+ require_react.useIsomorphicLayoutEffect(() => {
24
+ const swapped = capture.current;
25
+ capture.current = null;
26
+ if (swapped && rootRef.current) require_morph.playSwap(rootRef.current, swapped);
27
+ optionsRef.current.onSlide?.(false);
28
+ }, [displayed]);
29
+ (0, react.useEffect)(() => {
30
+ const next = followUps.current.shift();
31
+ if (!next) return;
32
+ rootRef.current?.offsetWidth;
33
+ setDisplayed(next);
34
+ }, [displayed]);
35
+ (0, react.useEffect)(() => {
36
+ const current = displayedRef.current;
37
+ const mode = optionsRef.current.swapMode(current.scene, target);
38
+ const timers = require_choreography.batchSteps(current, require_choreography.planSteps(current, target, require_choreography.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 (require_choreography.isSwap(from, states[0], mode)) {
44
+ const freeze = turns?.(from.scene, states[0].scene) ?? false;
45
+ capture.current = require_morph.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
+ exports.useChoreography = useChoreography;