magic-pill 9.0.0-next.0 → 9.0.0-next.2
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 +15 -0
- package/README.md +6 -2
- package/dist/MagicPill/MagicPill.js +194 -2
- package/dist/MagicPill/MagicPill.mjs +194 -2
- package/dist/MagicPill/choreography.js +132 -2
- package/dist/MagicPill/choreography.mjs +125 -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 +90 -2
- package/dist/Pill/PillPanel.mjs +89 -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 +125 -35
- package/dist/utils/inputModality.js +23 -0
- package/dist/utils/inputModality.mjs +22 -0
- 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
|
@@ -29,10 +29,25 @@ Work towards 9.0.0, the first release with a stable API. It's a breaking release
|
|
|
29
29
|
- every rule sits in a `magic-pill` cascade layer and targets `data-mp` attributes, so your own CSS overrides it without `!important` and no class names collide;
|
|
30
30
|
- it loads no fonts (it uses your page's fonts unless you set `--mp-font-text`/`--mp-font-accent`);
|
|
31
31
|
- it's sized in `rem`, fits phone screens and safe areas, and supports reduced motion and forced colours
|
|
32
|
+
- the pill pops in and scales out from its centre; `--mp-scale-origin` changes that (e.g. `bottom` to come out of the edge a bottom pill sits on)
|
|
32
33
|
|
|
33
34
|
- A migration guide from 8.x, [MIGRATING.md](./MIGRATING.md), shipped in the package
|
|
34
35
|
- 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
36
|
|
|
37
|
+
### Changed
|
|
38
|
+
- 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)
|
|
39
|
+
- Size, measured and checked in CI: the quick start (`MagicPill` + `pill()`) adds about 5.3 kB of gzipped JavaScript and 2.3 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
|
|
40
|
+
|
|
41
|
+
### Fixed (since 9.0.0-next.0)
|
|
42
|
+
- 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"]`
|
|
43
|
+
- 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
|
|
44
|
+
- On touchscreens, a tapped action no longer stays open: labels open on hover only on devices that can really hover. Keyboard focus, an expanded panel and the current page still open them everywhere
|
|
45
|
+
- When a notification takes the pill over (from the navbar, or the next one in the queue), its message no longer wraps one word per line while the pill grows: `<MagicPill>` shows new content closed first, then the pill opens around it
|
|
46
|
+
- Long text no longer looks clipped while the pill changes size. When the pill closes, its content fades out first, then the pill shrinks; when it opens, the pill grows first, then the content fades in
|
|
47
|
+
- When the app removes the host's base layer (e.g. `{showNavbar && <PillNavbar … />}`), the pill closes around it into a circle, then scales out, instead of losing its content at once and collapsing into a narrow oval
|
|
48
|
+
- The pill scales from its centre instead of the edge it sits on (see `--mp-scale-origin`)
|
|
49
|
+
- On touchscreens, a notification whose panel was opened and closed no longer stays until the next tap elsewhere. The countdown pauses for keyboard focus and for a mouse or pen hovering, not for a tap, and after a tap the panel gives the focus back without showing it as keyboard focus
|
|
50
|
+
|
|
36
51
|
### Removed
|
|
37
52
|
- 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
53
|
- 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.5 kB gzipped** to your app (5.3 kB of JavaScript + 2.3 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
|
|
|
@@ -339,12 +341,13 @@ Twelve built-in icons, drawn in `currentColor` and decorative by default (`aria-
|
|
|
339
341
|
| `--mp-offset` / `--mp-z-index` | `1.5rem` from the viewport edges (plus safe areas) / `50` |
|
|
340
342
|
| `--mp-backdrop` | the overlay's backdrop |
|
|
341
343
|
| `--mp-duration-enter` / `--mp-duration-change` / `--mp-duration-exit` | `500ms` / `250ms` / `300ms` |
|
|
344
|
+
| `--mp-scale-origin` | `center`: where the pill pops in from and scales out to (e.g. `bottom` to come out of the edge) |
|
|
342
345
|
|
|
343
346
|
The pill fits small screens: sizes are in `rem`, fixed pills stay inside the viewport and its safe areas, and long panels scroll.
|
|
344
347
|
|
|
345
348
|
## Accessibility
|
|
346
349
|
- **Announcements:** each `<MagicPill>` has a polite live region that reads notifications out (or your `announce` text). A hidden pill leaves the accessibility tree.
|
|
347
|
-
- **Time to act:** the countdown pauses
|
|
350
|
+
- **Time to act:** the countdown pauses while a mouse hovers the pill, on keyboard focus, and while a panel is open. A tap doesn't pause it.
|
|
348
351
|
- **Panels and modals:** they take and give back the focus, and close on Escape. The overlay is a native modal `<dialog>`.
|
|
349
352
|
- **Accessible names:** every action has one (`label` is required), and icons are hidden from assistive technology.
|
|
350
353
|
- **Motion and contrast:** with `prefers-reduced-motion`, shape changes become short fades. In forced-colours mode, the shapes keep visible outlines.
|
|
@@ -376,6 +379,7 @@ npm install
|
|
|
376
379
|
npm run typecheck # TypeScript
|
|
377
380
|
npm test # Vitest test suite
|
|
378
381
|
npm run build # Builds dist/ with tsdown
|
|
382
|
+
npm run size # What an app ships, against the size budget (after a build)
|
|
379
383
|
```
|
|
380
384
|
|
|
381
385
|
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,194 @@
|
|
|
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_inputModality = require("../utils/inputModality.js");
|
|
9
|
+
const require_usePillQueue = require("../store/usePillQueue.js");
|
|
10
|
+
const require_react = require("../utils/react.js");
|
|
11
|
+
const require_choreography = require("./choreography.js");
|
|
12
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
13
|
+
|
|
14
|
+
// src/MagicPill/MagicPill.tsx
|
|
15
|
+
const DEFAULT_DURATION = 5e3;
|
|
16
|
+
const VISUALLY_HIDDEN = {
|
|
17
|
+
position: "absolute",
|
|
18
|
+
width: 1,
|
|
19
|
+
height: 1,
|
|
20
|
+
margin: -1,
|
|
21
|
+
padding: 0,
|
|
22
|
+
overflow: "hidden",
|
|
23
|
+
clip: "rect(0 0 0 0)",
|
|
24
|
+
whiteSpace: "nowrap",
|
|
25
|
+
border: 0
|
|
26
|
+
};
|
|
27
|
+
const hasContent = (node) => node !== void 0 && node !== null && node !== false;
|
|
28
|
+
function useDocumentHidden() {
|
|
29
|
+
const [hidden, setHidden] = (0, react.useState)(false);
|
|
30
|
+
(0, react.useEffect)(() => {
|
|
31
|
+
const update = () => setHidden(document.visibilityState === "hidden");
|
|
32
|
+
update();
|
|
33
|
+
document.addEventListener("visibilitychange", update);
|
|
34
|
+
return () => document.removeEventListener("visibilitychange", update);
|
|
35
|
+
}, []);
|
|
36
|
+
return hidden;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The batteries-included pill: a <Pill> with a base layer, a queue of
|
|
40
|
+
* notifications fed by pill(name), and a live region that announces them.
|
|
41
|
+
*/
|
|
42
|
+
const MagicPill = (0, react.forwardRef)(function MagicPill({ name = "main", children, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, linkComponent, classNames, placement = "bottom", variant, onPointerEnter, onPointerLeave, onFocus, onBlur, ...rest }, forwardedRef) {
|
|
43
|
+
const store = require_store.getStore(name);
|
|
44
|
+
const { current } = require_usePillQueue.usePillQueue(name);
|
|
45
|
+
const rootRef = (0, react.useRef)(null);
|
|
46
|
+
const ref = (0, react.useMemo)(() => require_react.mergeRefs(rootRef, forwardedRef), [forwardedRef]);
|
|
47
|
+
(0, react.useEffect)(() => store.registerHost(), [store]);
|
|
48
|
+
(0, react.useEffect)(require_inputModality.trackInputModality, []);
|
|
49
|
+
const hasBase = hasContent(children);
|
|
50
|
+
const lastBase = (0, react.useRef)(children);
|
|
51
|
+
if (hasBase) lastBase.current = children;
|
|
52
|
+
const target = (0, react.useMemo)(() => current ? {
|
|
53
|
+
kind: "notification",
|
|
54
|
+
item: current
|
|
55
|
+
} : hasBase ? require_choreography.BASE : idle === "rest" ? require_choreography.IDLE : require_choreography.NONE, [
|
|
56
|
+
current,
|
|
57
|
+
hasBase,
|
|
58
|
+
idle
|
|
59
|
+
]);
|
|
60
|
+
const [displayed, setDisplayed] = (0, react.useState)(() => ({
|
|
61
|
+
scene: target.kind === "notification" ? require_choreography.NONE : target,
|
|
62
|
+
presence: target.kind === "base" ? "compact" : target.kind === "idle" ? "rest" : "hidden"
|
|
63
|
+
}));
|
|
64
|
+
const displayedRef = (0, react.useRef)(displayed);
|
|
65
|
+
displayedRef.current = displayed;
|
|
66
|
+
const followUps = (0, react.useRef)([]);
|
|
67
|
+
(0, react.useEffect)(() => {
|
|
68
|
+
const next = followUps.current.shift();
|
|
69
|
+
if (!next) return;
|
|
70
|
+
rootRef.current?.offsetWidth;
|
|
71
|
+
setDisplayed(next);
|
|
72
|
+
}, [displayed]);
|
|
73
|
+
(0, react.useEffect)(() => {
|
|
74
|
+
const current = displayedRef.current;
|
|
75
|
+
const timers = require_choreography.batchSteps(current, require_choreography.planSteps(current, target, require_choreography.readTimings(rootRef.current))).map(({ at, states }) => {
|
|
76
|
+
const apply = () => {
|
|
77
|
+
followUps.current = states.slice(1);
|
|
78
|
+
setDisplayed(states[0]);
|
|
79
|
+
};
|
|
80
|
+
if (at > 0) return setTimeout(apply, at);
|
|
81
|
+
apply();
|
|
82
|
+
});
|
|
83
|
+
return () => {
|
|
84
|
+
timers.forEach(clearTimeout);
|
|
85
|
+
followUps.current = [];
|
|
86
|
+
};
|
|
87
|
+
}, [target]);
|
|
88
|
+
const [hovered, setHovered] = (0, react.useState)(false);
|
|
89
|
+
const [focused, setFocused] = (0, react.useState)(false);
|
|
90
|
+
const documentHidden = useDocumentHidden();
|
|
91
|
+
(0, react.useEffect)(() => {
|
|
92
|
+
if (displayed.presence === "hidden") {
|
|
93
|
+
setHovered(false);
|
|
94
|
+
setFocused(false);
|
|
95
|
+
}
|
|
96
|
+
}, [displayed.presence]);
|
|
97
|
+
const shown = displayed.scene.kind === "notification" && require_choreography.isOpen(displayed.presence) ? displayed.scene.item : null;
|
|
98
|
+
const paused = hovered || focused || documentHidden || displayed.presence === "panel";
|
|
99
|
+
(0, react.useEffect)(() => {
|
|
100
|
+
if (!shown || paused) return;
|
|
101
|
+
const ms = shown.input.duration ?? duration;
|
|
102
|
+
if (!Number.isFinite(ms)) return;
|
|
103
|
+
const timer = setTimeout(() => store.dismiss(shown.id, "timeout"), ms);
|
|
104
|
+
return () => clearTimeout(timer);
|
|
105
|
+
}, [
|
|
106
|
+
shown,
|
|
107
|
+
paused,
|
|
108
|
+
duration,
|
|
109
|
+
store
|
|
110
|
+
]);
|
|
111
|
+
const [announcement, setAnnouncement] = (0, react.useState)(null);
|
|
112
|
+
(0, react.useEffect)(() => {
|
|
113
|
+
if (!shown) return;
|
|
114
|
+
const { announce } = shown.input;
|
|
115
|
+
if (announce === false) return;
|
|
116
|
+
const root = rootRef.current;
|
|
117
|
+
const text = typeof announce === "string" ? announce : (root?.querySelector("[data-mp=\"message\"]") ?? root)?.textContent?.trim() ?? "";
|
|
118
|
+
if (text) setAnnouncement((previous) => previous?.key === shown ? previous : {
|
|
119
|
+
key: shown,
|
|
120
|
+
text
|
|
121
|
+
});
|
|
122
|
+
}, [shown]);
|
|
123
|
+
const handlePresenceChange = (next) => {
|
|
124
|
+
const { scene, presence } = displayedRef.current;
|
|
125
|
+
if (next === "hidden") {
|
|
126
|
+
if (scene.kind === "notification") store.dismiss(scene.item.id, "dismissed");
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (require_choreography.isOpen(next) && require_choreography.isOpen(presence)) setDisplayed({
|
|
130
|
+
scene,
|
|
131
|
+
presence: next
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
const renderScene = (scene) => {
|
|
135
|
+
switch (scene.kind) {
|
|
136
|
+
case "base": return hasBase ? children : lastBase.current;
|
|
137
|
+
case "idle": return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_parts.PillIcon, { children: idleIcon });
|
|
138
|
+
case "notification": {
|
|
139
|
+
const { id, input } = scene.item;
|
|
140
|
+
if (input.render) return input.render({
|
|
141
|
+
id,
|
|
142
|
+
dismiss: () => store.dismiss(id, "dismissed")
|
|
143
|
+
});
|
|
144
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillNotification.NotificationContent, {
|
|
145
|
+
icon: input.icon,
|
|
146
|
+
message: input.message,
|
|
147
|
+
action: input.action,
|
|
148
|
+
panel: input.panel,
|
|
149
|
+
linkComponent,
|
|
150
|
+
classNames,
|
|
151
|
+
onAction: (event) => {
|
|
152
|
+
if (!event.defaultPrevented) store.dismiss(id, "action");
|
|
153
|
+
}
|
|
154
|
+
}, id);
|
|
155
|
+
}
|
|
156
|
+
default: return null;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const sceneVariant = displayed.scene.kind === "notification" ? displayed.scene.item.input.variant ?? variant : variant;
|
|
160
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Pill, {
|
|
161
|
+
...rest,
|
|
162
|
+
ref,
|
|
163
|
+
placement,
|
|
164
|
+
variant: sceneVariant,
|
|
165
|
+
presence: displayed.presence,
|
|
166
|
+
onPresenceChange: handlePresenceChange,
|
|
167
|
+
onPointerEnter: (event) => {
|
|
168
|
+
onPointerEnter?.(event);
|
|
169
|
+
if (event.pointerType !== "touch") setHovered(true);
|
|
170
|
+
},
|
|
171
|
+
onPointerLeave: (event) => {
|
|
172
|
+
onPointerLeave?.(event);
|
|
173
|
+
setHovered(false);
|
|
174
|
+
},
|
|
175
|
+
onFocus: (event) => {
|
|
176
|
+
onFocus?.(event);
|
|
177
|
+
if (require_inputModality.getInputModality() === "keyboard") setFocused(true);
|
|
178
|
+
},
|
|
179
|
+
onBlur: (event) => {
|
|
180
|
+
onBlur?.(event);
|
|
181
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
|
|
182
|
+
},
|
|
183
|
+
children: renderScene(displayed.scene)
|
|
184
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
185
|
+
role: "status",
|
|
186
|
+
"aria-live": "polite",
|
|
187
|
+
"aria-atomic": "true",
|
|
188
|
+
"data-mp": "announcer",
|
|
189
|
+
style: VISUALLY_HIDDEN,
|
|
190
|
+
children: announcement && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: announcement.text }, announcement.key.id + announcement.text)
|
|
191
|
+
})] });
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
module.exports = MagicPill;
|
|
@@ -1,2 +1,194 @@
|
|
|
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 { getInputModality, trackInputModality } from "../utils/inputModality.mjs";
|
|
9
|
+
import { usePillQueue } from "../store/usePillQueue.mjs";
|
|
10
|
+
import { mergeRefs } from "../utils/react.mjs";
|
|
11
|
+
import { BASE, IDLE, NONE, batchSteps, isOpen, planSteps, readTimings } from "./choreography.mjs";
|
|
12
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
13
|
+
|
|
14
|
+
// src/MagicPill/MagicPill.tsx
|
|
15
|
+
const DEFAULT_DURATION = 5e3;
|
|
16
|
+
const VISUALLY_HIDDEN = {
|
|
17
|
+
position: "absolute",
|
|
18
|
+
width: 1,
|
|
19
|
+
height: 1,
|
|
20
|
+
margin: -1,
|
|
21
|
+
padding: 0,
|
|
22
|
+
overflow: "hidden",
|
|
23
|
+
clip: "rect(0 0 0 0)",
|
|
24
|
+
whiteSpace: "nowrap",
|
|
25
|
+
border: 0
|
|
26
|
+
};
|
|
27
|
+
const hasContent = (node) => node !== void 0 && node !== null && node !== false;
|
|
28
|
+
function useDocumentHidden() {
|
|
29
|
+
const [hidden, setHidden] = useState(false);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const update = () => setHidden(document.visibilityState === "hidden");
|
|
32
|
+
update();
|
|
33
|
+
document.addEventListener("visibilitychange", update);
|
|
34
|
+
return () => document.removeEventListener("visibilitychange", update);
|
|
35
|
+
}, []);
|
|
36
|
+
return hidden;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The batteries-included pill: a <Pill> with a base layer, a queue of
|
|
40
|
+
* notifications fed by pill(name), and a live region that announces them.
|
|
41
|
+
*/
|
|
42
|
+
const MagicPill = forwardRef(function MagicPill({ name = "main", children, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, linkComponent, classNames, placement = "bottom", variant, onPointerEnter, onPointerLeave, onFocus, onBlur, ...rest }, forwardedRef) {
|
|
43
|
+
const store = getStore(name);
|
|
44
|
+
const { current } = usePillQueue(name);
|
|
45
|
+
const rootRef = useRef(null);
|
|
46
|
+
const ref = useMemo(() => mergeRefs(rootRef, forwardedRef), [forwardedRef]);
|
|
47
|
+
useEffect(() => store.registerHost(), [store]);
|
|
48
|
+
useEffect(trackInputModality, []);
|
|
49
|
+
const hasBase = hasContent(children);
|
|
50
|
+
const lastBase = useRef(children);
|
|
51
|
+
if (hasBase) lastBase.current = children;
|
|
52
|
+
const target = useMemo(() => current ? {
|
|
53
|
+
kind: "notification",
|
|
54
|
+
item: current
|
|
55
|
+
} : hasBase ? BASE : idle === "rest" ? IDLE : NONE, [
|
|
56
|
+
current,
|
|
57
|
+
hasBase,
|
|
58
|
+
idle
|
|
59
|
+
]);
|
|
60
|
+
const [displayed, setDisplayed] = useState(() => ({
|
|
61
|
+
scene: target.kind === "notification" ? NONE : target,
|
|
62
|
+
presence: target.kind === "base" ? "compact" : target.kind === "idle" ? "rest" : "hidden"
|
|
63
|
+
}));
|
|
64
|
+
const displayedRef = useRef(displayed);
|
|
65
|
+
displayedRef.current = displayed;
|
|
66
|
+
const followUps = useRef([]);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
const next = followUps.current.shift();
|
|
69
|
+
if (!next) return;
|
|
70
|
+
rootRef.current?.offsetWidth;
|
|
71
|
+
setDisplayed(next);
|
|
72
|
+
}, [displayed]);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
const current = displayedRef.current;
|
|
75
|
+
const timers = batchSteps(current, planSteps(current, target, readTimings(rootRef.current))).map(({ at, states }) => {
|
|
76
|
+
const apply = () => {
|
|
77
|
+
followUps.current = states.slice(1);
|
|
78
|
+
setDisplayed(states[0]);
|
|
79
|
+
};
|
|
80
|
+
if (at > 0) return setTimeout(apply, at);
|
|
81
|
+
apply();
|
|
82
|
+
});
|
|
83
|
+
return () => {
|
|
84
|
+
timers.forEach(clearTimeout);
|
|
85
|
+
followUps.current = [];
|
|
86
|
+
};
|
|
87
|
+
}, [target]);
|
|
88
|
+
const [hovered, setHovered] = useState(false);
|
|
89
|
+
const [focused, setFocused] = useState(false);
|
|
90
|
+
const documentHidden = useDocumentHidden();
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (displayed.presence === "hidden") {
|
|
93
|
+
setHovered(false);
|
|
94
|
+
setFocused(false);
|
|
95
|
+
}
|
|
96
|
+
}, [displayed.presence]);
|
|
97
|
+
const shown = displayed.scene.kind === "notification" && isOpen(displayed.presence) ? displayed.scene.item : null;
|
|
98
|
+
const paused = hovered || focused || documentHidden || displayed.presence === "panel";
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
if (!shown || paused) return;
|
|
101
|
+
const ms = shown.input.duration ?? duration;
|
|
102
|
+
if (!Number.isFinite(ms)) return;
|
|
103
|
+
const timer = setTimeout(() => store.dismiss(shown.id, "timeout"), ms);
|
|
104
|
+
return () => clearTimeout(timer);
|
|
105
|
+
}, [
|
|
106
|
+
shown,
|
|
107
|
+
paused,
|
|
108
|
+
duration,
|
|
109
|
+
store
|
|
110
|
+
]);
|
|
111
|
+
const [announcement, setAnnouncement] = useState(null);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (!shown) return;
|
|
114
|
+
const { announce } = shown.input;
|
|
115
|
+
if (announce === false) return;
|
|
116
|
+
const root = rootRef.current;
|
|
117
|
+
const text = typeof announce === "string" ? announce : (root?.querySelector("[data-mp=\"message\"]") ?? root)?.textContent?.trim() ?? "";
|
|
118
|
+
if (text) setAnnouncement((previous) => previous?.key === shown ? previous : {
|
|
119
|
+
key: shown,
|
|
120
|
+
text
|
|
121
|
+
});
|
|
122
|
+
}, [shown]);
|
|
123
|
+
const handlePresenceChange = (next) => {
|
|
124
|
+
const { scene, presence } = displayedRef.current;
|
|
125
|
+
if (next === "hidden") {
|
|
126
|
+
if (scene.kind === "notification") store.dismiss(scene.item.id, "dismissed");
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (isOpen(next) && isOpen(presence)) setDisplayed({
|
|
130
|
+
scene,
|
|
131
|
+
presence: next
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
const renderScene = (scene) => {
|
|
135
|
+
switch (scene.kind) {
|
|
136
|
+
case "base": return hasBase ? children : lastBase.current;
|
|
137
|
+
case "idle": return /* @__PURE__ */ jsx(PillIcon, { children: idleIcon });
|
|
138
|
+
case "notification": {
|
|
139
|
+
const { id, input } = scene.item;
|
|
140
|
+
if (input.render) return input.render({
|
|
141
|
+
id,
|
|
142
|
+
dismiss: () => store.dismiss(id, "dismissed")
|
|
143
|
+
});
|
|
144
|
+
return /* @__PURE__ */ jsx(NotificationContent, {
|
|
145
|
+
icon: input.icon,
|
|
146
|
+
message: input.message,
|
|
147
|
+
action: input.action,
|
|
148
|
+
panel: input.panel,
|
|
149
|
+
linkComponent,
|
|
150
|
+
classNames,
|
|
151
|
+
onAction: (event) => {
|
|
152
|
+
if (!event.defaultPrevented) store.dismiss(id, "action");
|
|
153
|
+
}
|
|
154
|
+
}, id);
|
|
155
|
+
}
|
|
156
|
+
default: return null;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const sceneVariant = displayed.scene.kind === "notification" ? displayed.scene.item.input.variant ?? variant : variant;
|
|
160
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Pill, {
|
|
161
|
+
...rest,
|
|
162
|
+
ref,
|
|
163
|
+
placement,
|
|
164
|
+
variant: sceneVariant,
|
|
165
|
+
presence: displayed.presence,
|
|
166
|
+
onPresenceChange: handlePresenceChange,
|
|
167
|
+
onPointerEnter: (event) => {
|
|
168
|
+
onPointerEnter?.(event);
|
|
169
|
+
if (event.pointerType !== "touch") setHovered(true);
|
|
170
|
+
},
|
|
171
|
+
onPointerLeave: (event) => {
|
|
172
|
+
onPointerLeave?.(event);
|
|
173
|
+
setHovered(false);
|
|
174
|
+
},
|
|
175
|
+
onFocus: (event) => {
|
|
176
|
+
onFocus?.(event);
|
|
177
|
+
if (getInputModality() === "keyboard") setFocused(true);
|
|
178
|
+
},
|
|
179
|
+
onBlur: (event) => {
|
|
180
|
+
onBlur?.(event);
|
|
181
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
|
|
182
|
+
},
|
|
183
|
+
children: renderScene(displayed.scene)
|
|
184
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
185
|
+
role: "status",
|
|
186
|
+
"aria-live": "polite",
|
|
187
|
+
"aria-atomic": "true",
|
|
188
|
+
"data-mp": "announcer",
|
|
189
|
+
style: VISUALLY_HIDDEN,
|
|
190
|
+
children: announcement && /* @__PURE__ */ jsx("span", { children: announcement.text }, announcement.key.id + announcement.text)
|
|
191
|
+
})] });
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
export { MagicPill as default };
|
|
@@ -1,2 +1,132 @@
|
|
|
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: fade it out, 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.fade + 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.fade + 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
|
+
/**
|
|
61
|
+
* Groups the steps by the time they're due. Steps due at the same time
|
|
62
|
+
* collapse into the last one, except new content swapped in while the pill
|
|
63
|
+
* is closed: it's shown closed first, so the stylesheet can transition it
|
|
64
|
+
* open (otherwise it appears at the circle's width and wraps word by word).
|
|
65
|
+
*/
|
|
66
|
+
function batchSteps(current, steps) {
|
|
67
|
+
const batches = [];
|
|
68
|
+
let at = 0;
|
|
69
|
+
let scene = current.scene;
|
|
70
|
+
let unshown = false;
|
|
71
|
+
for (const { delay, scene: nextScene, presence } of steps) {
|
|
72
|
+
at += delay;
|
|
73
|
+
const state = {
|
|
74
|
+
scene: nextScene,
|
|
75
|
+
presence
|
|
76
|
+
};
|
|
77
|
+
const swapped = nextScene !== scene;
|
|
78
|
+
scene = nextScene;
|
|
79
|
+
const batch = batches[batches.length - 1];
|
|
80
|
+
if (batch && batch.at === at) {
|
|
81
|
+
if (unshown && isOpen(presence)) {
|
|
82
|
+
batch.states.push(state);
|
|
83
|
+
unshown = false;
|
|
84
|
+
} else {
|
|
85
|
+
batch.states[batch.states.length - 1] = state;
|
|
86
|
+
unshown = (unshown || swapped) && !isOpen(presence);
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
batches.push({
|
|
90
|
+
at,
|
|
91
|
+
states: [state]
|
|
92
|
+
});
|
|
93
|
+
unshown = swapped && !isOpen(presence);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return batches;
|
|
97
|
+
}
|
|
98
|
+
/** "500ms" or "0.5s" → 500; anything else → the fallback. */
|
|
99
|
+
function parseDuration(value, fallback) {
|
|
100
|
+
const match = /^\s*([\d.]+)(ms|s)\s*$/.exec(value);
|
|
101
|
+
if (!match) return fallback;
|
|
102
|
+
const amount = Number.parseFloat(match[1]);
|
|
103
|
+
return match[2] === "s" ? amount * 1e3 : amount;
|
|
104
|
+
}
|
|
105
|
+
/** The timings the stylesheet uses for this pill, or its defaults (e.g. when unstyled). */
|
|
106
|
+
function readTimings(element) {
|
|
107
|
+
const style = element ? getComputedStyle(element) : null;
|
|
108
|
+
const read = (name, fallback) => parseDuration(style?.getPropertyValue(name) ?? "", fallback);
|
|
109
|
+
const reduced = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
110
|
+
return reduced ? {
|
|
111
|
+
enter: 0,
|
|
112
|
+
fade: 0,
|
|
113
|
+
change: 0,
|
|
114
|
+
exit: 150,
|
|
115
|
+
reduced
|
|
116
|
+
} : {
|
|
117
|
+
enter: read("--_mp-enter", 500),
|
|
118
|
+
fade: read("--_mp-fade", 150),
|
|
119
|
+
change: read("--_mp-change", 250),
|
|
120
|
+
exit: read("--_mp-exit", 300),
|
|
121
|
+
reduced
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
exports.BASE = BASE;
|
|
126
|
+
exports.IDLE = IDLE;
|
|
127
|
+
exports.NONE = NONE;
|
|
128
|
+
exports.batchSteps = batchSteps;
|
|
129
|
+
exports.isOpen = isOpen;
|
|
130
|
+
exports.parseDuration = parseDuration;
|
|
131
|
+
exports.planSteps = planSteps;
|
|
132
|
+
exports.readTimings = readTimings;
|