magic-pill 8.1.0 → 9.0.0-next.1

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 (207) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/MIGRATING.md +224 -0
  3. package/README.md +312 -257
  4. package/dist/MagicPill/MagicPill.d.mts +27 -0
  5. package/dist/MagicPill/MagicPill.d.ts +27 -0
  6. package/dist/MagicPill/MagicPill.js +185 -0
  7. package/dist/MagicPill/MagicPill.mjs +185 -0
  8. package/dist/MagicPill/choreography.js +91 -0
  9. package/dist/MagicPill/choreography.mjs +85 -0
  10. package/dist/Pill/Pill.d.mts +7 -0
  11. package/dist/Pill/Pill.d.ts +7 -0
  12. package/dist/Pill/Pill.js +106 -0
  13. package/dist/Pill/Pill.mjs +106 -0
  14. package/dist/Pill/PillAction.d.mts +7 -0
  15. package/dist/Pill/PillAction.d.ts +7 -0
  16. package/dist/Pill/PillAction.js +67 -0
  17. package/dist/Pill/PillAction.mjs +67 -0
  18. package/dist/Pill/PillContext.d.mts +3 -0
  19. package/dist/Pill/PillContext.d.ts +3 -0
  20. package/dist/Pill/PillContext.js +26 -0
  21. package/dist/Pill/PillContext.mjs +24 -0
  22. package/dist/Pill/PillPanel.d.mts +9 -0
  23. package/dist/Pill/PillPanel.d.ts +9 -0
  24. package/dist/Pill/PillPanel.js +88 -0
  25. package/dist/Pill/PillPanel.mjs +87 -0
  26. package/dist/Pill/parts.d.mts +10 -0
  27. package/dist/Pill/parts.d.ts +10 -0
  28. package/dist/Pill/parts.js +46 -0
  29. package/dist/Pill/parts.mjs +43 -0
  30. package/dist/Pill/types.d.mts +88 -0
  31. package/dist/Pill/types.d.ts +88 -0
  32. package/dist/PillNavbar/PillNavbar.d.mts +31 -0
  33. package/dist/PillNavbar/PillNavbar.d.ts +31 -0
  34. package/dist/PillNavbar/PillNavbar.js +44 -0
  35. package/dist/PillNavbar/PillNavbar.mjs +44 -0
  36. package/dist/PillNavbar/matchPath.d.mts +2 -0
  37. package/dist/PillNavbar/matchPath.d.ts +2 -0
  38. package/dist/PillNavbar/matchPath.js +34 -0
  39. package/dist/PillNavbar/matchPath.mjs +34 -0
  40. package/dist/PillNotification/PillNotification.d.mts +51 -0
  41. package/dist/PillNotification/PillNotification.d.ts +51 -0
  42. package/dist/PillNotification/PillNotification.js +64 -0
  43. package/dist/PillNotification/PillNotification.mjs +63 -0
  44. package/dist/icons/ArrowIcon.d.mts +4 -0
  45. package/dist/icons/ArrowIcon.d.ts +4 -0
  46. package/dist/icons/ArrowIcon.js +21 -0
  47. package/dist/icons/ArrowIcon.mjs +21 -0
  48. package/dist/icons/BoxIcon.d.mts +4 -0
  49. package/dist/icons/BoxIcon.d.ts +4 -0
  50. package/dist/icons/BoxIcon.js +21 -0
  51. package/dist/icons/BoxIcon.mjs +21 -0
  52. package/dist/icons/CheckIcon.d.mts +4 -0
  53. package/dist/icons/CheckIcon.d.ts +4 -0
  54. package/dist/icons/CheckIcon.js +17 -0
  55. package/dist/icons/CheckIcon.mjs +17 -0
  56. package/dist/icons/CloseIcon.d.mts +4 -0
  57. package/dist/icons/CloseIcon.d.ts +4 -0
  58. package/dist/icons/CloseIcon.js +17 -0
  59. package/dist/icons/CloseIcon.mjs +17 -0
  60. package/dist/icons/CouponIcon.d.mts +4 -0
  61. package/dist/icons/CouponIcon.d.ts +4 -0
  62. package/dist/icons/CouponIcon.js +17 -0
  63. package/dist/icons/CouponIcon.mjs +17 -0
  64. package/dist/icons/DocumentIcon.d.mts +4 -0
  65. package/dist/icons/DocumentIcon.d.ts +4 -0
  66. package/dist/icons/DocumentIcon.js +17 -0
  67. package/dist/icons/DocumentIcon.mjs +17 -0
  68. package/dist/icons/HelpIcon.d.mts +4 -0
  69. package/dist/icons/HelpIcon.d.ts +4 -0
  70. package/dist/icons/HelpIcon.js +17 -0
  71. package/dist/icons/HelpIcon.mjs +17 -0
  72. package/dist/icons/InfoIcon.d.mts +4 -0
  73. package/dist/icons/InfoIcon.d.ts +4 -0
  74. package/dist/icons/InfoIcon.js +17 -0
  75. package/dist/icons/InfoIcon.mjs +17 -0
  76. package/dist/icons/MailIcon.d.mts +4 -0
  77. package/dist/icons/MailIcon.d.ts +4 -0
  78. package/dist/icons/MailIcon.js +17 -0
  79. package/dist/icons/MailIcon.mjs +17 -0
  80. package/dist/icons/PhoneIcon.d.mts +4 -0
  81. package/dist/icons/PhoneIcon.d.ts +4 -0
  82. package/dist/icons/PhoneIcon.js +21 -0
  83. package/dist/icons/PhoneIcon.mjs +21 -0
  84. package/dist/icons/PlusIcon.d.mts +4 -0
  85. package/dist/icons/PlusIcon.d.ts +4 -0
  86. package/dist/icons/PlusIcon.js +17 -0
  87. package/dist/icons/PlusIcon.mjs +17 -0
  88. package/dist/icons/UndoIcon.d.mts +4 -0
  89. package/dist/icons/UndoIcon.d.ts +4 -0
  90. package/dist/icons/UndoIcon.js +21 -0
  91. package/dist/icons/UndoIcon.mjs +21 -0
  92. package/dist/icons/types.d.mts +3 -0
  93. package/dist/icons/types.d.ts +3 -0
  94. package/dist/index.d.mts +25 -8
  95. package/dist/index.d.ts +25 -9
  96. package/dist/index.js +50 -1
  97. package/dist/index.mjs +24 -1
  98. package/dist/store/store.d.mts +63 -0
  99. package/dist/store/store.d.ts +63 -0
  100. package/dist/store/store.js +104 -0
  101. package/dist/store/store.mjs +102 -0
  102. package/dist/store/usePillQueue.d.mts +6 -0
  103. package/dist/store/usePillQueue.d.ts +6 -0
  104. package/dist/store/usePillQueue.js +17 -0
  105. package/dist/store/usePillQueue.mjs +17 -0
  106. package/dist/styles.css +456 -0
  107. package/dist/utils/react.js +23 -0
  108. package/dist/utils/react.mjs +21 -0
  109. package/dist/utils/url.js +22 -0
  110. package/dist/utils/url.mjs +22 -0
  111. package/package.json +7 -4
  112. package/dist/MagicPill.d.mts +0 -6
  113. package/dist/MagicPill.d.ts +0 -7
  114. package/dist/MagicPill.js +0 -2
  115. package/dist/MagicPill.js.map +0 -1
  116. package/dist/MagicPill.mjs +0 -2
  117. package/dist/MagicPill.mjs.map +0 -1
  118. package/dist/Navbar/Navbar.js +0 -2
  119. package/dist/Navbar/Navbar.js.map +0 -1
  120. package/dist/Navbar/Navbar.mjs +0 -2
  121. package/dist/Navbar/Navbar.mjs.map +0 -1
  122. package/dist/Notification/Notification.js +0 -2
  123. package/dist/Notification/Notification.js.map +0 -1
  124. package/dist/Notification/Notification.mjs +0 -2
  125. package/dist/Notification/Notification.mjs.map +0 -1
  126. package/dist/defaultPillData.d.mts +0 -2
  127. package/dist/defaultPillData.d.ts +0 -3
  128. package/dist/defaultPillData.js +0 -7
  129. package/dist/defaultPillData.js.map +0 -1
  130. package/dist/defaultPillData.mjs +0 -7
  131. package/dist/defaultPillData.mjs.map +0 -1
  132. package/dist/icons/arrow_label.js +0 -2
  133. package/dist/icons/arrow_label.js.map +0 -1
  134. package/dist/icons/arrow_label.mjs +0 -2
  135. package/dist/icons/arrow_label.mjs.map +0 -1
  136. package/dist/icons/box_label.js +0 -2
  137. package/dist/icons/box_label.js.map +0 -1
  138. package/dist/icons/box_label.mjs +0 -2
  139. package/dist/icons/box_label.mjs.map +0 -1
  140. package/dist/icons/checkmark.js +0 -2
  141. package/dist/icons/checkmark.js.map +0 -1
  142. package/dist/icons/checkmark.mjs +0 -2
  143. package/dist/icons/checkmark.mjs.map +0 -1
  144. package/dist/icons/checkmark_label.js +0 -2
  145. package/dist/icons/checkmark_label.js.map +0 -1
  146. package/dist/icons/checkmark_label.mjs +0 -2
  147. package/dist/icons/checkmark_label.mjs.map +0 -1
  148. package/dist/icons/coupon.js +0 -2
  149. package/dist/icons/coupon.js.map +0 -1
  150. package/dist/icons/coupon.mjs +0 -2
  151. package/dist/icons/coupon.mjs.map +0 -1
  152. package/dist/icons/cross_label.js +0 -2
  153. package/dist/icons/cross_label.js.map +0 -1
  154. package/dist/icons/cross_label.mjs +0 -2
  155. package/dist/icons/cross_label.mjs.map +0 -1
  156. package/dist/icons/info.js +0 -2
  157. package/dist/icons/info.js.map +0 -1
  158. package/dist/icons/info.mjs +0 -2
  159. package/dist/icons/info.mjs.map +0 -1
  160. package/dist/icons/info_label.js +0 -2
  161. package/dist/icons/info_label.js.map +0 -1
  162. package/dist/icons/info_label.mjs +0 -2
  163. package/dist/icons/info_label.mjs.map +0 -1
  164. package/dist/icons/mail_label.js +0 -2
  165. package/dist/icons/mail_label.js.map +0 -1
  166. package/dist/icons/mail_label.mjs +0 -2
  167. package/dist/icons/mail_label.mjs.map +0 -1
  168. package/dist/icons/plus_label.js +0 -2
  169. package/dist/icons/plus_label.js.map +0 -1
  170. package/dist/icons/plus_label.mjs +0 -2
  171. package/dist/icons/plus_label.mjs.map +0 -1
  172. package/dist/icons/questionmark.js +0 -2
  173. package/dist/icons/questionmark.js.map +0 -1
  174. package/dist/icons/questionmark.mjs +0 -2
  175. package/dist/icons/questionmark.mjs.map +0 -1
  176. package/dist/icons/quotation_label.js +0 -2
  177. package/dist/icons/quotation_label.js.map +0 -1
  178. package/dist/icons/quotation_label.mjs +0 -2
  179. package/dist/icons/quotation_label.mjs.map +0 -1
  180. package/dist/icons/tel_label.js +0 -2
  181. package/dist/icons/tel_label.js.map +0 -1
  182. package/dist/icons/tel_label.mjs +0 -2
  183. package/dist/icons/tel_label.mjs.map +0 -1
  184. package/dist/icons/undo_label.js +0 -2
  185. package/dist/icons/undo_label.js.map +0 -1
  186. package/dist/icons/undo_label.mjs +0 -2
  187. package/dist/icons/undo_label.mjs.map +0 -1
  188. package/dist/magicpill.css +0 -315
  189. package/dist/types/index.d.ts +0 -4
  190. package/dist/types/magicPill.d.mts +0 -50
  191. package/dist/types/magicPill.d.ts +0 -50
  192. package/dist/types/navbar.d.mts +0 -27
  193. package/dist/types/navbar.d.ts +0 -28
  194. package/dist/types/notification.d.mts +0 -140
  195. package/dist/types/notification.d.ts +0 -141
  196. package/dist/utils/handlers.d.mts +0 -20
  197. package/dist/utils/handlers.d.ts +0 -21
  198. package/dist/utils/handlers.js +0 -2
  199. package/dist/utils/handlers.js.map +0 -1
  200. package/dist/utils/handlers.mjs +0 -2
  201. package/dist/utils/handlers.mjs.map +0 -1
  202. package/dist/utils/icons.d.mts +0 -20
  203. package/dist/utils/icons.d.ts +0 -20
  204. package/dist/utils/icons.js +0 -2
  205. package/dist/utils/icons.js.map +0 -1
  206. package/dist/utils/icons.mjs +0 -2
  207. package/dist/utils/icons.mjs.map +0 -1
