magic-pill 9.3.0 → 9.4.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +114 -3
  3. package/dist/MagicPill/MagicPill.d.mts +51 -1
  4. package/dist/MagicPill/MagicPill.d.ts +51 -1
  5. package/dist/MagicPill/MagicPill.js +53 -4
  6. package/dist/MagicPill/MagicPill.mjs +55 -6
  7. package/dist/MagicPill/flag.js +1 -59
  8. package/dist/MagicPill/flag.mjs +1 -58
  9. package/dist/Pill/PillProgress.d.mts +12 -0
  10. package/dist/Pill/PillProgress.d.ts +12 -0
  11. package/dist/Pill/PillProgress.js +42 -0
  12. package/dist/Pill/PillProgress.mjs +42 -0
  13. package/dist/Pill/goo.js +576 -0
  14. package/dist/Pill/goo.mjs +566 -0
  15. package/dist/Pill/morph.js +11 -4
  16. package/dist/Pill/morph.mjs +10 -5
  17. package/dist/PillNavbar/PillNavbar.d.mts +8 -0
  18. package/dist/PillNavbar/PillNavbar.d.ts +8 -0
  19. package/dist/PillNavbar/PillNavbar.js +3 -2
  20. package/dist/PillNavbar/PillNavbar.mjs +3 -2
  21. package/dist/PillNavbar/room.d.mts +3 -0
  22. package/dist/PillNavbar/room.d.ts +2 -0
  23. package/dist/PillNavbar/room.js +89 -17
  24. package/dist/PillNavbar/room.mjs +89 -18
  25. package/dist/index.d.mts +3 -2
  26. package/dist/index.d.ts +3 -2
  27. package/dist/index.js +2 -0
  28. package/dist/index.mjs +2 -1
  29. package/dist/satellites/Actions.d.mts +12 -0
  30. package/dist/satellites/Actions.d.ts +11 -0
  31. package/dist/satellites/Actions.js +181 -0
  32. package/dist/satellites/Actions.mjs +177 -0
  33. package/dist/satellites/Orbitals.js +585 -0
  34. package/dist/satellites/Orbitals.mjs +585 -0
  35. package/dist/satellites/Satellites.js +634 -0
  36. package/dist/satellites/Satellites.mjs +634 -0
  37. package/dist/satellites/layout.js +209 -0
  38. package/dist/satellites/layout.mjs +209 -0
  39. package/dist/satellites/measure.js +25 -0
  40. package/dist/satellites/measure.mjs +24 -0
  41. package/dist/satellites/orbits.js +139 -0
  42. package/dist/satellites/orbits.mjs +136 -0
  43. package/dist/satellites/types.d.mts +58 -0
  44. package/dist/satellites/types.d.ts +58 -0
  45. package/dist/satellites.d.mts +5 -0
  46. package/dist/satellites.d.ts +5 -0
  47. package/dist/satellites.js +14 -0
  48. package/dist/satellites.mjs +12 -0
  49. package/dist/store/store.d.mts +103 -0
  50. package/dist/store/store.d.ts +103 -0
  51. package/dist/store/store.js +52 -1
  52. package/dist/store/store.mjs +52 -2
  53. package/dist/styles.css +322 -8
  54. package/package.json +11 -1
