magic-pill 9.2.0 → 9.4.0-next.1

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 (96) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/MIGRATING.md +2 -0
  3. package/README.md +158 -17
  4. package/dist/MagicPill/MagicPill.d.mts +43 -1
  5. package/dist/MagicPill/MagicPill.d.ts +43 -1
  6. package/dist/MagicPill/MagicPill.js +53 -6
  7. package/dist/MagicPill/MagicPill.mjs +56 -9
  8. package/dist/MagicPill/flag.js +1 -59
  9. package/dist/MagicPill/flag.mjs +1 -58
  10. package/dist/MagicPill/useChoreography.js +2 -1
  11. package/dist/MagicPill/useChoreography.mjs +3 -2
  12. package/dist/Pill/PillProgress.d.mts +12 -0
  13. package/dist/Pill/PillProgress.d.ts +12 -0
  14. package/dist/Pill/PillProgress.js +42 -0
  15. package/dist/Pill/PillProgress.mjs +42 -0
  16. package/dist/Pill/goo.js +353 -0
  17. package/dist/Pill/goo.mjs +347 -0
  18. package/dist/Pill/modal.js +2 -1
  19. package/dist/Pill/modal.mjs +3 -2
  20. package/dist/Pill/morph.js +7 -4
  21. package/dist/Pill/morph.mjs +8 -5
  22. package/dist/Pill/travel.js +2 -1
  23. package/dist/Pill/travel.mjs +3 -2
  24. package/dist/Pill/zone.js +3 -2
  25. package/dist/Pill/zone.mjs +4 -3
  26. package/dist/PillForm/PillSubmit.js +1 -1
  27. package/dist/PillForm/PillSubmit.mjs +3 -3
  28. package/dist/PillForm/popover.js +2 -1
  29. package/dist/PillForm/popover.mjs +3 -2
  30. package/dist/PillNavbar/PillNavbar.d.mts +8 -0
  31. package/dist/PillNavbar/PillNavbar.d.ts +8 -0
  32. package/dist/PillNavbar/PillNavbar.js +3 -2
  33. package/dist/PillNavbar/PillNavbar.mjs +3 -2
  34. package/dist/PillNavbar/room.d.mts +3 -0
  35. package/dist/PillNavbar/room.d.ts +2 -0
  36. package/dist/PillNavbar/room.js +89 -17
  37. package/dist/PillNavbar/room.mjs +89 -18
  38. package/dist/PillOptics/PillOptics.js +2 -1
  39. package/dist/PillOptics/PillOptics.mjs +3 -2
  40. package/dist/forms.css +2 -1
  41. package/dist/index.d.mts +30 -15
  42. package/dist/index.d.ts +30 -15
  43. package/dist/index.js +40 -12
  44. package/dist/index.mjs +40 -13
  45. package/dist/satellites/Actions.d.mts +12 -0
  46. package/dist/satellites/Actions.d.ts +11 -0
  47. package/dist/satellites/Actions.js +59 -0
  48. package/dist/satellites/Actions.mjs +56 -0
  49. package/dist/satellites/Orbitals.js +554 -0
  50. package/dist/satellites/Orbitals.mjs +554 -0
  51. package/dist/satellites/Satellites.js +400 -0
  52. package/dist/satellites/Satellites.mjs +400 -0
  53. package/dist/satellites/layout.js +156 -0
  54. package/dist/satellites/layout.mjs +156 -0
  55. package/dist/satellites/measure.js +25 -0
  56. package/dist/satellites/measure.mjs +24 -0
  57. package/dist/satellites/orbits.js +139 -0
  58. package/dist/satellites/orbits.mjs +136 -0
  59. package/dist/satellites/types.d.mts +54 -0
  60. package/dist/satellites/types.d.ts +54 -0
  61. package/dist/satellites.d.mts +5 -0
  62. package/dist/satellites.d.ts +5 -0
  63. package/dist/satellites.js +14 -0
  64. package/dist/satellites.mjs +12 -0
  65. package/dist/store/store.d.mts +103 -0
  66. package/dist/store/store.d.ts +103 -0
  67. package/dist/store/store.js +52 -1
  68. package/dist/store/store.mjs +52 -2
  69. package/dist/styles.css +174 -10
  70. package/dist/utils/react.js +8 -1
  71. package/dist/utils/react.mjs +8 -2
  72. package/package.json +12 -2
  73. package/dist/icons/ArrowIcon.d.mts +0 -4
  74. package/dist/icons/ArrowIcon.d.ts +0 -4
  75. package/dist/icons/BoxIcon.d.mts +0 -4
  76. package/dist/icons/BoxIcon.d.ts +0 -4
  77. package/dist/icons/CheckIcon.d.mts +0 -4
  78. package/dist/icons/CheckIcon.d.ts +0 -4
  79. package/dist/icons/CloseIcon.d.mts +0 -4
  80. package/dist/icons/CloseIcon.d.ts +0 -4
  81. package/dist/icons/CouponIcon.d.mts +0 -4
  82. package/dist/icons/CouponIcon.d.ts +0 -4
  83. package/dist/icons/DocumentIcon.d.mts +0 -4
  84. package/dist/icons/DocumentIcon.d.ts +0 -4
  85. package/dist/icons/HelpIcon.d.mts +0 -4
  86. package/dist/icons/HelpIcon.d.ts +0 -4
  87. package/dist/icons/InfoIcon.d.mts +0 -4
  88. package/dist/icons/InfoIcon.d.ts +0 -4
  89. package/dist/icons/MailIcon.d.mts +0 -4
  90. package/dist/icons/MailIcon.d.ts +0 -4
  91. package/dist/icons/PhoneIcon.d.mts +0 -4
  92. package/dist/icons/PhoneIcon.d.ts +0 -4
  93. package/dist/icons/PlusIcon.d.mts +0 -4
  94. package/dist/icons/PlusIcon.d.ts +0 -4
  95. package/dist/icons/UndoIcon.d.mts +0 -4
  96. package/dist/icons/UndoIcon.d.ts +0 -4
