@monster-orange/den-ui 0.5.1 → 0.7.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 (45) hide show
  1. package/README.md +515 -1
  2. package/bin/den-ui-parity.mjs +440 -0
  3. package/inventory/fonts-dir.cjs +8 -0
  4. package/inventory/fonts-dir.mjs +10 -0
  5. package/inventory/nav-inventory.d.ts +133 -0
  6. package/inventory/nav-inventory.js +839 -0
  7. package/inventory/reference/den-chromium-1280.json +5388 -0
  8. package/inventory/reference/den-chromium-390.json +3834 -0
  9. package/inventory/reference/den-webkit-1280.json +5388 -0
  10. package/inventory/reference/den-webkit-390.json +3834 -0
  11. package/package.json +40 -3
  12. package/src/busy-words.d.ts +2 -0
  13. package/src/busy-words.js +6 -0
  14. package/src/busy.css +155 -0
  15. package/src/busy.d.ts +80 -0
  16. package/src/busy.js +105 -35
  17. package/src/dialog.css +103 -0
  18. package/src/dialog.d.ts +44 -0
  19. package/src/dialog.js +95 -30
  20. package/src/icons.d.ts +4 -0
  21. package/src/icons.js +43 -0
  22. package/src/kit.d.ts +217 -0
  23. package/src/kit.js +299 -0
  24. package/src/marks/den.svg +6 -0
  25. package/src/report-shot.d.ts +47 -0
  26. package/src/report-shot.js +437 -0
  27. package/src/report.css +387 -0
  28. package/src/report.d.ts +61 -0
  29. package/src/report.js +909 -0
  30. package/src/shell-client.d.ts +149 -0
  31. package/src/shell-client.js +820 -0
  32. package/src/shell-html.d.ts +305 -0
  33. package/src/shell-html.js +903 -0
  34. package/src/shell.css +1010 -0
  35. package/src/strings.d.ts +18 -0
  36. package/src/strings.js +565 -0
  37. package/src/time.d.ts +18 -0
  38. package/src/time.js +14 -2
  39. package/src/tokens.css +81 -67
  40. package/src/update-pill.d.ts +17 -0
  41. package/src/version.d.ts +7 -0
  42. package/src/version.js +31 -0
  43. package/src/whatsnew.css +235 -0
  44. package/src/whatsnew.d.ts +36 -0
  45. package/src/whatsnew.js +411 -0
