magic-pill 9.0.0-next.1 → 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 CHANGED
@@ -29,17 +29,24 @@ 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
 
36
37
  ### Changed
37
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)
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
+ - 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
39
40
 
40
41
  ### Fixed (since 9.0.0-next.0)
41
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"]`
42
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
43
50
 
44
51
  ### Removed
45
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`
package/README.md CHANGED
@@ -27,7 +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
+ - [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.
31
31
  - [X] Written in TypeScript, zero runtime dependencies
32
32
 
33
33
  ## Installation
@@ -341,12 +341,13 @@ Twelve built-in icons, drawn in `currentColor` and decorative by default (`aria-
341
341
  | `--mp-offset` / `--mp-z-index` | `1.5rem` from the viewport edges (plus safe areas) / `50` |
342
342
  | `--mp-backdrop` | the overlay's backdrop |
343
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) |
344
345
 
345
346
  The pill fits small screens: sizes are in `rem`, fixed pills stay inside the viewport and its safe areas, and long panels scroll.
346
347
 
347
348
  ## Accessibility
348
349
  - **Announcements:** each `<MagicPill>` has a polite live region that reads notifications out (or your `announce` text). A hidden pill leaves the accessibility tree.
349
- - **Time to act:** the countdown pauses on hover and keyboard focus, and while a panel is open.
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.
350
351
  - **Panels and modals:** they take and give back the focus, and close on Escape. The overlay is a native modal `<dialog>`.
351
352
  - **Accessible names:** every action has one (`label` is required), and icons are hidden from assistive technology.
352
353
  - **Motion and contrast:** with `prefers-reduced-motion`, shape changes become short fades. In forced-colours mode, the shapes keep visible outlines.
@@ -5,6 +5,7 @@ const require_Pill = require("../Pill/Pill.js");
5
5
  const require_parts = require("../Pill/parts.js");
6
6
  const require_PillNotification = require("../PillNotification/PillNotification.js");
7
7
  const require_store = require("../store/store.js");
8
+ const require_inputModality = require("../utils/inputModality.js");
8
9
  const require_usePillQueue = require("../store/usePillQueue.js");
9
10
  const require_react = require("../utils/react.js");
10
11
  const require_choreography = require("./choreography.js");
@@ -38,13 +39,16 @@ function useDocumentHidden() {
38
39
  * The batteries-included pill: a <Pill> with a base layer, a queue of
39
40
  * notifications fed by pill(name), and a live region that announces them.
40
41
  */
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 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) {
42
43
  const store = require_store.getStore(name);
43
44
  const { current } = require_usePillQueue.usePillQueue(name);
44
45
  const rootRef = (0, react.useRef)(null);
45
46
  const ref = (0, react.useMemo)(() => require_react.mergeRefs(rootRef, forwardedRef), [forwardedRef]);
46
47
  (0, react.useEffect)(() => store.registerHost(), [store]);
48
+ (0, react.useEffect)(require_inputModality.trackInputModality, []);
47
49
  const hasBase = hasContent(children);
50
+ const lastBase = (0, react.useRef)(children);
51
+ if (hasBase) lastBase.current = children;
48
52
  const target = (0, react.useMemo)(() => current ? {
49
53
  kind: "notification",
50
54
  item: current
@@ -59,22 +63,27 @@ const MagicPill = (0, react.forwardRef)(function MagicPill({ name = "main", chil
59
63
  }));
60
64
  const displayedRef = (0, react.useRef)(displayed);
61
65
  displayedRef.current = displayed;
66
+ const followUps = (0, react.useRef)([]);
62
67
  (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));
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();
76
82
  });
77
- return () => timers.forEach(clearTimeout);
83
+ return () => {
84
+ timers.forEach(clearTimeout);
85
+ followUps.current = [];
86
+ };
78
87
  }, [target]);
79
88
  const [hovered, setHovered] = (0, react.useState)(false);
80
89
  const [focused, setFocused] = (0, react.useState)(false);