@@ -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/Pill/modal.ts
6
7
  /**
@@ -31,7 +32,7 @@ function useModal(rootRef, isOverlay, presence, lastSize) {
31
32
  }
32
33
  if (presence === "rest" || !dialog.contains(document.activeElement)) dialog.focus();
33
34
  };
34
- (0, react.useLayoutEffect)(() => {
35
+ require_react.useIsomorphicLayoutEffect(() => {
35
36
  show();
36
37
  const dialog = rootRef.current;
37
38
  if (!isOverlay || isVisible || !dialog?.open) return;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
- import { useEffect, useLayoutEffect } from "react";
3
+ import { useEffect } from "react";
4
+ import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
4
5
 
5
6
  // src/Pill/modal.ts
6
7
  /**
@@ -31,7 +32,7 @@ function useModal(rootRef, isOverlay, presence, lastSize) {
31
32
  }
32
33
  if (presence === "rest" || !dialog.contains(document.activeElement)) dialog.focus();
33
34
  };
34
- useLayoutEffect(() => {
35
+ useIsomorphicLayoutEffect(() => {
35
36
  show();
36
37
  const dialog = rootRef.current;
37
38
  if (!isOverlay || isVisible || !dialog?.open) return;
@@ -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
  const require_timing = require("../utils/timing.js");
5
6
 
6
7
  // src/Pill/morph.ts
@@ -132,7 +133,7 @@ function planExit(start, timings, exit) {
132
133
  easing: "ease-in"
133
134
  },
134
135
  {
135
- scale: "0",
136
+ scale: start.to ?? "0",
136
137
  visibility: "visible"
137
138
  }
138
139
  ],
@@ -165,9 +166,11 @@ function playExit(element, timings) {
165
166
  const content = fadingContent(element);
166
167
  const opacity = content.length > 0 ? getComputedStyle(content[0]).opacity : "0";
167
168
  const exit = require_timing.parseDuration(style.getPropertyValue("--_mp-exit"), 300);
169
+ const to = style.getPropertyValue("--_mp-exit-scale").trim() || void 0;
168
170
  const plan = planExit({
169
171
  scale,
170
- content: Number(opacity) > 0 ? opacity : void 0
172
+ content: Number(opacity) > 0 ? opacity : void 0,
173
+ to
171
174
  }, timings, exit);
172
175
  if (!plan) return [];
173
176
  const animations = [element.animate(plan.pill.keyframes, plan.pill.options)];
@@ -403,7 +406,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
403
406
  const lastPresence = (0, react.useRef)(presence);
404
407
  const running = (0, react.useRef)([]);
405
408
  const openPanel = (0, react.useRef)(void 0);
406
- (0, react.useLayoutEffect)(() => {
409
+ require_react.useIsomorphicLayoutEffect(() => {
407
410
  const element = rootRef.current;
408
411
  if (!element || typeof ResizeObserver === "undefined") return;
409
412
  const observer = new ResizeObserver((entries) => {
@@ -422,7 +425,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
422
425
  running.current.forEach((animation) => animation.cancel());
423
426
  };
424
427
  }, [rootRef, lastSize]);
425
- (0, react.useLayoutEffect)(() => {
428
+ require_react.useIsomorphicLayoutEffect(() => {
426
429
  const element = rootRef.current;
427
430
  const previous = lastPresence.current;
428
431
  lastPresence.current = presence;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
- import { Component, useLayoutEffect, useRef } from "react";
3
+ import { Component, useRef } from "react";
4
+ import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
4
5
  import { parseDuration, prefersReducedMotion } from "../utils/timing.mjs";
5
6
 
6
7
  // src/Pill/morph.ts
@@ -132,7 +133,7 @@ function planExit(start, timings, exit) {
132
133
  easing: "ease-in"
133
134
  },
134
135
  {
135
- scale: "0",
136
+ scale: start.to ?? "0",
136
137
  visibility: "visible"
137
138
  }
138
139
  ],
@@ -165,9 +166,11 @@ function playExit(element, timings) {
165
166
  const content = fadingContent(element);
166
167
  const opacity = content.length > 0 ? getComputedStyle(content[0]).opacity : "0";
167
168
  const exit = parseDuration(style.getPropertyValue("--_mp-exit"), 300);
169
+ const to = style.getPropertyValue("--_mp-exit-scale").trim() || void 0;
168
170
  const plan = planExit({
169
171
  scale,
170
- content: Number(opacity) > 0 ? opacity : void 0
172
+ content: Number(opacity) > 0 ? opacity : void 0,
173
+ to
171
174
  }, timings, exit);
172
175
  if (!plan) return [];
173
176
  const animations = [element.animate(plan.pill.keyframes, plan.pill.options)];
@@ -403,7 +406,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
403
406
  const lastPresence = useRef(presence);
404
407
  const running = useRef([]);
405
408
  const openPanel = useRef(void 0);
406
- useLayoutEffect(() => {
409
+ useIsomorphicLayoutEffect(() => {
407
410
  const element = rootRef.current;
408
411
  if (!element || typeof ResizeObserver === "undefined") return;
409
412
  const observer = new ResizeObserver((entries) => {
@@ -422,7 +425,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
422
425
  running.current.forEach((animation) => animation.cancel());
423
426
  };
424
427
  }, [rootRef, lastSize]);
425
- useLayoutEffect(() => {
428
+ useIsomorphicLayoutEffect(() => {
426
429
  const element = rootRef.current;
427
430
  const previous = lastPresence.current;
428
431
  lastPresence.current = presence;
@@ -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
  const require_morph = require("./morph.js");
5
6
  const require_timing = require("../utils/timing.js");
6
7
 
@@ -35,7 +36,7 @@ function useTravel(rootRef, { placement, orientation, presence, enabled }) {
35
36
  if (enabled && travelsThroughCircle(shown, target) && !CLOSED.has(presence) && !require_timing.prefersReducedMotion()) setPhase("closing");
36
37
  else setShown(target);
37
38
  }
38
- (0, react.useLayoutEffect)(() => {
39
+ require_react.useIsomorphicLayoutEffect(() => {
39
40
  if (phase !== "closing") return;
40
41
  const root = rootRef.current;
41
42
  const active = document.activeElement;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
- import { useEffect, useLayoutEffect, useRef, useState } from "react";
3
+ import { useEffect, useRef, useState } from "react";
4
+ import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
4
5
  import { readMorphTimings } from "./morph.mjs";
5
6
  import { prefersReducedMotion } from "../utils/timing.mjs";
6
7
 
@@ -35,7 +36,7 @@ function useTravel(rootRef, { placement, orientation, presence, enabled }) {
35
36
  if (enabled && travelsThroughCircle(shown, target) && !CLOSED.has(presence) && !prefersReducedMotion()) setPhase("closing");
36
37
  else setShown(target);
37
38
  }
38
- useLayoutEffect(() => {
39
+ useIsomorphicLayoutEffect(() => {
39
40
  if (phase !== "closing") return;
40
41
  const root = rootRef.current;
41
42
  const active = document.activeElement;
package/dist/Pill/zone.js CHANGED
@@ -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
  const require_morph = require("./morph.js");
5
6
  const require_timing = require("../utils/timing.js");
6
7
 
@@ -97,7 +98,7 @@ function useZone(rootRef, zone, presence) {
97
98
  size: 0,
98
99
  frozen: presence === "panel"
99
100
  });
100
- (0, react.useLayoutEffect)(() => {
101
+ require_react.useIsomorphicLayoutEffect(() => {
101
102
  const rung = rungRef.current;
102
103
  const element = rootRef.current;
103
104
  if (!zone || !element) return;
@@ -124,7 +125,7 @@ function useZone(rootRef, zone, presence) {
124
125
  arrange(zone, () => travel(element));
125
126
  };
126
127
  }, [rootRef, zone]);
127
- (0, react.useLayoutEffect)(() => {
128
+ require_react.useIsomorphicLayoutEffect(() => {
128
129
  const rung = rungRef.current;
129
130
  const previous = rung.presence;
130
131
  const { element } = rung;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
- import { useLayoutEffect, useRef } from "react";
3
+ import { useRef } from "react";
4
+ import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
4
5
  import { EASING, MORPH, cancel, readMorphTimings } from "./morph.mjs";
5
6
  import { parseDuration, prefersReducedMotion } from "../utils/timing.mjs";
6
7
 
@@ -97,7 +98,7 @@ function useZone(rootRef, zone, presence) {
97
98
  size: 0,
98
99
  frozen: presence === "panel"
99
100
  });
100
- useLayoutEffect(() => {
101
+ useIsomorphicLayoutEffect(() => {
101
102
  const rung = rungRef.current;
102
103
  const element = rootRef.current;
103
104
  if (!zone || !element) return;
@@ -124,7 +125,7 @@ function useZone(rootRef, zone, presence) {
124
125
  arrange(zone, () => travel(element));
125
126
  };
126
127
  }, [rootRef, zone]);
127
- useLayoutEffect(() => {
128
+ useIsomorphicLayoutEffect(() => {
128
129
  const rung = rungRef.current;
129
130
  const previous = rung.presence;
130
131
  const { element } = rung;
@@ -33,7 +33,7 @@ function useBusyLabel(busyLabel, interval, isBusy) {
33
33
  /** One label replacing another: the action widens or narrows with it. */