@@ -0,0 +1,27 @@
1
+ import { ReactNode } from "react";
2
+ import { LinkComponent, PillProps } from "../Pill/types.mjs";
3
+ import { PillNotificationClassNames } from "../PillNotification/PillNotification.mjs";
4
+ export interface MagicPillProps extends Omit<PillProps, "presence" | "defaultPresence" | "onPresenceChange" | "children"> {
5
+ /** Default "main". Two hosts with the same name warn in development. */
6
+ name?: string;
7
+ /**
8
+ * The base layer (e.g. a <PillNavbar>), shown whenever no notification is.
9
+ * Notifications take the pill over, then it returns to this content.
10
+ */
11
+ children?: ReactNode;
12
+ /** Default display time for notifications, in ms. Default 5000. */
13
+ duration?: number;
14
+ /** With no base layer and nothing queued: hide (default) or rest as a circle. */
15
+ idle?: "hidden" | "rest";
16
+ /** The icon shown when `idle="rest"`. */
17
+ idleIcon?: ReactNode;
18
+ /** Passed to notification actions that have an `href`. */
19
+ linkComponent?: LinkComponent;
20
+ classNames?: PillNotificationClassNames;
21
+ }
22
+ /**
23
+ * The batteries-included pill: a <Pill> with a base layer, a queue of
24
+ * notifications fed by pill(name), and a live region that announces them.
25
+ */
26
+ declare const MagicPill: import("react").ForwardRefExoticComponent<MagicPillProps & import("react").RefAttributes<HTMLElement>>;
27
+ export { MagicPill as default };
@@ -0,0 +1,27 @@
1
+ import { ReactNode } from "react";
2
+ import { LinkComponent, PillProps } from "../Pill/types.js";
3
+ import { PillNotificationClassNames } from "../PillNotification/PillNotification.js";
4
+ export interface MagicPillProps extends Omit<PillProps, "presence" | "defaultPresence" | "onPresenceChange" | "children"> {
5
+ /** Default "main". Two hosts with the same name warn in development. */
6
+ name?: string;
7
+ /**
8
+ * The base layer (e.g. a <PillNavbar>), shown whenever no notification is.
9
+ * Notifications take the pill over, then it returns to this content.
10
+ */
11
+ children?: ReactNode;
12
+ /** Default display time for notifications, in ms. Default 5000. */
13
+ duration?: number;
14
+ /** With no base layer and nothing queued: hide (default) or rest as a circle. */
15
+ idle?: "hidden" | "rest";
16
+ /** The icon shown when `idle="rest"`. */
17
+ idleIcon?: ReactNode;
18
+ /** Passed to notification actions that have an `href`. */
19
+ linkComponent?: LinkComponent;
20
+ classNames?: PillNotificationClassNames;
21
+ }
22
+ /**
23
+ * The batteries-included pill: a <Pill> with a base layer, a queue of
24
+ * notifications fed by pill(name), and a live region that announces them.
25
+ */
26
+ declare const MagicPill: import("react").ForwardRefExoticComponent<MagicPillProps & import("react").RefAttributes<HTMLElement>>;
27
+ export { MagicPill as default };
@@ -0,0 +1,185 @@
1
+ "use client";
2
+
3
+ let react = require("react");
4
+ const require_Pill = require("../Pill/Pill.js");
5
+ const require_parts = require("../Pill/parts.js");
6
+ const require_PillNotification = require("../PillNotification/PillNotification.js");
7
+ const require_store = require("../store/store.js");
8
+ const require_usePillQueue = require("../store/usePillQueue.js");
9
+ const require_react = require("../utils/react.js");
10
+ const require_choreography = require("./choreography.js");
11
+ let react_jsx_runtime = require("react/jsx-runtime");
12
+
13
+ // src/MagicPill/MagicPill.tsx
14
+ const DEFAULT_DURATION = 5e3;
15
+ const VISUALLY_HIDDEN = {
16
+ position: "absolute",
17
+ width: 1,
18
+ height: 1,
19
+ margin: -1,
20
+ padding: 0,
21
+ overflow: "hidden",
22
+ clip: "rect(0 0 0 0)",
23
+ whiteSpace: "nowrap",
24
+ border: 0
25
+ };
26
+ const hasContent = (node) => node !== void 0 && node !== null && node !== false;
27
+ function useDocumentHidden() {
28
+ const [hidden, setHidden] = (0, react.useState)(false);
29
+ (0, react.useEffect)(() => {
30
+ const update = () => setHidden(document.visibilityState === "hidden");
31
+ update();
32
+ document.addEventListener("visibilitychange", update);
33
+ return () => document.removeEventListener("visibilitychange", update);
34
+ }, []);
35
+ return hidden;
36
+ }
37
+ /**
38
+ * The batteries-included pill: a <Pill> with a base layer, a queue of
39
+ * notifications fed by pill(name), and a live region that announces them.
40
+ */
41
+ const MagicPill = (0, react.forwardRef)(function MagicPill({ name = "main", children, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, linkComponent, classNames, placement = "bottom", variant, onMouseEnter, onMouseLeave, onFocus, onBlur, ...rest }, forwardedRef) {
42
+ const store = require_store.getStore(name);
43
+ const { current } = require_usePillQueue.usePillQueue(name);
44
+ const rootRef = (0, react.useRef)(null);
45
+ const ref = (0, react.useMemo)(() => require_react.mergeRefs(rootRef, forwardedRef), [forwardedRef]);
46
+ (0, react.useEffect)(() => store.registerHost(), [store]);
47
+ const hasBase = hasContent(children);
48
+ const target = (0, react.useMemo)(() => current ? {
49
+ kind: "notification",
50
+ item: current
51
+ } : hasBase ? require_choreography.BASE : idle === "rest" ? require_choreography.IDLE : require_choreography.NONE, [
52
+ current,
53
+ hasBase,
54
+ idle
55
+ ]);
56
+ const [displayed, setDisplayed] = (0, react.useState)(() => ({
57
+ scene: target.kind === "notification" ? require_choreography.NONE : target,
58
+ presence: target.kind === "base" ? "compact" : target.kind === "idle" ? "rest" : "hidden"
59
+ }));
60
+ const displayedRef = (0, react.useRef)(displayed);
61
+ displayedRef.current = displayed;
62
+ (0, react.useEffect)(() => {
63
+ const steps = require_choreography.planSteps(displayedRef.current, target, require_choreography.readTimings(rootRef.current));
64
+ const timers = [];
65
+ let at = 0;
66
+ steps.forEach((step, index) => {
67
+ at += step.delay;
68
+ const next = steps[index + 1];
69
+ if (next && next.delay === 0) return;
70
+ const apply = () => setDisplayed({
71
+ scene: step.scene,
72
+ presence: step.presence
73
+ });
74
+ if (at === 0) apply();
75
+ else timers.push(setTimeout(apply, at));
76
+ });
77
+ return () => timers.forEach(clearTimeout);
78
+ }, [target]);
79
+ const [hovered, setHovered] = (0, react.useState)(false);
80
+ const [focused, setFocused] = (0, react.useState)(false);
81
+ const documentHidden = useDocumentHidden();
82
+ (0, react.useEffect)(() => {
83
+ if (displayed.presence === "hidden") {
84
+ setHovered(false);
85
+ setFocused(false);
86
+ }
87
+ }, [displayed.presence]);
88
+ const shown = displayed.scene.kind === "notification" && require_choreography.isOpen(displayed.presence) ? displayed.scene.item : null;
89
+ const paused = hovered || focused || documentHidden || displayed.presence === "panel";
90
+ (0, react.useEffect)(() => {
91
+ if (!shown || paused) return;
92
+ const ms = shown.input.duration ?? duration;
93
+ if (!Number.isFinite(ms)) return;
94
+ const timer = setTimeout(() => store.dismiss(shown.id, "timeout"), ms);
95
+ return () => clearTimeout(timer);
96
+ }, [
97
+ shown,
98
+ paused,
99
+ duration,
100
+ store
101
+ ]);
102
+ const [announcement, setAnnouncement] = (0, react.useState)(null);
103
+ (0, react.useEffect)(() => {
104
+ if (!shown) return;
105
+ const { announce } = shown.input;
106
+ if (announce === false) return;
107
+ const root = rootRef.current;
108
+ const text = typeof announce === "string" ? announce : (root?.querySelector("[data-mp=\"message\"]") ?? root)?.textContent?.trim() ?? "";
109
+ if (text) setAnnouncement((previous) => previous?.key === shown ? previous : {
110
+ key: shown,
111
+ text
112
+ });
113
+ }, [shown]);
114
+ const handlePresenceChange = (next) => {
115
+ const { scene, presence } = displayedRef.current;
116
+ if (next === "hidden") {
117
+ if (scene.kind === "notification") store.dismiss(scene.item.id, "dismissed");
118
+ return;
119
+ }
120
+ if (require_choreography.isOpen(next) && require_choreography.isOpen(presence)) setDisplayed({
121
+ scene,
122
+ presence: next
123
+ });
124
+ };
125
+ const renderScene = (scene) => {
126
+ switch (scene.kind) {
127
+ case "base": return children;
128
+ case "idle": return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_parts.PillIcon, { children: idleIcon });
129
+ case "notification": {
130
+ const { id, input } = scene.item;
131
+ if (input.render) return input.render({
132
+ id,
133
+ dismiss: () => store.dismiss(id, "dismissed")
134
+ });
135
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillNotification.NotificationContent, {
136
+ icon: input.icon,
137
+ message: input.message,
138
+ action: input.action,
139
+ panel: input.panel,
140
+ linkComponent,
141
+ classNames,
142
+ onAction: (event) => {
143
+ if (!event.defaultPrevented) store.dismiss(id, "action");
144
+ }
145
+ }, id);
146
+ }
147
+ default: return null;
148
+ }
149
+ };
150
+ const sceneVariant = displayed.scene.kind === "notification" ? displayed.scene.item.input.variant ?? variant : variant;
151
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Pill, {
152
+ ...rest,
153
+ ref,
154
+ placement,
155
+ variant: sceneVariant,
156
+ presence: displayed.presence,
157
+ onPresenceChange: handlePresenceChange,
158
+ onMouseEnter: (event) => {
159
+ onMouseEnter?.(event);
160
+ setHovered(true);
161
+ },
162
+ onMouseLeave: (event) => {
163
+ onMouseLeave?.(event);
164
+ setHovered(false);
165
+ },
166
+ onFocus: (event) => {
167
+ onFocus?.(event);
168
+ setFocused(true);
169
+ },
170
+ onBlur: (event) => {
171
+ onBlur?.(event);
172
+ setFocused(event.currentTarget.contains(event.relatedTarget));
173
+ },
174
+ children: renderScene(displayed.scene)
175
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
176
+ role: "status",
177
+ "aria-live": "polite",
178
+ "aria-atomic": "true",
179
+ "data-mp": "announcer",
180
+ style: VISUALLY_HIDDEN,
181
+ children: announcement && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: announcement.text }, announcement.key.id + announcement.text)
182
+ })] });
183
+ });
184
+
185
+ module.exports = MagicPill;
@@ -0,0 +1,185 @@
1
+ "use client";
2
+
3
+ import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
4
+ import Pill from "../Pill/Pill.mjs";
5
+ import { PillIcon } from "../Pill/parts.mjs";
6
+ import { NotificationContent } from "../PillNotification/PillNotification.mjs";
7
+ import { getStore } from "../store/store.mjs";
8
+ import { usePillQueue } from "../store/usePillQueue.mjs";
9
+ import { mergeRefs } from "../utils/react.mjs";
10
+ import { BASE, IDLE, NONE, isOpen, planSteps, readTimings } from "./choreography.mjs";
11
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
12
+
13
+ // src/MagicPill/MagicPill.tsx
14
+ const DEFAULT_DURATION = 5e3;
15
+ const VISUALLY_HIDDEN = {
16
+ position: "absolute",
17
+ width: 1,
18
+ height: 1,
19
+ margin: -1,
20
+ padding: 0,
21
+ overflow: "hidden",
22
+ clip: "rect(0 0 0 0)",
23
+ whiteSpace: "nowrap",
24
+ border: 0
25
+ };
26
+ const hasContent = (node) => node !== void 0 && node !== null && node !== false;
27
+ function useDocumentHidden() {
28
+ const [hidden, setHidden] = useState(false);
29
+ useEffect(() => {
30
+ const update = () => setHidden(document.visibilityState === "hidden");
31
+ update();
32
+ document.addEventListener("visibilitychange", update);
33
+ return () => document.removeEventListener("visibilitychange", update);
34
+ }, []);
35
+ return hidden;
36
+ }
37
+ /**
38
+ * The batteries-included pill: a <Pill> with a base layer, a queue of
39
+ * notifications fed by pill(name), and a live region that announces them.
40
+ */
41
+ const MagicPill = forwardRef(function MagicPill({ name = "main", children, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, linkComponent, classNames, placement = "bottom", variant, onMouseEnter, onMouseLeave, onFocus, onBlur, ...rest }, forwardedRef) {
42
+ const store = getStore(name);
43
+ const { current } = usePillQueue(name);
44
+ const rootRef = useRef(null);
45
+ const ref = useMemo(() => mergeRefs(rootRef, forwardedRef), [forwardedRef]);
46
+ useEffect(() => store.registerHost(), [store]);
47
+ const hasBase = hasContent(children);
48
+ const target = useMemo(() => current ? {
49
+ kind: "notification",
50
+ item: current
51
+ } : hasBase ? BASE : idle === "rest" ? IDLE : NONE, [
52
+ current,
53
+ hasBase,
54
+ idle
55
+ ]);
56
+ const [displayed, setDisplayed] = useState(() => ({
57
+ scene: target.kind === "notification" ? NONE : target,
58
+ presence: target.kind === "base" ? "compact" : target.kind === "idle" ? "rest" : "hidden"
59
+ }));
60
+ const displayedRef = useRef(displayed);
61
+ displayedRef.current = displayed;
62
+ useEffect(() => {
63
+ const steps = planSteps(displayedRef.current, target, readTimings(rootRef.current));
64
+ const timers = [];
65
+ let at = 0;
66
+ steps.forEach((step, index) => {
67
+ at += step.delay;
68
+ const next = steps[index + 1];
69
+ if (next && next.delay === 0) return;
70
+ const apply = () => setDisplayed({
71
+ scene: step.scene,
72
+ presence: step.presence
73
+ });
74
+ if (at === 0) apply();
75
+ else timers.push(setTimeout(apply, at));
76
+ });
77
+ return () => timers.forEach(clearTimeout);
78
+ }, [target]);
79
+ const [hovered, setHovered] = useState(false);
80
+ const [focused, setFocused] = useState(false);
81
+ const documentHidden = useDocumentHidden();
82
+ useEffect(() => {
83
+ if (displayed.presence === "hidden") {
84
+ setHovered(false);
85
+ setFocused(false);
86
+ }
87
+ }, [displayed.presence]);
88
+ const shown = displayed.scene.kind === "notification" && isOpen(displayed.presence) ? displayed.scene.item : null;
89
+ const paused = hovered || focused || documentHidden || displayed.presence === "panel";
90
+ useEffect(() => {
91
+ if (!shown || paused) return;
92
+ const ms = shown.input.duration ?? duration;
93
+ if (!Number.isFinite(ms)) return;
94
+ const timer = setTimeout(() => store.dismiss(shown.id, "timeout"), ms);
95
+ return () => clearTimeout(timer);
96
+ }, [
97
+ shown,
98
+ paused,
99
+ duration,
100
+ store
101
+ ]);
102
+ const [announcement, setAnnouncement] = useState(null);
103
+ useEffect(() => {
104
+ if (!shown) return;
105
+ const { announce } = shown.input;
106
+ if (announce === false) return;
107
+ const root = rootRef.current;
108
+ const text = typeof announce === "string" ? announce : (root?.querySelector("[data-mp=\"message\"]") ?? root)?.textContent?.trim() ?? "";
109
+ if (text) setAnnouncement((previous) => previous?.key === shown ? previous : {
110
+ key: shown,
111
+ text
112
+ });
113
+ }, [shown]);
114
+ const handlePresenceChange = (next) => {
115
+ const { scene, presence } = displayedRef.current;
116
+ if (next === "hidden") {
117
+ if (scene.kind === "notification") store.dismiss(scene.item.id, "dismissed");
118
+ return;
119
+ }
120
+ if (isOpen(next) && isOpen(presence)) setDisplayed({
121
+ scene,
122
+ presence: next
123
+ });
124
+ };
125
+ const renderScene = (scene) => {
126
+ switch (scene.kind) {
127
+ case "base": return children;
128
+ case "idle": return /* @__PURE__ */ jsx(PillIcon, { children: idleIcon });
129
+ case "notification": {
130
+ const { id, input } = scene.item;
131
+ if (input.render) return input.render({
132
+ id,
133
+ dismiss: () => store.dismiss(id, "dismissed")
134
+ });
135
+ return /* @__PURE__ */ jsx(NotificationContent, {
136
+ icon: input.icon,
137
+ message: input.message,
138
+ action: input.action,
139
+ panel: input.panel,
140
+ linkComponent,
141
+ classNames,
142
+ onAction: (event) => {
143
+ if (!event.defaultPrevented) store.dismiss(id, "action");
144
+ }
145
+ }, id);
146
+ }
147
+ default: return null;
148
+ }
149
+ };
150
+ const sceneVariant = displayed.scene.kind === "notification" ? displayed.scene.item.input.variant ?? variant : variant;
151
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Pill, {
152
+ ...rest,
153
+ ref,
154
+ placement,
155
+ variant: sceneVariant,
156
+ presence: displayed.presence,
157
+ onPresenceChange: handlePresenceChange,
158
+ onMouseEnter: (event) => {
159
+ onMouseEnter?.(event);
160
+ setHovered(true);
161
+ },
162
+ onMouseLeave: (event) => {
163
+ onMouseLeave?.(event);
164
+ setHovered(false);
165
+ },
166
+ onFocus: (event) => {
167
+ onFocus?.(event);
168
+ setFocused(true);
169
+ },
170
+ onBlur: (event) => {
171
+ onBlur?.(event);
172
+ setFocused(event.currentTarget.contains(event.relatedTarget));
173
+ },
174
+ children: renderScene(displayed.scene)
175
+ }), /* @__PURE__ */ jsx("div", {
176
+ role: "status",
177
+ "aria-live": "polite",
178
+ "aria-atomic": "true",
179
+ "data-mp": "announcer",
180
+ style: VISUALLY_HIDDEN,
181
+ children: announcement && /* @__PURE__ */ jsx("span", { children: announcement.text }, announcement.key.id + announcement.text)
182
+ })] });
183
+ });
184
+
185
+ export { MagicPill as default };
@@ -0,0 +1,91 @@
1
+ // src/MagicPill/choreography.ts
2
+ const NONE = Object.freeze({ kind: "none" });
3
+ const BASE = Object.freeze({ kind: "base" });
4
+ const IDLE = Object.freeze({ kind: "idle" });
5
+ const OPEN_LEVELS = /* @__PURE__ */ new Set([
6
+ "compact",
7
+ "expanded",
8
+ "panel"
9
+ ]);
10
+ const isOpen = (presence) => OPEN_LEVELS.has(presence);
11
+ const sceneKey = (scene) => scene.kind === "notification" ? `notification:${scene.item.id}` : scene.kind;
12
+ /** Where each scene settles: the base and notifications are pills, idle is a circle. */
13
+ const settledPresence = (scene) => scene.kind === "none" ? "hidden" : scene.kind === "idle" ? "rest" : "compact";
14
+ /**
15
+ * The steps from what's displayed to the target scene:
16
+ * - new content: collapse to the circle, swap the content there, grow back;
17
+ * - nothing left to show: hide (the stylesheet shrinks, then scales out), then drop the content;
18
+ * - from hidden: pop in as a circle, then grow into the pill.
19
+ * Open levels the user chose (expanded, panel) are kept while the scene stays the same.
20
+ */
21
+ function planSteps(current, target, timings) {
22
+ const steps = [];
23
+ let { scene, presence } = current;
24
+ const go = (delay, nextPresence, nextScene = scene) => {
25
+ steps.push({
26
+ delay,
27
+ presence: nextPresence,
28
+ scene: nextScene
29
+ });
30
+ presence = nextPresence;
31
+ scene = nextScene;
32
+ };
33
+ if (sceneKey(scene) !== sceneKey(target)) {
34
+ if (target.kind === "none") {
35
+ if (presence !== "hidden") go(0, "hidden");
36
+ go(timings.reduced ? timings.exit : timings.change + timings.exit, "hidden", target);
37
+ return steps;
38
+ }
39
+ if (isOpen(presence)) {
40
+ if (timings.reduced) {
41
+ go(0, "hidden");
42
+ go(timings.exit, "hidden", target);
43
+ } else {
44
+ go(0, "rest");
45
+ go(timings.change, "rest", target);
46
+ }
47
+ } else go(0, presence, target);
48
+ } else if (scene !== target) go(0, presence, target);
49
+ const settled = settledPresence(target);
50
+ if (settled === "hidden" || settled === "rest") {
51
+ if (presence !== settled) go(0, settled);
52
+ } else if (!isOpen(presence)) {
53
+ if (presence === "hidden" && !timings.reduced) {
54
+ go(0, "rest");
55
+ go(timings.enter, "compact");
56
+ } else go(0, "compact");
57
+ }
58
+ return steps;
59
+ }
60
+ /** "500ms" or "0.5s" → 500; anything else → the fallback. */
61
+ function parseDuration(value, fallback) {
62
+ const match = /^\s*([\d.]+)(ms|s)\s*$/.exec(value);
63
+ if (!match) return fallback;
64
+ const amount = Number.parseFloat(match[1]);
65
+ return match[2] === "s" ? amount * 1e3 : amount;
66
+ }
67
+ /** The timings the stylesheet uses for this pill, or its defaults (e.g. when unstyled). */
68
+ function readTimings(element) {
69
+ const style = element ? getComputedStyle(element) : null;
70
+ const read = (name, fallback) => parseDuration(style?.getPropertyValue(name) ?? "", fallback);
71
+ const reduced = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
72
+ return reduced ? {
73
+ enter: 0,
74
+ change: 0,
75
+ exit: 150,
76
+ reduced
77
+ } : {
78
+ enter: read("--_mp-enter", 500),
79
+ change: read("--_mp-change", 250),
80
+ exit: read("--_mp-exit", 300),
81
+ reduced
82
+ };
83
+ }
84
+
85
+ exports.BASE = BASE;
86
+ exports.IDLE = IDLE;
87
+ exports.NONE = NONE;
88
+ exports.isOpen = isOpen;
89
+ exports.parseDuration = parseDuration;
90
+ exports.planSteps = planSteps;
91
+ exports.readTimings = readTimings;
@@ -0,0 +1,85 @@
1
+ // src/MagicPill/choreography.ts
2
+ const NONE = Object.freeze({ kind: "none" });
3
+ const BASE = Object.freeze({ kind: "base" });
4
+ const IDLE = Object.freeze({ kind: "idle" });
5
+ const OPEN_LEVELS = /* @__PURE__ */ new Set([
6
+ "compact",
7
+ "expanded",
8
+ "panel"
9
+ ]);
10
+ const isOpen = (presence) => OPEN_LEVELS.has(presence);
11
+ const sceneKey = (scene) => scene.kind === "notification" ? `notification:${scene.item.id}` : scene.kind;
12
+ /** Where each scene settles: the base and notifications are pills, idle is a circle. */
13
+ const settledPresence = (scene) => scene.kind === "none" ? "hidden" : scene.kind === "idle" ? "rest" : "compact";
14
+ /**
15
+ * The steps from what's displayed to the target scene:
16
+ * - new content: collapse to the circle, swap the content there, grow back;
17
+ * - nothing left to show: hide (the stylesheet shrinks, then scales out), then drop the content;
18
+ * - from hidden: pop in as a circle, then grow into the pill.
19
+ * Open levels the user chose (expanded, panel) are kept while the scene stays the same.
20
+ */
21
+ function planSteps(current, target, timings) {
22
+ const steps = [];
23
+ let { scene, presence } = current;
24
+ const go = (delay, nextPresence, nextScene = scene) => {
25
+ steps.push({
26
+ delay,
27
+ presence: nextPresence,
28
+ scene: nextScene
29
+ });
30
+ presence = nextPresence;
31
+ scene = nextScene;
32
+ };
33
+ if (sceneKey(scene) !== sceneKey(target)) {
34
+ if (target.kind === "none") {
35
+ if (presence !== "hidden") go(0, "hidden");
36
+ go(timings.reduced ? timings.exit : timings.change + timings.exit, "hidden", target);
37
+ return steps;
38
+ }
39
+ if (isOpen(presence)) {
40
+ if (timings.reduced) {
41
+ go(0, "hidden");
42
+ go(timings.exit, "hidden", target);
43
+ } else {
44
+ go(0, "rest");
45
+ go(timings.change, "rest", target);
46
+ }
47
+ } else go(0, presence, target);
48
+ } else if (scene !== target) go(0, presence, target);
49
+ const settled = settledPresence(target);
50
+ if (settled === "hidden" || settled === "rest") {
51
+ if (presence !== settled) go(0, settled);
52
+ } else if (!isOpen(presence)) {
53
+ if (presence === "hidden" && !timings.reduced) {
54
+ go(0, "rest");
55
+ go(timings.enter, "compact");
56
+ } else go(0, "compact");
57
+ }
58
+ return steps;
59
+ }
60
+ /** "500ms" or "0.5s" → 500; anything else → the fallback. */
61
+ function parseDuration(value, fallback) {
62
+ const match = /^\s*([\d.]+)(ms|s)\s*$/.exec(value);
63
+ if (!match) return fallback;
64
+ const amount = Number.parseFloat(match[1]);
65
+ return match[2] === "s" ? amount * 1e3 : amount;
66
+ }
67
+ /** The timings the stylesheet uses for this pill, or its defaults (e.g. when unstyled). */
68
+ function readTimings(element) {
69
+ const style = element ? getComputedStyle(element) : null;
70
+ const read = (name, fallback) => parseDuration(style?.getPropertyValue(name) ?? "", fallback);
71
+ const reduced = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
72
+ return reduced ? {
73
+ enter: 0,
74
+ change: 0,
75
+ exit: 150,
76
+ reduced
77
+ } : {
78
+ enter: read("--_mp-enter", 500),
79
+ change: read("--_mp-change", 250),
80
+ exit: read("--_mp-exit", 300),
81
+ reduced
82
+ };
83
+ }
84
+
85
+ export { BASE, IDLE, NONE, isOpen, parseDuration, planSteps, readTimings };
@@ -0,0 +1,7 @@
1
+ import { PillProps } from "./types.mjs";
2
+ /**
3
+ * The pill: a container with the Magic Pill design language. Put any parts
4
+ * (or anything else) inside. Its look and animations follow its data attributes.
5
+ */
6
+ declare const Pill: import("react").ForwardRefExoticComponent<PillProps & import("react").RefAttributes<HTMLElement>>;
7
+ export { Pill as default };
@@ -0,0 +1,7 @@
1
+ import { PillProps } from "./types.js";
2
+ /**
3
+ * The pill: a container with the Magic Pill design language. Put any parts
4
+ * (or anything else) inside. Its look and animations follow its data attributes.
5
+ */
6
+ declare const Pill: import("react").ForwardRefExoticComponent<PillProps & import("react").RefAttributes<HTMLElement>>;
7
+ export = Pill;