@hansenexus/hud 0.7.0 → 0.9.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 (77) hide show
  1. package/README.md +153 -14
  2. package/dist/{access-Bc709qpl.d.ts → access-CwPCPsv0.d.ts} +1 -1
  3. package/dist/auto/runtime.d.ts +9 -9
  4. package/dist/auto.d.ts +2 -2
  5. package/dist/auto.js +1 -1
  6. package/dist/cli.js +34 -1
  7. package/dist/{define-hud-DTv3njuU.d.ts → define-hud-B0aFXrcK.d.ts} +1 -1
  8. package/dist/{design-BXG1a4we.js → design-BIXY1-Ex.js} +26 -8
  9. package/dist/{force-state-panel-mu-txJnA.js → force-state-panel-D5oDydLs.js} +1 -1
  10. package/dist/{handler-DMzSTqyG.d.ts → handler-BmFhaA2D.d.ts} +1 -1
  11. package/dist/{index-C3wzZS3o.d.ts → index-5B7foZut.d.ts} +2 -2
  12. package/dist/{index-D-hI-DRJ.d.ts → index-B2KCKkN0.d.ts} +1 -1
  13. package/dist/index-B7qaEUgh.d.ts +44 -0
  14. package/dist/{index-CQXiuMZQ.d.ts → index-BGFyuJyN.d.ts} +1 -1
  15. package/dist/{index-Caohnz3B.d.ts → index-BeqWehuh.d.ts} +38 -10
  16. package/dist/{index-BJJoAXJ2.d.ts → index-BqKysxTq.d.ts} +1 -1
  17. package/dist/{index-DLCSLWW4.d.ts → index-D97Fnoie.d.ts} +2 -2
  18. package/dist/{index-BR5OB7DO.d.ts → index-DS0yT0c-.d.ts} +1 -1
  19. package/dist/{index-BNoPbSYD.d.ts → index-DvlCkGB1.d.ts} +1 -1
  20. package/dist/index.d.ts +3 -3
  21. package/dist/index.js +49 -196
  22. package/dist/lanes-panel-Disg54DY.js +395 -0
  23. package/dist/navigation-CX6AXm7H.js +176 -0
  24. package/dist/next/config.cjs +2 -0
  25. package/dist/next/config.d.cts +1 -0
  26. package/dist/next/config.d.ts +1 -0
  27. package/dist/next/config.js +2 -0
  28. package/dist/next.d.ts +4 -1
  29. package/dist/next.js +12 -1
  30. package/dist/obstacles-CCmRFTcZ.js +176 -0
  31. package/dist/plugins/a11y.d.ts +1 -1
  32. package/dist/plugins/access/server.d.ts +3 -3
  33. package/dist/plugins/access.d.ts +2 -2
  34. package/dist/plugins/agent/server.js +1 -1
  35. package/dist/plugins/agent.d.ts +2 -2
  36. package/dist/plugins/capture.d.ts +2 -2
  37. package/dist/plugins/data/server.d.ts +1 -1
  38. package/dist/plugins/data/server.js +1 -1
  39. package/dist/plugins/data.d.ts +1 -1
  40. package/dist/plugins/design/server.d.ts +55 -15
  41. package/dist/plugins/design/server.js +187 -122
  42. package/dist/plugins/design.d.ts +3 -3
  43. package/dist/plugins/design.js +1 -1
  44. package/dist/plugins/inspect.d.ts +1 -1
  45. package/dist/plugins/lanes/server.d.ts +165 -0
  46. package/dist/plugins/lanes/server.js +845 -0
  47. package/dist/plugins/lanes.d.ts +22 -0
  48. package/dist/plugins/lanes.js +30 -0
  49. package/dist/plugins/observe.d.ts +1 -1
  50. package/dist/plugins/ops/server.d.ts +1 -1
  51. package/dist/plugins/ops.d.ts +1 -1
  52. package/dist/plugins/routes/server.d.ts +1 -1
  53. package/dist/plugins/routes.d.ts +3 -3
  54. package/dist/plugins/routes.js +381 -52
  55. package/dist/plugins/seo.d.ts +1 -1
  56. package/dist/plugins/vitals.d.ts +1 -1
  57. package/dist/{plugins-BZT-1LBe.d.ts → plugins-pL2CoWzI.d.ts} +7 -0
  58. package/dist/same-origin-BHuZO1_H.js +36 -0
  59. package/dist/server.d.ts +2 -2
  60. package/dist/store.d.ts +1 -1
  61. package/dist/store.js +1 -1
  62. package/dist/types-BkrkOP3h.js +6 -0
  63. package/dist/types-Dazn3aWZ.d.ts +86 -0
  64. package/dist/{meta-C_G7MB5w.d.ts → types-Dc6JWZR7.d.ts} +6 -6
  65. package/dist/variant-bar-BzZ0ln-C.js +768 -0
  66. package/dist/variant-picks-Byc0hC8a.js +144 -0
  67. package/dist/variants-export-vEtZ-OGC.js +75 -0
  68. package/dist/{variants-panel-B6zWcknV.js → variants-panel-D23v5KIp.js} +71 -54
  69. package/dist/variants-types-BOsZ5fDz.js +65 -0
  70. package/dist/variants-types-VkW7vFZF.d.ts +139 -0
  71. package/dist/variants.d.ts +1 -1
  72. package/dist/variants.js +1 -1
  73. package/dist/vite.d.ts +2 -2
  74. package/package.json +2 -2
  75. package/dist/index-B1vHAY9p.d.ts +0 -20
  76. package/dist/same-origin-BGet22CC.js +0 -21
  77. package/dist/variants-types-CcRf119M.d.ts +0 -63