@@ -0,0 +1,136 @@
1
+ // src/satellites/orbits.ts
2
+ /** How far a wheel can turn: from 0 to this many orbitals. */
3
+ const wheelEnd = (orbit) => Math.max(0, orbit.count - orbit.visible);
4
+ /**
5
+ * Where orbital `index` is with the wheel turned by `turn` orbitals: on the
6
+ * arc's slots, fading out past either end (the next one peeks out at a
7
+ * fifth), mirrored in a right-to-left page (the first one on the right).
8
+ */
9
+ function spotOn(orbit, index, turn = 0) {
10
+ const { count, visible } = orbit;
11
+ const slot = orbit.rtl ? count - 1 - index + turn - (count - visible) : index - turn;
12
+ const angle = orbit.first + slot * orbit.step;
13
+ const past = slot < 0 ? -slot : slot > visible - 1 ? slot - (visible - 1) : 0;
14
+ const peeks = slot < 0 ? orbit.peek.low : orbit.peek.high;
15
+ const shown = orbit.wheel ? Math.min(1, Math.max(0, 1 - past / (peeks ? 1.25 : 1))) : 1;
16
+ return {
17
+ x: orbit.x + Math.cos(radians(angle)) * orbit.radius,
18
+ y: orbit.y + Math.sin(radians(angle)) * orbit.radius,
19
+ angle,
20
+ shown
21
+ };
22
+ }
23
+ const radians = (degrees) => degrees * Math.PI / 180;
24
+ function planOrbit(input) {
25
+ const { electron, count, size, distance, away, obstacles, viewport } = input;
26
+ const x = electron.left + electron.width / 2;
27
+ const y = electron.top + electron.height / 2;
28
+ const margin = 8;
29
+ const pad = size / 2 + 6;
30
+ const free = (px, py) => px >= size / 2 + margin && px <= viewport.width - size / 2 - margin && py >= size / 2 + margin && py <= viewport.height - size / 2 - margin && !obstacles.some((box) => Math.abs(px - (box.left + box.width / 2)) < box.width / 2 + pad && Math.abs(py - (box.top + box.height / 2)) < box.height / 2 + pad);
31
+ const at = (radius, angle) => ({
32
+ x: x + Math.cos(radians(angle)) * radius,
33
+ y: y + Math.sin(radians(angle)) * radius
34
+ });
35
+ const tightest = (radius) => 2 * Math.asin(Math.min(1, (size + 8) / (2 * radius))) * 180 / Math.PI;
36
+ const arcAt = (radius) => {
37
+ const ok = (offset) => {
38
+ const spot = at(radius, away + offset);
39
+ return free(spot.x, spot.y);
40
+ };
41
+ let seed = null;
42
+ for (let offset = 0; offset <= 180 && seed === null; offset++) if (ok(offset)) seed = offset;
43
+ else if (ok(-offset)) seed = -offset;
44
+ if (seed === null) return null;
45
+ let low = seed;
46
+ let high = seed;
47
+ while (low > -180 && ok(low - 1)) low--;
48
+ while (high < 180 && ok(high + 1)) high++;
49
+ return {
50
+ low,
51
+ high
52
+ };
53
+ };
54
+ const place = (radius, step, first, visible, clear) => {
55
+ const wheel = count > visible;
56
+ const clearAt = (angle) => {
57
+ const spot = at(radius, angle);
58
+ return free(spot.x, spot.y);
59
+ };
60
+ const room = wheel && (visible + 1) * step < 360;
61
+ const peek = {
62
+ low: room && clearAt(first - step),
63
+ high: room && clearAt(first + visible * step)
64
+ };
65
+ const orbit = {
66
+ x,
67
+ y,
68
+ radius,
69
+ step,
70
+ first,
71
+ count,
72
+ visible,
73
+ wheel,
74
+ peek,
75
+ rtl: input.rtl,
76
+ spots: [],
77
+ clear
78
+ };
79
+ orbit.spots = Array.from({ length: count }, (_, index) => spotOn(orbit, index));
80
+ return orbit;
81
+ };
82
+ const base = electron.width / 2 + distance + size / 2;
83
+ const radii = [];
84
+ for (let radius = base; radius <= base + size * 1.25; radius += 4) radii.push(radius);
85
+ for (const wanted of input.spread > 0 ? [input.spread, 0] : [0]) for (const radius of radii) {
86
+ const arc = arcAt(radius);
87
+ if (!arc) continue;
88
+ const step = count > 1 ? Math.max(tightest(radius), wanted > 0 ? wanted / (count - 1) : tightest(radius) + 2) : 0;
89
+ const needed = step * (count - 1);
90
+ if (needed <= arc.high - arc.low) return place(radius, step, away + Math.min(Math.max(0, arc.low + needed / 2), arc.high - needed / 2) - needed / 2, count, true);
91
+ }
92
+ let best = null;
93
+ for (const radius of radii) {
94
+ const arc = arcAt(radius);
95
+ if (!arc) continue;
96
+ const step = tightest(radius) + 2;
97
+ const visible = Math.max(1, Math.min(count, Math.floor((arc.high - arc.low) / step) + 1));
98
+ if (!best || visible > best.visible) best = {
99
+ radius,
100
+ arc,
101
+ step,
102
+ visible
103
+ };
104
+ }
105
+ if (!best) {
106
+ const step = count > 1 ? tightest(base) + 2 : 0;
107
+ return place(base, step, away - step * (count - 1) / 2, count, false);
108
+ }
109
+ const width = best.step * (best.visible - 1);
110
+ const centre = Math.min(Math.max(0, best.arc.low + width / 2), best.arc.high - width / 2);
111
+ return place(best.radius, best.step, away + centre - width / 2, best.visible, true);
112
+ }
113
+ /**
114
+ * Which orbital a point is over, by its angle from the electron's centre: -1
115
+ * for none (or the middle). On a wheel turned by `turn`, only the ones that show.
116
+ */
117
+ function orbitalAt(orbit, size, electronRadius, px, py, turn = 0) {
118
+ const dx = px - orbit.x;
119
+ const dy = py - orbit.y;
120
+ const distance = Math.hypot(dx, dy);
121
+ if (distance < electronRadius * .85 || distance > orbit.radius + size) return -1;
122
+ const angle = Math.atan2(dy, dx) * 180 / Math.PI;
123
+ let best = -1;
124
+ let bestGap = 1e400;
125
+ Array.from({ length: orbit.count }, (_, index) => spotOn(orbit, index, turn)).forEach((spot, index) => {
126
+ if (spot.shown < .5) return;
127
+ const gap = Math.abs((angle - spot.angle + 540) % 360 - 180);
128
+ if (gap < bestGap) {
129
+ bestGap = gap;
130
+ best = index;
131
+ }
132
+ });
133
+ return bestGap <= Math.max(orbit.step / 2 + 8, 30) ? best : -1;
134
+ }
135
+
136
+ export { orbitalAt, planOrbit, spotOn, wheelEnd };
@@ -0,0 +1,58 @@
1
+ import { ComponentType, RefObject } from "react";
2
+ import { LinkComponent, PillPlacement, PillTheme, PillVariant } from "../Pill/types.mjs";
3
+ import { PillStore } from "../store/store.mjs";
4
+ /** Where electrons go: further along the edge, before the pill, or one row towards the page. */
5
+ export type ElectronSide = "after" | "before" | "beside";
6
+ /** Several electrons line up along the pill, or stack across it. */
7
+ export type ElectronLine = "along" | "across";
8
+ /** The satellites gathered around the pill, or spread to the screen's edges. */
9
+ export type SatelliteArrangement = "gather" | "spread";
10
+ export type ElectronEnding = "join" | "pop";
11
+ /** Where the page's actions go: on the pill's start side, its end side, or beside it. */
12
+ export type ActionsSide = "start" | "end" | "beside";
13
+ /** The page's actions: a pill of their own, or in the pill after a divider. */
14
+ export type ActionsStyle = "detached" | "inside";
15
+ /** The page's actions run like the pill (turning across when room runs out: "auto"), or horizontal, or vertical. */
16
+ export type ActionsOrientation = "auto" | "horizontal" | "vertical";
17
+ /** Which satellites are nearer the pill on a side they share: the electrons, or the page's actions. */
18
+ export type NearestToPill = "electrons" | "actions";
19
+ /** What opens an electron's orbitals: a long press or hovering it with a mouse, a long press only, or a tap. */
20
+ export type OrbitalsOpenOn = "press-hover" | "press" | "tap";
21
+ /** The host, as its satellites see it. */
22
+ export interface SatelliteHost {
23
+ name: string;
24
+ store: PillStore;
25
+ rootRef: RefObject<HTMLElement | null>;
26
+ placement: PillPlacement;
27
+ vertical: boolean;
28
+ pathname?: string;
29
+ /** A notification has the pill, or it's on its way to or from one (the electrons tuck in, unless kept out) */
30
+ notifying: boolean;
31
+ linkComponent?: LinkComponent;
32
+ variant?: PillVariant;
33
+ theme?: PillTheme;
34
+ electronSide: ElectronSide;
35
+ electronLine: ElectronLine;
36
+ keepElectronsOut: boolean;
37
+ electronEnding: ElectronEnding;
38
+ arrange: SatelliteArrangement;
39
+ orbitalsOpenOn: OrbitalsOpenOn;
40
+ orbitalLabels: boolean;
41
+ orbitalsStayOpen: boolean;
42
+ actionsStyle: ActionsStyle;
43
+ actionsSide: ActionsSide;
44
+ actionsOrientation: ActionsOrientation;
45
+ actionsWheel: boolean;
46
+ nearestToPill: NearestToPill;
47
+ actionsLabel: string;
48
+ }
49
+ /** `satellites` from `magic-pill/satellites`, passed to `<MagicPill satellites={satellites}>`. */
50
+ export interface PillSatellites {
51
+ Layer: ComponentType<{
52
+ host: SatelliteHost;
53
+ }>;
54
+ /** The page's actions in the pill itself, after a divider (`actionsStyle="inside"`) */
55
+ Inside: ComponentType<{
56
+ host: SatelliteHost;
57
+ }>;
58
+ }
@@ -0,0 +1,58 @@
1
+ import { ComponentType, RefObject } from "react";
2
+ import { LinkComponent, PillPlacement, PillTheme, PillVariant } from "../Pill/types.js";
3
+ import { PillStore } from "../store/store.js";
4
+ /** Where electrons go: further along the edge, before the pill, or one row towards the page. */
5
+ export type ElectronSide = "after" | "before" | "beside";
6
+ /** Several electrons line up along the pill, or stack across it. */
7
+ export type ElectronLine = "along" | "across";
8
+ /** The satellites gathered around the pill, or spread to the screen's edges. */
9
+ export type SatelliteArrangement = "gather" | "spread";
10
+ export type ElectronEnding = "join" | "pop";
11
+ /** Where the page's actions go: on the pill's start side, its end side, or beside it. */
12
+ export type ActionsSide = "start" | "end" | "beside";
13
+ /** The page's actions: a pill of their own, or in the pill after a divider. */
14
+ export type ActionsStyle = "detached" | "inside";
15
+ /** The page's actions run like the pill (turning across when room runs out: "auto"), or horizontal, or vertical. */
16
+ export type ActionsOrientation = "auto" | "horizontal" | "vertical";
17
+ /** Which satellites are nearer the pill on a side they share: the electrons, or the page's actions. */
18
+ export type NearestToPill = "electrons" | "actions";
19
+ /** What opens an electron's orbitals: a long press or hovering it with a mouse, a long press only, or a tap. */
20
+ export type OrbitalsOpenOn = "press-hover" | "press" | "tap";
21
+ /** The host, as its satellites see it. */
22
+ export interface SatelliteHost {
23
+ name: string;
24
+ store: PillStore;
25
+ rootRef: RefObject<HTMLElement | null>;
26
+ placement: PillPlacement;
27
+ vertical: boolean;
28
+ pathname?: string;
29
+ /** A notification has the pill, or it's on its way to or from one (the electrons tuck in, unless kept out) */
30
+ notifying: boolean;
31
+ linkComponent?: LinkComponent;
32
+ variant?: PillVariant;
33
+ theme?: PillTheme;
34
+ electronSide: ElectronSide;
35
+ electronLine: ElectronLine;
36
+ keepElectronsOut: boolean;
37
+ electronEnding: ElectronEnding;
38
+ arrange: SatelliteArrangement;
39
+ orbitalsOpenOn: OrbitalsOpenOn;
40
+ orbitalLabels: boolean;
41
+ orbitalsStayOpen: boolean;
42
+ actionsStyle: ActionsStyle;
43
+ actionsSide: ActionsSide;
44
+ actionsOrientation: ActionsOrientation;
45
+ actionsWheel: boolean;
46
+ nearestToPill: NearestToPill;
47
+ actionsLabel: string;
48
+ }
49
+ /** `satellites` from `magic-pill/satellites`, passed to `<MagicPill satellites={satellites}>`. */
50
+ export interface PillSatellites {
51
+ Layer: ComponentType<{
52
+ host: SatelliteHost;
53
+ }>;
54
+ /** The page's actions in the pill itself, after a divider (`actionsStyle="inside"`) */
55
+ Inside: ComponentType<{
56
+ host: SatelliteHost;
57
+ }>;
58
+ }
@@ -0,0 +1,5 @@
1
+ import { ActionsOrientation, ActionsSide, ActionsStyle, ElectronEnding, ElectronLine, ElectronSide, NearestToPill, OrbitalsOpenOn, PillSatellites, SatelliteArrangement } from "./satellites/types.mjs";
2
+ import { usePillActions } from "./satellites/Actions.mjs";
3
+ import { Electron, ElectronOptions, Orbital, PillActionItem } from "./store/store.mjs";
4
+ export declare const satellites: PillSatellites;
5
+ export { type ActionsOrientation, type ActionsSide, type ActionsStyle, type Electron, type ElectronEnding, type ElectronLine, type ElectronOptions, type ElectronSide, type NearestToPill, type Orbital, type OrbitalsOpenOn, type PillActionItem, type PillSatellites, type SatelliteArrangement, usePillActions };
@@ -0,0 +1,5 @@
1
+ import { ActionsOrientation, ActionsSide, ActionsStyle, ElectronEnding, ElectronLine, ElectronSide, NearestToPill, OrbitalsOpenOn, PillSatellites, SatelliteArrangement } from "./satellites/types.js";
2
+ import { usePillActions } from "./satellites/Actions.js";
3
+ import { Electron, ElectronOptions, Orbital, PillActionItem } from "./store/store.js";
4
+ export declare const satellites: PillSatellites;
5
+ export { type ActionsOrientation, type ActionsSide, type ActionsStyle, type Electron, type ElectronEnding, type ElectronLine, type ElectronOptions, type ElectronSide, type NearestToPill, type Orbital, type OrbitalsOpenOn, type PillActionItem, type PillSatellites, type SatelliteArrangement, usePillActions };
@@ -0,0 +1,14 @@
1
+ "use client";
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+ const require_Actions = require("./satellites/Actions.js");
5
+ const require_Satellites = require("./satellites/Satellites.js");
6
+
7
+ // src/satellites/index.ts
8
+ const satellites = {
9
+ Layer: require_Satellites.SatellitesLayer,
10
+ Inside: require_Actions.InsideActions
11
+ };
12
+
13
+ exports.satellites = satellites;
14
+ exports.usePillActions = require_Actions.usePillActions;
@@ -0,0 +1,12 @@
1
+ "use client";
2
+
3
+ import { InsideActions, usePillActions } from "./satellites/Actions.mjs";
4
+ import { SatellitesLayer } from "./satellites/Satellites.mjs";
5
+
6
+ // src/satellites/index.ts
7
+ const satellites = {
8
+ Layer: SatellitesLayer,
9
+ Inside: InsideActions
10
+ };
11
+
12
+ export { satellites, usePillActions };
@@ -53,6 +53,81 @@ export interface PillQueueState {
53
53
  /** The ones waiting behind it, first in, first out. */
54
54
  readonly queued: readonly QueuedNotification[];
55
55
  }