@@ -124,7 +133,7 @@ const MagicPill = (0, react.forwardRef)(function MagicPill({ name = "main", chil
124
133
  };
125
134
  const renderScene = (scene) => {
126
135
  switch (scene.kind) {
127
- case "base": return children;
136
+ case "base": return hasBase ? children : lastBase.current;
128
137
  case "idle": return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_parts.PillIcon, { children: idleIcon });
129
138
  case "notification": {
130
139
  const { id, input } = scene.item;
@@ -155,21 +164,21 @@ const MagicPill = (0, react.forwardRef)(function MagicPill({ name = "main", chil
155
164
  variant: sceneVariant,
156
165
  presence: displayed.presence,
157
166
  onPresenceChange: handlePresenceChange,
158
- onMouseEnter: (event) => {
159
- onMouseEnter?.(event);
160
- setHovered(true);
167
+ onPointerEnter: (event) => {
168
+ onPointerEnter?.(event);
169
+ if (event.pointerType !== "touch") setHovered(true);
161
170
  },
162
- onMouseLeave: (event) => {
163
- onMouseLeave?.(event);
171
+ onPointerLeave: (event) => {
172
+ onPointerLeave?.(event);
164
173
  setHovered(false);
165
174
  },
166
175
  onFocus: (event) => {
167
176
  onFocus?.(event);
168
- setFocused(true);
177
+ if (require_inputModality.getInputModality() === "keyboard") setFocused(true);
169
178
  },
170
179
  onBlur: (event) => {
171
180
  onBlur?.(event);
172
- setFocused(event.currentTarget.contains(event.relatedTarget));
181
+ if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
173
182
  },
174
183
  children: renderScene(displayed.scene)
175
184
  }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
@@ -5,9 +5,10 @@ import Pill from "../Pill/Pill.mjs";
5
5
  import { PillIcon } from "../Pill/parts.mjs";
6
6
  import { NotificationContent } from "../PillNotification/PillNotification.mjs";
7
7
  import { getStore } from "../store/store.mjs";
8
+ import { getInputModality, trackInputModality } from "../utils/inputModality.mjs";
8
9
  import { usePillQueue } from "../store/usePillQueue.mjs";
9
10
  import { mergeRefs } from "../utils/react.mjs";
10
- import { BASE, IDLE, NONE, isOpen, planSteps, readTimings } from "./choreography.mjs";
11
+ import { BASE, IDLE, NONE, batchSteps, isOpen, planSteps, readTimings } from "./choreography.mjs";
11
12
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
12
13
 
13
14
  // src/MagicPill/MagicPill.tsx
@@ -38,13 +39,16 @@ function useDocumentHidden() {
38
39
  * The batteries-included pill: a <Pill> with a base layer, a queue of
39
40
  * notifications fed by pill(name), and a live region that announces them.
40
41
  */
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 MagicPill = forwardRef(function MagicPill({ name = "main", children, duration = DEFAULT_DURATION, idle = "hidden", idleIcon, linkComponent, classNames, placement = "bottom", variant, onPointerEnter, onPointerLeave, onFocus, onBlur, ...rest }, forwardedRef) {
42
43
  const store = getStore(name);
43
44
  const { current } = usePillQueue(name);
44
45
  const rootRef = useRef(null);
45
46
  const ref = useMemo(() => mergeRefs(rootRef, forwardedRef), [forwardedRef]);
46
47
  useEffect(() => store.registerHost(), [store]);
48
+ useEffect(trackInputModality, []);
47
49
  const hasBase = hasContent(children);
50
+ const lastBase = useRef(children);
51
+ if (hasBase) lastBase.current = children;
48
52
  const target = useMemo(() => current ? {
49
53
  kind: "notification",
50
54
  item: current
@@ -59,22 +63,27 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, durat
59
63
  }));
60
64
  const displayedRef = useRef(displayed);
61
65
  displayedRef.current = displayed;
66
+ const followUps = useRef([]);
62
67
  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));
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();
76
82
  });
77
- return () => timers.forEach(clearTimeout);
83
+ return () => {
84
+ timers.forEach(clearTimeout);
85
+ followUps.current = [];
86
+ };
78
87
  }, [target]);
79
88
  const [hovered, setHovered] = useState(false);
80
89
  const [focused, setFocused] = useState(false);
@@ -124,7 +133,7 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, durat
124
133
  };
125
134
  const renderScene = (scene) => {
126
135
  switch (scene.kind) {
127
- case "base": return children;
136
+ case "base": return hasBase ? children : lastBase.current;
128
137
  case "idle": return /* @__PURE__ */ jsx(PillIcon, { children: idleIcon });
129
138
  case "notification": {
130
139
  const { id, input } = scene.item;
@@ -155,21 +164,21 @@ const MagicPill = forwardRef(function MagicPill({ name = "main", children, durat
155
164
  variant: sceneVariant,
156
165
  presence: displayed.presence,
157
166
  onPresenceChange: handlePresenceChange,
158
- onMouseEnter: (event) => {
159
- onMouseEnter?.(event);
160
- setHovered(true);
167
+ onPointerEnter: (event) => {
168
+ onPointerEnter?.(event);
169
+ if (event.pointerType !== "touch") setHovered(true);
161
170
  },
162
- onMouseLeave: (event) => {
163
- onMouseLeave?.(event);
171
+ onPointerLeave: (event) => {
172
+ onPointerLeave?.(event);
164
173
  setHovered(false);
165
174
  },
166
175
  onFocus: (event) => {
167
176
  onFocus?.(event);
168
- setFocused(true);
177
+ if (getInputModality() === "keyboard") setFocused(true);
169
178
  },
170
179
  onBlur: (event) => {
171
180
  onBlur?.(event);
172
- setFocused(event.currentTarget.contains(event.relatedTarget));
181
+ if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
173
182
  },
174
183
  children: renderScene(displayed.scene)
175
184
  }), /* @__PURE__ */ jsx("div", {
@@ -13,7 +13,7 @@ const sceneKey = (scene) => scene.kind === "notification" ? `notification:${scen
13
13
  const settledPresence = (scene) => scene.kind === "none" ? "hidden" : scene.kind === "idle" ? "rest" : "compact";
14
14
  /**
15
15
  * The steps from what's displayed to the target scene:
16
- * - new content: collapse to the circle, swap the content there, grow back;
16
+ * - new content: fade it out, collapse to the circle, swap the content there, grow back;
17
17
  * - nothing left to show: hide (the stylesheet shrinks, then scales out), then drop the content;
18
18
  * - from hidden: pop in as a circle, then grow into the pill.
19
19
  * Open levels the user chose (expanded, panel) are kept while the scene stays the same.
@@ -33,7 +33,7 @@ function planSteps(current, target, timings) {
33
33
  if (sceneKey(scene) !== sceneKey(target)) {
34
34
  if (target.kind === "none") {
35
35
  if (presence !== "hidden") go(0, "hidden");
36
- go(timings.reduced ? timings.exit : timings.change + timings.exit, "hidden", target);
36
+ go(timings.reduced ? timings.exit : timings.fade + timings.change + timings.exit, "hidden", target);
37
37
  return steps;
38
38
  }
39
39
  if (isOpen(presence)) {
@@ -42,7 +42,7 @@ function planSteps(current, target, timings) {
42
42
  go(timings.exit, "hidden", target);
43
43
  } else {
44
44
  go(0, "rest");
45
- go(timings.change, "rest", target);
45
+ go(timings.fade + timings.change, "rest", target);
46
46
  }
47
47
  } else go(0, presence, target);
48
48
  } else if (scene !== target) go(0, presence, target);
@@ -57,6 +57,44 @@ function planSteps(current, target, timings) {
57
57
  }
58
58
  return steps;
59
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
+ }
60
98
  /** "500ms" or "0.5s" → 500; anything else → the fallback. */
61
99
  function parseDuration(value, fallback) {
62
100
  const match = /^\s*([\d.]+)(ms|s)\s*$/.exec(value);
@@ -71,11 +109,13 @@ function readTimings(element) {
71
109
  const reduced = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
72
110
  return reduced ? {
73
111
  enter: 0,
112
+ fade: 0,
74
113
  change: 0,
75
114
  exit: 150,
76
115
  reduced
77
116
  } : {
78
117
  enter: read("--_mp-enter", 500),
118
+ fade: read("--_mp-fade", 150),
79
119
  change: read("--_mp-change", 250),
80
120
  exit: read("--_mp-exit", 300),
81
121
  reduced
@@ -85,6 +125,7 @@ function readTimings(element) {
85
125
  exports.BASE = BASE;
86
126
  exports.IDLE = IDLE;
87
127
  exports.NONE = NONE;
128
+ exports.batchSteps = batchSteps;
88
129
  exports.isOpen = isOpen;
89
130
  exports.parseDuration = parseDuration;
90
131
  exports.planSteps = planSteps;
@@ -13,7 +13,7 @@ const sceneKey = (scene) => scene.kind === "notification" ? `notification:${scen
13
13
  const settledPresence = (scene) => scene.kind === "none" ? "hidden" : scene.kind === "idle" ? "rest" : "compact";
14
14
  /**
15
15
  * The steps from what's displayed to the target scene:
16
- * - new content: collapse to the circle, swap the content there, grow back;
16
+ * - new content: fade it out, collapse to the circle, swap the content there, grow back;
17
17
  * - nothing left to show: hide (the stylesheet shrinks, then scales out), then drop the content;
18
18
  * - from hidden: pop in as a circle, then grow into the pill.
19
19
  * Open levels the user chose (expanded, panel) are kept while the scene stays the same.
@@ -33,7 +33,7 @@ function planSteps(current, target, timings) {
33
33
  if (sceneKey(scene) !== sceneKey(target)) {
34
34
  if (target.kind === "none") {
35
35
  if (presence !== "hidden") go(0, "hidden");
36
- go(timings.reduced ? timings.exit : timings.change + timings.exit, "hidden", target);
36
+ go(timings.reduced ? timings.exit : timings.fade + timings.change + timings.exit, "hidden", target);
37
37
  return steps;
38
38
  }
39
39
  if (isOpen(presence)) {
@@ -42,7 +42,7 @@ function planSteps(current, target, timings) {
42
42
  go(timings.exit, "hidden", target);
43
43
  } else {
44
44
  go(0, "rest");
45
- go(timings.change, "rest", target);
45
+ go(timings.fade + timings.change, "rest", target);
46
46
  }
47
47
  } else go(0, presence, target);
48
48
  } else if (scene !== target) go(0, presence, target);
@@ -57,6 +57,44 @@ function planSteps(current, target, timings) {
57
57
  }
58
58
  return steps;
59
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
+ }
60
98
  /** "500ms" or "0.5s" → 500; anything else → the fallback. */
61
99
  function parseDuration(value, fallback) {
62
100
  const match = /^\s*([\d.]+)(ms|s)\s*$/.exec(value);
@@ -71,15 +109,17 @@ function readTimings(element) {
71
109
  const reduced = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
72
110
  return reduced ? {
73
111
  enter: 0,
112
+ fade: 0,
74
113
  change: 0,
75
114
  exit: 150,
76
115
  reduced
77
116
  } : {
78
117
  enter: read("--_mp-enter", 500),
118
+ fade: read("--_mp-fade", 150),
79
119
  change: read("--_mp-change", 250),
80
120
  exit: read("--_mp-exit", 300),
81
121
  reduced
82
122
  };
83
123
  }
84
124
 
85
- export { BASE, IDLE, NONE, isOpen, parseDuration, planSteps, readTimings };
125
+ export { BASE, IDLE, NONE, batchSteps, isOpen, parseDuration, planSteps, readTimings };
@@ -4,6 +4,7 @@ let react = require("react");
4
4
  const require_PillAction = require("./PillAction.js");
5
5
  const require_PillContext = require("./PillContext.js");
6
6
  const require_CloseIcon = require("../icons/CloseIcon.js");
7
+ const require_inputModality = require("../utils/inputModality.js");
7
8
  const require_react = require("../utils/react.js");
8
9
  let react_jsx_runtime = require("react/jsx-runtime");
9
10
 
@@ -20,13 +21,14 @@ const PillPanel = (0, react.forwardRef)(function PillPanel({ title, closeLabel =
20
21
  const panelRef = (0, react.useRef)(null);
21
22
  const ref = (0, react.useMemo)(() => require_react.mergeRefs(panelRef, forwardedRef), [forwardedRef]);
22
23
  const wasOpenRef = (0, react.useRef)(isOpen);
24
+ (0, react.useEffect)(require_inputModality.trackInputModality, []);
23
25
  (0, react.useEffect)(() => {
24
26
  const wasOpen = wasOpenRef.current;
25
27
  wasOpenRef.current = isOpen;
26
28
  if (isOpen && !wasOpen) panelRef.current?.focus({ preventScroll: true });
27
29
  else if (!isOpen && wasOpen) {
28
30
  const active = document.activeElement;
29
- if (!active || active === document.body || rootRef.current?.contains(active)) panelTriggerRef.current?.focus();
31
+ if (!active || active === document.body || rootRef.current?.contains(active)) panelTriggerRef.current?.focus(require_inputModality.getInputModality() === "pointer" ? { focusVisible: false } : void 0);
30
32
  }
31
33
  }, [
32
34
  isOpen,
@@ -4,6 +4,7 @@ import { forwardRef, useEffect, useId, useMemo, useRef } from "react";
4
4
  import PillAction from "./PillAction.mjs";
5
5
  import { useInternalPill } from "./PillContext.mjs";
6
6
  import CloseIcon from "../icons/CloseIcon.mjs";
7
+ import { getInputModality, trackInputModality } from "../utils/inputModality.mjs";
7
8
  import { mergeRefs } from "../utils/react.mjs";
8
9
  import { jsx, jsxs } from "react/jsx-runtime";
9
10
 
@@ -20,13 +21,14 @@ const PillPanel = forwardRef(function PillPanel({ title, closeLabel = "Close", c
20
21
  const panelRef = useRef(null);
21
22
  const ref = useMemo(() => mergeRefs(panelRef, forwardedRef), [forwardedRef]);
22
23
  const wasOpenRef = useRef(isOpen);
24
+ useEffect(trackInputModality, []);
23
25
  useEffect(() => {
24
26
  const wasOpen = wasOpenRef.current;
25
27
  wasOpenRef.current = isOpen;
26
28
  if (isOpen && !wasOpen) panelRef.current?.focus({ preventScroll: true });
27
29
  else if (!isOpen && wasOpen) {
28
30
  const active = document.activeElement;
29
- if (!active || active === document.body || rootRef.current?.contains(active)) panelTriggerRef.current?.focus();
31
+ if (!active || active === document.body || rootRef.current?.contains(active)) panelTriggerRef.current?.focus(getInputModality() === "pointer" ? { focusVisible: false } : void 0);
30
32
  }
31
33
  }, [
32
34
  isOpen,
package/dist/styles.css CHANGED
@@ -62,6 +62,8 @@
62
62
  --_mp-enter: var(--mp-duration-enter, 500ms);
63
63
  --_mp-exit: var(--mp-duration-exit, 300ms);
64
64
  --_mp-change: var(--mp-duration-change, 250ms);
65
+ /* The content fades out before the pill shrinks, and in once it has grown */
66
+ --_mp-fade: 150ms;
65
67
  /* The entry overshoot from the mockups' "pop" */
66
68
  --_mp-ease-enter: cubic-bezier(0.34, 1.56, 0.64, 1);
67
69
 
@@ -84,6 +86,9 @@
84
86
  font-size: var(--mp-text-size, clamp(0.9375rem, 0.875rem + 0.25vw, 1.0625rem));
85
87
  line-height: 1.3;
86
88
  scale: 1;
89
+ /* Where the pill pops in from and scales out to: its centre, or e.g.
90
+ bottom to come out of the edge a bottom pill sits on */
91
+ transform-origin: var(--mp-scale-origin, center);
87
92
  /* Lets the width animate from its real size (fit-content) to the
88
93
  circle's, where supported; elsewhere max-inline-size does the work */
89
94
  interpolate-size: allow-keywords;
@@ -120,40 +125,63 @@
120
125
  inline-size: var(--_mp-rest-size);
121
126
  max-inline-size: var(--_mp-rest-size);
122
127
  max-block-size: var(--_mp-rest-size);
123
- /* Shrink to a circle, then scale out, then stop being visible */
128
+ /* Fade the content out, shrink to a circle, scale out, then stop being visible */
124
129
  transition:
125
- scale var(--_mp-exit) ease-in var(--_mp-change),
126
- inline-size var(--_mp-change) ease-in-out,
127
- block-size var(--_mp-change) ease-in-out,
128
- max-inline-size var(--_mp-change) ease-in-out,
129
- max-block-size var(--_mp-change) ease-in-out,
130
- border-radius var(--_mp-change) ease-in-out,
131
- padding var(--_mp-change) ease-in-out,
132
- background-color var(--_mp-change) ease-in-out,
133
- visibility 0s linear calc(var(--_mp-change) + var(--_mp-exit));
130
+ scale var(--_mp-exit) ease-in calc(var(--_mp-fade) + var(--_mp-change)),
131
+ inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
132
+ block-size var(--_mp-change) ease-in-out var(--_mp-fade),
133
+ max-inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
134
+ max-block-size var(--_mp-change) ease-in-out var(--_mp-fade),
135
+ border-radius var(--_mp-change) ease-in-out var(--_mp-fade),
136
+ padding var(--_mp-change) ease-in-out var(--_mp-fade),
137
+ background-color var(--_mp-change) ease-in-out var(--_mp-fade),
138
+ visibility 0s linear calc(var(--_mp-fade) + var(--_mp-change) + var(--_mp-exit));
139
+ /* An open horizontal pill has no height cap (none): without this, the
140
+ circle's cap would clip the content at once, while it still fades */
141
+ transition-behavior: allow-discrete;
134
142
  }
135
143
 
136
- /* A circle: both axes clamped, whatever the hidden content would need */
144
+ /* A circle: both axes clamped, whatever the hidden content would need.
145
+ It shrinks once the content has faded out */
137
146
  &[data-presence="rest"] {
138
147
  inline-size: var(--_mp-rest-size);
139
148
  max-inline-size: var(--_mp-rest-size);
140
149
  max-block-size: var(--_mp-rest-size);
150
+ transition:
151
+ scale var(--_mp-enter) var(--_mp-ease-enter),
152
+ inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
153
+ block-size var(--_mp-change) ease-in-out var(--_mp-fade),
154
+ max-inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
155
+ max-block-size var(--_mp-change) ease-in-out var(--_mp-fade),
156
+ border-radius var(--_mp-change) ease-in-out var(--_mp-fade),
157
+ padding var(--_mp-change) ease-in-out var(--_mp-fade),
158
+ background-color var(--_mp-change) ease-in-out var(--_mp-fade),
159
+ visibility 0s;
160
+ transition-behavior: allow-discrete;
141
161
  }
142
162
 
143
- /* The content stays while the pill closes over it, then hides. The
144
- message unwraps at once when the pill shrinks, and may wrap again
145
- only once the pill has grown (a discrete transition, where supported) */
163
+ /* The content is never seen while the pill changes size, so long text
164
+ never overflows or reflows word by word. Closing: it fades out as it
165
+ is, then the message unwraps (out of sight) and the pill shrinks over
166
+ it. Opening: the pill grows with the message on one line, then it may
167
+ wrap and the content fades in (discrete transitions, where
168
+ supported). <MagicPill> renders new content closed first, so it opens
169
+ the same way (L21) */
146
170
  & > :not([data-mp="icon"], [data-mp="panel"]) {
147
171
  transition:
172
+ opacity var(--_mp-fade) ease-out var(--_mp-change),
148
173
  visibility 0s,
149
174
  white-space 0s linear var(--_mp-change);
150
175
  transition-behavior: allow-discrete;
151
176
  }
152
177
 
153
178
  &:is([data-presence="rest"], [data-presence="hidden"]) > :not([data-mp="icon"], [data-mp="panel"]) {
179
+ opacity: 0;
154
180
  transition:
155
- visibility 0s linear var(--_mp-change),
156
- white-space 0s;
181
+ opacity var(--_mp-fade) ease-in,
182
+ visibility 0s linear calc(var(--_mp-fade) + var(--_mp-change)),
183
+ white-space 0s linear var(--_mp-fade);
184
+ transition-behavior: allow-discrete;
157
185
  }
158
186
 
159
187
  &:is([data-presence="rest"], [data-presence="hidden"]) > [data-mp="message"] {
@@ -182,12 +210,10 @@
182
210
 
183
211
  &[data-placement^="top"] {
184
212
  inset-block-start: calc(var(--_mp-offset) + env(safe-area-inset-top, 0px));
185
- transform-origin: top;
186
213
  }
187
214
 
188
215
  &[data-placement^="bottom"] {
189
216
  inset-block-end: calc(var(--_mp-offset) + env(safe-area-inset-bottom, 0px));
190
- transform-origin: bottom;
191
217
  }
192
218
 
193
219
  /* Centred with auto margins, not left: 50% + translate, which would
@@ -215,12 +241,10 @@
215
241
 
216
242
  &[data-placement="left"] {
217
243
  left: calc(var(--_mp-offset) + env(safe-area-inset-left, 0px));
218
- transform-origin: left;
219
244
  }
220
245
 
221
246
  &[data-placement="right"] {
222
247
  right: calc(var(--_mp-offset) + env(safe-area-inset-right, 0px));
223
- transform-origin: right;
224
248
  }
225
249
 
226
250
  &[data-placement="overlay"] {
@@ -368,7 +392,7 @@
368
392
  margin var(--_mp-change) ease-in-out;
369
393
  }
370
394
 
371
- & [data-mp="action"]:is(:hover, :focus-visible, [data-expanded], [aria-current="page"], [aria-current="true"]),
395
+ & [data-mp="action"]:is(:focus-visible, [data-expanded], [aria-current="page"], [aria-current="true"]),
372
396
  &:is([data-presence="expanded"], [data-presence="panel"]) [data-mp="action"] {
373
397
  & [data-mp="action-body"] {
374
398
  padding-inline-end: calc(var(--_mp-action-size) / 2.5);
@@ -384,6 +408,25 @@
384
408
  }
385
409
  }
386
410
 
411
+ /* The same on hover, only where the device really hovers: a tap on a
412
+ touchscreen leaves a "sticky" hover behind until the next tap (L20) */
413
+ @media (hover: hover) {
414
+ & [data-mp="action"]:hover {
415
+ & [data-mp="action-body"] {
416
+ padding-inline-end: calc(var(--_mp-action-size) / 2.5);
417
+ }
418
+
419
+ & [data-mp="label"] {
420
+ max-inline-size: 20rem;
421
+ opacity: 1;
422
+ }
423
+
424
+ & [data-mp="action-icon"] + [data-mp="label"] {
425
+ margin-inline-start: 0.5rem;
426
+ }
427
+ }
428
+ }
429
+
387
430
  /* Panel */
388
431
  & [data-mp="panel"] {
389
432
  display: grid;
@@ -436,6 +479,24 @@
436
479
  visibility 0s linear 150ms;
437
480
  }
438
481
 
482
+ &[data-presence="rest"] {
483
+ transition:
484
+ opacity 150ms linear,
485
+ visibility 0s;
486
+ }
487
+
488
+ /* Nothing resizes, so the content doesn't wait: it fades with the
489
+ pill, and the message unwraps at once */
490
+ &:not([data-presence="rest"], [data-presence="hidden"]) > :not([data-mp="icon"], [data-mp="panel"]) {
491
+ transition: opacity 150ms linear;
492
+ }
493
+
494
+ &:is([data-presence="rest"], [data-presence="hidden"]) > :not([data-mp="icon"], [data-mp="panel"]) {
495
+ transition:
496
+ opacity 150ms linear,
497
+ visibility 0s linear 150ms;
498
+ }
499
+
439
500
  & [data-mp="label"],
440
501
  & [data-mp="action-body"],
441
502
  & [data-mp="message"],
@@ -0,0 +1,23 @@
1
+ // src/utils/inputModality.ts
2
+ let modality = "keyboard";
3
+ let listening = false;
4
+ const onPointer = () => {
5
+ modality = "pointer";
6
+ };
7
+ const onKey = (event) => {
8
+ if (event.metaKey || event.ctrlKey || event.altKey) return;
9
+ modality = "keyboard";
10
+ };
11
+ /** Starts listening (once, in the capture phase, so nothing can stop it). */
12
+ function trackInputModality() {
13
+ if (listening || typeof document === "undefined") return;
14
+ listening = true;
15
+ document.addEventListener("pointerdown", onPointer, true);
16
+ document.addEventListener("keydown", onKey, true);
17
+ }
18
+ function getInputModality() {
19
+ return modality;
20
+ }
21
+
22
+ exports.getInputModality = getInputModality;
23
+ exports.trackInputModality = trackInputModality;
@@ -0,0 +1,22 @@
1
+ // src/utils/inputModality.ts
2
+ let modality = "keyboard";
3
+ let listening = false;
4
+ const onPointer = () => {
5
+ modality = "pointer";
6
+ };
7
+ const onKey = (event) => {
8
+ if (event.metaKey || event.ctrlKey || event.altKey) return;
9
+ modality = "keyboard";
10
+ };
11
+ /** Starts listening (once, in the capture phase, so nothing can stop it). */
12
+ function trackInputModality() {
13
+ if (listening || typeof document === "undefined") return;
14
+ listening = true;
15
+ document.addEventListener("pointerdown", onPointer, true);
16
+ document.addEventListener("keydown", onKey, true);
17
+ }
18
+ function getInputModality() {
19
+ return modality;
20
+ }
21
+
22
+ export { getInputModality, trackInputModality };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magic-pill",
3
- "version": "9.0.0-next.1",
3
+ "version": "9.0.0-next.2",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",