magic-pill 9.4.0-next.3 → 9.4.0-next.4

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 (45) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +10 -7
  3. package/dist/MagicPill/MagicPill.d.mts +2 -1
  4. package/dist/MagicPill/MagicPill.d.ts +2 -1
  5. package/dist/MagicPill/MagicPill.js +10 -3
  6. package/dist/MagicPill/MagicPill.mjs +10 -3
  7. package/dist/Pill/Pill.js +1 -0
  8. package/dist/Pill/Pill.mjs +1 -0
  9. package/dist/Pill/goo.js +60 -28
  10. package/dist/Pill/goo.mjs +60 -28
  11. package/dist/Pill/modal.js +3 -1
  12. package/dist/Pill/modal.mjs +3 -1
  13. package/dist/Pill/morph.js +56 -2
  14. package/dist/Pill/morph.mjs +56 -3
  15. package/dist/PillForm/PillForm.js +6 -1
  16. package/dist/PillForm/PillForm.mjs +7 -2
  17. package/dist/PillForm/dates.js +14 -3
  18. package/dist/PillForm/dates.mjs +14 -3
  19. package/dist/PillForm/room.js +46 -0
  20. package/dist/PillForm/room.mjs +46 -0
  21. package/dist/PillNavbar/PillNavbar.js +3 -1
  22. package/dist/PillNavbar/PillNavbar.mjs +4 -2
  23. package/dist/PillNavbar/room.js +65 -11
  24. package/dist/PillNavbar/room.mjs +65 -12
  25. package/dist/PillNotification/PillNotification.js +2 -4
  26. package/dist/PillNotification/PillNotification.mjs +2 -4
  27. package/dist/PillNotification/RowsBody.js +77 -0
  28. package/dist/PillNotification/RowsBody.mjs +77 -0
  29. package/dist/forms.css +14 -0
  30. package/dist/satellites/Actions.js +181 -6
  31. package/dist/satellites/Actions.mjs +182 -7
  32. package/dist/satellites/Orbitals.js +67 -17
  33. package/dist/satellites/Orbitals.mjs +69 -19
  34. package/dist/satellites/Satellites.js +197 -84
  35. package/dist/satellites/Satellites.mjs +198 -86
  36. package/dist/satellites/handoff.js +36 -0
  37. package/dist/satellites/handoff.mjs +34 -0
  38. package/dist/satellites/measure.js +22 -0
  39. package/dist/satellites/measure.mjs +22 -1
  40. package/dist/satellites/orbits.js +12 -0
  41. package/dist/satellites/orbits.mjs +12 -1
  42. package/dist/satellites/types.d.mts +7 -1
  43. package/dist/satellites/types.d.ts +7 -1
  44. package/dist/styles.css +78 -15
  45. package/package.json +1 -1
@@ -187,6 +187,44 @@ const readSize = (element) => ({
187
187
  width: element.offsetWidth,
188
188
  height: element.offsetHeight
189
189
  });
190
+ /**
191
+ * Its size once what's changing inside it has got there: a label folding as a panel closes is still
192
+ * open when the pill measures where it's going (a transition). Measured then on a copy laid out with
193
+ * no transitions, so the pill goes there in one motion, instead of shrinking, then jumping (or
194
+ * settling in a second step) as the label folds.
195
+ */
196
+ function readSettledSize(element) {
197
+ if (!(typeof CSSTransition !== "undefined" && (element.getAnimations?.({ subtree: true }) ?? []).some((animation) => animation instanceof CSSTransition && animation.effect instanceof KeyframeEffect && animation.effect.target !== element)) || element.tagName === "DIALOG") return readSize(element);
198
+ return onCopy(element, readSize, (copy) => {
199
+ const actions = element.querySelectorAll("[data-mp=\"action\"]");
200
+ copy.querySelectorAll("[data-mp=\"action\"]").forEach((action, index) => {
201
+ if (actions[index]?.matches(":hover, :focus-visible")) action.setAttribute("data-pointed", "");
202
+ });
203
+ });
204
+ }
205
+ /**
206
+ * A copy of the pill laid out next to it as it ends up: hidden, with no transitions nor animations
207
+ * (`[data-mp-measure]`), sharing no id or radio group with it. `read` measures it, then it's gone.
208
+ */
209
+ function onCopy(element, read, prepare) {
210
+ const copy = element.cloneNode(true);
211
+ copy.querySelectorAll("[id], [name]").forEach((node) => {
212
+ node.removeAttribute("id");
213
+ node.removeAttribute("name");
214
+ });
215
+ copy.removeAttribute("id");
216
+ copy.setAttribute("data-mp-measure", "");
217
+ copy.inert = true;
218
+ copy.style.visibility = "hidden";
219
+ copy.style.pointerEvents = "none";
220
+ prepare?.(copy);
221
+ element.after(copy);
222
+ try {
223
+ return read(copy);
224
+ } finally {
225
+ copy.remove();
226
+ }
227
+ }
190
228
  /** Stops the pill's animations with this id (and the ones in it). Returns whether anything ran. */