56
+ /** One of the current page's actions, next to the pill (beta, `magic-pill/satellites`). */
57
+ export interface PillActionItem {
58
+ icon: ReactNode;
59
+ /** Its name: shown on hover and focus, or all the time with `expanded` */
60
+ label: string;
61
+ onClick?: (event: {
62
+ preventDefault(): void;
63
+ }) => void;
64
+ /** A link instead (with the host's `linkComponent`) */
65
+ href?: string;
66
+ expanded?: boolean;
67
+ id?: string;
68
+ }
69
+ /** One of an electron's quick actions (beta). */
70
+ export interface Orbital {
71
+ icon: ReactNode;
72
+ /** Its name: shown next to it while it's under the finger, the pointer or the focus */
73
+ label: string;
74
+ onClick?: (event: {
75
+ preventDefault(): void;
76
+ }) => void;
77
+ /** A link instead (with the host's `linkComponent`) */
78
+ href?: string;
79
+ id?: string;
80
+ }
81
+ /**
82
+ * An electron (beta): a small round pill that splits out of the host to keep
83
+ * a background activity (a timer, an upload…) in reach while the person goes
84
+ * elsewhere in the app. Shown by `magic-pill/satellites`, passed to the host.
85
+ */
86
+ export interface ElectronOptions {
87
+ /** Its accessible name, e.g. "Focus timer". */
88
+ label: string;
89
+ /** A short live text, e.g. "4:34" or "64%". */
90
+ value?: string;
91
+ /**
92
+ * What screen readers read for the value, e.g. "4 minutes left". Default:
93
+ * `value`. Round it, so that the name doesn't change every second.
94
+ */
95
+ valueText?: string;
96
+ /** An icon; with `progress` and no `value`, it sits inside the ring. */
97
+ icon?: ReactNode;
98
+ /** 0 to 1: draws a progress ring around its content. */
99
+ progress?: number;
100
+ variant?: PillVariant;
101
+ /** Where it came from: a tap goes back there. Default: the path it was started on. */
102
+ href?: string;
103
+ /** Instead of going back: what a tap does. */
104
+ onClick?: (event: {
105
+ preventDefault(): void;
106
+ }) => void;
107
+ /** Tucked into the pill while the current path is its `href` (needs the host's `pathname`). Default true. */
108
+ hideOnOrigin?: boolean;
109
+ /** How it leaves when it ends: back into the pill ("join") or scaling out ("pop"). Default: the host's. */
110
+ ending?: "join" | "pop";
111
+ /** Said once by screen readers whenever it changes, for a milestone ("Break time"). */
112
+ announce?: string;
113
+ /** Your own content instead of the ring, the value and the icon. */
114
+ render?: () => ReactNode;
115
+ /**
116
+ * Its quick actions: a long press (or hovering it with a mouse, or a tap,
117
+ * see the host's `orbitalsOpenOn`) fans them out around it.
118
+ */
119
+ orbitals?: readonly Orbital[];
120
+ /** The label of the orbital that goes back, when a tap opens them (`orbitalsOpenOn="tap"`). Default "Back". */
121
+ backLabel?: string;
122
+ /** Starting an electron with an id that's already there updates it in place. */
123
+ id?: string;
124
+ }
125
+ export interface Electron {
126
+ readonly id: string;
127
+ readonly input: ElectronOptions;
128
+ /** Where a tap takes the person back to: its `href`, or the path it was started on */
129
+ readonly origin: string | undefined;
130
+ }
56
131
  export interface PillController {
57
132
  readonly name: string;
58
133
  /**
@@ -65,6 +140,34 @@ export interface PillController {
65
140
  dismiss: (id?: string) => void;
66
141
  /** Empty the queue and dismiss the visible notification. */
67
142
  clear: () => void;
143
+ /** Start an electron (beta, `magic-pill/satellites`). Returns its id. */
144
+ electron: (input: ElectronOptions) => string;
145
+ /** Change an electron's options, e.g. its value every second. */
146
+ updateElectron: (id: string, patch: Partial<ElectronOptions>) => void;
147
+ /** End an electron: it joins back into the pill (or pops away). */
148
+ endElectron: (id: string) => void;
149
+ /**
150
+ * The current page's actions, next to the pill (beta, `magic-pill/satellites`);
151
+ * `null` or none: they go. `usePillActions()` does it for a page while it's mounted.
152
+ */
153
+ setActions: (items: readonly PillActionItem[] | null) => void;
154
+ }
155
+ /** One named pill's queue. Internal: the host uses more than the controller exposes. */
156
+ export interface PillStore {
157
+ getState: () => PillQueueState;
158
+ subscribe: (listener: () => void) => () => void;
159
+ notify: (input: NotifyInput) => string;
160
+ update: (id: string, patch: Partial<NotifyOptions>) => void;
161
+ dismiss: (id: string | undefined, reason: DismissReason) => void;
162
+ clear: () => void;
163
+ /** Counts mounted hosts, to warn about two hosts with the same name. Returns the cleanup. */
164
+ registerHost: () => () => void;
165
+ getElectrons: () => readonly Electron[];
166
+ electron: (input: ElectronOptions) => string;
167
+ updateElectron: (id: string, patch: Partial<ElectronOptions>) => void;
168
+ endElectron: (id: string) => void;
169
+ getActions: () => readonly PillActionItem[] | null;
170
+ setActions: (items: readonly PillActionItem[] | null) => void;
68
171
  }
