magic-pill 9.0.0 → 9.1.0

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 (149) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +259 -15
  3. package/dist/MagicPill/MagicPill.d.mts +7 -1
  4. package/dist/MagicPill/MagicPill.d.ts +7 -1
  5. package/dist/MagicPill/MagicPill.js +40 -17
  6. package/dist/MagicPill/MagicPill.mjs +42 -19
  7. package/dist/MagicPill/choreography.js +1 -1
  8. package/dist/MagicPill/choreography.mjs +1 -1
  9. package/dist/Pill/Pill.js +53 -17
  10. package/dist/Pill/Pill.mjs +54 -18
  11. package/dist/Pill/PillContext.js +3 -1
  12. package/dist/Pill/PillContext.mjs +3 -1
  13. package/dist/Pill/PillPanel.d.mts +5 -5
  14. package/dist/Pill/PillPanel.d.ts +5 -5
  15. package/dist/Pill/PillPanel.js +28 -20
  16. package/dist/Pill/PillPanel.mjs +28 -20
  17. package/dist/Pill/morph.js +25 -7
  18. package/dist/Pill/morph.mjs +22 -8
  19. package/dist/Pill/scene.js +90 -0
  20. package/dist/Pill/scene.mjs +89 -0
  21. package/dist/Pill/themed.js +36 -0
  22. package/dist/Pill/themed.mjs +34 -0
  23. package/dist/Pill/types.d.mts +50 -0
  24. package/dist/Pill/types.d.ts +50 -0
  25. package/dist/Pill/zone.js +160 -0
  26. package/dist/Pill/zone.mjs +159 -0
  27. package/dist/PillForm/Calendar.d.mts +15 -0
  28. package/dist/PillForm/Calendar.d.ts +16 -0
  29. package/dist/PillForm/Calendar.js +371 -0
  30. package/dist/PillForm/Calendar.mjs +370 -0
  31. package/dist/PillForm/FormContext.d.mts +2 -0
  32. package/dist/PillForm/FormContext.d.ts +2 -0
  33. package/dist/PillForm/FormContext.js +8 -0
  34. package/dist/PillForm/FormContext.mjs +8 -0
  35. package/dist/PillForm/PillCheckbox.d.mts +8 -0
  36. package/dist/PillForm/PillCheckbox.d.ts +8 -0
  37. package/dist/PillForm/PillCheckbox.js +45 -0
  38. package/dist/PillForm/PillCheckbox.mjs +45 -0
  39. package/dist/PillForm/PillChoices.d.mts +39 -0
  40. package/dist/PillForm/PillChoices.d.ts +39 -0
  41. package/dist/PillForm/PillChoices.js +103 -0
  42. package/dist/PillForm/PillChoices.mjs +103 -0
  43. package/dist/PillForm/PillCode.d.mts +12 -0
  44. package/dist/PillForm/PillCode.d.ts +12 -0
  45. package/dist/PillForm/PillCode.js +60 -0
  46. package/dist/PillForm/PillCode.mjs +60 -0
  47. package/dist/PillForm/PillColor.d.mts +10 -0
  48. package/dist/PillForm/PillColor.d.ts +10 -0
  49. package/dist/PillForm/PillColor.js +56 -0
  50. package/dist/PillForm/PillColor.mjs +56 -0
  51. package/dist/PillForm/PillDate.d.mts +44 -0
  52. package/dist/PillForm/PillDate.d.ts +44 -0
  53. package/dist/PillForm/PillDate.js +193 -0
  54. package/dist/PillForm/PillDate.mjs +193 -0
  55. package/dist/PillForm/PillField.d.mts +38 -0
  56. package/dist/PillForm/PillField.d.ts +38 -0
  57. package/dist/PillForm/PillField.js +81 -0
  58. package/dist/PillForm/PillField.mjs +80 -0
  59. package/dist/PillForm/PillFields.d.mts +11 -0
  60. package/dist/PillForm/PillFields.d.ts +11 -0
  61. package/dist/PillForm/PillFields.js +21 -0
  62. package/dist/PillForm/PillFields.mjs +21 -0
  63. package/dist/PillForm/PillFile.d.mts +8 -0
  64. package/dist/PillForm/PillFile.d.ts +8 -0
  65. package/dist/PillForm/PillFile.js +55 -0
  66. package/dist/PillForm/PillFile.mjs +55 -0
  67. package/dist/PillForm/PillForm.d.mts +29 -0
  68. package/dist/PillForm/PillForm.d.ts +29 -0
  69. package/dist/PillForm/PillForm.js +66 -0
  70. package/dist/PillForm/PillForm.mjs +65 -0
  71. package/dist/PillForm/PillFormPanel.d.mts +24 -0
  72. package/dist/PillForm/PillFormPanel.d.ts +24 -0
  73. package/dist/PillForm/PillFormPanel.js +53 -0
  74. package/dist/PillForm/PillFormPanel.mjs +53 -0
  75. package/dist/PillForm/PillInput.d.mts +20 -0
  76. package/dist/PillForm/PillInput.d.ts +20 -0
  77. package/dist/PillForm/PillInput.js +84 -0
  78. package/dist/PillForm/PillInput.mjs +84 -0
  79. package/dist/PillForm/PillSelect.d.mts +23 -0
  80. package/dist/PillForm/PillSelect.d.ts +23 -0
  81. package/dist/PillForm/PillSelect.js +87 -0
  82. package/dist/PillForm/PillSelect.mjs +87 -0
  83. package/dist/PillForm/PillSlider.d.mts +17 -0
  84. package/dist/PillForm/PillSlider.d.ts +17 -0
  85. package/dist/PillForm/PillSlider.js +61 -0
  86. package/dist/PillForm/PillSlider.mjs +61 -0
  87. package/dist/PillForm/PillStepper.d.mts +13 -0
  88. package/dist/PillForm/PillStepper.d.ts +13 -0
  89. package/dist/PillForm/PillStepper.js +84 -0
  90. package/dist/PillForm/PillStepper.mjs +84 -0
  91. package/dist/PillForm/PillSubmit.d.mts +14 -0
  92. package/dist/PillForm/PillSubmit.d.ts +14 -0
  93. package/dist/PillForm/PillSubmit.js +90 -0
  94. package/dist/PillForm/PillSubmit.mjs +89 -0
  95. package/dist/PillForm/PillSwitch.d.mts +5 -0
  96. package/dist/PillForm/PillSwitch.d.ts +5 -0
  97. package/dist/PillForm/PillSwitch.js +38 -0
  98. package/dist/PillForm/PillSwitch.mjs +38 -0
  99. package/dist/PillForm/PillTextarea.d.mts +5 -0
  100. package/dist/PillForm/PillTextarea.d.ts +5 -0
  101. package/dist/PillForm/PillTextarea.js +49 -0
  102. package/dist/PillForm/PillTextarea.mjs +49 -0
  103. package/dist/PillForm/dates.d.mts +1 -0
  104. package/dist/PillForm/dates.d.ts +1 -0
  105. package/dist/PillForm/dates.js +155 -0
  106. package/dist/PillForm/dates.mjs +139 -0
  107. package/dist/PillForm/glyphs.js +65 -0
  108. package/dist/PillForm/glyphs.mjs +56 -0
  109. package/dist/PillForm/keyboard.js +34 -0
  110. package/dist/PillForm/keyboard.mjs +34 -0
  111. package/dist/PillForm/popover.js +84 -0
  112. package/dist/PillForm/popover.mjs +83 -0
  113. package/dist/PillForm/send.d.mts +6 -0
  114. package/dist/PillForm/send.d.ts +6 -0
  115. package/dist/PillForm/send.js +44 -0
  116. package/dist/PillForm/send.mjs +44 -0
  117. package/dist/PillForm/validity.js +74 -0
  118. package/dist/PillForm/validity.mjs +72 -0
  119. package/dist/PillNavbar/PillNavbar.d.mts +29 -6
  120. package/dist/PillNavbar/PillNavbar.d.ts +29 -6
  121. package/dist/PillNavbar/PillNavbar.js +35 -11
  122. package/dist/PillNavbar/PillNavbar.mjs +36 -12
  123. package/dist/PillNavbar/room.js +84 -0
  124. package/dist/PillNavbar/room.mjs +83 -0
  125. package/dist/PillNotification/PillNotification.d.mts +6 -0
  126. package/dist/PillNotification/PillNotification.d.ts +6 -0
  127. package/dist/PillNotification/PillNotification.js +3 -0
  128. package/dist/PillNotification/PillNotification.mjs +3 -0
  129. package/dist/PillOptics/PillOptics.d.mts +8 -0
  130. package/dist/PillOptics/PillOptics.d.ts +8 -0
  131. package/dist/PillOptics/PillOptics.js +195 -0
  132. package/dist/PillOptics/PillOptics.mjs +195 -0
  133. package/dist/PillOptics/adaptive.js +212 -0
  134. package/dist/PillOptics/adaptive.mjs +208 -0
  135. package/dist/PillOptics/lens.js +77 -0
  136. package/dist/PillOptics/lens.mjs +73 -0
  137. package/dist/PillOptics/light.js +37 -0
  138. package/dist/PillOptics/light.mjs +36 -0
  139. package/dist/calendar.css +499 -0
  140. package/dist/forms.css +1452 -0
  141. package/dist/index.d.mts +24 -3
  142. package/dist/index.d.ts +24 -3
  143. package/dist/index.js +36 -0
  144. package/dist/index.mjs +19 -1
  145. package/dist/styles.css +169 -19
  146. package/dist/themes.css +449 -0
  147. package/dist/utils/visuallyHidden.js +18 -0
  148. package/dist/utils/visuallyHidden.mjs +18 -0
  149. package/package.json +4 -1