191
229
  function cancel(element, id, subtree) {
192
230
  let cancelled = false;
@@ -442,7 +480,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
442
480
  const from = stopMorphs(element) || running.current.length > 0 ? current : lastSize.current;
443
481
  running.current.forEach((animation) => animation.cancel());
444
482
  running.current = [];
445
- const to = readSize(element);
483
+ const to = readSettledSize(element);
446
484
  lastSize.current = to;
447
485
  if (!from) return;
448
486
  const panel = readPanel(element);
@@ -476,7 +514,22 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
476
514
  if (animations.length === 0) return;
477
515
  running.current = animations;
478
516
  animations[0].finished.then(() => {
479
- if (running.current === animations) running.current = [];
517
+ if (running.current !== animations) return;
518
+ running.current = [];
519
+ const now = readSize(element);
520
+ if (CLOSED.has(presence) || Math.abs(now.width - to.width) <= 1 && Math.abs(now.height - to.height) <= 1) return;
521
+ lastSize.current = now;
522
+ running.current = [element.animate([{
523
+ width: `${to.width}px`,
524
+ height: `${to.height}px`
525
+ }, {
526
+ width: `${now.width}px`,
527
+ height: `${now.height}px`
528
+ }], {
529
+ id: MORPH,
530
+ duration: timings.change,
531
+ easing: "ease-in-out"
532
+ })];
480
533
  }).catch(() => {});
481
534
  }, [
482
535
  rootRef,
@@ -574,6 +627,7 @@ exports.PlacementWatch = PlacementWatch;
574
627
  exports.animateMove = animateMove;
575
628
  exports.cancel = cancel;
576
629
  exports.captureSwap = captureSwap;
630
+ exports.onCopy = onCopy;
577
631
  exports.planExit = planExit;
578
632
  exports.planMorph = planMorph;
579
633
  exports.planSwap = planSwap;
@@ -187,6 +187,44 @@ const readSize = (element) => ({
187
187
  width: element.offsetWidth,
188
188
  height: element.offsetHeight
189
189
  });
190
+ /**
191
+ * Its size once what's changing inside it has got there: a label folding as a panel closes is still
192
+ * open when the pill measures where it's going (a transition). Measured then on a copy laid out with
193
+ * no transitions, so the pill goes there in one motion, instead of shrinking, then jumping (or
194
+ * settling in a second step) as the label folds.
195
+ */
196
+ function readSettledSize(element) {
197
+ if (!(typeof CSSTransition !== "undefined" && (element.getAnimations?.({ subtree: true }) ?? []).some((animation) => animation instanceof CSSTransition && animation.effect instanceof KeyframeEffect && animation.effect.target !== element)) || element.tagName === "DIALOG") return readSize(element);
198
+ return onCopy(element, readSize, (copy) => {
199
+ const actions = element.querySelectorAll("[data-mp=\"action\"]");
200
+ copy.querySelectorAll("[data-mp=\"action\"]").forEach((action, index) => {
201
+ if (actions[index]?.matches(":hover, :focus-visible")) action.setAttribute("data-pointed", "");
202
+ });
203
+ });
204
+ }
205
+ /**
206
+ * A copy of the pill laid out next to it as it ends up: hidden, with no transitions nor animations
207
+ * (`[data-mp-measure]`), sharing no id or radio group with it. `read` measures it, then it's gone.
208
+ */
209
+ function onCopy(element, read, prepare) {
210
+ const copy = element.cloneNode(true);
211
+ copy.querySelectorAll("[id], [name]").forEach((node) => {
212
+ node.removeAttribute("id");
213
+ node.removeAttribute("name");
214
+ });
215
+ copy.removeAttribute("id");
216
+ copy.setAttribute("data-mp-measure", "");
217
+ copy.inert = true;
218
+ copy.style.visibility = "hidden";
219
+ copy.style.pointerEvents = "none";
220
+ prepare?.(copy);
221
+ element.after(copy);
222
+ try {
223
+ return read(copy);
224
+ } finally {
225
+ copy.remove();
226
+ }
227
+ }
190
228
  /** Stops the pill's animations with this id (and the ones in it). Returns whether anything ran. */
191
229
  function cancel(element, id, subtree) {
192
230
  let cancelled = false;
@@ -442,7 +480,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
442
480
  const from = stopMorphs(element) || running.current.length > 0 ? current : lastSize.current;
443
481
  running.current.forEach((animation) => animation.cancel());
444
482
  running.current = [];
445
- const to = readSize(element);
483
+ const to = readSettledSize(element);
446
484
  lastSize.current = to;
447
485
  if (!from) return;
448
486
  const panel = readPanel(element);
@@ -476,7 +514,22 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
476
514
  if (animations.length === 0) return;
477
515
  running.current = animations;
478
516
  animations[0].finished.then(() => {
479
- if (running.current === animations) running.current = [];
517
+ if (running.current !== animations) return;
518
+ running.current = [];
519
+ const now = readSize(element);
520
+ if (CLOSED.has(presence) || Math.abs(now.width - to.width) <= 1 && Math.abs(now.height - to.height) <= 1) return;
521
+ lastSize.current = now;
522
+ running.current = [element.animate([{
523
+ width: `${to.width}px`,
524
+ height: `${to.height}px`
525
+ }, {
526
+ width: `${now.width}px`,
527
+ height: `${now.height}px`
528
+ }], {
529
+ id: MORPH,
530
+ duration: timings.change,
531
+ easing: "ease-in-out"
532
+ })];
480
533
  }).catch(() => {});
481
534
  }, [
482
535
  rootRef,
@@ -565,4 +618,4 @@ var PlacementWatch = class extends Component {
565
618
  }
566
619
  };
567
620
 
568
- export { EASING, EXIT, ExitWatch, MORPH, MOVE, PlacementWatch, animateMove, cancel, captureSwap, planExit, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
621
+ export { EASING, EXIT, ExitWatch, MORPH, MOVE, PlacementWatch, animateMove, cancel, captureSwap, onCopy, planExit, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
@@ -5,7 +5,9 @@ const require_glyphs = require("./glyphs.js");
5
5
  const require_send = require("./send.js");
6
6
  const require_FormContext = require("./FormContext.js");
7
7
  const require_keyboard = require("./keyboard.js");
8
+ const require_room = require("./room.js");
8
9
  const require_PillContext = require("../Pill/PillContext.js");
10
+ const require_react = require("../utils/react.js");
9
11
  let react_jsx_runtime = require("react/jsx-runtime");
10
12
 
11
13
  // src/PillForm/PillForm.tsx
@@ -24,11 +26,14 @@ const PillForm = (0, react.forwardRef)(function PillForm({ look, layout, errors
24
26
  errors,
25
27
  isBusy
26
28
  ]);
29
+ const formRef = (0, react.useRef)(null);
30
+ require_room.useFormRoom(formRef);
31
+ const refs = (0, react.useMemo)(() => require_react.mergeRefs(ref, formRef), [ref]);
27
32
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_FormContext.FormContext.Provider, {
28
33
  value: context,
29
34
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("form", {
30
35
  ...rest,
31
- ref,
36
+ ref: refs,
32
37
  "data-mp": "form",
33
38
  "data-look": look,
34
39
  "data-failed": sending.failure ? "" : void 0,
@@ -1,11 +1,13 @@
1
1
  "use client";
2
2
 
3
- import { forwardRef, useMemo } from "react";
3
+ import { forwardRef, useMemo, useRef } from "react";
4
4
  import { AlertGlyph } from "./glyphs.mjs";
5
5
  import { useSend } from "./send.mjs";
6
6
  import { FormContext } from "./FormContext.mjs";
7
7
  import { useKeyboard } from "./keyboard.mjs";
8
+ import { useFormRoom } from "./room.mjs";
8
9
  import { useInternalPill } from "../Pill/PillContext.mjs";
10
+ import { mergeRefs } from "../utils/react.mjs";
9
11
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
10
12
 
11
13
  // src/PillForm/PillForm.tsx
@@ -24,11 +26,14 @@ const PillForm = forwardRef(function PillForm({ look, layout, errors = "inline",
24
26
  errors,
25
27
  isBusy
26
28
  ]);
29
+ const formRef = useRef(null);
30
+ useFormRoom(formRef);
31
+ const refs = useMemo(() => mergeRefs(ref, formRef), [ref]);
27
32
  return /* @__PURE__ */ jsx(FormContext.Provider, {
28
33
  value: context,
29
34
  children: /* @__PURE__ */ jsx("form", {
30
35
  ...rest,
31
- ref,
36
+ ref: refs,
32
37
  "data-mp": "form",
33
38
  "data-look": look,
34
39
  "data-failed": sending.failure ? "" : void 0,
@@ -112,7 +112,18 @@ const FORMATS = {
112
112
  timeStyle: "short"
113
113
  }
114
114
  };
115
- /** A value, as it's written there. */
115
+ /**
116
+ * Starts with a capital, as a label does: some languages write their days and
117
+ * months in lowercase ("jeudi 24 décembre", "décembre 2026"), which reads as
118
+ * a broken label once it stands alone in a field or a calendar.
119
+ */
120
+ function asLabel(text, locale) {
121
+ const first = text.codePointAt(0);
122
+ if (first === void 0) return text;
123
+ const letter = String.fromCodePoint(first);
124
+ return letter.toLocaleUpperCase(locale) + text.slice(letter.length);
125
+ }
126
+ /** A value, as it's written there, starting with a capital. */
116
127
  function formatValue(value, type, locale, options) {
117
128
  if (!value) return "";
118
129
  const formatter = new Intl.DateTimeFormat(locale, {
@@ -127,13 +138,13 @@ function formatValue(value, type, locale, options) {
127
138
  if (type === "week") {
128
139
  const monday = parseDay(day);
129
140
  const sunday = parseDay(addDays(day, 6));
130
- return monday && sunday ? formatter.formatRange(monday, sunday) : value;
141
+ return monday && sunday ? asLabel(formatter.formatRange(monday, sunday), locale) : value;
131
142
  }
132
143
  const date = parseDay(day || "2000-01-01");
133
144
  if (!date || !day && type !== "time") return value;
134
145
  const [hours, minutes] = timeOf(value, type).split(":").map(Number);
135
146
  date.setUTCHours(hours || 0, minutes || 0);
136
- return formatter.format(date);
147
+ return asLabel(formatter.format(date), locale);
137
148
  }
138
149
 
139
150
  exports.addDays = addDays;
@@ -112,7 +112,18 @@ const FORMATS = {
112
112
  timeStyle: "short"
113
113
  }
114
114
  };
115
- /** A value, as it's written there. */
115
+ /**
116
+ * Starts with a capital, as a label does: some languages write their days and
117
+ * months in lowercase ("jeudi 24 décembre", "décembre 2026"), which reads as
118
+ * a broken label once it stands alone in a field or a calendar.
119
+ */
120
+ function asLabel(text, locale) {
121
+ const first = text.codePointAt(0);
122
+ if (first === void 0) return text;
123
+ const letter = String.fromCodePoint(first);
124
+ return letter.toLocaleUpperCase(locale) + text.slice(letter.length);
125
+ }
126
+ /** A value, as it's written there, starting with a capital. */
116
127
  function formatValue(value, type, locale, options) {
117
128
  if (!value) return "";
118
129
  const formatter = new Intl.DateTimeFormat(locale, {
@@ -127,13 +138,13 @@ function formatValue(value, type, locale, options) {
127
138
  if (type === "week") {
128
139
  const monday = parseDay(day);
129
140
  const sunday = parseDay(addDays(day, 6));
130
- return monday && sunday ? formatter.formatRange(monday, sunday) : value;
141
+ return monday && sunday ? asLabel(formatter.formatRange(monday, sunday), locale) : value;
131
142
  }
132
143
  const date = parseDay(day || "2000-01-01");
133
144
  if (!date || !day && type !== "time") return value;
134
145
  const [hours, minutes] = timeOf(value, type).split(":").map(Number);
135
146
  date.setUTCHours(hours || 0, minutes || 0);
136
- return formatter.format(date);
147
+ return asLabel(formatter.format(date), locale);
137
148
  }
138
149
 
139
150
  export { addDays, addMonths, dayOf, firstDayOfWeek, formatDay, formatValue, isDayOutside, isOutside, localFirstDay, monthGrid, monthOf, parseDay, timeOf, toValue, today, usesTwelveHours, weekOf };
@@ -0,0 +1,46 @@
1
+ "use client";
2
+
3
+ let react = require("react");
4
+
5
+ // src/PillForm/room.ts
6
+ /** A field narrower than this has no room to type in, in rem */
7
+ const LEAST = 9;
8
+ function useFormRoom(formRef) {
9
+ (0, react.useEffect)(() => {
10
+ const form = formRef.current;
11
+ if (!form || form.parentElement?.getAttribute("data-mp") !== "pill") return;
12
+ let open = 0;
13
+ const update = () => {
14
+ const pill = form.parentElement;
15
+ const field = form.querySelector("[data-mp=\"field\"]");
16
+ const submit = form.querySelector("[data-mp=\"action\"][type=\"submit\"][data-expanded]");
17
+ if (!field || !submit) return form.removeAttribute("data-tight");
18
+ const least = LEAST * (Number.parseFloat(getComputedStyle(document.documentElement).fontSize) || 16);
19
+ const max = Number.parseFloat(getComputedStyle(pill).maxInlineSize);
20
+ let room = Number.isFinite(max) ? max - pill.offsetWidth : Number.POSITIVE_INFINITY;
21
+ const container = pill.parentElement;
22
+ if (pill.dataset.placement === "inline" && container) {
23
+ const style = getComputedStyle(container);
24
+ const inside = container.clientWidth - (Number.parseFloat(style.paddingLeft) || 0) - (Number.parseFloat(style.paddingRight) || 0);
25
+ room = Math.min(room, inside - pill.offsetWidth);
26
+ }
27
+ const could = field.offsetWidth + Math.max(0, room);
28
+ if (!form.hasAttribute("data-tight")) {
29
+ if (could >= least) return;
30
+ open = submit.offsetWidth;
31
+ form.setAttribute("data-tight", "");
32
+ } else if (could - Math.max(0, open - submit.offsetWidth) >= least + 1) form.removeAttribute("data-tight");
33
+ };
34
+ update();
35
+ const sizes = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(update);
36
+ sizes?.observe(form);
37
+ window.addEventListener("resize", update);
38
+ return () => {
39
+ sizes?.disconnect();
40
+ window.removeEventListener("resize", update);
41
+ form.removeAttribute("data-tight");
42
+ };
43
+ }, [formRef]);
44
+ }
45
+
46
+ exports.useFormRoom = useFormRoom;
@@ -0,0 +1,46 @@
1
+ "use client";
2
+
3
+ import { useEffect } from "react";
4
+
5
+ // src/PillForm/room.ts
6
+ /** A field narrower than this has no room to type in, in rem */
7
+ const LEAST = 9;
8
+ function useFormRoom(formRef) {
9
+ useEffect(() => {
10
+ const form = formRef.current;
11
+ if (!form || form.parentElement?.getAttribute("data-mp") !== "pill") return;
12
+ let open = 0;
13
+ const update = () => {
14
+ const pill = form.parentElement;
15
+ const field = form.querySelector("[data-mp=\"field\"]");
16
+ const submit = form.querySelector("[data-mp=\"action\"][type=\"submit\"][data-expanded]");
17
+ if (!field || !submit) return form.removeAttribute("data-tight");
18
+ const least = LEAST * (Number.parseFloat(getComputedStyle(document.documentElement).fontSize) || 16);
19
+ const max = Number.parseFloat(getComputedStyle(pill).maxInlineSize);
20
+ let room = Number.isFinite(max) ? max - pill.offsetWidth : Number.POSITIVE_INFINITY;
21
+ const container = pill.parentElement;
22
+ if (pill.dataset.placement === "inline" && container) {
23
+ const style = getComputedStyle(container);
24
+ const inside = container.clientWidth - (Number.parseFloat(style.paddingLeft) || 0) - (Number.parseFloat(style.paddingRight) || 0);
25
+ room = Math.min(room, inside - pill.offsetWidth);
26
+ }
27
+ const could = field.offsetWidth + Math.max(0, room);
28
+ if (!form.hasAttribute("data-tight")) {
29
+ if (could >= least) return;
30
+ open = submit.offsetWidth;
31
+ form.setAttribute("data-tight", "");
32
+ } else if (could - Math.max(0, open - submit.offsetWidth) >= least + 1) form.removeAttribute("data-tight");
33
+ };
34
+ update();
35
+ const sizes = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(update);
36
+ sizes?.observe(form);
37
+ window.addEventListener("resize", update);
38
+ return () => {
39
+ sizes?.disconnect();
40
+ window.removeEventListener("resize", update);
41
+ form.removeAttribute("data-tight");
42
+ };
43
+ }, [formRef]);
44
+ }
45
+
46
+ export { useFormRoom };
@@ -7,6 +7,7 @@ const require_PillPanel = require("../Pill/PillPanel.js");
7
7
  const require_matchPath = require("./matchPath.js");
8
8
  const require_room = require("./room.js");
9
9
  const require_react = require("../utils/react.js");
10
+ const require_PillContext = require("../Pill/PillContext.js");
10
11
  let react_jsx_runtime = require("react/jsx-runtime");
11
12
 
12
13
  // src/PillNavbar/PillNavbar.tsx
@@ -30,7 +31,8 @@ const isLink = (item) => item.href !== void 0;
30
31
  const PillNavbar = (0, react.forwardRef)(function PillNavbar({ items, pathname, linkComponent, labels = "auto", "aria-label": ariaLabel = "Main", ...rest }, forwardedRef) {
31
32
  const navRef = (0, react.useRef)(null);
32
33
  const ref = (0, react.useMemo)(() => require_react.mergeRefs(navRef, forwardedRef), [forwardedRef]);
33
- require_room.useRoom(navRef, labels);
34
+ const presence = (0, react.useContext)(require_PillContext.PillContext)?.presence;
35
+ require_room.useRoom(navRef, labels, presence);
34
36
  const locationPathname = useLocationPathname(pathname === void 0);
35
37
  const links = items.filter(isLink);
36
38
  const active = links[require_matchPath.findActiveIndex(pathname ?? locationPathname, links)];
@@ -1,12 +1,13 @@
1
1
  "use client";
2
2
 
3
- import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
3
+ import { forwardRef, useContext, useEffect, useMemo, useRef, useState } from "react";
4
4
  import { PillActions } from "../Pill/parts.mjs";
5
5
  import PillAction from "../Pill/PillAction.mjs";
6
6
  import { PillPanelTrigger } from "../Pill/PillPanel.mjs";
7
7
  import { findActiveIndex } from "./matchPath.mjs";
8
8
  import { useRoom } from "./room.mjs";
9
9
  import { mergeRefs } from "../utils/react.mjs";
10
+ import { PillContext } from "../Pill/PillContext.mjs";
10
11
  import { jsx } from "react/jsx-runtime";
11
12
 
12
13
  // src/PillNavbar/PillNavbar.tsx
@@ -30,7 +31,8 @@ const isLink = (item) => item.href !== void 0;
30
31
  const PillNavbar = forwardRef(function PillNavbar({ items, pathname, linkComponent, labels = "auto", "aria-label": ariaLabel = "Main", ...rest }, forwardedRef) {
31
32
  const navRef = useRef(null);
32
33
  const ref = useMemo(() => mergeRefs(navRef, forwardedRef), [forwardedRef]);
33
- useRoom(navRef, labels);
34
+ const presence = useContext(PillContext)?.presence;
35
+ useRoom(navRef, labels, presence);
34
36
  const locationPathname = useLocationPathname(pathname === void 0);
35
37
  const links = items.filter(isLink);
36
38
  const active = links[findActiveIndex(pathname ?? locationPathname, links)];
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  let react = require("react");
4
+ const require_react = require("../utils/react.js");
4
5
 
5
6
  // src/PillNavbar/room.ts
6
7
  const ACTION = "[data-mp=\"action\"]";
@@ -9,12 +10,15 @@ const ACTION = "[data-mp=\"action\"]";
9
10
  * Removed by the caller. `free`: without the room its satellites take beside it (what they plan
10
11
  * with: measured within it, the pill would only seem to fit what they left it the time before).
11
12
  */
12
- function layOutCopy(pill, prepare = () => {}, free = false) {
13
+ function layOutCopy(pill, prepare = () => {}, free = false, presence) {
13
14
  const copy = pill.cloneNode(false);
14
15
  const side = copy.dataset.placement === "left" || copy.dataset.placement === "right";
15
16
  if (free && !side) copy.style.removeProperty("--_mp-shift-x");
17
+ const planned = copy.style.getPropertyValue("--_mp-room-x");
18
+ if (!free && !side && planned) copy.style.setProperty("--_mp-shift-x", planned);
16
19
  if (copy.dataset.presence === "rest" || copy.dataset.presence === "hidden") copy.dataset.presence = "compact";
17
- for (const child of pill.children) if (child.getAttribute("data-mp") !== "panel") copy.append(child.cloneNode(true));
20
+ if (presence !== void 0 && presence !== "rest" && presence !== "hidden") copy.dataset.presence = presence;
21
+ for (const child of pill.children) if (child.getAttribute("data-mp") !== "panel" && !child.hasAttribute("data-leaving")) copy.append(child.cloneNode(true));
18
22
  copy.querySelectorAll("[id], [name]").forEach((node) => {
19
23
  node.removeAttribute("id");
20
24
  node.removeAttribute("name");
@@ -41,14 +45,14 @@ function layOutCopy(pill, prepare = () => {}, free = false) {
41
45
  * even if it's folded). Laid out on a copy, removed before anything is
42
46
  * painted: the labels on screen may be moving, and the app's own styles count.
43
47
  */
44
- function fitsWith(navbar, item, { unfold = false } = {}) {
48
+ function fitsWith(navbar, item, { unfold = false, presence } = {}) {
45
49
  const pill = navbar.closest("[data-mp=\"pill\"]");
46
50
  if (!pill) return true;
47
51
  const index = item ? [...navbar.querySelectorAll(ACTION)].indexOf(item) : -1;
48
52
  const { copy, navbar: copied } = layOutCopy(pill, (laidOut) => {
49
53
  if (unfold) laidOut.removeAttribute("data-folded");
50
54
  if (index >= 0) laidOut.querySelectorAll(ACTION)[index]?.setAttribute("data-expanded", "");
51
- });
55
+ }, false, presence);
52
56
  const fits = !copied || isInside(copied, copy);
53
57
  copy.remove();
54
58
  return fits;
@@ -58,11 +62,26 @@ function fitsWith(navbar, item, { unfold = false } = {}) {
58
62
  * if it has no navbar that can fold them (`labels="current"`). The
59
63
  * satellites ask, to know whether an electron fits next to it.
60
64
  */
61
- function foldedWidth(pill) {
65
+ function foldedWidth(pill, { content = false } = {}) {
62
66
  const navbar = pill.querySelector("[data-mp=\"navbar\"]");
63
67
  if (!navbar || navbar.dataset.labels === "current") return pill.offsetWidth;
64
68
  const { copy } = layOutCopy(pill, (copied) => copied.setAttribute("data-folded", ""), true);
65
- const width = copy.offsetWidth;
69
+ const width = content ? Math.max(copy.offsetWidth, copy.scrollWidth + (Number.parseFloat(getComputedStyle(copy).paddingRight) || 0)) : copy.offsetWidth;
70
+ copy.remove();
71
+ return width;
72
+ }
73
+ /**
74
+ * How wide the pill is with its navbar's current page's label open, when that label folds by itself
75
+ * (`labels="auto"`); undefined otherwise. The satellites plan from it and from `foldedWidth`, never
76
+ * from the width the pill has at the moment: the label folds because of the room they take, and the
77
+ * room they take depended on whether it had folded (back and forth, every frame).
78
+ */
79
+ function unfoldedWidth(pill) {
80
+ const navbar = pill.querySelector("[data-mp=\"navbar\"]");
81
+ if (!navbar || navbar.dataset.labels !== "auto") return void 0;
82
+ const { copy } = layOutCopy(pill, (copied) => copied.removeAttribute("data-folded"), true);
83
+ const style = getComputedStyle(copy);
84
+ const width = Math.max(copy.offsetWidth, copy.scrollWidth + (Number.parseFloat(style.paddingRight) || 0));
66
85
  copy.remove();
67
86
  return width;
68
87
  }
@@ -80,12 +99,25 @@ function openSize(pill) {
80
99
  copy.remove();
81
100
  return size;
82
101
  }
83
- /** Whether the navbar ends before the pill's padding does. */
102
+ /**
103
+ * Whether the navbar ends before the pill's padding does, with what's beside it in the pill too (the
104
+ * page's actions after a divider): nothing of the pill's content runs past it. The navbar alone used
105
+ * to be measured against the whole pill, and squeezed by the actions next to it
106
+ */
84
107
  function isInside(navbar, pill) {
85
108
  const style = getComputedStyle(pill);
86
109
  const room = pill.clientWidth - (parseFloat(style.paddingLeft) || 0) - (parseFloat(style.paddingRight) || 0);
87
- return navbar.scrollWidth <= room + 1;
110
+ const beside = [...pill.children].filter((child) => child !== navbar && !child.contains(navbar) && child.getAttribute("data-mp") !== "panel");
111
+ const others = beside.reduce((sum, child) => sum + child.offsetWidth, 0);
112
+ const gaps = beside.length * (parseFloat(style.columnGap) || 0);
113
+ return navbar.scrollWidth + others + gaps <= room + 1;
88
114
  }
115
+ /**
116
+ * What a measurement on a copy of the pill depends on: the screen's size, the page's root (a theme, a
117
+ * font size set there) and the pill's markup, which the copy is made from. Not its animations: a
118
+ * copy has none, so a pill morphing or travelling gets the same answer at every frame
119
+ */
120
+ const inputsOf = (pill) => `${window.innerWidth}x${window.innerHeight} ${document.documentElement.className} ${document.documentElement.getAttribute("style") ?? ""} ${pill.outerHTML}`;
89
121
  const isHorizontal = (navbar) => {
90
122
  const pill = navbar.closest("[data-mp=\"pill\"]");
91
123
  return pill?.dataset.orientation === "horizontal" && pill.dataset.unstyled === void 0;
@@ -95,7 +127,16 @@ const isHorizontal = (navbar) => {
95
127
  * the keyboard) has no room for its label next to the open ones, and
96
128
  * `data-folded` while the current page's label folds at rest (`labels`).
97
129
  */
98
- function useRoom(ref, labels = "auto") {
130
+ function useRoom(ref, labels = "auto", presence) {
131
+ require_react.useIsomorphicLayoutEffect(() => {
132
+ const navbar = ref.current;
133
+ if (!navbar || labels !== "auto" || presence === void 0) return;
134
+ if (isHorizontal(navbar) && !fitsWith(navbar, null, {
135
+ unfold: true,
136
+ presence
137
+ })) navbar.setAttribute("data-folded", "");
138
+ else navbar.removeAttribute("data-folded");
139
+ }, [presence]);
99
140
  (0, react.useEffect)(() => {
100
141
  const navbar = ref.current;
101
142
  if (!navbar) return;
@@ -109,11 +150,21 @@ function useRoom(ref, labels = "auto") {
109
150
  }
110
151
  const pill = navbar.closest("[data-mp=\"pill\"]");
111
152
  let frame = 0;
153
+ let decided = "";
154
+ const check = () => {
155
+ if (pill && inputsOf(pill) === decided) return;
156
+ fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true }));
157
+ decided = pill ? inputsOf(pill) : "";
158
+ };
112
159
  const update = () => {
113
160
  cancelAnimationFrame(frame);
114
- frame = requestAnimationFrame(() => fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true })));
161
+ frame = requestAnimationFrame(check);
162
+ };
163
+ const fontsLoaded = () => {
164
+ decided = "";
165
+ update();
115
166
  };
116
- fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true }));
167
+ check();
117
168
  const mutations = new MutationObserver(update);
118
169
  mutations.observe(navbar, {
119
170
  subtree: true,
@@ -131,11 +182,13 @@ function useRoom(ref, labels = "auto") {
131
182
  const sizes = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(update);
132
183
  if (pill) sizes?.observe(pill);
133
184
  window.addEventListener("resize", update);
185
+ document.fonts?.addEventListener?.("loadingdone", fontsLoaded);
134
186
  return () => {
135
187
  cancelAnimationFrame(frame);
136
188
  mutations.disconnect();
137
189
  sizes?.disconnect();
138
190
  window.removeEventListener("resize", update);
191
+ document.fonts?.removeEventListener?.("loadingdone", fontsLoaded);
139
192
  fold(false);
140
193
  };
141
194
  }, [ref, labels]);
@@ -174,4 +227,5 @@ function useRoom(ref, labels = "auto") {
174
227
  exports.fitsWith = fitsWith;
175
228
  exports.foldedWidth = foldedWidth;
176
229
  exports.openSize = openSize;
230
+ exports.unfoldedWidth = unfoldedWidth;
177
231
  exports.useRoom = useRoom;