69
172
  /** Control the <MagicPill> with this name (default "main") from anywhere. */
70
173
  export declare function pill(name?: string): PillController;
@@ -53,6 +53,81 @@ export interface PillQueueState {
53
53
  /** The ones waiting behind it, first in, first out. */
54
54
  readonly queued: readonly QueuedNotification[];
55
55
  }
56
+ /** One of the current page's actions, next to the pill (beta, `magic-pill/satellites`). */
57
+ export interface PillActionItem {
58
+ icon: ReactNode;
59
+ /** Its name: shown on hover and focus, or all the time with `expanded` */
60
+ label: string;
61
+ onClick?: (event: {
62
+ preventDefault(): void;
63
+ }) => void;
64
+ /** A link instead (with the host's `linkComponent`) */
65
+ href?: string;
66
+ expanded?: boolean;
67
+ id?: string;
68
+ }
69
+ /** One of an electron's quick actions (beta). */
70
+ export interface Orbital {
71
+ icon: ReactNode;
72
+ /** Its name: shown next to it while it's under the finger, the pointer or the focus */
73
+ label: string;
74
+ onClick?: (event: {
75
+ preventDefault(): void;
76
+ }) => void;
77
+ /** A link instead (with the host's `linkComponent`) */
78
+ href?: string;
79
+ id?: string;
80
+ }
81
+ /**
82
+ * An electron (beta): a small round pill that splits out of the host to keep
83
+ * a background activity (a timer, an upload…) in reach while the person goes
84
+ * elsewhere in the app. Shown by `magic-pill/satellites`, passed to the host.
85
+ */
86
+ export interface ElectronOptions {
87
+ /** Its accessible name, e.g. "Focus timer". */
88
+ label: string;
89
+ /** A short live text, e.g. "4:34" or "64%". */
90
+ value?: string;
91
+ /**
92
+ * What screen readers read for the value, e.g. "4 minutes left". Default:
93
+ * `value`. Round it, so that the name doesn't change every second.
94
+ */
95
+ valueText?: string;
96
+ /** An icon; with `progress` and no `value`, it sits inside the ring. */
97
+ icon?: ReactNode;
98
+ /** 0 to 1: draws a progress ring around its content. */
99
+ progress?: number;
100
+ variant?: PillVariant;
101
+ /** Where it came from: a tap goes back there. Default: the path it was started on. */
102
+ href?: string;
103
+ /** Instead of going back: what a tap does. */
104
+ onClick?: (event: {
105
+ preventDefault(): void;
106
+ }) => void;
107
+ /** Tucked into the pill while the current path is its `href` (needs the host's `pathname`). Default true. */
108
+ hideOnOrigin?: boolean;
109
+ /** How it leaves when it ends: back into the pill ("join") or scaling out ("pop"). Default: the host's. */
110
+ ending?: "join" | "pop";
111
+ /** Said once by screen readers whenever it changes, for a milestone ("Break time"). */
112
+ announce?: string;
113
+ /** Your own content instead of the ring, the value and the icon. */
114
+ render?: () => ReactNode;
115
+ /**
116
+ * Its quick actions: a long press (or hovering it with a mouse, or a tap,
117
+ * see the host's `orbitalsOpenOn`) fans them out around it.
118
+ */
119
+ orbitals?: readonly Orbital[];
120
+ /** The label of the orbital that goes back, when a tap opens them (`orbitalsOpenOn="tap"`). Default "Back". */
121
+ backLabel?: string;
122
+ /** Starting an electron with an id that's already there updates it in place. */
123
+ id?: string;
124
+ }
125
+ export interface Electron {
126
+ readonly id: string;
127
+ readonly input: ElectronOptions;
128
+ /** Where a tap takes the person back to: its `href`, or the path it was started on */
129
+ readonly origin: string | undefined;
130
+ }
56
131
  export interface PillController {
57
132
  readonly name: string;
58
133
  /**
@@ -65,6 +140,34 @@ export interface PillController {
65
140
  dismiss: (id?: string) => void;
66
141
  /** Empty the queue and dismiss the visible notification. */
67
142
  clear: () => void;
143
+ /** Start an electron (beta, `magic-pill/satellites`). Returns its id. */
144
+ electron: (input: ElectronOptions) => string;
145
+ /** Change an electron's options, e.g. its value every second. */
146
+ updateElectron: (id: string, patch: Partial<ElectronOptions>) => void;
147
+ /** End an electron: it joins back into the pill (or pops away). */
148
+ endElectron: (id: string) => void;
149
+ /**
150
+ * The current page's actions, next to the pill (beta, `magic-pill/satellites`);
151
+ * `null` or none: they go. `usePillActions()` does it for a page while it's mounted.
152
+ */
153
+ setActions: (items: readonly PillActionItem[] | null) => void;
154
+ }
155
+ /** One named pill's queue. Internal: the host uses more than the controller exposes. */
156
+ export interface PillStore {
157
+ getState: () => PillQueueState;
158
+ subscribe: (listener: () => void) => () => void;
159
+ notify: (input: NotifyInput) => string;
160
+ update: (id: string, patch: Partial<NotifyOptions>) => void;
161
+ dismiss: (id: string | undefined, reason: DismissReason) => void;
162
+ clear: () => void;
163
+ /** Counts mounted hosts, to warn about two hosts with the same name. Returns the cleanup. */
164
+ registerHost: () => () => void;
165
+ getElectrons: () => readonly Electron[];
166
+ electron: (input: ElectronOptions) => string;
167
+ updateElectron: (id: string, patch: Partial<ElectronOptions>) => void;
168
+ endElectron: (id: string) => void;
169
+ getActions: () => readonly PillActionItem[] | null;
170
+ setActions: (items: readonly PillActionItem[] | null) => void;
68
171
  }