@@ -10,13 +10,13 @@ import { jsx, jsxs } from "react/jsx-runtime";
10
10
 
11
11
  // src/Pill/PillPanel.tsx
12
12
  /**
13
- * Content shown when the pill's presence is `panel` (one panel per pill).
14
- * Labelled by its title, focused when it opens, gives focus back to its
13
+ * Content shown when the pill's presence is `panel`. One per pill, or several
14
+ * with a `name` each. Labelled by its title, focused when it opens, gives focus back to its
15
15
  * trigger when it closes.
16
16
  */
17
- const PillPanel = forwardRef(function PillPanel({ title, closeLabel = "Close", closeIcon, children, ...rest }, forwardedRef) {
18
- const { presence, closePanel, panelId, panelTriggerRef, rootRef } = useInternalPill("<PillPanel>");
19
- const isOpen = presence === "panel";
17
+ const PillPanel = forwardRef(function PillPanel({ name, title, titleAction, footer, closeLabel = "Close", closeIcon, closeExpanded = false, children, ...rest }, forwardedRef) {
18
+ const { presence, panel, closePanel, panelIdOf, panelTriggerRef, rootRef } = useInternalPill("<PillPanel>");
19
+ const isOpen = presence === "panel" && panel === name;
20
20
  const titleId = useId();
21
21
  const panelRef = useRef(null);
22
22
  const ref = useMemo(() => mergeRefs(panelRef, forwardedRef), [forwardedRef]);
@@ -39,49 +39,57 @@ const PillPanel = forwardRef(function PillPanel({ title, closeLabel = "Close", c
39
39
  return /* @__PURE__ */ jsxs("div", {
40
40
  ...rest,
41
41
  ref,
42
- id: panelId,
42
+ id: panelIdOf(name),
43
43
  "data-mp": "panel",
44
44
  "data-open": isOpen ? "" : void 0,
45
45
  role: "region",
46
46
  "aria-labelledby": hasTitle ? titleId : void 0,
47
47
  tabIndex: -1,
48
48
  children: [
49
- hasTitle && /* @__PURE__ */ jsx("div", {
50
- id: titleId,
51
- "data-mp": "panel-title",
52
- children: title
49
+ hasTitle && /* @__PURE__ */ jsxs("div", {
50
+ "data-mp": "panel-head",
51
+ children: [/* @__PURE__ */ jsx("div", {
52
+ id: titleId,
53
+ "data-mp": "panel-title",
54
+ children: title
55
+ }), titleAction && /* @__PURE__ */ jsx("div", {
56
+ "data-mp": "panel-title-action",
57
+ children: titleAction
58
+ })]
53
59
  }),
54
60
  /* @__PURE__ */ jsx("div", {
55
61
  "data-mp": "panel-body",
56
62
  children
57
63
  }),
58
- /* @__PURE__ */ jsx("div", {
64
+ /* @__PURE__ */ jsxs("div", {
59
65
  "data-mp": "panel-footer",
60
- children: /* @__PURE__ */ jsx(PillAction, {
66
+ children: [footer, /* @__PURE__ */ jsx(PillAction, {
61
67
  icon: closeIcon ?? /* @__PURE__ */ jsx(CloseIcon, {}),
62
68
  label: closeLabel,
63
- onClick: closePanel
64
- })
69
+ onClick: closePanel,
70
+ "data-collapsed": closeExpanded ? void 0 : "",
71
+ "data-quiet": footer ? "" : void 0
72
+ })]
65
73
  })
66
74
  ]
67
75
  });
68
76
  });
69
- /** An action that opens (and closes) the pill's panel. */
70
- const PillPanelTrigger = forwardRef(function PillPanelTrigger(props, forwardedRef) {
71
- const { presence, setPresence, closePanel, panelId, panelTriggerRef } = useInternalPill("<PillPanelTrigger>");
72
- const isOpen = presence === "panel";
77
+ /** An action that opens (and closes) the pill's panel, or the one named `panel`. */
78
+ const PillPanelTrigger = forwardRef(function PillPanelTrigger({ panel: name, ...props }, forwardedRef) {
79
+ const { presence, panel, openPanel, closePanel, panelIdOf, panelTriggerRef } = useInternalPill("<PillPanelTrigger>");
80
+ const isOpen = presence === "panel" && panel === name;
73
81
  const triggerRef = useRef(null);
74
82
  const ref = useMemo(() => mergeRefs(triggerRef, forwardedRef), [forwardedRef]);
75
83
  const toggle = () => {
76
84
  panelTriggerRef.current = triggerRef.current;
77
85
  if (isOpen) closePanel();
78
- else setPresence("panel");
86
+ else openPanel(name);
79
87
  };
80
88
  return /* @__PURE__ */ jsx(PillAction, {
81
89
  ...props,
82
90
  ref,
83
91
  "aria-expanded": isOpen,
84
- "aria-controls": panelId,
92
+ "aria-controls": panelIdOf(name),
85
93
  onClick: toggle
86
94
  });
87
95
  });
@@ -107,10 +107,7 @@ const readSize = (element) => ({
107
107
  width: element.offsetWidth,
108
108
  height: element.offsetHeight
109
109
  });
110
- /**
111
- * Stops the size animations running on the pill (and in it), and drops the
112
- * copy of the content a swap slides out (but `keep`). Returns whether anything ran.
113
- */
110
+ /** Stops the pill's animations with this id (and the ones in it). Returns whether anything ran. */
114
111
  function cancel(element, id, subtree) {
115
112
  let cancelled = false;
116
113
  for (const animation of element.getAnimations?.({ subtree }) ?? []) if (animation.id === id) {
@@ -119,6 +116,10 @@ function cancel(element, id, subtree) {
119
116
  }
120
117
  return cancelled;
121
118
  }
119
+ /**
120
+ * Stops the size animations running on the pill (and in it), and drops the
121
+ * copy of the content a swap slides out (but `keep`). Returns whether anything ran.
122
+ */
122
123
  function stopMorphs(element, keep) {
123
124
  let stopped = cancel(element, MORPH, true);
124
125
  for (const outgoing of element.querySelectorAll(":scope > [data-mp=\"outgoing\"]")) if (outgoing !== keep) {
@@ -200,6 +201,17 @@ function planSwap(from, to, timings, anchor, down = false) {
200
201
  })
201
202
  };
202
203
  }
204
+ const CONTROLS = "input, textarea, select";
205
+ /** What was typed or chosen: a copied control starts from its default value. */
206
+ function copyValues(from, to) {
207
+ const copies = to.querySelectorAll(CONTROLS);
208
+ [...from.querySelectorAll(CONTROLS)].filter((control) => !to.contains(control) && !control.closest("[data-mp=\"outgoing\"]")).forEach((control, index) => {
209
+ const copy = copies[index];
210
+ if (!copy || control.type === "file") return;
211
+ if (control.type === "checkbox" || control.type === "radio") copy.checked = control.checked;
212
+ else copy.value = control.value;
213
+ });
214
+ }
203
215
  /**
204
216
  * Before new content replaces the pill's: a still copy of what's on screen,
205
217
  * laid over it, to slide out once the new content is in. Null when there's
@@ -211,6 +223,7 @@ function captureSwap(element) {
211
223
  const outgoing = document.createElement("div");
212
224
  for (const child of element.children) if (child.getAttribute("data-mp") !== "outgoing") outgoing.append(child.cloneNode(true));
213
225
  stopMorphs(element);
226
+ copyValues(element, outgoing);
214
227
  for (const node of outgoing.querySelectorAll("[id], [name]")) {
215
228
  node.removeAttribute("id");
216
229
  node.removeAttribute("name");
@@ -255,8 +268,9 @@ function readMorphTimings(element) {
255
268
  };
256
269
  }
257
270
  function readPanel(pill) {
258
- const element = pill.querySelector(":scope > [data-mp=\"panel\"]");
259
- if (!element || getComputedStyle(element).display === "none") return void 0;
271
+ const shown = [...pill.querySelectorAll(":scope > [data-mp=\"panel\"]")].filter((panel) => getComputedStyle(panel).display !== "none");
272
+ const element = shown.find((panel) => panel.hasAttribute("data-open")) ?? shown[0];
273
+ if (!element) return void 0;
260
274
  const pillRect = pill.getBoundingClientRect();
261
275
  const rect = element.getBoundingClientRect();
262
276
  return {
@@ -326,7 +340,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
326
340
  if (plan.panel && panel) {
327
341
  animations.push(panel.element.animate(plan.panel, options));
328
342
  const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
329
- if (footer) animations.push(footer.animate([{ position: "static" }, { position: "static" }], options));
343
+ if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
330
344
  }
331
345
  if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
332
346
  if (plan.content) {
@@ -396,10 +410,14 @@ var PlacementWatch = class extends react.Component {
396
410
  }
397
411
  };
398
412
 
413
+ exports.EASING = EASING;
414
+ exports.MORPH = MORPH;
399
415
  exports.PlacementWatch = PlacementWatch;
400
416
  exports.animateMove = animateMove;
417
+ exports.cancel = cancel;
401
418
  exports.captureSwap = captureSwap;
402
419
  exports.planMorph = planMorph;
403
420
  exports.planSwap = planSwap;
404
421
  exports.playSwap = playSwap;
422
+ exports.readMorphTimings = readMorphTimings;
405
423
  exports.useSizeMorph = useSizeMorph;
@@ -107,10 +107,7 @@ const readSize = (element) => ({
107
107
  width: element.offsetWidth,
108
108
  height: element.offsetHeight
109
109
  });
110
- /**
111
- * Stops the size animations running on the pill (and in it), and drops the
112
- * copy of the content a swap slides out (but `keep`). Returns whether anything ran.
113
- */
110
+ /** Stops the pill's animations with this id (and the ones in it). Returns whether anything ran. */
114
111
  function cancel(element, id, subtree) {
115
112
  let cancelled = false;
116
113
  for (const animation of element.getAnimations?.({ subtree }) ?? []) if (animation.id === id) {
@@ -119,6 +116,10 @@ function cancel(element, id, subtree) {
119
116
  }
120
117
  return cancelled;
121
118
  }
119
+ /**
120
+ * Stops the size animations running on the pill (and in it), and drops the
121
+ * copy of the content a swap slides out (but `keep`). Returns whether anything ran.
122
+ */
122
123
  function stopMorphs(element, keep) {
123
124
  let stopped = cancel(element, MORPH, true);
124
125
  for (const outgoing of element.querySelectorAll(":scope > [data-mp=\"outgoing\"]")) if (outgoing !== keep) {
@@ -200,6 +201,17 @@ function planSwap(from, to, timings, anchor, down = false) {
200
201
  })
201
202
  };
202
203
  }
204
+ const CONTROLS = "input, textarea, select";
205
+ /** What was typed or chosen: a copied control starts from its default value. */
206
+ function copyValues(from, to) {
207
+ const copies = to.querySelectorAll(CONTROLS);
208
+ [...from.querySelectorAll(CONTROLS)].filter((control) => !to.contains(control) && !control.closest("[data-mp=\"outgoing\"]")).forEach((control, index) => {
209
+ const copy = copies[index];
210
+ if (!copy || control.type === "file") return;
211
+ if (control.type === "checkbox" || control.type === "radio") copy.checked = control.checked;
212
+ else copy.value = control.value;
213
+ });
214
+ }
203
215
  /**
204
216
  * Before new content replaces the pill's: a still copy of what's on screen,
205
217
  * laid over it, to slide out once the new content is in. Null when there's
@@ -211,6 +223,7 @@ function captureSwap(element) {
211
223
  const outgoing = document.createElement("div");
212
224
  for (const child of element.children) if (child.getAttribute("data-mp") !== "outgoing") outgoing.append(child.cloneNode(true));
213
225
  stopMorphs(element);
226
+ copyValues(element, outgoing);
214
227
  for (const node of outgoing.querySelectorAll("[id], [name]")) {
215
228
  node.removeAttribute("id");
216
229
  node.removeAttribute("name");
@@ -255,8 +268,9 @@ function readMorphTimings(element) {
255
268
  };
256
269
  }
257
270
  function readPanel(pill) {
258
- const element = pill.querySelector(":scope > [data-mp=\"panel\"]");
259
- if (!element || getComputedStyle(element).display === "none") return void 0;
271
+ const shown = [...pill.querySelectorAll(":scope > [data-mp=\"panel\"]")].filter((panel) => getComputedStyle(panel).display !== "none");
272
+ const element = shown.find((panel) => panel.hasAttribute("data-open")) ?? shown[0];
273
+ if (!element) return void 0;
260
274
  const pillRect = pill.getBoundingClientRect();
261
275
  const rect = element.getBoundingClientRect();
262
276
  return {
@@ -326,7 +340,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
326
340
  if (plan.panel && panel) {
327
341
  animations.push(panel.element.animate(plan.panel, options));
328
342
  const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
329
- if (footer) animations.push(footer.animate([{ position: "static" }, { position: "static" }], options));
343
+ if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
330
344
  }
331
345
  if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
332
346
  if (plan.content) {
@@ -396,4 +410,4 @@ var PlacementWatch = class extends Component {
396
410
  }
397
411
  };
398
412
 
399
- export { PlacementWatch, animateMove, captureSwap, planMorph, planSwap, playSwap, useSizeMorph };
413
+ export { EASING, MORPH, PlacementWatch, animateMove, cancel, captureSwap, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
@@ -0,0 +1,90 @@
1
+ "use client";
2
+
3
+ let react = require("react");
4
+ const require_morph = require("./morph.js");
5
+ const require_timing = require("../utils/timing.js");
6
+
7
+ // src/Pill/scene.ts
8
+ const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
9
+ /**
10
+ * Through the circle: the old content stays while the pill closes on it, the
11
+ * new one is laid out in the closed pill, then the pill opens. Returns what to
12
+ * show, and the level to show it at.
13
+ */
14
+ function useScene(rootRef, { scene, change, presence, enabled, onClosed, children, variant }) {
15
+ const [shownScene, setShownScene] = (0, react.useState)(scene);
16
+ const [isOpening, setIsOpening] = (0, react.useState)(false);
17
+ const animates = enabled && change === "circle" && !CLOSED.has(presence);
18
+ const isClosing = shownScene !== scene && animates;
19
+ const shown = (0, react.useRef)({
20
+ children,
21
+ variant
22
+ });
23
+ if (!isClosing) shown.current = {
24
+ children,
25
+ variant
26
+ };
27
+ const closed = (0, react.useRef)(onClosed);
28
+ closed.current = onClosed;
29
+ const hadFocus = (0, react.useRef)(false);
30
+ (0, react.useEffect)(() => {
31
+ if (shownScene === scene) return;
32
+ const root = rootRef.current;
33
+ if (!isClosing || !root || require_timing.prefersReducedMotion()) {
34
+ setShownScene(scene);
35
+ return;
36
+ }
37
+ hadFocus.current = root.contains(document.activeElement);
38
+ const { fade, change: duration } = require_morph.readMorphTimings(root);
39
+ const timer = setTimeout(() => {
40
+ closed.current();
41
+ setShownScene(scene);
42
+ setIsOpening(true);
43
+ }, fade + duration);
44
+ return () => clearTimeout(timer);
45
+ }, [
46
+ rootRef,
47
+ scene,
48
+ shownScene,
49
+ isClosing
50
+ ]);
51
+ (0, react.useEffect)(() => {
52
+ const root = rootRef.current;
53
+ if (!isOpening || !root) return;
54
+ if (hadFocus.current) focusIfLost(root);
55
+ root.offsetWidth;
56
+ setIsOpening(false);
57
+ }, [rootRef, isOpening]);
58
+ return {
59
+ ...shown.current,
60
+ key: isClosing ? shownScene : scene,
61
+ presence: isClosing || isOpening && !CLOSED.has(presence) ? "rest" : presence
62
+ };
63
+ }
64
+ /** The focus was on what left: the pill takes it, so the keyboard goes on from there. */
65
+ function focusIfLost(root) {
66
+ if (document.activeElement !== document.body) return;
67
+ if (!root.hasAttribute("tabindex")) root.tabIndex = -1;
68
+ root.focus({ preventScroll: true });
69
+ }
70
+ /**
71
+ * Sliding: the old content is copied just before React updates the page, and
72
+ * slides out as the new one comes in.
73
+ */
74
+ var SceneWatch = class extends react.Component {
75
+ getSnapshotBeforeUpdate(previous) {
76
+ const { scene, slides, target } = this.props;
77
+ const presence = target.current?.dataset.presence;
78
+ if (!slides || previous.scene === scene || !presence || CLOSED.has(presence) || presence === "panel") return null;
79
+ return require_morph.captureSwap(target.current);
80
+ }
81
+ componentDidUpdate(_props, _state, capture) {
82
+ if (capture && this.props.target.current) require_morph.playSwap(this.props.target.current, capture);
83
+ }
84
+ render() {
85
+ return null;
86
+ }
87
+ };
88
+
89
+ exports.SceneWatch = SceneWatch;
90
+ exports.useScene = useScene;
@@ -0,0 +1,89 @@
1
+ "use client";
2
+
3
+ import { Component, useEffect, useRef, useState } from "react";
4
+ import { captureSwap, playSwap, readMorphTimings } from "./morph.mjs";
5
+ import { prefersReducedMotion } from "../utils/timing.mjs";
6
+
7
+ // src/Pill/scene.ts
8
+ const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
9
+ /**
10
+ * Through the circle: the old content stays while the pill closes on it, the
11
+ * new one is laid out in the closed pill, then the pill opens. Returns what to
12
+ * show, and the level to show it at.
13
+ */
14
+ function useScene(rootRef, { scene, change, presence, enabled, onClosed, children, variant }) {
15
+ const [shownScene, setShownScene] = useState(scene);
16
+ const [isOpening, setIsOpening] = useState(false);
17
+ const animates = enabled && change === "circle" && !CLOSED.has(presence);
18
+ const isClosing = shownScene !== scene && animates;
19
+ const shown = useRef({
20
+ children,
21
+ variant
22
+ });
23
+ if (!isClosing) shown.current = {
24
+ children,
25
+ variant
26
+ };
27
+ const closed = useRef(onClosed);
28
+ closed.current = onClosed;
29
+ const hadFocus = useRef(false);
30
+ useEffect(() => {
31
+ if (shownScene === scene) return;
32
+ const root = rootRef.current;
33
+ if (!isClosing || !root || prefersReducedMotion()) {
34
+ setShownScene(scene);
35
+ return;
36
+ }
37
+ hadFocus.current = root.contains(document.activeElement);
38
+ const { fade, change: duration } = readMorphTimings(root);
39
+ const timer = setTimeout(() => {
40
+ closed.current();
41
+ setShownScene(scene);
42
+ setIsOpening(true);
43
+ }, fade + duration);
44
+ return () => clearTimeout(timer);
45
+ }, [
46
+ rootRef,
47
+ scene,
48
+ shownScene,
49
+ isClosing
50
+ ]);
51
+ useEffect(() => {
52
+ const root = rootRef.current;
53
+ if (!isOpening || !root) return;
54
+ if (hadFocus.current) focusIfLost(root);
55
+ root.offsetWidth;
56
+ setIsOpening(false);
57
+ }, [rootRef, isOpening]);
58
+ return {
59
+ ...shown.current,
60
+ key: isClosing ? shownScene : scene,
61
+ presence: isClosing || isOpening && !CLOSED.has(presence) ? "rest" : presence
62
+ };
63
+ }
64
+ /** The focus was on what left: the pill takes it, so the keyboard goes on from there. */
65
+ function focusIfLost(root) {
66
+ if (document.activeElement !== document.body) return;
67
+ if (!root.hasAttribute("tabindex")) root.tabIndex = -1;
68
+ root.focus({ preventScroll: true });
69
+ }
70
+ /**
71
+ * Sliding: the old content is copied just before React updates the page, and
72
+ * slides out as the new one comes in.
73
+ */
74
+ var SceneWatch = class extends Component {
75
+ getSnapshotBeforeUpdate(previous) {
76
+ const { scene, slides, target } = this.props;
77
+ const presence = target.current?.dataset.presence;
78
+ if (!slides || previous.scene === scene || !presence || CLOSED.has(presence) || presence === "panel") return null;
79
+ return captureSwap(target.current);
80
+ }
81
+ componentDidUpdate(_props, _state, capture) {
82
+ if (capture && this.props.target.current) playSwap(this.props.target.current, capture);
83
+ }
84
+ render() {
85
+ return null;
86
+ }
87
+ };
88
+
89
+ export { SceneWatch, useScene };
@@ -0,0 +1,36 @@
1
+ "use client";
2
+
3
+ let react = require("react");
4
+
5
+ // src/Pill/themed.ts
6
+ const pills = /* @__PURE__ */ new Set();
7
+ const listeners = /* @__PURE__ */ new Set();
8
+ const notify = () => listeners.forEach((listener) => listener());
9
+ const themedPills = pills;
10
+ /** Called when a themed pill comes, goes, or changes theme or level. Returns the unsubscribe. */
11
+ function onThemedPills(listener) {
12
+ listeners.add(listener);
13
+ return () => void listeners.delete(listener);
14
+ }
15
+ /** Lists the pill while it has a theme. `isOverlay`: its element is another one. */
16
+ function useThemed(rootRef, theme, presence, isOverlay) {
17
+ (0, react.useEffect)(() => {
18
+ const element = rootRef.current;
19
+ if (!element || theme === void 0 || theme === "plain") return;
20
+ pills.add(element);
21
+ notify();
22
+ return () => {
23
+ pills.delete(element);
24
+ notify();
25
+ };
26
+ }, [
27
+ rootRef,
28
+ theme,
29
+ presence,
30
+ isOverlay
31
+ ]);
32
+ }
33
+
34
+ exports.onThemedPills = onThemedPills;
35
+ exports.themedPills = themedPills;
36
+ exports.useThemed = useThemed;
@@ -0,0 +1,34 @@
1
+ "use client";
2
+
3
+ import { useEffect } from "react";
4
+
5
+ // src/Pill/themed.ts
6
+ const pills = /* @__PURE__ */ new Set();
7
+ const listeners = /* @__PURE__ */ new Set();
8
+ const notify = () => listeners.forEach((listener) => listener());
9
+ const themedPills = pills;
10
+ /** Called when a themed pill comes, goes, or changes theme or level. Returns the unsubscribe. */
11
+ function onThemedPills(listener) {
12
+ listeners.add(listener);
13
+ return () => void listeners.delete(listener);
14
+ }
15
+ /** Lists the pill while it has a theme. `isOverlay`: its element is another one. */
16
+ function useThemed(rootRef, theme, presence, isOverlay) {
17
+ useEffect(() => {
18
+ const element = rootRef.current;
19
+ if (!element || theme === void 0 || theme === "plain") return;
20
+ pills.add(element);
21
+ notify();
22
+ return () => {
23
+ pills.delete(element);
24
+ notify();
25
+ };
26
+ }, [
27
+ rootRef,
28
+ theme,
29
+ presence,
30
+ isOverlay
31
+ ]);
32
+ }
33
+
34
+ export { onThemedPills, themedPills, useThemed };
@@ -22,6 +22,12 @@ export type PillOrientation = "horizontal" | "vertical";
22
22
  * define your own: `[data-mp="pill"][data-variant="brand"] { --mp-surface: … }`.
23
23
  */
24
24
  export type PillVariant = "neutral" | "success" | "danger" | (string & {});
25
+ /**
26
+ * The pill's material. `glass` and `liquid` need `magic-pill/themes.css`. Any
27
+ * other string is written to `data-theme` so you can define your own:
28
+ * `[data-mp="pill"][data-theme="paper"] { --mp-surface: … }`.
29
+ */
30
+ export type PillTheme = "plain" | "glass" | "liquid" | (string & {});
25
31
  /**
26
32
  * A component that renders a link from an `href`, such as Next's `Link`.
27
33
  * React Router's `Link` takes `to`: wrap it.
@@ -30,6 +36,8 @@ export type LinkComponent = ComponentType<AnchorHTMLAttributes<HTMLAnchorElement
30
36
  href: string;
31
37
  ref?: Ref<HTMLAnchorElement>;
32
38
  }>;
39
+ /** How a pill goes from one scene to the next: through the circle, sliding, or at once. */
40
+ export type SceneChange = "circle" | "slide" | "none";
33
41
  export interface PillProps extends Omit<ComponentPropsWithoutRef<"div">, "children"> {
34
42
  children?: ReactNode;
35
43
  /** Controlled presence. */
@@ -40,14 +48,37 @@ export interface PillProps extends Omit<ComponentPropsWithoutRef<"div">, "childr
40
48
  onPresenceChange?: (presence: PillPresence) => void;
41
49
  /** Default `inline`. */
42
50
  placement?: PillPlacement;
51
+ /**
52
+ * Fixed placements only: pills with the same placement line up one after
53
+ * the other, the first one set up keeping its spot. `false` leaves this
54
+ * pill out: it stays in its spot and the others ignore it. Default `true`.
55
+ */
56
+ ladder?: boolean;
43
57
  /**
44
58
  * `overlay` only: a click outside the modal does what Escape does (closes
45
59
  * the panel if it's open, the modal otherwise). Default `false`.
46
60
  */
47
61
  closeOnBackdropClick?: boolean;
62
+ /**
63
+ * `overlay` only. `glow` is an animated backdrop, and needs
64
+ * `magic-pill/themes.css`. Any other string is written to `data-backdrop` so
65
+ * you can define your own:
66
+ * `[data-mp="pill"][data-backdrop="dots"]::backdrop { … }`. Default `plain`.
67
+ */
68
+ backdrop?: "plain" | "glow" | (string & {});
48
69
  orientation?: PillOrientation;
49
70
  /** Default `neutral`. */
50
71
  variant?: PillVariant;
72
+ /** The variant picks the colour, the theme the material. Default `plain`. */
73
+ theme?: PillTheme;
74
+ /**
75
+ * A name for what the pill shows. When it changes, the pill doesn't jump to
76
+ * the new content: it closes on the old one and opens on the new one, as
77
+ * between a host's navbar and its notifications.
78
+ */
79
+ scene?: string | number;
80
+ /** How: through the circle (default), sliding in the open pill, or at once. */
81
+ sceneChange?: SceneChange;
51
82
  /** Keeps the behaviour and the data attributes, drops the visual styles. */
52
83
  unstyled?: boolean;
53
84
  }
@@ -78,14 +109,33 @@ export interface PillLinkActionProps extends PillActionBase, Omit<ComponentProps
78
109
  export type PillActionProps = PillButtonActionProps | PillLinkActionProps;
79
110
  export interface PillPanelProps extends Omit<ComponentPropsWithoutRef<"div">, "title"> {
80
111
  title?: ReactNode;
112
+ /**
113
+ * For several panels in one pill: the trigger whose `panel` is the same
114
+ * opens this one. Default: the pill's only panel, with no name.
115
+ */
116
+ name?: string;
117
+ /** At the end of the title's row, e.g. `<PillAction label="Default" … />`. */
118
+ titleAction?: ReactNode;
119
+ /** In the footer, before the close button, e.g. `<PillAction label="Save" … />`. */
120
+ footer?: ReactNode;
81
121
  /** Default "Close". */
82
122
  closeLabel?: string;
83
123
  /** Default <CloseIcon />. */
84
124
  closeIcon?: ReactNode;
125
+ /** Always show the close button's label, not only on hover and focus. Default `false`. */
126
+ closeExpanded?: boolean;
127
+ }
128
+ export interface PillPanelTriggerProps extends Omit<PillButtonActionProps, "onClick"> {
129
+ /** The `name` of the panel it opens, when the pill has several. */
130
+ panel?: string;
85
131
  }
86
132
  export interface PillContextValue {
87
133
  presence: PillPresence;
88
134
  setPresence: (presence: PillPresence) => void;
135
+ /** Opens the pill's panel, or the one with that name. */
136
+ openPanel: (name?: string) => void;
137
+ /** Back to the level the pill had before its panel opened. */
138
+ closePanel: () => void;
89
139
  placement: PillPlacement;
90
140
  orientation: PillOrientation;
91
141
  variant: PillVariant;