@@ -0,0 +1,768 @@
1
+ import { a as usePanels, n as useObstacles, r as useVisualViewport } from "./obstacles-CCmRFTcZ.js";
2
+ import { a as getVariantGroupsSnapshot, c as subscribeVariantGroups, i as getVariantGroupStore, n as VARIANT_PARAM, o as parseVariantParam, r as formatVariantParam, s as resolveVariant } from "./variant-groups-TIfO3NIj.js";
3
+ import { t as NOTE_MAX } from "./variants-types-BOsZ5fDz.js";
4
+ import { t as navigateTo } from "./navigation-CX6AXm7H.js";
5
+ import { o as usePicks, t as NO_PICK } from "./variant-picks-Byc0hC8a.js";
6
+ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
7
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
8
+ //#region src/plugins/design/variant-bar-guard.ts
9
+ /**
10
+ * Keeps the bar usable while the app shows a modal sheet. Modal libraries
11
+ * assume they own every press and every focus change outside their own
12
+ * content, and the HUD's host is outside:
13
+ *
14
+ * - a press there dismisses the sheet (a document or window listener),
15
+ * - focus there is pulled back (`focusin` / `focusout` on the document),
16
+ * - everything outside may be `aria-hidden` or `inert`,
17
+ * - the body has `pointer-events: none` (the bar sets its own `auto`).
18
+ *
19
+ * So the page never sees the bar's presses and focus changes, the bar's
20
+ * buttons never take focus from the sheet, and the two attributes are taken
21
+ * off the host again. The bar itself works on `click`, which stops at the host.
22
+ */
23
+ const PRESS_EVENTS = [
24
+ "pointerdown",
25
+ "pointerup",
26
+ "mousedown",
27
+ "mouseup",
28
+ "touchstart",
29
+ "touchend"
30
+ ];
31
+ const FOCUS_EVENTS = ["focusin", "focusout"];
32
+ /** Keystrokes in the note must not reach the app's shortcuts: it sees the host, not a text field. */
33
+ const KEY_EVENTS = [
34
+ "keyup",
35
+ "keypress",
36
+ "beforeinput",
37
+ "input"
38
+ ];
39
+ const isField = (target) => target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement;
40
+ /** Takes focus off whatever holds it, so a focus trap sees a plain blur and lets go. */
41
+ function releaseFocus(host) {
42
+ const active = document.activeElement;
43
+ if (active instanceof HTMLElement && active !== host && active !== document.body) active.blur();
44
+ }
45
+ /** Guards the bar's root element. Returns the cleanup. */
46
+ function guardBar(bar) {
47
+ const root = bar.getRootNode();
48
+ const host = root instanceof ShadowRoot ? root.host : null;
49
+ if (!host) return () => {};
50
+ const inBar = (event) => event.composedPath().includes(bar);
51
+ const onPress = (event) => {
52
+ if (!inBar(event)) return;
53
+ event.stopPropagation();
54
+ if (event.type !== "mousedown") return;
55
+ if (isField(event.composedPath()[0])) releaseFocus(host);
56
+ else event.preventDefault();
57
+ };
58
+ const stop = (event) => {
59
+ if (inBar(event)) event.stopPropagation();
60
+ };
61
+ const onKey = (event) => {
62
+ if (inBar(event) && isField(event.composedPath()[0])) event.stopPropagation();
63
+ };
64
+ for (const type of PRESS_EVENTS) window.addEventListener(type, onPress, true);
65
+ for (const type of FOCUS_EVENTS) window.addEventListener(type, stop, true);
66
+ host.addEventListener("click", stop);
67
+ for (const type of KEY_EVENTS) host.addEventListener(type, onKey);
68
+ const uncover = () => {
69
+ for (const name of ["inert", "aria-hidden"]) if (host.hasAttribute(name)) host.removeAttribute(name);
70
+ };
71
+ uncover();
72
+ const observer = new MutationObserver(uncover);
73
+ observer.observe(host, {
74
+ attributes: true,
75
+ attributeFilter: ["inert", "aria-hidden"]
76
+ });
77
+ return () => {
78
+ for (const type of PRESS_EVENTS) window.removeEventListener(type, onPress, true);
79
+ for (const type of FOCUS_EVENTS) window.removeEventListener(type, stop, true);
80
+ host.removeEventListener("click", stop);
81
+ for (const type of KEY_EVENTS) host.removeEventListener(type, onKey);
82
+ observer.disconnect();
83
+ };
84
+ }
85
+ //#endregion
86
+ //#region src/plugins/design/variant-bar-place.ts
87
+ const MARGIN = 12;
88
+ const CLEARANCE = 8;
89
+ /** The bar never rises further than this share of the viewport height. */
90
+ const MAX_RISE_SHARE = .4;
91
+ /** Narrower than this the bar gives up making room sideways and rises instead. */
92
+ const MIN_WIDTH = 260;
93
+ /**
94
+ * The strip at the bottom edge the bar centres itself in. An obstacle hugging
95
+ * the left or right edge (the rail, its phone button, a chat bubble, an open
96
+ * side sheet) narrows the strip; one that spans the bottom (a navigation, a
97
+ * cookie banner) or leaves too little width lifts it, up to a cap: past that
98
+ * the bar overlaps rather than leaving the screen.
99
+ */
100
+ function placeBar({ viewport, reserve, height, obstacles }) {
101
+ const place = {
102
+ left: reserve.left + MARGIN,
103
+ right: reserve.right + MARGIN,
104
+ bottom: reserve.bottom + MARGIN
105
+ };
106
+ const maxBottom = viewport.h * MAX_RISE_SHARE;
107
+ for (let round = 0; round <= obstacles.length; round++) {
108
+ const top = viewport.h - place.bottom - height;
109
+ const bottom = viewport.h - place.bottom;
110
+ const hit = obstacles.find((o) => o.top < bottom && o.top + o.height > top && o.left < viewport.w - place.right && o.left + o.width > place.left);
111
+ if (!hit) return place;
112
+ const fromLeft = hit.left + hit.width + CLEARANCE;
113
+ const fromRight = viewport.w - hit.left + CLEARANCE;
114
+ const sideways = Math.min(fromLeft - place.left, fromRight - place.right);
115
+ if (viewport.w - place.left - place.right - sideways >= Math.max(MIN_WIDTH, viewport.w / 2)) {
116
+ if (fromLeft - place.left <= fromRight - place.right) place.left = fromLeft;
117
+ else place.right = fromRight;
118
+ continue;
119
+ }
120
+ const lifted = viewport.h - hit.top + CLEARANCE;
121
+ if (lifted <= place.bottom || lifted > maxBottom) return place;
122
+ place.bottom = lifted;
123
+ }
124
+ return place;
125
+ }
126
+ /** Rects of the page's bottom navigation: not fixed in an app shell that fills the viewport itself. */
127
+ function bottomNavRects(viewport) {
128
+ const rects = [];
129
+ for (const el of document.querySelectorAll("nav, [role='navigation'], [role='tablist'], footer")) {
130
+ const { left, top, width, height } = el.getBoundingClientRect();
131
+ if (width < viewport.w / 2 || height === 0 || height > viewport.h / 4) continue;
132
+ if (Math.abs(viewport.h - (top + height)) > 4) continue;
133
+ rects.push({
134
+ left,
135
+ top,
136
+ width,
137
+ height
138
+ });
139
+ }
140
+ return rects;
141
+ }
142
+ //#endregion
143
+ //#region src/plugins/design/variant-bar-styles.ts
144
+ /** Variant bar styles, rendered by the bar inside the HUD's shadow root. Dark like the rail, in both themes. */
145
+ const VARIANT_BAR_CSS = `
146
+ .vbar-zone {
147
+ position: fixed;
148
+ /* Under the panels' bottom sheet (1000) and the rail (1001), over floating panels. */
149
+ z-index: 990;
150
+ display: flex;
151
+ justify-content: center;
152
+ pointer-events: none;
153
+ }
154
+ .vbar-zone[data-edge] { padding-bottom: env(safe-area-inset-bottom, 0px); }
155
+ .vbar {
156
+ --hud-bg: rgba(24, 24, 27, 0.94);
157
+ --hud-bg-subtle: rgba(255, 255, 255, 0.1);
158
+ --hud-fg: #f4f4f5;
159
+ --hud-fg-muted: #a1a1aa;
160
+ --hud-border: rgba(255, 255, 255, 0.16);
161
+ --hud-accent: #7dd3fc;
162
+ --hud-accent-fg: #082f49;
163
+ --hud-good: #4ade80;
164
+ --hud-poor: #fca5a5;
165
+ display: flex;
166
+ flex-direction: column;
167
+ gap: 4px;
168
+ max-width: 100%;
169
+ padding: 4px;
170
+ background: var(--hud-bg);
171
+ -webkit-backdrop-filter: blur(10px);
172
+ backdrop-filter: blur(10px);
173
+ border: 1px solid var(--hud-border);
174
+ border-radius: 12px;
175
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
176
+ color: var(--hud-fg);
177
+ font-family: var(--hud-font);
178
+ font-size: 12px;
179
+ line-height: 1.2;
180
+ pointer-events: auto;
181
+ user-select: none;
182
+ -webkit-user-select: none;
183
+ touch-action: manipulation;
184
+ }
185
+ .vbar-row { display: flex; align-items: center; gap: 2px; min-width: 0; }
186
+ .vbar-main { flex-wrap: wrap; justify-content: center; }
187
+ .vbar-scroll {
188
+ display: flex;
189
+ align-items: center;
190
+ gap: 2px;
191
+ min-width: 0;
192
+ overflow-x: auto;
193
+ scrollbar-width: none;
194
+ }
195
+ .vbar-scroll::-webkit-scrollbar { display: none; }
196
+ .hud .vbar-button {
197
+ flex: none;
198
+ display: inline-flex;
199
+ align-items: center;
200
+ justify-content: center;
201
+ gap: 5px;
202
+ min-width: 30px;
203
+ height: 30px;
204
+ padding: 0 9px;
205
+ border-radius: 8px;
206
+ color: var(--hud-fg-muted);
207
+ white-space: nowrap;
208
+ }
209
+ .hud .vbar-button:hover { background: var(--hud-bg-subtle); color: var(--hud-fg); }
210
+ .hud .vbar-button[aria-pressed="true"], .hud .vbar-button[aria-current="page"] {
211
+ background: var(--hud-bg-subtle);
212
+ color: var(--hud-fg);
213
+ }
214
+ .hud .vbar-button[data-picked] { color: var(--hud-good); }
215
+ .hud .vbar-pick { min-width: 76px; font-weight: 600; color: var(--hud-fg); border: 1px solid var(--hud-border); }
216
+ .hud .vbar-pick[aria-pressed="true"] {
217
+ background: var(--hud-good);
218
+ border-color: var(--hud-good);
219
+ color: #052e16;
220
+ }
221
+ .hud .vbar-pick[aria-pressed="true"]:hover { background: var(--hud-good); color: #052e16; }
222
+ .vbar-sep { flex: none; width: 1px; height: 18px; margin: 0 3px; background: var(--hud-border); }
223
+ .vbar-muted { color: var(--hud-fg-muted); padding: 0 8px; }
224
+ .vbar-error { margin: 0; padding: 2px 8px; color: var(--hud-poor); text-align: center; overflow-wrap: anywhere; }
225
+ .vbar-note { display: flex; flex-direction: column; gap: 4px; padding: 4px; }
226
+ .hud .vbar-field {
227
+ width: 100%;
228
+ box-sizing: border-box;
229
+ min-height: 64px;
230
+ margin: 0;
231
+ padding: 6px 8px;
232
+ border: 1px solid var(--hud-border);
233
+ border-radius: 8px;
234
+ background: rgba(0, 0, 0, 0.35);
235
+ color: var(--hud-fg);
236
+ font: inherit;
237
+ /* 16 px: iOS zooms the page into a smaller field. */
238
+ font-size: 16px;
239
+ line-height: 1.4;
240
+ resize: vertical;
241
+ user-select: text;
242
+ -webkit-user-select: text;
243
+ }
244
+ .vbar-note-actions { display: flex; justify-content: flex-end; gap: 4px; }
245
+ .hud .vbar-save { color: var(--hud-accent); font-weight: 600; }
246
+
247
+ @media (max-width: 899px) {
248
+ .hud .vbar-button { min-width: 40px; height: 40px; }
249
+ .vbar { border-radius: 14px; }
250
+ /* The row wraps here; a separator would hang at a line end. */
251
+ .vbar-sep { display: none; }
252
+ }
253
+ `;
254
+ //#endregion
255
+ //#region src/plugins/design/variant-pages.ts
256
+ const trimSlash = (path) => path.length > 1 ? path.replace(/\/+$/, "") : path;
257
+ /** Whether `href` is the page `location` shows. A hash or a query counts only when `href` names one. */
258
+ function isCurrentHref(href, location) {
259
+ let url;
260
+ let here;
261
+ try {
262
+ here = new URL(location.href);
263
+ url = new URL(href, here);
264
+ } catch {
265
+ return false;
266
+ }
267
+ if (url.origin !== here.origin || trimSlash(url.pathname) !== trimSlash(here.pathname)) return false;
268
+ if (url.hash && trimSlash(url.hash) !== trimSlash(here.hash)) return false;
269
+ if (!href.split("#")[0]?.includes("?")) return true;
270
+ return [...url.searchParams].every(([key, value]) => key === "variant" || here.searchParams.get(key) === value);
271
+ }
272
+ /**
273
+ * The page the bar stands on. A page whose `href` is the location wins, the
274
+ * one the user asked for (`wanted`) first when several share it, then one
275
+ * whose group is mounted. Elsewhere (a detail page) the first mounted group
276
+ * on the list decides, so a pick never lands on a group that is not showing.
277
+ */
278
+ function currentPage(pages, location, groups, wanted = null) {
279
+ const mounted = new Set(groups.map((group) => group.id));
280
+ const here = pages.filter((page) => isCurrentHref(page.href, location));
281
+ return here.find((page) => page.group === wanted && mounted.has(page.group)) ?? here.find((page) => mounted.has(page.group)) ?? here[0] ?? pages.find((page) => page.group === wanted && mounted.has(page.group)) ?? pages.find((page) => mounted.has(page.group));
282
+ }
283
+ /** The item `delta` steps on, wrapping; the first or last when there is no current one. */
284
+ function stepFrom(items, index, delta) {
285
+ if (items.length === 0) return void 0;
286
+ if (index < 0) return items[delta === 1 ? 0 : items.length - 1];
287
+ return items[(index + delta + items.length) % items.length];
288
+ }
289
+ function shownVariant(group, snapshot) {
290
+ const id = resolveVariant(group, snapshot.choices);
291
+ return group.variants.find((variant) => variant.id === id);
292
+ }
293
+ /**
294
+ * `href` with the location's variant choices carried along, so going to the
295
+ * next page and back shows what was chosen. Left alone across origins, and
296
+ * for a pure `#hash`, which keeps the query by itself.
297
+ */
298
+ function withChoices(href, location) {
299
+ if (href.startsWith("#")) return href;
300
+ const choices = parseVariantParam(new URLSearchParams(location.search).getAll(VARIANT_PARAM));
301
+ if (Object.keys(choices).length === 0) return href;
302
+ let url;
303
+ try {
304
+ const here = new URL(location.href);
305
+ url = new URL(href, here);
306
+ if (url.origin !== here.origin) return href;
307
+ } catch {
308
+ return href;
309
+ }
310
+ const own = parseVariantParam(url.searchParams.getAll(VARIANT_PARAM));
311
+ url.searchParams.set(VARIANT_PARAM, formatVariantParam({
312
+ ...choices,
313
+ ...own
314
+ }));
315
+ return `${url.pathname}${url.search}${url.hash}`;
316
+ }
317
+ //#endregion
318
+ //#region src/plugins/design/variant-bar.tsx
319
+ const POLL_MS = 400;
320
+ function navigationApi() {
321
+ const api = window.navigation;
322
+ return api instanceof EventTarget ? api : void 0;
323
+ }
324
+ /** A router's `pushState` fires no event; the Navigation API reports it, elsewhere a slow poll does. */
325
+ function subscribeLocation(listener) {
326
+ let last = window.location.href;
327
+ const check = () => {
328
+ if (window.location.href === last) return;
329
+ last = window.location.href;
330
+ getVariantGroupStore().syncSearch(window.location.search);
331
+ listener();
332
+ };
333
+ const later = () => queueMicrotask(check);
334
+ window.addEventListener("popstate", later);
335
+ window.addEventListener("hashchange", later);
336
+ const api = navigationApi();
337
+ api?.addEventListener("currententrychange", later);
338
+ const timer = api ? void 0 : setInterval(check, POLL_MS);
339
+ return () => {
340
+ window.removeEventListener("popstate", later);
341
+ window.removeEventListener("hashchange", later);
342
+ api?.removeEventListener("currententrychange", later);
343
+ clearInterval(timer);
344
+ };
345
+ }
346
+ const readHref = () => window.location.href;
347
+ const hiddenKey = (app) => `hansenexus-hud:variant-bar:${app}`;
348
+ function loadHidden(app) {
349
+ try {
350
+ return window.localStorage.getItem(hiddenKey(app)) === "hidden";
351
+ } catch {
352
+ return false;
353
+ }
354
+ }
355
+ function saveHidden(app, hidden) {
356
+ try {
357
+ if (hidden) window.localStorage.setItem(hiddenKey(app), "hidden");
358
+ else window.localStorage.removeItem(hiddenKey(app));
359
+ } catch {}
360
+ }
361
+ function sameRects(a, b) {
362
+ return a.length === b.length && a.every((r, i) => {
363
+ const o = b[i];
364
+ return o !== void 0 && r.left === o.left && r.top === o.top && r.width === o.width && r.height === o.height;
365
+ });
366
+ }
367
+ /** The bar's height, the HUD's own dock and the page's bottom navigation, re-measured as they change. */
368
+ function useMeasured(zone) {
369
+ const [measured, setMeasured] = useState({
370
+ height: 0,
371
+ rects: []
372
+ });
373
+ useLayoutEffect(() => {
374
+ if (!zone) return;
375
+ const root = zone.getRootNode();
376
+ let frame = 0;
377
+ const measure = () => {
378
+ frame = 0;
379
+ const viewport = {
380
+ w: document.documentElement.clientWidth || window.innerWidth,
381
+ h: document.documentElement.clientHeight || window.innerHeight
382
+ };
383
+ const rects = bottomNavRects(viewport);
384
+ if (root instanceof ShadowRoot) for (const el of root.querySelectorAll(".rail, .pill")) {
385
+ const { left, top, width, height } = el.getBoundingClientRect();
386
+ const sheet = width > 200 && width * height > viewport.w * viewport.h / 4;
387
+ if (width > 0 && height > 0 && !sheet) rects.push({
388
+ left,
389
+ top,
390
+ width,
391
+ height
392
+ });
393
+ }
394
+ const height = zone.offsetHeight;
395
+ setMeasured((prev) => prev.height === height && sameRects(prev.rects, rects) ? prev : {
396
+ height,
397
+ rects
398
+ });
399
+ };
400
+ const schedule = () => {
401
+ if (!frame) frame = requestAnimationFrame(measure);
402
+ };
403
+ measure();
404
+ const watch = {
405
+ childList: true,
406
+ subtree: true,
407
+ attributes: true,
408
+ attributeFilter: [
409
+ "class",
410
+ "style",
411
+ "hidden"
412
+ ]
413
+ };
414
+ const observer = new MutationObserver(schedule);
415
+ observer.observe(root, watch);
416
+ observer.observe(document.body, watch);
417
+ const resize = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(schedule);
418
+ resize?.observe(zone);
419
+ window.addEventListener("resize", schedule);
420
+ root.addEventListener("transitionend", schedule);
421
+ return () => {
422
+ observer.disconnect();
423
+ resize?.disconnect();
424
+ if (frame) cancelAnimationFrame(frame);
425
+ window.removeEventListener("resize", schedule);
426
+ root.removeEventListener("transitionend", schedule);
427
+ };
428
+ }, [zone]);
429
+ return measured;
430
+ }
431
+ const ARROWS = {
432
+ ArrowLeft: ["variant", -1],
433
+ ArrowRight: ["variant", 1],
434
+ ArrowUp: ["page", -1],
435
+ ArrowDown: ["page", 1]
436
+ };
437
+ /** Looks through shadow roots: from the outside a field in one is its host element. */
438
+ function isTyping(event) {
439
+ const target = event.composedPath()[0];
440
+ if (!(target instanceof HTMLElement)) return false;
441
+ return target.isContentEditable || [
442
+ "INPUT",
443
+ "TEXTAREA",
444
+ "SELECT"
445
+ ].includes(target.tagName);
446
+ }
447
+ /**
448
+ * The variant bar: steps through the app's pages with variants and the
449
+ * variants of the page it stands on, and records a pick and a note per page.
450
+ * A pick only records (`POST /:group/pick`); no brief is queued and no agent
451
+ * starts. Mounted by `variants({ pages })` in development builds.
452
+ */
453
+ function VariantBar({ pages, navigate, ctx }) {
454
+ const snapshot = useSyncExternalStore(subscribeVariantGroups, getVariantGroupsSnapshot, getVariantGroupsSnapshot);
455
+ useSyncExternalStore(subscribeLocation, readHref, readHref);
456
+ const { byGroup, error, save } = usePicks(ctx);
457
+ const [hidden, setHidden] = useState(() => loadHidden(ctx.app));
458
+ /** The group asked for last: decides when pages share an address. */
459
+ const [wanted, setWanted] = useState(null);
460
+ const [noting, setNoting] = useState(null);
461
+ const [zone, setZone] = useState(null);
462
+ const page = currentPage(pages, window.location, snapshot.groups, wanted);
463
+ const group = page && snapshot.groups.find((g) => g.id === page.group);
464
+ const shown = group && shownVariant(group, snapshot);
465
+ const recorded = (group && byGroup[group.id]) ?? NO_PICK;
466
+ const canPick = ctx.can("variants.decide");
467
+ const goTo = useCallback((next) => {
468
+ if (!next) return;
469
+ setWanted(next.group);
470
+ if (isCurrentHref(next.href, window.location)) return;
471
+ const href = withChoices(next.href, window.location);
472
+ if (navigate) navigate(href);
473
+ else if (href.startsWith("#")) window.location.assign(href);
474
+ else navigateTo(href);
475
+ }, [navigate]);
476
+ const stepPage = (delta) => goTo(stepFrom(pages, page ? pages.indexOf(page) : -1, delta));
477
+ const show = (target, id) => getVariantGroupStore().choose(target.id, id === target.defaultId ? null : id);
478
+ const stepVariant = (delta) => {
479
+ if (!group || !shown) return;
480
+ const next = stepFrom(group.variants, group.variants.findIndex((v) => v.id === shown.id), delta);
481
+ if (next) show(group, next.id);
482
+ };
483
+ const togglePick = () => {
484
+ if (!group || !shown) return;
485
+ save(group, { pick: recorded.pick === shown.id ? null : shown.id });
486
+ };
487
+ const hide = (next) => {
488
+ setHidden(next);
489
+ setNoting(null);
490
+ saveHidden(ctx.app, next);
491
+ };
492
+ const steps = useRef({
493
+ stepPage,
494
+ stepVariant
495
+ });
496
+ useEffect(() => {
497
+ steps.current = {
498
+ stepPage,
499
+ stepVariant
500
+ };
501
+ });
502
+ useEffect(() => {
503
+ const onKeyDown = (event) => {
504
+ if (!event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
505
+ const step = ARROWS[event.key];
506
+ if (!step || isTyping(event)) return;
507
+ event.preventDefault();
508
+ event.stopPropagation();
509
+ if (step[0] === "variant") steps.current.stepVariant(step[1]);
510
+ else steps.current.stepPage(step[1]);
511
+ };
512
+ window.addEventListener("keydown", onKeyDown, true);
513
+ return () => window.removeEventListener("keydown", onKeyDown, true);
514
+ }, []);
515
+ useEffect(() => zone ? guardBar(zone) : void 0, [zone]);
516
+ const visual = useVisualViewport();
517
+ const { reserve } = usePanels();
518
+ const obstacles = useObstacles();
519
+ const { height, rects } = useMeasured(zone);
520
+ const place = useMemo(() => placeBar({
521
+ viewport: {
522
+ w: visual.w,
523
+ h: visual.h
524
+ },
525
+ reserve,
526
+ height,
527
+ obstacles: [...rects, ...obstacles].map((o) => ({
528
+ ...o,
529
+ left: o.left - visual.x,
530
+ top: o.top - visual.y
531
+ }))
532
+ }), [
533
+ visual,
534
+ reserve,
535
+ height,
536
+ rects,
537
+ obstacles
538
+ ]);
539
+ const style = visual.detached ? {
540
+ left: visual.x + place.left,
541
+ width: visual.w - place.left - place.right,
542
+ top: visual.y + visual.h - place.bottom - height
543
+ } : {
544
+ left: place.left,
545
+ right: place.right,
546
+ bottom: place.bottom
547
+ };
548
+ const onEdge = place.bottom - reserve.bottom <= 12;
549
+ const pickedCount = pages.filter((p) => byGroup[p.group]?.pick).length;
550
+ let content;
551
+ if (hidden) content = /* @__PURE__ */ jsx("section", {
552
+ className: "vbar",
553
+ "aria-label": "Design variants",
554
+ "data-hud-variant-bar": "pill",
555
+ children: /* @__PURE__ */ jsxs("button", {
556
+ type: "button",
557
+ className: "vbar-button",
558
+ "aria-label": "Show the variant bar",
559
+ onClick: () => hide(false),
560
+ children: [/* @__PURE__ */ jsx("span", { children: group && shown ? shortName(group, shown.id) : "Variants" }), /* @__PURE__ */ jsxs("span", { children: [
561
+ pickedCount,
562
+ "/",
563
+ pages.length,
564
+ " picked"
565
+ ] })]
566
+ })
567
+ });
568
+ else content = /* @__PURE__ */ jsxs("section", {
569
+ className: "vbar",
570
+ "aria-label": "Design variants",
571
+ "data-hud-variant-bar": "bar",
572
+ children: [
573
+ error && /* @__PURE__ */ jsx("p", {
574
+ className: "vbar-error",
575
+ role: "alert",
576
+ children: error
577
+ }),
578
+ group && noting === group.id && /* @__PURE__ */ jsx(NoteForm, {
579
+ label: page.label,
580
+ initial: recorded.note,
581
+ onSave: (note) => save(group, { note }),
582
+ onClose: () => setNoting(null)
583
+ }, group.id),
584
+ /* @__PURE__ */ jsxs("div", {
585
+ className: "vbar-row vbar-main",
586
+ children: [
587
+ /* @__PURE__ */ jsx("nav", {
588
+ className: "vbar-scroll",
589
+ "aria-label": "Pages with variants",
590
+ children: pages.map((p) => {
591
+ const pick = byGroup[p.group]?.pick;
592
+ return /* @__PURE__ */ jsxs("button", {
593
+ type: "button",
594
+ className: "vbar-button",
595
+ "aria-current": p === page ? "page" : void 0,
596
+ "data-picked": pick ? "" : void 0,
597
+ title: pick ? `${p.label}: ${pick} picked` : p.label,
598
+ onClick: () => goTo(p),
599
+ children: [p.label, pick ? ` ✓ ${pick.length <= 2 ? pick.toUpperCase() : ""}`.trimEnd() : ""]
600
+ }, `${p.group} ${p.href}`);
601
+ })
602
+ }),
603
+ /* @__PURE__ */ jsx("span", {
604
+ className: "vbar-sep",
605
+ "aria-hidden": "true"
606
+ }),
607
+ group && shown ? /* @__PURE__ */ jsxs("div", {
608
+ className: "vbar-row",
609
+ children: [
610
+ /* @__PURE__ */ jsx("button", {
611
+ type: "button",
612
+ className: "vbar-button",
613
+ "aria-label": "Previous variant",
614
+ title: "Previous variant (⌥←)",
615
+ onClick: () => stepVariant(-1),
616
+ children: "‹"
617
+ }),
618
+ /* @__PURE__ */ jsx("div", {
619
+ className: "vbar-scroll",
620
+ children: group.variants.map((variant) => /* @__PURE__ */ jsxs("button", {
621
+ type: "button",
622
+ className: "vbar-button",
623
+ "aria-pressed": variant.id === shown.id,
624
+ "aria-label": variant.label,
625
+ title: variant.label,
626
+ "data-variant": variant.id,
627
+ "data-picked": recorded.pick === variant.id ? "" : void 0,
628
+ onClick: () => show(group, variant.id),
629
+ children: [variant.id === shown.id ? variant.label : shortName(group, variant.id), recorded.pick === variant.id ? " ✓" : ""]
630
+ }, variant.id))
631
+ }),
632
+ /* @__PURE__ */ jsx("button", {
633
+ type: "button",
634
+ className: "vbar-button",
635
+ "aria-label": "Next variant",
636
+ title: "Next variant (⌥→)",
637
+ onClick: () => stepVariant(1),
638
+ children: "›"
639
+ })
640
+ ]
641
+ }) : /* @__PURE__ */ jsx("span", {
642
+ className: "vbar-muted",
643
+ children: "No variants on this page"
644
+ }),
645
+ /* @__PURE__ */ jsx("span", {
646
+ className: "vbar-sep",
647
+ "aria-hidden": "true"
648
+ }),
649
+ /* @__PURE__ */ jsxs("div", {
650
+ className: "vbar-row",
651
+ children: [canPick && group && shown && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("button", {
652
+ type: "button",
653
+ className: "vbar-button vbar-pick",
654
+ "aria-pressed": recorded.pick === shown.id,
655
+ title: recorded.pick === shown.id ? `Take back the pick of ${shown.label}` : `Pick ${shown.label}. Records only, starts nothing.`,
656
+ onClick: togglePick,
657
+ children: recorded.pick === shown.id ? "✓ Picked" : "Pick"
658
+ }), /* @__PURE__ */ jsxs("button", {
659
+ type: "button",
660
+ className: "vbar-button",
661
+ "aria-expanded": noting === group.id,
662
+ "data-picked": recorded.note ? "" : void 0,
663
+ title: recorded.note || "Add a note",
664
+ onClick: () => setNoting(noting === group.id ? null : group.id),
665
+ children: ["Note", recorded.note ? " •" : ""]
666
+ })] }), /* @__PURE__ */ jsx("button", {
667
+ type: "button",
668
+ className: "vbar-button",
669
+ "aria-label": "Hide the variant bar",
670
+ title: "Hide",
671
+ onClick: () => hide(true),
672
+ children: "×"
673
+ })]
674
+ })
675
+ ]
676
+ })
677
+ ]
678
+ });
679
+ return /* @__PURE__ */ jsxs("div", {
680
+ className: "vbar-zone",
681
+ "data-edge": onEdge || void 0,
682
+ ref: setZone,
683
+ style,
684
+ children: [/* @__PURE__ */ jsx("style", { children: VARIANT_BAR_CSS }), content]
685
+ });
686
+ }
687
+ /** A variant's chip: the id of a lettered variant, else its label. */
688
+ function shortName(group, id) {
689
+ if (id.length <= 2) return id.toUpperCase();
690
+ return group.variants.find((variant) => variant.id === id)?.label ?? id;
691
+ }
692
+ function NoteForm({ label, initial, onSave, onClose }) {
693
+ const [draft, setDraft] = useState(initial);
694
+ const field = useRef(null);
695
+ const latest = useRef({
696
+ draft,
697
+ initial,
698
+ onSave,
699
+ cancelled: false
700
+ });
701
+ useEffect(() => {
702
+ latest.current = {
703
+ ...latest.current,
704
+ draft,
705
+ initial,
706
+ onSave
707
+ };
708
+ });
709
+ useEffect(() => () => {
710
+ const { draft: text, initial: before, onSave: store, cancelled } = latest.current;
711
+ if (!cancelled && text.trim() !== before.trim()) store(text.trim());
712
+ }, []);
713
+ useEffect(() => {
714
+ const el = field.current;
715
+ const root = el?.getRootNode();
716
+ if (!el) return;
717
+ if (root instanceof ShadowRoot) releaseFocus(root.host);
718
+ el.focus();
719
+ el.setSelectionRange(el.value.length, el.value.length);
720
+ }, []);
721
+ useEffect(() => {
722
+ const onKeyDown = (event) => {
723
+ if (event.composedPath()[0] !== field.current) return;
724
+ event.stopPropagation();
725
+ const save = event.key === "Enter" && (event.metaKey || event.ctrlKey);
726
+ if (event.key !== "Escape" && !save) return;
727
+ event.preventDefault();
728
+ latest.current.cancelled = event.key === "Escape";
729
+ onClose();
730
+ };
731
+ window.addEventListener("keydown", onKeyDown, true);
732
+ return () => window.removeEventListener("keydown", onKeyDown, true);
733
+ }, [onClose]);
734
+ return /* @__PURE__ */ jsxs("form", {
735
+ className: "vbar-note",
736
+ onSubmit: (e) => {
737
+ e.preventDefault();
738
+ onClose();
739
+ },
740
+ children: [/* @__PURE__ */ jsx("textarea", {
741
+ ref: field,
742
+ className: "vbar-field",
743
+ "aria-label": `Note on ${label}`,
744
+ placeholder: `Note on ${label}`,
745
+ maxLength: NOTE_MAX,
746
+ value: draft,
747
+ onChange: (e) => setDraft(e.target.value)
748
+ }), /* @__PURE__ */ jsxs("div", {
749
+ className: "vbar-note-actions",
750
+ children: [/* @__PURE__ */ jsx("button", {
751
+ type: "button",
752
+ className: "vbar-button",
753
+ onClick: () => {
754
+ latest.current.cancelled = true;
755
+ onClose();
756
+ },
757
+ children: "Cancel"
758
+ }), /* @__PURE__ */ jsx("button", {
759
+ type: "submit",
760
+ className: "vbar-button vbar-save",
761
+ title: "⌘Enter",
762
+ children: "Save"
763
+ })]
764
+ })]
765
+ });
766
+ }
767
+ //#endregion
768
+ export { VariantBar, VariantBar as default };