69
172
  /** Control the <MagicPill> with this name (default "main") from anywhere. */
70
173
  export declare function pill(name?: string): PillController;
@@ -1,6 +1,7 @@
1
1
  const require_react = require("../utils/react.js");
2
2
 
3
3
  // src/store/store.ts
4
+ const NO_ELECTRONS = Object.freeze([]);
4
5
  const EMPTY_STATE = Object.freeze({
5
6
  current: null,
6
7
  queued: Object.freeze([])
@@ -11,7 +12,13 @@ function createStore(name) {
11
12
  let items = [];
12
13
  let state = EMPTY_STATE;
13
14
  let hosts = 0;
15
+ let electrons = NO_ELECTRONS;
16
+ let actions = null;
14
17
  const listeners = /* @__PURE__ */ new Set();
18
+ const setElectrons = (next) => {
19
+ electrons = next.length === 0 ? NO_ELECTRONS : next;
20
+ listeners.forEach((listener) => listener());
21
+ };
15
22
  const commit = (next) => {
16
23
  items = next;
17
24
  state = next.length === 0 ? EMPTY_STATE : {
@@ -62,6 +69,45 @@ function createStore(name) {
62
69
  commit([]);
63
70
  dismissed.forEach((item) => item.input.onDismiss?.(item.id, "cleared"));
64
71
  },
72
+ getElectrons: () => electrons,
73
+ electron(input) {
74
+ const id = input.id || `mp-${++lastId}`;
75
+ if (isServer()) {
76
+ require_react.devWarn(`pill("${name}").electron() was called on the server and does nothing: call it from client code.`);
77
+ return id;
78
+ }
79
+ const existing = electrons.find((electron) => electron.id === id);
80
+ if (existing) setElectrons(electrons.map((electron) => electron === existing ? {
81
+ ...existing,
82
+ input
83
+ } : electron));
84
+ else setElectrons([...electrons, {
85
+ id,
86
+ input,
87
+ origin: input.href ?? window.location.pathname
88
+ }]);
89
+ return id;
90
+ },
91
+ updateElectron(id, patch) {
92
+ const existing = electrons.find((electron) => electron.id === id);
93
+ if (existing) setElectrons(electrons.map((electron) => electron === existing ? {
94
+ ...existing,
95
+ input: {
96
+ ...existing.input,
97
+ ...patch
98
+ }
99
+ } : electron));
100
+ },
101
+ endElectron(id) {
102
+ if (electrons.some((electron) => electron.id === id)) setElectrons(electrons.filter((electron) => electron.id !== id));
103
+ },
104
+ getActions: () => actions,
105
+ setActions(items) {
106
+ const next = items && items.length > 0 ? items : null;
107
+ if (next === actions) return;
108
+ actions = next;
109
+ listeners.forEach((listener) => listener());
110
+ },
65
111
  registerHost() {
66
112
  hosts += 1;
67
113
  if (hosts > 1) require_react.devWarn(`Several <MagicPill name="${name}"> are mounted. Give each one its own name.`);
@@ -92,7 +138,11 @@ function pill(name = "main") {
92
138
  notify: (input) => store.notify(input),
93
139
  update: (id, patch) => store.update(id, patch),
94
140
  dismiss: (id) => store.dismiss(id, "dismissed"),
95
- clear: () => store.clear()
141
+ clear: () => store.clear(),
142
+ electron: (input) => store.electron(input),
143
+ updateElectron: (id, patch) => store.updateElectron(id, patch),
144
+ endElectron: (id) => store.endElectron(id),
145
+ setActions: (items) => store.setActions(items)
96
146
  });
97
147
  controllers.set(name, controller);
98
148
  }
@@ -100,5 +150,6 @@ function pill(name = "main") {
100
150
  }
101
151
 
102
152
  exports.EMPTY_STATE = EMPTY_STATE;
153
+ exports.NO_ELECTRONS = NO_ELECTRONS;
103
154
  exports.getStore = getStore;
104
155
  exports.pill = pill;