34
34
  function useLabelMorph(buttonRef, label) {
35
35
  const previous = (0, react.useRef)(null);
36
- (0, react.useLayoutEffect)(() => {
36
+ require_react.useIsomorphicLayoutEffect(() => {
37
37
  const text = buttonRef.current?.querySelector("[data-mp=\"label\"] > span");
38
38
  if (!text) return;
39
39
  text.getAnimations?.().forEach((animation) => animation.cancel());
@@ -1,10 +1,10 @@
1
1
  "use client";
2
2
 
3
- import { forwardRef, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
3
+ import { forwardRef, useContext, useEffect, useMemo, useRef, useState } from "react";
4
4
  import { FormContext } from "./FormContext.mjs";
5
5
  import PillAction from "../Pill/PillAction.mjs";
6
6
  import CheckIcon from "../icons/CheckIcon.mjs";
7
- import { mergeRefs } from "../utils/react.mjs";
7
+ import { mergeRefs, useIsomorphicLayoutEffect } from "../utils/react.mjs";
8
8
  import { prefersReducedMotion } from "../utils/timing.mjs";
9
9
  import { VISUALLY_HIDDEN } from "../utils/visuallyHidden.mjs";
10
10
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
@@ -33,7 +33,7 @@ function useBusyLabel(busyLabel, interval, isBusy) {
33
33
  /** One label replacing another: the action widens or narrows with it. */
34
34
  function useLabelMorph(buttonRef, label) {
35
35
  const previous = useRef(null);
36
- useLayoutEffect(() => {
36
+ useIsomorphicLayoutEffect(() => {
37
37
  const text = buttonRef.current?.querySelector("[data-mp=\"label\"] > span");
38
38
  if (!text) return;
39
39
  text.getAnimations?.().forEach((animation) => animation.cancel());
@@ -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/PillForm/popover.ts
6
7
  /** From the field, and from the screen's edges */
@@ -27,7 +28,7 @@ function placePopover(field, popover, viewport, isRtl) {
27
28
  * elsewhere closes it. Without popovers, it's shown in the page, under the field.
28
29
  */
29
30
  function usePopover(popoverRef, fieldRef, isOpen, onClose) {
30
- (0, react.useLayoutEffect)(() => {
31
+ require_react.useIsomorphicLayoutEffect(() => {
31
32
  const popover = popoverRef.current;
32
33
  const field = fieldRef.current;
33
34
  if (!isOpen || !popover || !field || !supportsPopover()) return;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
- import { useEffect, useLayoutEffect } from "react";
3
+ import { useEffect } from "react";
4
+ import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
4
5
 
5
6
  // src/PillForm/popover.ts
6
7
  /** From the field, and from the screen's edges */
@@ -27,7 +28,7 @@ function placePopover(field, popover, viewport, isRtl) {
27
28
  * elsewhere closes it. Without popovers, it's shown in the page, under the field.
28
29
  */
29
30
  function usePopover(popoverRef, fieldRef, isOpen, onClose) {
30
- useLayoutEffect(() => {
31
+ useIsomorphicLayoutEffect(() => {
31
32
  const popover = popoverRef.current;
32
33
  const field = fieldRef.current;
33
34
  if (!isOpen || !popover || !field || !supportsPopover()) return;
@@ -1,6 +1,7 @@
1
1
  import { ComponentPropsWithoutRef, MouseEvent, ReactNode } from "react";
2
2
  import { LinkComponent } from "../Pill/types.mjs";
3
3
  import { NavMatch } from "./matchPath.mjs";
4
+ import { NavLabels } from "./room.mjs";
4
5
  interface NavItemBase {
5
6
  label: string;
6
7
  icon?: ReactNode;
@@ -43,6 +44,13 @@ export interface PillNavbarProps extends Omit<ComponentPropsWithoutRef<"nav">, "
43
44
  */
44
45
  pathname?: string;
45
46
  linkComponent?: LinkComponent;
47
+ /**
48
+ * Which labels show at rest: the current page's ("current"), none
49
+ * ("closed"), or the current page's while the pill has room for it
50
+ * ("auto", the default: it folds next to an electron on a narrow screen).
51
+ * An item pointed at or focused shows its label in every case.
52
+ */
53
+ labels?: NavLabels;
46
54
  /** Default "Main". */
47
55
  "aria-label"?: string;
48
56
  }
@@ -1,6 +1,7 @@
1
1
  import { ComponentPropsWithoutRef, MouseEvent, ReactNode } from "react";
2
2
  import { LinkComponent } from "../Pill/types.js";
3
3
  import { NavMatch } from "./matchPath.js";
4
+ import { NavLabels } from "./room.js";
4
5
  interface NavItemBase {
5
6
  label: string;
6
7
  icon?: ReactNode;
@@ -43,6 +44,13 @@ export interface PillNavbarProps extends Omit<ComponentPropsWithoutRef<"nav">, "
43
44
  */
44
45
  pathname?: string;
45
46
  linkComponent?: LinkComponent;
47
+ /**
48
+ * Which labels show at rest: the current page's ("current"), none
49
+ * ("closed"), or the current page's while the pill has room for it
50
+ * ("auto", the default: it folds next to an electron on a narrow screen).
51
+ * An item pointed at or focused shows its label in every case.
52
+ */
53
+ labels?: NavLabels;
46
54
  /** Default "Main". */
47
55
  "aria-label"?: string;
48
56
  }
@@ -27,10 +27,10 @@ const isLink = (item) => item.href !== void 0;
27
27
  * A <nav> of icon links, actions and panel triggers. The active link shows its
28
28
  * label and gets `aria-current="page"`. Render it inside a <Pill>, or as the base layer of a <MagicPill>.
29
29
  */
30
- const PillNavbar = (0, react.forwardRef)(function PillNavbar({ items, pathname, linkComponent, "aria-label": ariaLabel = "Main", ...rest }, forwardedRef) {
30
+ const PillNavbar = (0, react.forwardRef)(function PillNavbar({ items, pathname, linkComponent, labels = "auto", "aria-label": ariaLabel = "Main", ...rest }, forwardedRef) {
31
31
  const navRef = (0, react.useRef)(null);
32
32
  const ref = (0, react.useMemo)(() => require_react.mergeRefs(navRef, forwardedRef), [forwardedRef]);
33
- require_room.useRoom(navRef);
33
+ require_room.useRoom(navRef, labels);
34
34
  const locationPathname = useLocationPathname(pathname === void 0);
35
35
  const links = items.filter(isLink);
36
36
  const active = links[require_matchPath.findActiveIndex(pathname ?? locationPathname, links)];
@@ -62,6 +62,7 @@ const PillNavbar = (0, react.forwardRef)(function PillNavbar({ items, pathname,
62
62
  ...rest,
63
63
  ref,
64
64
  "data-mp": "navbar",
65
+ "data-labels": labels,
65
66
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_parts.PillActions, { children: items.map(renderItem) })
66
67
  });
67
68
  });
@@ -27,10 +27,10 @@ const isLink = (item) => item.href !== void 0;
27
27
  * A <nav> of icon links, actions and panel triggers. The active link shows its
28
28
  * label and gets `aria-current="page"`. Render it inside a <Pill>, or as the base layer of a <MagicPill>.
29
29
  */
30
- const PillNavbar = forwardRef(function PillNavbar({ items, pathname, linkComponent, "aria-label": ariaLabel = "Main", ...rest }, forwardedRef) {
30
+ const PillNavbar = forwardRef(function PillNavbar({ items, pathname, linkComponent, labels = "auto", "aria-label": ariaLabel = "Main", ...rest }, forwardedRef) {
31
31
  const navRef = useRef(null);
32
32
  const ref = useMemo(() => mergeRefs(navRef, forwardedRef), [forwardedRef]);
33
- useRoom(navRef);
33
+ useRoom(navRef, labels);
34
34
  const locationPathname = useLocationPathname(pathname === void 0);
35
35
  const links = items.filter(isLink);
36
36
  const active = links[findActiveIndex(pathname ?? locationPathname, links)];
@@ -62,6 +62,7 @@ const PillNavbar = forwardRef(function PillNavbar({ items, pathname, linkCompone
62
62
  ...rest,
63
63
  ref,
64
64
  "data-mp": "navbar",
65
+ "data-labels": labels,
65
66
  children: /* @__PURE__ */ jsx(PillActions, { children: items.map(renderItem) })
66
67
  });
67
68
  });
@@ -0,0 +1,3 @@
1
+ import "react";
2
+ /** Which labels a navbar shows at rest. */
3
+ export type NavLabels = "current" | "closed" | "auto";
@@ -0,0 +1,2 @@
1
+ /** Which labels a navbar shows at rest. */
2
+ export type NavLabels = "current" | "closed" | "auto";
@@ -4,35 +4,62 @@ let react = require("react");
4
4
 
5
5
  // src/PillNavbar/room.ts
6
6
  const ACTION = "[data-mp=\"action\"]";
7
- /**
8
- * Whether the navbar still fits its pill with this item's label open. Laid out
9
- * on a copy, removed before anything is painted: the labels on screen may be
10
- * moving, and the app's own styles count.
11
- */
12
- function fitsWith(navbar, item) {
13
- const pill = navbar.closest("[data-mp=\"pill\"]");
14
- if (!pill) return true;
15
- const index = [...navbar.querySelectorAll(ACTION)].indexOf(item);
7
+ /** A copy of the pill with its navbar, laid out next to it, hidden, to measure. Removed by the caller. */
8
+ function layOutCopy(pill, prepare) {
16
9
  const copy = pill.cloneNode(false);
17
10
  for (const child of pill.children) if (child.getAttribute("data-mp") !== "panel") copy.append(child.cloneNode(true));
18
11
  copy.querySelectorAll("[id], [name]").forEach((node) => {
19
12
  node.removeAttribute("id");
20
13
  node.removeAttribute("name");
21
14
  });
22
- const copiedNavbar = copy.querySelector("[data-mp=\"navbar\"]");
23
- if (!copiedNavbar) return true;
24
- copiedNavbar.removeAttribute("data-tight");
25
- copiedNavbar.querySelectorAll(ACTION)[index]?.setAttribute("data-expanded", "");
15
+ const navbar = copy.querySelector("[data-mp=\"navbar\"]");
16
+ if (!navbar) return null;
17
+ navbar.removeAttribute("data-tight");
18
+ prepare(navbar);
26
19
  copy.querySelectorAll("[data-mp=\"navbar\"], [data-mp=\"actions\"], [data-mp=\"action\"]").forEach((node) => {
27
20
  node.style.flexShrink = "0";
28
21
  });
29
22
  copy.inert = true;
30
23
  copy.style.visibility = "hidden";
31
24
  pill.after(copy);
32
- const fits = isInside(copiedNavbar, copy);
33
- copy.remove();
25
+ return {
26
+ copy,
27
+ navbar
28
+ };
29
+ }
30
+ /**
31
+ * Whether the navbar still fits its pill with this item's label open (or as
32
+ * it is at rest, with no item; `unfold`: with the current page's label open
33
+ * even if it's folded). Laid out on a copy, removed before anything is
34
+ * painted: the labels on screen may be moving, and the app's own styles count.
35
+ */
36
+ function fitsWith(navbar, item, { unfold = false } = {}) {
37
+ const pill = navbar.closest("[data-mp=\"pill\"]");
38
+ if (!pill) return true;
39
+ const index = item ? [...navbar.querySelectorAll(ACTION)].indexOf(item) : -1;
40
+ const laidOut = layOutCopy(pill, (copied) => {
41
+ if (unfold) copied.removeAttribute("data-folded");
42
+ if (index >= 0) copied.querySelectorAll(ACTION)[index]?.setAttribute("data-expanded", "");
43
+ });
44
+ if (!laidOut) return true;
45
+ const fits = isInside(laidOut.navbar, laidOut.copy);
46
+ laidOut.copy.remove();
34
47
  return fits;
35
48
  }
49
+ /**
50
+ * How wide the pill is with all its navbar's labels folded, or its own width
51
+ * if it has no navbar that can fold them (`labels="current"`). The
52
+ * satellites ask, to know whether an electron fits next to it.
53
+ */
54
+ function foldedWidth(pill) {
55
+ const navbar = pill.querySelector("[data-mp=\"navbar\"]");
56
+ if (!navbar || navbar.dataset.labels === "current") return pill.offsetWidth;
57
+ const laidOut = layOutCopy(pill, (copied) => copied.setAttribute("data-folded", ""));
58
+ if (!laidOut) return pill.offsetWidth;
59
+ const width = laidOut.copy.offsetWidth;
60
+ laidOut.copy.remove();
61
+ return width;
62
+ }
36
63
  /** Whether the navbar ends before the pill's padding does. */
37
64
  function isInside(navbar, pill) {
38
65
  const style = getComputedStyle(pill);
@@ -45,9 +72,53 @@ const isHorizontal = (navbar) => {
45
72
  };
46
73
  /**
47
74
  * Writes `data-tight` on the navbar while the item pointed at (or focused with
48
- * the keyboard) has no room for its label next to the open ones.
75
+ * the keyboard) has no room for its label next to the open ones, and
76
+ * `data-folded` while the current page's label folds at rest (`labels`).
49
77
  */
50
- function useRoom(ref) {
78
+ function useRoom(ref, labels = "auto") {
79
+ (0, react.useEffect)(() => {
80
+ const navbar = ref.current;
81
+ if (!navbar) return;
82
+ const fold = (folded) => {
83
+ if (folded) navbar.setAttribute("data-folded", "");
84
+ else navbar.removeAttribute("data-folded");
85
+ };
86
+ if (labels !== "auto") {
87
+ fold(labels === "closed");
88
+ return () => fold(false);
89
+ }
90
+ const pill = navbar.closest("[data-mp=\"pill\"]");
91
+ let frame = 0;
92
+ const update = () => {
93
+ cancelAnimationFrame(frame);
94
+ frame = requestAnimationFrame(() => fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true })));
95
+ };
96
+ fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true }));
97
+ const mutations = new MutationObserver(update);
98
+ mutations.observe(navbar, {
99
+ subtree: true,
100
+ attributes: true,
101
+ attributeFilter: ["aria-current"]
102
+ });
103
+ if (pill) mutations.observe(pill, {
104
+ attributes: true,
105
+ attributeFilter: [
106
+ "style",
107
+ "data-orientation",
108
+ "data-presence"
109
+ ]
110
+ });
111
+ const sizes = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(update);
112
+ if (pill) sizes?.observe(pill);
113
+ window.addEventListener("resize", update);
114
+ return () => {
115
+ cancelAnimationFrame(frame);
116
+ mutations.disconnect();
117
+ sizes?.disconnect();
118
+ window.removeEventListener("resize", update);
119
+ fold(false);
120
+ };
121
+ }, [ref, labels]);
51
122
  (0, react.useEffect)(() => {
52
123
  const navbar = ref.current;
53
124
  if (!navbar) return;
@@ -81,4 +152,5 @@ function useRoom(ref) {
81
152
  }
82
153
 
83
154
  exports.fitsWith = fitsWith;
155
+ exports.foldedWidth = foldedWidth;
84
156
  exports.useRoom = useRoom;
@@ -4,35 +4,62 @@ import { useEffect } from "react";
4
4
 
5
5
  // src/PillNavbar/room.ts
6
6
  const ACTION = "[data-mp=\"action\"]";
7
- /**
8
- * Whether the navbar still fits its pill with this item's label open. Laid out
9
- * on a copy, removed before anything is painted: the labels on screen may be
10
- * moving, and the app's own styles count.
11
- */
12
- function fitsWith(navbar, item) {
13
- const pill = navbar.closest("[data-mp=\"pill\"]");
14
- if (!pill) return true;
15
- const index = [...navbar.querySelectorAll(ACTION)].indexOf(item);
7
+ /** A copy of the pill with its navbar, laid out next to it, hidden, to measure. Removed by the caller. */
8
+ function layOutCopy(pill, prepare) {
16
9
  const copy = pill.cloneNode(false);
17
10
  for (const child of pill.children) if (child.getAttribute("data-mp") !== "panel") copy.append(child.cloneNode(true));
18
11
  copy.querySelectorAll("[id], [name]").forEach((node) => {
19
12
  node.removeAttribute("id");
20
13
  node.removeAttribute("name");
21
14
  });
22
- const copiedNavbar = copy.querySelector("[data-mp=\"navbar\"]");
23
- if (!copiedNavbar) return true;
24
- copiedNavbar.removeAttribute("data-tight");
25
- copiedNavbar.querySelectorAll(ACTION)[index]?.setAttribute("data-expanded", "");
15
+ const navbar = copy.querySelector("[data-mp=\"navbar\"]");
16
+ if (!navbar) return null;
17
+ navbar.removeAttribute("data-tight");
18
+ prepare(navbar);
26
19
  copy.querySelectorAll("[data-mp=\"navbar\"], [data-mp=\"actions\"], [data-mp=\"action\"]").forEach((node) => {
27
20
  node.style.flexShrink = "0";
28
21
  });
29
22
  copy.inert = true;
30
23
  copy.style.visibility = "hidden";
31
24
  pill.after(copy);
32
- const fits = isInside(copiedNavbar, copy);
33
- copy.remove();
25
+ return {
26
+ copy,
27
+ navbar
28
+ };
29
+ }
30
+ /**
31
+ * Whether the navbar still fits its pill with this item's label open (or as
32
+ * it is at rest, with no item; `unfold`: with the current page's label open
33
+ * even if it's folded). Laid out on a copy, removed before anything is
34
+ * painted: the labels on screen may be moving, and the app's own styles count.
35
+ */
36
+ function fitsWith(navbar, item, { unfold = false } = {}) {
37
+ const pill = navbar.closest("[data-mp=\"pill\"]");
38
+ if (!pill) return true;
39
+ const index = item ? [...navbar.querySelectorAll(ACTION)].indexOf(item) : -1;
40
+ const laidOut = layOutCopy(pill, (copied) => {
41
+ if (unfold) copied.removeAttribute("data-folded");
42
+ if (index >= 0) copied.querySelectorAll(ACTION)[index]?.setAttribute("data-expanded", "");
43
+ });
44
+ if (!laidOut) return true;
45
+ const fits = isInside(laidOut.navbar, laidOut.copy);
46
+ laidOut.copy.remove();
34
47
  return fits;
35
48
  }
49
+ /**
50
+ * How wide the pill is with all its navbar's labels folded, or its own width
51
+ * if it has no navbar that can fold them (`labels="current"`). The
52
+ * satellites ask, to know whether an electron fits next to it.
53
+ */
54
+ function foldedWidth(pill) {
55
+ const navbar = pill.querySelector("[data-mp=\"navbar\"]");
56
+ if (!navbar || navbar.dataset.labels === "current") return pill.offsetWidth;
57
+ const laidOut = layOutCopy(pill, (copied) => copied.setAttribute("data-folded", ""));
58
+ if (!laidOut) return pill.offsetWidth;
59
+ const width = laidOut.copy.offsetWidth;
60
+ laidOut.copy.remove();
61
+ return width;
62
+ }
36
63
  /** Whether the navbar ends before the pill's padding does. */
37
64
  function isInside(navbar, pill) {
38
65
  const style = getComputedStyle(pill);
@@ -45,9 +72,53 @@ const isHorizontal = (navbar) => {
45
72
  };
46
73
  /**
47
74
  * Writes `data-tight` on the navbar while the item pointed at (or focused with
48
- * the keyboard) has no room for its label next to the open ones.
75
+ * the keyboard) has no room for its label next to the open ones, and
76
+ * `data-folded` while the current page's label folds at rest (`labels`).
49
77
  */
50
- function useRoom(ref) {
78
+ function useRoom(ref, labels = "auto") {
79
+ useEffect(() => {
80
+ const navbar = ref.current;
81
+ if (!navbar) return;
82
+ const fold = (folded) => {
83
+ if (folded) navbar.setAttribute("data-folded", "");
84
+ else navbar.removeAttribute("data-folded");
85
+ };
86
+ if (labels !== "auto") {
87
+ fold(labels === "closed");
88
+ return () => fold(false);
89
+ }
90
+ const pill = navbar.closest("[data-mp=\"pill\"]");
91
+ let frame = 0;
92
+ const update = () => {
93
+ cancelAnimationFrame(frame);
94
+ frame = requestAnimationFrame(() => fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true })));
95
+ };
96
+ fold(isHorizontal(navbar) && !fitsWith(navbar, null, { unfold: true }));
97
+ const mutations = new MutationObserver(update);
98
+ mutations.observe(navbar, {
99
+ subtree: true,
100
+ attributes: true,
101
+ attributeFilter: ["aria-current"]
102
+ });
103
+ if (pill) mutations.observe(pill, {
104
+ attributes: true,
105
+ attributeFilter: [
106
+ "style",
107
+ "data-orientation",
108
+ "data-presence"
109
+ ]
110
+ });
111
+ const sizes = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(update);
112
+ if (pill) sizes?.observe(pill);
113
+ window.addEventListener("resize", update);
114
+ return () => {
115
+ cancelAnimationFrame(frame);
116
+ mutations.disconnect();
117
+ sizes?.disconnect();
118
+ window.removeEventListener("resize", update);
119
+ fold(false);
120
+ };
121
+ }, [ref, labels]);
51
122
  useEffect(() => {
52
123
  const navbar = ref.current;
53
124
  if (!navbar) return;
@@ -80,4 +151,4 @@ function useRoom(ref) {
80
151
  }, [ref]);
81
152
  }
82
153
 
83
- export { fitsWith, useRoom };
154
+ export { fitsWith, foldedWidth, useRoom };