magic-pill 9.0.0-next.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.
- package/CHANGELOG.md +8 -0
- package/README.md +4 -1
- package/dist/MagicPill/MagicPill.js +185 -2
- package/dist/MagicPill/MagicPill.mjs +185 -2
- package/dist/MagicPill/choreography.js +91 -2
- package/dist/MagicPill/choreography.mjs +85 -2
- package/dist/Pill/Pill.js +106 -2
- package/dist/Pill/Pill.mjs +106 -2
- package/dist/Pill/PillAction.js +67 -2
- package/dist/Pill/PillAction.mjs +67 -2
- package/dist/Pill/PillContext.js +26 -2
- package/dist/Pill/PillContext.mjs +24 -2
- package/dist/Pill/PillPanel.js +88 -2
- package/dist/Pill/PillPanel.mjs +87 -2
- package/dist/Pill/parts.js +46 -2
- package/dist/Pill/parts.mjs +43 -2
- package/dist/PillNavbar/PillNavbar.js +44 -2
- package/dist/PillNavbar/PillNavbar.mjs +44 -2
- package/dist/PillNavbar/matchPath.js +34 -2
- package/dist/PillNavbar/matchPath.mjs +34 -2
- package/dist/PillNotification/PillNotification.js +64 -2
- package/dist/PillNotification/PillNotification.mjs +63 -2
- package/dist/icons/ArrowIcon.js +21 -2
- package/dist/icons/ArrowIcon.mjs +21 -2
- package/dist/icons/BoxIcon.js +21 -2
- package/dist/icons/BoxIcon.mjs +21 -2
- package/dist/icons/CheckIcon.js +17 -2
- package/dist/icons/CheckIcon.mjs +17 -2
- package/dist/icons/CloseIcon.js +17 -2
- package/dist/icons/CloseIcon.mjs +17 -2
- package/dist/icons/CouponIcon.js +17 -2
- package/dist/icons/CouponIcon.mjs +17 -2
- package/dist/icons/DocumentIcon.js +17 -2
- package/dist/icons/DocumentIcon.mjs +17 -2
- package/dist/icons/HelpIcon.js +17 -2
- package/dist/icons/HelpIcon.mjs +17 -2
- package/dist/icons/InfoIcon.js +17 -2
- package/dist/icons/InfoIcon.mjs +17 -2
- package/dist/icons/MailIcon.js +17 -2
- package/dist/icons/MailIcon.mjs +17 -2
- package/dist/icons/PhoneIcon.js +21 -2
- package/dist/icons/PhoneIcon.mjs +21 -2
- package/dist/icons/PlusIcon.js +17 -2
- package/dist/icons/PlusIcon.mjs +17 -2
- package/dist/icons/UndoIcon.js +21 -2
- package/dist/icons/UndoIcon.mjs +21 -2
- package/dist/index.js +50 -1
- package/dist/index.mjs +24 -1
- package/dist/store/store.js +104 -2
- package/dist/store/store.mjs +102 -2
- package/dist/store/usePillQueue.js +17 -2
- package/dist/store/usePillQueue.mjs +17 -2
- package/dist/styles.css +46 -17
- package/dist/utils/react.js +23 -2
- package/dist/utils/react.mjs +21 -2
- package/dist/utils/url.js +22 -2
- package/dist/utils/url.mjs +22 -2
- package/package.json +3 -2
- package/dist/MagicPill/MagicPill.js.map +0 -1
- package/dist/MagicPill/MagicPill.mjs.map +0 -1
- package/dist/MagicPill/choreography.js.map +0 -1
- package/dist/MagicPill/choreography.mjs.map +0 -1
- package/dist/Pill/Pill.js.map +0 -1
- package/dist/Pill/Pill.mjs.map +0 -1
- package/dist/Pill/PillAction.js.map +0 -1
- package/dist/Pill/PillAction.mjs.map +0 -1
- package/dist/Pill/PillContext.js.map +0 -1
- package/dist/Pill/PillContext.mjs.map +0 -1
- package/dist/Pill/PillPanel.js.map +0 -1
- package/dist/Pill/PillPanel.mjs.map +0 -1
- package/dist/Pill/parts.js.map +0 -1
- package/dist/Pill/parts.mjs.map +0 -1
- package/dist/PillNavbar/PillNavbar.js.map +0 -1
- package/dist/PillNavbar/PillNavbar.mjs.map +0 -1
- package/dist/PillNavbar/matchPath.js.map +0 -1
- package/dist/PillNavbar/matchPath.mjs.map +0 -1
- package/dist/PillNotification/PillNotification.js.map +0 -1
- package/dist/PillNotification/PillNotification.mjs.map +0 -1
- package/dist/icons/ArrowIcon.js.map +0 -1
- package/dist/icons/ArrowIcon.mjs.map +0 -1
- package/dist/icons/BoxIcon.js.map +0 -1
- package/dist/icons/BoxIcon.mjs.map +0 -1
- package/dist/icons/CheckIcon.js.map +0 -1
- package/dist/icons/CheckIcon.mjs.map +0 -1
- package/dist/icons/CloseIcon.js.map +0 -1
- package/dist/icons/CloseIcon.mjs.map +0 -1
- package/dist/icons/CouponIcon.js.map +0 -1
- package/dist/icons/CouponIcon.mjs.map +0 -1
- package/dist/icons/DocumentIcon.js.map +0 -1
- package/dist/icons/DocumentIcon.mjs.map +0 -1
- package/dist/icons/HelpIcon.js.map +0 -1
- package/dist/icons/HelpIcon.mjs.map +0 -1
- package/dist/icons/InfoIcon.js.map +0 -1
- package/dist/icons/InfoIcon.mjs.map +0 -1
- package/dist/icons/MailIcon.js.map +0 -1
- package/dist/icons/MailIcon.mjs.map +0 -1
- package/dist/icons/PhoneIcon.js.map +0 -1
- package/dist/icons/PhoneIcon.mjs.map +0 -1
- package/dist/icons/PlusIcon.js.map +0 -1
- package/dist/icons/PlusIcon.mjs.map +0 -1
- package/dist/icons/UndoIcon.js.map +0 -1
- package/dist/icons/UndoIcon.mjs.map +0 -1
- package/dist/store/store.js.map +0 -1
- package/dist/store/store.mjs.map +0 -1
- package/dist/store/usePillQueue.js.map +0 -1
- package/dist/store/usePillQueue.mjs.map +0 -1
- package/dist/utils/react.js.map +0 -1
- package/dist/utils/react.mjs.map +0 -1
- package/dist/utils/url.js.map +0 -1
- package/dist/utils/url.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -33,6 +33,14 @@ Work towards 9.0.0, the first release with a stable API. It's a breaking release
|
|
|
33
33
|
- A migration guide from 8.x, [MIGRATING.md](./MIGRATING.md), shipped in the package
|
|
34
34
|
- The README is rewritten for v9: quick start, notifications, the host and its base layer, the primitive, React Server Components, styling and tokens, accessibility and security
|
|
35
35
|
|
|
36
|
+
### Changed
|
|
37
|
+
- The published JavaScript is readable (not minified) and has no source maps. Your app bundles exactly the same amount of code (your bundler minifies it), and the package is about 40% smaller to install (196 kB unpacked instead of 331 kB in 9.0.0-next.0)
|
|
38
|
+
- Size, measured and checked in CI: the quick start (`MagicPill` + `pill()`) adds about 4.9 kB of gzipped JavaScript and 2.2 kB of gzipped CSS to an app (both minified, as your bundler does); a hand-written `Pill` with its parts, about 1.4 kB of JavaScript
|
|
39
|
+
|
|
40
|
+
### Fixed (since 9.0.0-next.0)
|
|
41
|
+
- Global resets on links and buttons, such as `a { color: inherit }` or `button { background: none }`, no longer blank out the pill's actions: their look now lives on an inner `[data-mp="action-body"]`
|
|
42
|
+
- In a narrow container, an inline pill wraps its message instead of cutting off its action. While the pill grows, the message still stays on one line
|
|
43
|
+
|
|
36
44
|
### Removed
|
|
37
45
|
- The 8.x API: the `pillData` prop and `mode`, the types (`MagicPillType`, `Mode`, `NotificationIcon`, `CTAIcon`, `MagicPillNotification*`, `MagicPillNavbar*`), the 15 handlers (`updateMode` … `handleActiveNavItem`), `Icons`, `CTAIcons`, `getIconComponent` and `defaultPillData`
|
|
38
46
|
- The 8.x stylesheet: its `.magicPill`/`.CTA`/… classes, the `--mp__*` variables and the Google Fonts `@import`. The `magic-pill/magicpill.css` and `magic-pill/dist/magicpill.css` paths now serve the new stylesheet
|
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ On top of it come ready-made **templates** (a notification, a navbar) and a **co
|
|
|
27
27
|
- [X] Accessible: screen reader announcements, keyboard focus pauses the countdown, focus handling for panels and modals
|
|
28
28
|
- [X] Works in any React 18+ app, including React Server Components (Next.js App Router), with any router
|
|
29
29
|
- [X] Styles you can override without `!important`, design tokens, no fonts loaded behind your back
|
|
30
|
+
- [X] Tiny: the quick start adds about **7 kB gzipped** to your app (4.9 kB of JavaScript + 2.2 kB of CSS, minified by your bundler), checked in CI. Unused parts are tree-shaken.
|
|
30
31
|
- [X] Written in TypeScript, zero runtime dependencies
|
|
31
32
|
|
|
32
33
|
## Installation
|
|
@@ -319,8 +320,9 @@ Twelve built-in icons, drawn in `currentColor` and decorative by default (`aria-
|
|
|
319
320
|
|
|
320
321
|
## Styling
|
|
321
322
|
- **Easy to override:** every rule sits in a `magic-pill` cascade layer. Any CSS of yours that isn't in a layer wins, whatever its specificity, with no `!important`.
|
|
322
|
-
- **No collisions:** parts are selected by `data-mp` attributes (`[data-mp="pill"]`, `"icon"`, `"divider"`, `"message"`, `"actions"`, `"action"`, `"action-icon"`, `"label"`, `"panel"`, `"panel-title"`, `"panel-body"`, `"panel-footer"`, `"navbar"`), and states by `data-presence`, `data-placement`, `data-orientation` and `data-variant`. There are no class names of ours to clash with yours.
|
|
323
|
+
- **No collisions:** parts are selected by `data-mp` attributes (`[data-mp="pill"]`, `"icon"`, `"divider"`, `"message"`, `"actions"`, `"action"`, `"action-body"`, `"action-icon"`, `"label"`, `"panel"`, `"panel-title"`, `"panel-body"`, `"panel-footer"`, `"navbar"`), and states by `data-presence`, `data-placement`, `data-orientation` and `data-variant`. There are no class names of ours to clash with yours.
|
|
323
324
|
- **Classes:** every part takes a `className`; templates and `<MagicPill>` take `classNames`.
|
|
325
|
+
- **Global resets are safe:** an action's look lives on its inner `[data-mp="action-body"]`, so app-wide rules such as `a { color: inherit }` or `button { background: none }` don't break it. To restyle an action, target `[data-mp="action-body"]`.
|
|
324
326
|
- **Tailwind CSS v4:** declare the layer order once, before your imports, so utilities win: `@layer theme, base, magic-pill, components, utilities;`.
|
|
325
327
|
- **Your own variants:** `variant="brand"` gives `data-variant="brand"`, so you can write `[data-variant="brand"] { --mp-surface: #4b2bd6; }`.
|
|
326
328
|
|
|
@@ -376,6 +378,7 @@ npm install
|
|
|
376
378
|
npm run typecheck # TypeScript
|
|
377
379
|
npm test # Vitest test suite
|
|
378
380
|
npm run build # Builds dist/ with tsdown
|
|
381
|
+
npm run size # What an app ships, against the size budget (after a build)
|
|
379
382
|
```
|
|
380
383
|
|
|
381
384
|
4. You're good to go! For easier development, we recommend creating a Next.js app and creating a symbolic link between the app and the package. Now, when you're making changes to the code in src/, the changes should be reflected in your Next.js app.
|
|
@@ -1,2 +1,185 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
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;
|
|
@@ -1,2 +1,185 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
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 };
|
|
@@ -1,2 +1,91 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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;
|
|
@@ -1,2 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 };
|