@@ -0,0 +1,820 @@
1
+ /**
2
+ * den-ui shell, in the page: hydrates the markup shell-html.js drew (on
3
+ * the server or in the page). It draws nothing of the frame itself; it
4
+ * binds the rail's keys, the collapse toggle, pins' Delete, and the ☰
5
+ * drawer, and repaints the rail when the product asks.
6
+ *
7
+ * const shell = hydrateShell(root, opts)
8
+ * root the element holding the shell (its .den-top and #den-rail)
9
+ * opts.lang the kit's words' language
10
+ * opts.onCollapse(next) the toggle was pressed: store it, then call
11
+ * shell.setCollapsed(next) (or repaint)
12
+ * opts.onUnpin(id) Delete on a pinned row: unpin it (and repaint);
13
+ * focus moves to the next pinned row by itself
14
+ * opts.inert() the elements made inert behind the open drawer
15
+ * (default: the top bar and #den-content)
16
+ * opts.menuOpen() true while a product menu is open: Escape is the
17
+ * menu's then, not the drawer's
18
+ *
19
+ * shell.rove() one tab stop in the rail (current, else first)
20
+ * shell.paint(html, force) the rail's new content, focus kept on the
21
+ * same entry; false when it was the same
22
+ * (force: paint it anyway)
23
+ * shell.setCollapsed(on) body.den-rail-collapsed
24
+ * shell.openDrawer(), closeDrawer(refocus), drawerOpen()
25
+ * shell.unpin(id) Delete's path: onUnpin, then focus
26
+ * syncPinToggles(root, isPinned, lang) every [data-pin-toggle] in root:
27
+ * ★ "Unpin x" pressed, or ☆ "Pin x"
28
+ *
29
+ * The drawer is the rail below 720px: a modal over the page. Focus moves
30
+ * into the rail (the current entry); the top bar and the page are inert
31
+ * behind a scrim; Escape or the scrim closes it and focus goes back to ☰;
32
+ * Tab stays inside. Wider than that, it is a rail again.
33
+ *
34
+ * 0.7.0, the pieces a host puts in the slots (shell-html.js draws them):
35
+ *
36
+ * hydrateScope(root, { onOpen(key, button) }) the crumbs' menus. A
37
+ * crumb with the kit's menu (links or HTML) opens it below its ▾:
38
+ * Enter, Space, ↓ or ↑ open it on an entry; ↑ ↓ Home End move; Esc
39
+ * or Tab closes it, Esc back on the ▾; a click outside closes it. A
40
+ * crumb whose menu is the host's (menu: true) calls onOpen. A long
41
+ * menu (the crumb's `filter`) opens on its filter field: typing
42
+ * hides the entries that don't hold it ("No match" when none do);
43
+ * ↓ ↑ go to the entries (↑ from the first, back to the field);
44
+ * Enter takes the first shown; Esc clears it, then closes.
45
+ * → { close(), isOpen() }
46
+ * attachMenuFilter(menu, { label, lang, filter?, onClose(refocus) })
47
+ * the same filter for a host's own menu (.den-mi entries): call it
48
+ * before showing the menu, then focus the field it returns (null:
49
+ * too few entries). Its Esc on an empty field calls onClose.
50
+ * hydrateSearch(button, { onActivate(via, event), keys? }) a click (or
51
+ * Enter) and ⌘K / Ctrl-K anywhere call onActivate ("click" or "key")
52
+ * and fire "den-ui:search" on the field. keys: false, no ⌘K.
53
+ * → { activate(via), destroy() }
54
+ * hydrateBell(button, opts) the bell and its panel (a dialog on
55
+ * <body>). opts: items (a list, or a function of none) and title,
56
+ * empty, foot (a function giving HTML), or panel() giving the whole
57
+ * content (HTML, or bellPanelHtml's options); onOpen, onClose,
58
+ * onEscape(e) (true: the host took that Esc), id, lang. A click
59
+ * toggles it; Esc closes it and focus goes back to the bell (in
60
+ * Safari too); a click outside closes it. Opening focuses its first
61
+ * link or button.
62
+ * → { el, open(), close(focus), isOpen(), paint(), setCount(n) }
63
+ * hydrateAccount(root, { onAction(event), menu(), onPick, onSubmit,
64
+ * lang }) the account entry: as an action (no href), a click or
65
+ * Enter calls onAction; with `menu: true` on the entry, its menu
66
+ * (accountMenuHtml's options from menu(), on <body>, as ◐ ▾'s: Esc
67
+ * and focus back, a press outside closes it). A form entry posts on
68
+ * its own; onSubmit(event, form) first (preventDefault: no post).
69
+ * → the menu's { open(), close(refocus), isOpen() }, or nothing
70
+ * hydrateAppearance(button, { menu(), onPick(kind, value, row), lang })
71
+ * ◐ ▾ and its menu (a role="menu" on <body>, placed under the
72
+ * button). menu() gives appearanceMenuHtml's options each time it
73
+ * opens; a pick calls onPick ("mode", "theme", "language" or "item",
74
+ * and its value) and closes it. Click, Enter, Space, ↓ or ↑ open it on
75
+ * the checked entry (else the first); ↑ ↓ Home End move; Esc or Tab
76
+ * closes it and focus goes back to ◐ ▾; a press outside closes it.
77
+ * → { open(), close(refocus), isOpen() }
78
+ * setWaiting(o | null, root?) the update-waiting pill after the
79
+ * version chip: { label, action, title?, name?, onClick } shows it
80
+ * (waitingHtml), null hides it.
81
+ */
82
+ import { kitT } from "./strings.js";
83
+ import {
84
+ accountMenuHtml,
85
+ appearanceMenuHtml,
86
+ bellCount,
87
+ bellPanelHtml,
88
+ menuFilterParts,
89
+ menuHasFilter,
90
+ waitingHtml,
91
+ } from "./shell-html.js";
92
+ import { focusBack } from "./dialog.js";
93
+
94
+ const all = (root, sel) =>
95
+ Array.prototype.slice.call(root.querySelectorAll(sel));
96
+
97
+ /** A pin toggle's look for its id: ★ "Unpin x" pressed, or ☆ "Pin x". */
98
+ export function syncPinToggles(root, isPinned, lang) {
99
+ if (!root || !root.querySelectorAll) return;
100
+ for (const b of all(root, "[data-pin-toggle]")) {
101
+ const id = b.getAttribute("data-pin-toggle");
102
+ const on = !!isPinned(id);
103
+ const label = kitT(lang, on ? "kit.pin.unpinX" : "kit.pin.pinX", { s: id });
104
+ b.textContent = on ? "★" : "☆";
105
+ b.setAttribute("aria-pressed", on ? "true" : "false");
106
+ b.setAttribute("aria-label", label);
107
+ b.title = label;
108
+ }
109
+ }
110
+
111
+ export function hydrateShell(root, opts = {}) {
112
+ const lang = () =>
113
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
114
+ const nav = root.querySelector(".den-rail");
115
+ const railBox = root.querySelector(".den-rail-box");
116
+ const menu = root.querySelector(".den-menu-btn");
117
+ let scrim = null;
118
+
119
+ function rove() {
120
+ if (!nav) return;
121
+ const links = all(nav, ".den-ni");
122
+ let stop = null;
123
+ for (const a of links)
124
+ if (a.getAttribute("aria-current") === "page") stop = a;
125
+ if (!stop) stop = links[0];
126
+ for (const a of links) a.tabIndex = a === stop ? 0 : -1;
127
+ }
128
+
129
+ let lastHtml = null;
130
+ function paint(html, force) {
131
+ if (!nav || (!force && html === lastHtml)) return false;
132
+ // Keep keyboard focus on the same entry across a repaint.
133
+ const active = document.activeElement;
134
+ const keep =
135
+ active && nav.contains(active) && active.dataset
136
+ ? active.dataset.nav
137
+ : null;
138
+ lastHtml = html;
139
+ nav.innerHTML = html;
140
+ rove();
141
+ if (keep) {
142
+ const again = nav.querySelector('[data-nav="' + keep + '"]');
143
+ if (again) {
144
+ again.tabIndex = 0;
145
+ again.focus();
146
+ }
147
+ }
148
+ return true;
149
+ }
150
+
151
+ function setCollapsed(on) {
152
+ if (document.body)
153
+ document.body.classList.toggle("den-rail-collapsed", !!on);
154
+ }
155
+
156
+ /**
157
+ * Delete on a pinned row: unpin it, and keep focus in the rail: the next
158
+ * pinned row, else the previous one, else the first entry.
159
+ */
160
+ function unpin(pinId) {
161
+ const pins = all(nav, ".den-ni[data-pin]");
162
+ const at = pins.findIndex((p) => p.dataset.pin === pinId);
163
+ const to = at < 0 ? null : pins[at + 1] || pins[at - 1] || null;
164
+ const toNav = to ? to.dataset.nav : null;
165
+ if (opts.onUnpin) opts.onUnpin(pinId);
166
+ const dest =
167
+ (toNav && nav.querySelector('[data-nav="' + toNav + '"]')) ||
168
+ nav.querySelector(".den-ni");
169
+ if (!dest) return;
170
+ for (const a of all(nav, ".den-ni")) a.tabIndex = a === dest ? 0 : -1;
171
+ dest.focus();
172
+ }
173
+
174
+ function drawerOpen() {
175
+ return document.body.classList.contains("den-rail-open");
176
+ }
177
+ function inertEls() {
178
+ if (opts.inert) return opts.inert();
179
+ return [
180
+ root.querySelector(".den-top"),
181
+ document.getElementById("den-content"),
182
+ ];
183
+ }
184
+ function setInert(on) {
185
+ for (const el of inertEls()) {
186
+ if (!el) continue;
187
+ if (on) el.setAttribute("inert", "");
188
+ else el.removeAttribute("inert");
189
+ }
190
+ }
191
+ function openDrawer() {
192
+ document.body.classList.add("den-rail-open");
193
+ if (menu) menu.setAttribute("aria-expanded", "true");
194
+ railBox.setAttribute("role", "dialog");
195
+ railBox.setAttribute("aria-modal", "true");
196
+ railBox.setAttribute("aria-label", kitT(lang(), "kit.pages"));
197
+ if (!scrim) {
198
+ scrim = document.createElement("div");
199
+ scrim.className = "den-scrim";
200
+ scrim.addEventListener("click", () => closeDrawer(true));
201
+ }
202
+ document.body.appendChild(scrim);
203
+ setInert(true);
204
+ const stop =
205
+ nav.querySelector('.den-ni[aria-current="page"]') ||
206
+ nav.querySelector('.den-ni[tabindex="0"]') ||
207
+ nav.querySelector(".den-ni");
208
+ if (stop) stop.focus();
209
+ }
210
+ function closeDrawer(refocus) {
211
+ if (!drawerOpen()) return;
212
+ document.body.classList.remove("den-rail-open");
213
+ if (menu) menu.setAttribute("aria-expanded", "false");
214
+ railBox.removeAttribute("role");
215
+ railBox.removeAttribute("aria-modal");
216
+ railBox.removeAttribute("aria-label");
217
+ if (scrim) scrim.remove();
218
+ setInert(false);
219
+ if (refocus && menu) menu.focus();
220
+ }
221
+
222
+ if (nav) {
223
+ // ↑/↓, Home and End move along the rail; Tab still leaves it (except
224
+ // in the narrow drawer, a modal: Tab stays inside).
225
+ nav.addEventListener("keydown", (e) => {
226
+ if (e.key === "Tab" && drawerOpen()) {
227
+ e.preventDefault();
228
+ return;
229
+ }
230
+ const links = all(nav, ".den-ni");
231
+ const i = links.indexOf(document.activeElement);
232
+ if (i < 0) return;
233
+ const pinId =
234
+ links[i].dataset && links[i].dataset.pin ? links[i].dataset.pin : null;
235
+ if (pinId && (e.key === "Delete" || e.key === "Backspace")) {
236
+ e.preventDefault();
237
+ unpin(pinId);
238
+ return;
239
+ }
240
+ const next =
241
+ e.key === "ArrowDown"
242
+ ? (i + 1) % links.length
243
+ : e.key === "ArrowUp"
244
+ ? (i - 1 + links.length) % links.length
245
+ : e.key === "Home"
246
+ ? 0
247
+ : e.key === "End"
248
+ ? links.length - 1
249
+ : -1;
250
+ if (next < 0) return;
251
+ e.preventDefault();
252
+ links[i].tabIndex = -1;
253
+ links[next].tabIndex = 0;
254
+ links[next].focus();
255
+ });
256
+ nav.addEventListener("click", (e) => {
257
+ const c =
258
+ e.target && e.target.closest
259
+ ? e.target.closest('[data-act="collapse"]')
260
+ : null;
261
+ if (!c) return;
262
+ const next = !document.body.classList.contains("den-rail-collapsed");
263
+ if (opts.onCollapse) opts.onCollapse(next);
264
+ else setCollapsed(next);
265
+ });
266
+ }
267
+ if (menu)
268
+ menu.addEventListener("click", () => {
269
+ if (drawerOpen()) closeDrawer(true);
270
+ else openDrawer();
271
+ });
272
+ document.addEventListener("keydown", (e) => {
273
+ if (
274
+ e.key === "Escape" &&
275
+ drawerOpen() &&
276
+ !(opts.menuOpen && opts.menuOpen())
277
+ ) {
278
+ e.preventDefault();
279
+ closeDrawer(true);
280
+ }
281
+ });
282
+ window.addEventListener("resize", () => {
283
+ if (drawerOpen() && window.innerWidth > 720) closeDrawer(false);
284
+ });
285
+
286
+ rove();
287
+ return {
288
+ nav,
289
+ rove,
290
+ paint,
291
+ setCollapsed,
292
+ unpin,
293
+ openDrawer,
294
+ closeDrawer,
295
+ drawerOpen,
296
+ syncPinToggles: (r, isPinned) => syncPinToggles(r, isPinned, lang()),
297
+ };
298
+ }
299
+
300
+ // ── 0.7.0: the scope crumbs, the search, the bell, the account ──────────
301
+
302
+ const ITEMS = '[role="menuitem"], a[href], button:not([disabled])';
303
+
304
+ /** A menu's filter: an entry shows when its words hold what was typed. */
305
+ function filterMenu(menu, text) {
306
+ const want = String(text || "")
307
+ .trim()
308
+ .toLowerCase();
309
+ let shown = 0;
310
+ for (const el of all(menu, ".den-mi")) {
311
+ el.hidden = !!want && !el.textContent.toLowerCase().includes(want);
312
+ if (!el.hidden) shown++;
313
+ }
314
+ const none = menu.querySelector(".den-mnone");
315
+ if (none) none.hidden = shown > 0;
316
+ }
317
+
318
+ /**
319
+ * A key in a menu with a filter field: in the field, ↓ ↑ go to the
320
+ * entries shown, Enter takes the first, Esc clears what was typed, then
321
+ * closes (close(true)), Tab closes; ↑ on the first entry goes back to the
322
+ * field. True when it took the key.
323
+ */
324
+ function filterKey(e, menu, field, close) {
325
+ const list = all(menu, ".den-mi").filter((el) => !el.hidden);
326
+ if (e.target === field) {
327
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
328
+ const to = e.key === "ArrowDown" ? list[0] : list[list.length - 1];
329
+ if (to) to.focus();
330
+ } else if (e.key === "Enter") {
331
+ if (list[0]) list[0].click();
332
+ } else if (e.key === "Escape") {
333
+ if (field.value) {
334
+ field.value = "";
335
+ filterMenu(menu, "");
336
+ } else close(true);
337
+ } else if (e.key === "Tab") {
338
+ close(false);
339
+ return true;
340
+ } else return true;
341
+ e.preventDefault();
342
+ e.stopPropagation();
343
+ return true;
344
+ }
345
+ if (e.key === "ArrowUp" && list[0] === e.target) {
346
+ e.preventDefault();
347
+ e.stopPropagation();
348
+ field.focus();
349
+ return true;
350
+ }
351
+ return false;
352
+ }
353
+
354
+ /**
355
+ * A type-to-filter field for a host's own menu (a role="menu" with .den-mi
356
+ * entries), as the kit's crumb menus have: put first in it, "No match"
357
+ * last, when it has more entries than `filter` allows (see the top).
358
+ * Call it before the menu is shown; focus the field it returns.
359
+ * o: { label, lang, filter?, onClose(refocus) } → the field, or null
360
+ */
361
+ export function attachMenuFilter(menu, o = {}) {
362
+ if (!menu || !menuHasFilter(o.filter, all(menu, ".den-mi").length))
363
+ return null;
364
+ if (!menu.id) menu.id = "den-menu";
365
+ const parts = menuFilterParts(menu.id, o.label, o.lang);
366
+ // A menu owns only its items: its rows move into a role="menu" list
367
+ // (with its name), and the pop-up holds the field, the list, "No match".
368
+ const list = document.createElement("div");
369
+ list.className = "den-mlist";
370
+ list.id = menu.id + "-list";
371
+ list.setAttribute("role", menu.getAttribute("role") || "menu");
372
+ for (const a of ["aria-label", "aria-labelledby"])
373
+ if (menu.hasAttribute(a)) {
374
+ list.setAttribute(a, menu.getAttribute(a));
375
+ menu.removeAttribute(a);
376
+ }
377
+ menu.removeAttribute("role");
378
+ while (menu.firstChild) list.appendChild(menu.firstChild);
379
+ menu.appendChild(list);
380
+ menu.insertAdjacentHTML("afterbegin", parts[0]);
381
+ menu.insertAdjacentHTML("beforeend", parts[1]);
382
+ const field = menu.querySelector("input.den-mfilter");
383
+ const close = (refocus) => {
384
+ if (o.onClose) o.onClose(refocus);
385
+ };
386
+ field.addEventListener("input", () => filterMenu(menu, field.value));
387
+ // First, before the host's own keys for its menu.
388
+ menu.addEventListener(
389
+ "keydown",
390
+ (e) => {
391
+ filterKey(e, menu, field, close);
392
+ },
393
+ true,
394
+ );
395
+ return field;
396
+ }
397
+
398
+ /** The scope crumbs' menus (see the top). */
399
+ export function hydrateScope(root, opts = {}) {
400
+ let open = null; // { btn, menu }
401
+ const menuOf = (btn) => {
402
+ const id = btn.getAttribute("aria-controls");
403
+ return id ? document.getElementById(id) : null;
404
+ };
405
+ const items = (menu) => all(menu, ITEMS).filter((el) => !el.hidden);
406
+ const fieldOf = (menu) => menu.querySelector("input.den-mfilter");
407
+ function close(refocus) {
408
+ if (!open) return;
409
+ const { btn, menu } = open;
410
+ open = null;
411
+ menu.hidden = true;
412
+ btn.setAttribute("aria-expanded", "false");
413
+ if (refocus) focusBack(btn);
414
+ }
415
+ function show(btn, at) {
416
+ const menu = menuOf(btn);
417
+ if (!menu) return;
418
+ if (open && open.btn !== btn) close(false);
419
+ // Below its ▾: the menu sits in the scope group, which places it.
420
+ const group = btn.closest(".den-scope");
421
+ const x = group
422
+ ? btn.getBoundingClientRect().left - group.getBoundingClientRect().left
423
+ : 0;
424
+ menu.style.setProperty("--den-menu-x", Math.round(x) + "px");
425
+ menu.hidden = false;
426
+ btn.setAttribute("aria-expanded", "true");
427
+ open = { btn, menu };
428
+ const field = fieldOf(menu);
429
+ if (field) {
430
+ // A long menu opens on its filter, empty.
431
+ field.value = "";
432
+ filterMenu(menu, "");
433
+ field.focus();
434
+ return;
435
+ }
436
+ const list = items(menu);
437
+ if (at === "first" && list[0]) list[0].focus();
438
+ else if (at === "last" && list.length) list[list.length - 1].focus();
439
+ }
440
+ root.addEventListener("click", (e) => {
441
+ const btn =
442
+ e.target && e.target.closest ? e.target.closest(".den-scope-btn") : null;
443
+ if (!btn || !root.contains(btn)) return;
444
+ if (!menuOf(btn)) {
445
+ if (opts.onOpen) opts.onOpen(btn.dataset.scope, btn);
446
+ return;
447
+ }
448
+ if (open && open.btn === btn) return close(false);
449
+ // A keyboard's Enter or Space (detail 0) starts on the first entry.
450
+ show(btn, e.detail === 0 ? "first" : null);
451
+ });
452
+ root.addEventListener("keydown", (e) => {
453
+ const t = e.target;
454
+ const btn = t && t.closest ? t.closest(".den-scope-btn") : null;
455
+ if (btn && menuOf(btn) && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
456
+ e.preventDefault();
457
+ show(btn, e.key === "ArrowDown" ? "first" : "last");
458
+ return;
459
+ }
460
+ if (!open || !open.menu.contains(t)) return;
461
+ const list = items(open.menu);
462
+ const field = fieldOf(open.menu);
463
+ if (field && filterKey(e, open.menu, field, close)) return;
464
+ const i = list.indexOf(t);
465
+ let next = -1;
466
+ if (e.key === "ArrowDown") next = (i + 1) % list.length;
467
+ else if (e.key === "ArrowUp") next = (i - 1 + list.length) % list.length;
468
+ else if (e.key === "Home") next = 0;
469
+ else if (e.key === "End") next = list.length - 1;
470
+ else if (e.key === "Escape") {
471
+ e.preventDefault();
472
+ e.stopPropagation();
473
+ close(true);
474
+ return;
475
+ } else if (e.key === "Tab") close(false);
476
+ if (next < 0 || !list.length) return;
477
+ e.preventDefault();
478
+ list[next].focus();
479
+ });
480
+ root.addEventListener("input", (e) => {
481
+ const t = e.target;
482
+ if (open && t && t.matches && t.matches("input.den-mfilter"))
483
+ filterMenu(open.menu, t.value);
484
+ });
485
+ // Esc on the ▾ itself, with its menu open.
486
+ document.addEventListener("keydown", (e) => {
487
+ if (e.key === "Escape" && open && !e.defaultPrevented) {
488
+ e.preventDefault();
489
+ close(true);
490
+ }
491
+ });
492
+ document.addEventListener("pointerdown", (e) => {
493
+ if (!open) return;
494
+ if (open.menu.contains(e.target) || open.btn.contains(e.target)) return;
495
+ close(false);
496
+ });
497
+ return { close: () => close(false), isOpen: () => !!open };
498
+ }
499
+
500
+ /** The search field: a click and ⌘K / Ctrl-K activate it (see the top). */
501
+ export function hydrateSearch(btn, opts = {}) {
502
+ if (!btn) return { activate() {}, destroy() {} };
503
+ function activate(via, e) {
504
+ btn.dispatchEvent(
505
+ new CustomEvent("den-ui:search", { bubbles: true, detail: { via } }),
506
+ );
507
+ if (opts.onActivate) opts.onActivate(via, e);
508
+ }
509
+ const onClick = (e) => activate("click", e);
510
+ const onKey = (e) => {
511
+ if (
512
+ (e.metaKey || e.ctrlKey) &&
513
+ !e.altKey &&
514
+ (e.key === "k" || e.key === "K" || e.code === "KeyK")
515
+ ) {
516
+ e.preventDefault();
517
+ activate("key", e);
518
+ }
519
+ };
520
+ btn.addEventListener("click", onClick);
521
+ if (opts.keys !== false) document.addEventListener("keydown", onKey);
522
+ return {
523
+ activate: (via) => activate(via || "call"),
524
+ destroy() {
525
+ btn.removeEventListener("click", onClick);
526
+ document.removeEventListener("keydown", onKey);
527
+ },
528
+ };
529
+ }
530
+
531
+ /** The bell and its panel (see the top). */
532
+ export function hydrateBell(btn, opts = {}) {
533
+ const lang = () =>
534
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
535
+ const val = (v) => (typeof v === "function" ? v() : v);
536
+ const pop = document.createElement("div");
537
+ pop.className = "den-bell-pop";
538
+ pop.id = opts.id || "den-bell-pop";
539
+ pop.setAttribute("role", "dialog");
540
+ pop.setAttribute("aria-labelledby", opts.titleId || "den-bell-h");
541
+ pop.tabIndex = -1;
542
+ pop.hidden = true;
543
+ document.body.appendChild(pop);
544
+ btn.setAttribute("aria-controls", pop.id);
545
+ const word = btn.getAttribute("title") || kitT(lang(), "kit.bell");
546
+
547
+ function paint() {
548
+ const own = opts.panel ? opts.panel() : null;
549
+ pop.innerHTML =
550
+ typeof own === "string"
551
+ ? own
552
+ : bellPanelHtml({
553
+ lang: lang(),
554
+ title: opts.title,
555
+ titleId: opts.titleId,
556
+ items: val(opts.items) || [],
557
+ empty: opts.empty,
558
+ foot: val(opts.foot) || "",
559
+ ...(own || {}),
560
+ });
561
+ }
562
+ const isOpen = () => !pop.hidden;
563
+ function open() {
564
+ if (isOpen()) return;
565
+ paint();
566
+ pop.hidden = false;
567
+ btn.setAttribute("aria-expanded", "true");
568
+ if (opts.onOpen) opts.onOpen();
569
+ const first = pop.querySelector("a[href], button:not([disabled])");
570
+ (first || pop).focus();
571
+ }
572
+ function close(focus = true) {
573
+ if (!isOpen()) return;
574
+ pop.hidden = true;
575
+ btn.setAttribute("aria-expanded", "false");
576
+ if (opts.onClose) opts.onClose();
577
+ if (focus) focusBack(btn);
578
+ }
579
+ function setCount(n) {
580
+ const c = Number(n) || 0;
581
+ const badge = btn.querySelector(".den-bell-n");
582
+ if (badge) {
583
+ badge.hidden = c <= 0;
584
+ badge.textContent = c <= 0 ? "0" : bellCount(c);
585
+ }
586
+ btn.setAttribute(
587
+ "aria-label",
588
+ c > 0 ? kitT(lang(), "kit.bell.n", { n: c }) : word,
589
+ );
590
+ }
591
+ btn.addEventListener("click", () => (isOpen() ? close() : open()));
592
+ document.addEventListener("keydown", (e) => {
593
+ if (e.key !== "Escape" || !isOpen()) return;
594
+ if (opts.onEscape && opts.onEscape(e) === true) return;
595
+ e.preventDefault();
596
+ close(true);
597
+ });
598
+ document.addEventListener("pointerdown", (e) => {
599
+ if (isOpen() && !pop.contains(e.target) && !btn.contains(e.target))
600
+ close(false);
601
+ });
602
+ return { el: pop, open, close, isOpen, paint, setCount };
603
+ }
604
+
605
+ /** The account entry: an action, or its menu (see the top). */
606
+ export function hydrateAccount(root, opts = {}) {
607
+ const lang = () =>
608
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
609
+ let menu;
610
+ if (opts.menu) {
611
+ // Delegated: a host may draw the rail again (and the entry with it).
612
+ const sel = '[data-den-account][aria-haspopup="menu"]';
613
+ const at = (e) =>
614
+ e.target && e.target.closest ? e.target.closest(sel) : null;
615
+ menu = popMenu(null, {
616
+ delegated: true,
617
+ id: "den-account",
618
+ label: () => "",
619
+ html: () => accountMenuHtml({ lang: lang(), ...(opts.menu() || {}) }),
620
+ onPick: opts.onPick,
621
+ onSubmit: opts.onSubmit,
622
+ });
623
+ root.addEventListener("click", (e) => {
624
+ const b = at(e);
625
+ if (b && root.contains(b)) menu.toggle(b, null);
626
+ });
627
+ }
628
+ root.addEventListener("click", (e) => {
629
+ const a =
630
+ e.target && e.target.closest
631
+ ? e.target.closest('[data-den-account][data-act="account"]')
632
+ : null;
633
+ if (!a || !root.contains(a)) return;
634
+ if (opts.onAction) opts.onAction(e);
635
+ });
636
+ return menu;
637
+ }
638
+
639
+ const TOP_MARGIN = 8;
640
+
641
+ /** ◐ ▾ and its menu (see the top). */
642
+ export function hydrateAppearance(btn, opts = {}) {
643
+ const lang = () =>
644
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
645
+ return popMenu(btn, {
646
+ id: "appearance-btn",
647
+ label: () => kitT(lang(), "kit.appearance"),
648
+ html: () =>
649
+ appearanceMenuHtml({
650
+ lang: lang(),
651
+ ...((opts.menu ? opts.menu() : null) || {}),
652
+ }),
653
+ onPick: opts.onPick,
654
+ });
655
+ }
656
+
657
+ /**
658
+ * A kit menu on <body> for a button (◐ ▾, the account entry): placed by
659
+ * it, kept in the window. o: { id (the button's, if it has none), label(),
660
+ * html(), onPick(kind, value, row), onSubmit(event, form) }. Opens on
661
+ * the checked entry, else the first; ↑ ↓ Home End move; Esc or Tab
662
+ * closes it and focus goes back to the button; a press outside closes
663
+ * it. A form's submit button submits it (onSubmit first: preventDefault
664
+ * there keeps it from posting, and closes the menu).
665
+ */
666
+ function popMenu(el, o) {
667
+ // The button it is open for: `el`, or (delegated) the one open() got.
668
+ let btn = el;
669
+ let pop = null;
670
+ const rows = () => (pop ? all(pop, ".den-mi") : []);
671
+ const isOpen = () => !!pop;
672
+ function close(refocus) {
673
+ if (!pop) return;
674
+ pop.remove();
675
+ pop = null;
676
+ btn.setAttribute("aria-expanded", "false");
677
+ btn.removeAttribute("aria-controls");
678
+ if (refocus) focusBack(btn);
679
+ }
680
+ // Under the button, kept in the window (as a host's own menus are).
681
+ function place() {
682
+ const r = btn.getBoundingClientRect();
683
+ const gap = 6;
684
+ const w = pop.offsetWidth;
685
+ let h = pop.offsetHeight;
686
+ const vw = window.innerWidth;
687
+ const vh = window.innerHeight;
688
+ const below = vh - r.bottom - gap - TOP_MARGIN;
689
+ const above = r.top - gap - TOP_MARGIN;
690
+ const up = h > below && above > below;
691
+ const room = Math.max(80, up ? above : below);
692
+ if (h > room) {
693
+ pop.style.setProperty("max-height", room + "px");
694
+ h = room;
695
+ }
696
+ const top = up ? r.top - gap - h : r.bottom + gap;
697
+ const left = Math.max(TOP_MARGIN, Math.min(r.left, vw - w - TOP_MARGIN));
698
+ pop.style.setProperty("top", Math.max(TOP_MARGIN, top) + "px");
699
+ pop.style.setProperty("left", left + "px");
700
+ }
701
+ function open(at, from) {
702
+ if (pop) return;
703
+ if (from) btn = from;
704
+ pop = document.createElement("div");
705
+ pop.className = "den-pop den-um";
706
+ pop.tabIndex = -1;
707
+ pop.id = (btn.id || o.id) + "-menu";
708
+ pop.setAttribute("role", "menu");
709
+ pop.setAttribute("aria-label", btn.getAttribute("aria-label") || o.label());
710
+ pop.innerHTML = o.html();
711
+ document.body.appendChild(pop);
712
+ btn.setAttribute("aria-expanded", "true");
713
+ btn.setAttribute("aria-controls", pop.id);
714
+ place();
715
+ pop.addEventListener("click", (e) => {
716
+ const row =
717
+ e.target && e.target.closest ? e.target.closest(".den-mi") : null;
718
+ // A form's own button: the form posts (see submit below).
719
+ if (!row || row.type === "submit") return;
720
+ const kind = row.dataset.kind;
721
+ const value = row.dataset.value;
722
+ if (row.tagName !== "A") e.preventDefault();
723
+ close(true);
724
+ if (o.onPick) o.onPick(kind, value == null ? null : value, row);
725
+ });
726
+ pop.addEventListener("submit", (e) => {
727
+ if (o.onSubmit) o.onSubmit(e, e.target);
728
+ if (e.defaultPrevented) close(true);
729
+ });
730
+ pop.addEventListener("keydown", (e) => {
731
+ const list = rows();
732
+ const i = list.indexOf(document.activeElement);
733
+ const next =
734
+ e.key === "ArrowDown"
735
+ ? (i + 1) % list.length
736
+ : e.key === "ArrowUp"
737
+ ? (i - 1 + list.length) % list.length
738
+ : e.key === "Home"
739
+ ? 0
740
+ : e.key === "End"
741
+ ? list.length - 1
742
+ : -1;
743
+ if (next >= 0 && list.length) {
744
+ e.preventDefault();
745
+ list[next].focus();
746
+ } else if (e.key === "Escape") {
747
+ e.preventDefault();
748
+ e.stopPropagation();
749
+ close(true);
750
+ } else if (e.key === "Tab") close(true);
751
+ else if (e.key === " " && list[i] && list[i].tagName === "A") {
752
+ e.preventDefault();
753
+ list[i].click();
754
+ }
755
+ });
756
+ const list = rows();
757
+ const first =
758
+ at === "last"
759
+ ? list[list.length - 1]
760
+ : pop.querySelector('.den-mi[aria-checked="true"]') || list[0];
761
+ if (first) first.focus();
762
+ else pop.focus();
763
+ }
764
+ if (!o.delegated) {
765
+ btn.addEventListener("click", () => (pop ? close(false) : open()));
766
+ btn.addEventListener("keydown", (e) => {
767
+ if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
768
+ e.preventDefault();
769
+ if (!pop) open(e.key === "ArrowUp" ? "last" : null);
770
+ });
771
+ }
772
+ document.addEventListener("mousedown", (e) => {
773
+ if (!pop) return;
774
+ if (pop.contains(e.target) || btn.contains(e.target)) return;
775
+ close(false);
776
+ });
777
+ document.addEventListener("keydown", (e) => {
778
+ if (e.key === "Escape" && pop && !e.defaultPrevented) {
779
+ e.preventDefault();
780
+ close(true);
781
+ }
782
+ });
783
+ window.addEventListener("resize", () => close(false));
784
+ return {
785
+ open: (from) => open(null, from),
786
+ close: (r) => close(!!r),
787
+ isOpen,
788
+ // Delegated: a click on `from` opens it there, or closes it.
789
+ toggle: (from, at) =>
790
+ pop && btn === from ? close(false) : (close(false), open(at, from)),
791
+ };
792
+ }
793
+
794
+ /** The update-waiting pill after the version chip (see the top). */
795
+ export function setWaiting(o, root = document) {
796
+ const find = (id) =>
797
+ root.getElementById
798
+ ? root.getElementById(id)
799
+ : root.querySelector("#" + id);
800
+ const id = (o && o.id) || "stale-pill";
801
+ let pill = find(id);
802
+ if (!o) {
803
+ if (pill) pill.style.display = "none";
804
+ return null;
805
+ }
806
+ if (!pill) {
807
+ const chip = find("version-pill");
808
+ if (!chip || !chip.parentNode) return null;
809
+ const box = document.createElement("div");
810
+ box.innerHTML = waitingHtml(o);
811
+ pill = box.firstChild;
812
+ chip.parentNode.insertBefore(pill, chip.nextSibling);
813
+ pill.addEventListener("click", (e) => {
814
+ if (pill._onClick) pill._onClick(e);
815
+ });
816
+ }
817
+ pill._onClick = o.onClick || null;
818
+ pill.style.display = "";
819
+ return pill;
820
+ }