@monster-orange/den-ui 0.6.6 → 0.7.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.
@@ -30,8 +30,66 @@
30
30
  * into the rail (the current entry); the top bar and the page are inert
31
31
  * behind a scrim; Escape or the scrim closes it and focus goes back to ☰;
32
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.
33
81
  */
34
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";
35
93
 
36
94
  const all = (root, sel) =>
37
95
  Array.prototype.slice.call(root.querySelectorAll(sel));
@@ -58,6 +116,47 @@ export function hydrateShell(root, opts = {}) {
58
116
  const menu = root.querySelector(".den-menu-btn");
59
117
  let scrim = null;
60
118
 
119
+ // Issue 303: a host chip that gives way keeps its dot and "…"; its
120
+ // whole words are its title and name, so it stays readable and
121
+ // reachable. A name the host set itself is kept.
122
+ const end = root.querySelector(".den-top .den-end");
123
+ function nameHostChips() {
124
+ if (!end) return;
125
+ for (const c of end.children) {
126
+ const words = (c.textContent || "").replace(/\s+/g, " ").trim();
127
+ const set = c.getAttribute("data-den-named");
128
+ if (set != null) {
129
+ // The host changed what the kit set: its own words win.
130
+ if (
131
+ c.getAttribute("title") !== set ||
132
+ c.getAttribute("aria-label") !== set
133
+ ) {
134
+ c.removeAttribute("data-den-named");
135
+ // …and what is left of the kit's words goes.
136
+ if (c.getAttribute("title") === set) c.removeAttribute("title");
137
+ if (c.getAttribute("aria-label") === set)
138
+ c.removeAttribute("aria-label");
139
+ continue;
140
+ }
141
+ } else if (c.hasAttribute("aria-label") || c.hasAttribute("title"))
142
+ continue;
143
+ if (!words || set === words) continue;
144
+ // (Only on a change: a write queues a mutation, which runs this.)
145
+ c.setAttribute("data-den-named", words);
146
+ c.setAttribute("aria-label", words);
147
+ c.setAttribute("title", words);
148
+ }
149
+ }
150
+ nameHostChips();
151
+ if (end && typeof MutationObserver === "function")
152
+ new MutationObserver(nameHostChips).observe(end, {
153
+ childList: true,
154
+ subtree: true,
155
+ characterData: true,
156
+ attributes: true,
157
+ attributeFilter: ["title", "aria-label"],
158
+ });
159
+
61
160
  function rove() {
62
161
  if (!nav) return;
63
162
  const links = all(nav, ".den-ni");
@@ -238,3 +337,525 @@ export function hydrateShell(root, opts = {}) {
238
337
  syncPinToggles: (r, isPinned) => syncPinToggles(r, isPinned, lang()),
239
338
  };
240
339
  }
340
+
341
+ // ── 0.7.0: the scope crumbs, the search, the bell, the account ──────────
342
+
343
+ const ITEMS = '[role="menuitem"], a[href], button:not([disabled])';
344
+
345
+ /** A menu's filter: an entry shows when its words hold what was typed. */
346
+ function filterMenu(menu, text) {
347
+ const want = String(text || "")
348
+ .trim()
349
+ .toLowerCase();
350
+ let shown = 0;
351
+ for (const el of all(menu, ".den-mi")) {
352
+ el.hidden = !!want && !el.textContent.toLowerCase().includes(want);
353
+ if (!el.hidden) shown++;
354
+ }
355
+ const none = menu.querySelector(".den-mnone");
356
+ if (none) none.hidden = shown > 0;
357
+ }
358
+
359
+ /**
360
+ * A key in a menu with a filter field: in the field, ↓ ↑ go to the
361
+ * entries shown, Enter takes the first, Esc clears what was typed, then
362
+ * closes (close(true)), Tab closes; ↑ on the first entry goes back to the
363
+ * field. True when it took the key.
364
+ */
365
+ function filterKey(e, menu, field, close) {
366
+ const list = all(menu, ".den-mi").filter((el) => !el.hidden);
367
+ if (e.target === field) {
368
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
369
+ const to = e.key === "ArrowDown" ? list[0] : list[list.length - 1];
370
+ if (to) to.focus();
371
+ } else if (e.key === "Enter") {
372
+ if (list[0]) list[0].click();
373
+ } else if (e.key === "Escape") {
374
+ if (field.value) {
375
+ field.value = "";
376
+ filterMenu(menu, "");
377
+ } else close(true);
378
+ } else if (e.key === "Tab") {
379
+ close(false);
380
+ return true;
381
+ } else return true;
382
+ e.preventDefault();
383
+ e.stopPropagation();
384
+ return true;
385
+ }
386
+ if (e.key === "ArrowUp" && list[0] === e.target) {
387
+ e.preventDefault();
388
+ e.stopPropagation();
389
+ field.focus();
390
+ return true;
391
+ }
392
+ return false;
393
+ }
394
+
395
+ /**
396
+ * A type-to-filter field for a host's own menu (a role="menu" with .den-mi
397
+ * entries), as the kit's crumb menus have: put first in it, "No match"
398
+ * last, when it has more entries than `filter` allows (see the top).
399
+ * Call it before the menu is shown; focus the field it returns.
400
+ * o: { label, lang, filter?, onClose(refocus) } → the field, or null
401
+ */
402
+ export function attachMenuFilter(menu, o = {}) {
403
+ if (!menu || !menuHasFilter(o.filter, all(menu, ".den-mi").length))
404
+ return null;
405
+ if (!menu.id) menu.id = "den-menu";
406
+ const parts = menuFilterParts(menu.id, o.label, o.lang);
407
+ // A menu owns only its items: its rows move into a role="menu" list
408
+ // (with its name), and the pop-up holds the field, the list, "No match".
409
+ const list = document.createElement("div");
410
+ list.className = "den-mlist";
411
+ list.id = menu.id + "-list";
412
+ list.setAttribute("role", menu.getAttribute("role") || "menu");
413
+ for (const a of ["aria-label", "aria-labelledby"])
414
+ if (menu.hasAttribute(a)) {
415
+ list.setAttribute(a, menu.getAttribute(a));
416
+ menu.removeAttribute(a);
417
+ }
418
+ menu.removeAttribute("role");
419
+ while (menu.firstChild) list.appendChild(menu.firstChild);
420
+ menu.appendChild(list);
421
+ menu.insertAdjacentHTML("afterbegin", parts[0]);
422
+ menu.insertAdjacentHTML("beforeend", parts[1]);
423
+ const field = menu.querySelector("input.den-mfilter");
424
+ const close = (refocus) => {
425
+ if (o.onClose) o.onClose(refocus);
426
+ };
427
+ field.addEventListener("input", () => filterMenu(menu, field.value));
428
+ // First, before the host's own keys for its menu.
429
+ menu.addEventListener(
430
+ "keydown",
431
+ (e) => {
432
+ filterKey(e, menu, field, close);
433
+ },
434
+ true,
435
+ );
436
+ return field;
437
+ }
438
+
439
+ /** The scope crumbs' menus (see the top). */
440
+ export function hydrateScope(root, opts = {}) {
441
+ let open = null; // { btn, menu }
442
+ const menuOf = (btn) => {
443
+ const id = btn.getAttribute("aria-controls");
444
+ return id ? document.getElementById(id) : null;
445
+ };
446
+ const items = (menu) => all(menu, ITEMS).filter((el) => !el.hidden);
447
+ const fieldOf = (menu) => menu.querySelector("input.den-mfilter");
448
+ function close(refocus) {
449
+ if (!open) return;
450
+ const { btn, menu } = open;
451
+ open = null;
452
+ menu.hidden = true;
453
+ btn.setAttribute("aria-expanded", "false");
454
+ if (refocus) focusBack(btn);
455
+ }
456
+ function show(btn, at) {
457
+ const menu = menuOf(btn);
458
+ if (!menu) return;
459
+ if (open && open.btn !== btn) close(false);
460
+ // Below its ▾: the menu sits in the scope group, which places it.
461
+ const group = btn.closest(".den-scope");
462
+ const x = group
463
+ ? btn.getBoundingClientRect().left - group.getBoundingClientRect().left
464
+ : 0;
465
+ menu.style.setProperty("--den-menu-x", Math.round(x) + "px");
466
+ menu.hidden = false;
467
+ btn.setAttribute("aria-expanded", "true");
468
+ open = { btn, menu };
469
+ const field = fieldOf(menu);
470
+ if (field) {
471
+ // A long menu opens on its filter, empty.
472
+ field.value = "";
473
+ filterMenu(menu, "");
474
+ field.focus();
475
+ return;
476
+ }
477
+ const list = items(menu);
478
+ if (at === "first" && list[0]) list[0].focus();
479
+ else if (at === "last" && list.length) list[list.length - 1].focus();
480
+ }
481
+ root.addEventListener("click", (e) => {
482
+ const btn =
483
+ e.target && e.target.closest ? e.target.closest(".den-scope-btn") : null;
484
+ if (!btn || !root.contains(btn)) return;
485
+ if (!menuOf(btn)) {
486
+ if (opts.onOpen) opts.onOpen(btn.dataset.scope, btn);
487
+ return;
488
+ }
489
+ if (open && open.btn === btn) return close(false);
490
+ // A keyboard's Enter or Space (detail 0) starts on the first entry.
491
+ show(btn, e.detail === 0 ? "first" : null);
492
+ });
493
+ root.addEventListener("keydown", (e) => {
494
+ const t = e.target;
495
+ const btn = t && t.closest ? t.closest(".den-scope-btn") : null;
496
+ if (btn && menuOf(btn) && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
497
+ e.preventDefault();
498
+ show(btn, e.key === "ArrowDown" ? "first" : "last");
499
+ return;
500
+ }
501
+ if (!open || !open.menu.contains(t)) return;
502
+ const list = items(open.menu);
503
+ const field = fieldOf(open.menu);
504
+ if (field && filterKey(e, open.menu, field, close)) return;
505
+ const i = list.indexOf(t);
506
+ let next = -1;
507
+ if (e.key === "ArrowDown") next = (i + 1) % list.length;
508
+ else if (e.key === "ArrowUp") next = (i - 1 + list.length) % list.length;
509
+ else if (e.key === "Home") next = 0;
510
+ else if (e.key === "End") next = list.length - 1;
511
+ else if (e.key === "Escape") {
512
+ e.preventDefault();
513
+ e.stopPropagation();
514
+ close(true);
515
+ return;
516
+ } else if (e.key === "Tab") close(false);
517
+ if (next < 0 || !list.length) return;
518
+ e.preventDefault();
519
+ list[next].focus();
520
+ });
521
+ root.addEventListener("input", (e) => {
522
+ const t = e.target;
523
+ if (open && t && t.matches && t.matches("input.den-mfilter"))
524
+ filterMenu(open.menu, t.value);
525
+ });
526
+ // Esc on the ▾ itself, with its menu open.
527
+ document.addEventListener("keydown", (e) => {
528
+ if (e.key === "Escape" && open && !e.defaultPrevented) {
529
+ e.preventDefault();
530
+ close(true);
531
+ }
532
+ });
533
+ document.addEventListener("pointerdown", (e) => {
534
+ if (!open) return;
535
+ if (open.menu.contains(e.target) || open.btn.contains(e.target)) return;
536
+ close(false);
537
+ });
538
+ return { close: () => close(false), isOpen: () => !!open };
539
+ }
540
+
541
+ /** The search field: a click and ⌘K / Ctrl-K activate it (see the top). */
542
+ export function hydrateSearch(btn, opts = {}) {
543
+ if (!btn) return { activate() {}, destroy() {} };
544
+ function activate(via, e) {
545
+ btn.dispatchEvent(
546
+ new CustomEvent("den-ui:search", { bubbles: true, detail: { via } }),
547
+ );
548
+ if (opts.onActivate) opts.onActivate(via, e);
549
+ }
550
+ const onClick = (e) => activate("click", e);
551
+ const onKey = (e) => {
552
+ if (
553
+ (e.metaKey || e.ctrlKey) &&
554
+ !e.altKey &&
555
+ (e.key === "k" || e.key === "K" || e.code === "KeyK")
556
+ ) {
557
+ e.preventDefault();
558
+ activate("key", e);
559
+ }
560
+ };
561
+ btn.addEventListener("click", onClick);
562
+ if (opts.keys !== false) document.addEventListener("keydown", onKey);
563
+ return {
564
+ activate: (via) => activate(via || "call"),
565
+ destroy() {
566
+ btn.removeEventListener("click", onClick);
567
+ document.removeEventListener("keydown", onKey);
568
+ },
569
+ };
570
+ }
571
+
572
+ /** The bell and its panel (see the top). */
573
+ export function hydrateBell(btn, opts = {}) {
574
+ const lang = () =>
575
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
576
+ const val = (v) => (typeof v === "function" ? v() : v);
577
+ const pop = document.createElement("div");
578
+ pop.className = "den-bell-pop";
579
+ pop.id = opts.id || "den-bell-pop";
580
+ pop.setAttribute("role", "dialog");
581
+ pop.setAttribute("aria-labelledby", opts.titleId || "den-bell-h");
582
+ pop.tabIndex = -1;
583
+ pop.hidden = true;
584
+ document.body.appendChild(pop);
585
+ btn.setAttribute("aria-controls", pop.id);
586
+ const word = btn.getAttribute("title") || kitT(lang(), "kit.bell");
587
+
588
+ function paint() {
589
+ const own = opts.panel ? opts.panel() : null;
590
+ pop.innerHTML =
591
+ typeof own === "string"
592
+ ? own
593
+ : bellPanelHtml({
594
+ lang: lang(),
595
+ title: opts.title,
596
+ titleId: opts.titleId,
597
+ items: val(opts.items) || [],
598
+ empty: opts.empty,
599
+ foot: val(opts.foot) || "",
600
+ ...(own || {}),
601
+ });
602
+ }
603
+ const isOpen = () => !pop.hidden;
604
+ function open() {
605
+ if (isOpen()) return;
606
+ paint();
607
+ pop.hidden = false;
608
+ btn.setAttribute("aria-expanded", "true");
609
+ if (opts.onOpen) opts.onOpen();
610
+ const first = pop.querySelector("a[href], button:not([disabled])");
611
+ (first || pop).focus();
612
+ }
613
+ function close(focus = true) {
614
+ if (!isOpen()) return;
615
+ pop.hidden = true;
616
+ btn.setAttribute("aria-expanded", "false");
617
+ if (opts.onClose) opts.onClose();
618
+ if (focus) focusBack(btn);
619
+ }
620
+ function setCount(n) {
621
+ const c = Number(n) || 0;
622
+ const badge = btn.querySelector(".den-bell-n");
623
+ if (badge) {
624
+ badge.hidden = c <= 0;
625
+ badge.textContent = c <= 0 ? "0" : bellCount(c);
626
+ }
627
+ btn.setAttribute(
628
+ "aria-label",
629
+ c > 0 ? kitT(lang(), "kit.bell.n", { n: c }) : word,
630
+ );
631
+ }
632
+ btn.addEventListener("click", () => (isOpen() ? close() : open()));
633
+ document.addEventListener("keydown", (e) => {
634
+ if (e.key !== "Escape" || !isOpen()) return;
635
+ if (opts.onEscape && opts.onEscape(e) === true) return;
636
+ e.preventDefault();
637
+ close(true);
638
+ });
639
+ document.addEventListener("pointerdown", (e) => {
640
+ if (isOpen() && !pop.contains(e.target) && !btn.contains(e.target))
641
+ close(false);
642
+ });
643
+ return { el: pop, open, close, isOpen, paint, setCount };
644
+ }
645
+
646
+ /** The account entry: an action, or its menu (see the top). */
647
+ export function hydrateAccount(root, opts = {}) {
648
+ const lang = () =>
649
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
650
+ let menu;
651
+ if (opts.menu) {
652
+ // Delegated: a host may draw the rail again (and the entry with it).
653
+ const sel = '[data-den-account][aria-haspopup="menu"]';
654
+ const at = (e) =>
655
+ e.target && e.target.closest ? e.target.closest(sel) : null;
656
+ menu = popMenu(null, {
657
+ delegated: true,
658
+ id: "den-account",
659
+ label: () => "",
660
+ html: () => accountMenuHtml({ lang: lang(), ...(opts.menu() || {}) }),
661
+ onPick: opts.onPick,
662
+ onSubmit: opts.onSubmit,
663
+ });
664
+ root.addEventListener("click", (e) => {
665
+ const b = at(e);
666
+ if (b && root.contains(b)) menu.toggle(b, null);
667
+ });
668
+ }
669
+ root.addEventListener("click", (e) => {
670
+ const a =
671
+ e.target && e.target.closest
672
+ ? e.target.closest('[data-den-account][data-act="account"]')
673
+ : null;
674
+ if (!a || !root.contains(a)) return;
675
+ if (opts.onAction) opts.onAction(e);
676
+ });
677
+ return menu;
678
+ }
679
+
680
+ const TOP_MARGIN = 8;
681
+
682
+ /** ◐ ▾ and its menu (see the top). */
683
+ export function hydrateAppearance(btn, opts = {}) {
684
+ const lang = () =>
685
+ typeof opts.lang === "function" ? opts.lang() : opts.lang;
686
+ return popMenu(btn, {
687
+ id: "appearance-btn",
688
+ label: () => kitT(lang(), "kit.appearance"),
689
+ html: () =>
690
+ appearanceMenuHtml({
691
+ lang: lang(),
692
+ ...((opts.menu ? opts.menu() : null) || {}),
693
+ }),
694
+ onPick: opts.onPick,
695
+ });
696
+ }
697
+
698
+ /**
699
+ * A kit menu on <body> for a button (◐ ▾, the account entry): placed by
700
+ * it, kept in the window. o: { id (the button's, if it has none), label(),
701
+ * html(), onPick(kind, value, row), onSubmit(event, form) }. Opens on
702
+ * the checked entry, else the first; ↑ ↓ Home End move; Esc or Tab
703
+ * closes it and focus goes back to the button; a press outside closes
704
+ * it. A form's submit button submits it (onSubmit first: preventDefault
705
+ * there keeps it from posting, and closes the menu).
706
+ */
707
+ function popMenu(el, o) {
708
+ // The button it is open for: `el`, or (delegated) the one open() got.
709
+ let btn = el;
710
+ let pop = null;
711
+ const rows = () => (pop ? all(pop, ".den-mi") : []);
712
+ const isOpen = () => !!pop;
713
+ function close(refocus) {
714
+ if (!pop) return;
715
+ pop.remove();
716
+ pop = null;
717
+ btn.setAttribute("aria-expanded", "false");
718
+ btn.removeAttribute("aria-controls");
719
+ if (refocus) focusBack(btn);
720
+ }
721
+ // Under the button, kept in the window (as a host's own menus are).
722
+ function place() {
723
+ const r = btn.getBoundingClientRect();
724
+ const gap = 6;
725
+ const w = pop.offsetWidth;
726
+ let h = pop.offsetHeight;
727
+ const vw = window.innerWidth;
728
+ const vh = window.innerHeight;
729
+ const below = vh - r.bottom - gap - TOP_MARGIN;
730
+ const above = r.top - gap - TOP_MARGIN;
731
+ const up = h > below && above > below;
732
+ const room = Math.max(80, up ? above : below);
733
+ if (h > room) {
734
+ pop.style.setProperty("max-height", room + "px");
735
+ h = room;
736
+ }
737
+ const top = up ? r.top - gap - h : r.bottom + gap;
738
+ const left = Math.max(TOP_MARGIN, Math.min(r.left, vw - w - TOP_MARGIN));
739
+ pop.style.setProperty("top", Math.max(TOP_MARGIN, top) + "px");
740
+ pop.style.setProperty("left", left + "px");
741
+ }
742
+ function open(at, from) {
743
+ if (pop) return;
744
+ if (from) btn = from;
745
+ pop = document.createElement("div");
746
+ pop.className = "den-pop den-um";
747
+ pop.tabIndex = -1;
748
+ pop.id = (btn.id || o.id) + "-menu";
749
+ pop.setAttribute("role", "menu");
750
+ pop.setAttribute("aria-label", btn.getAttribute("aria-label") || o.label());
751
+ pop.innerHTML = o.html();
752
+ document.body.appendChild(pop);
753
+ btn.setAttribute("aria-expanded", "true");
754
+ btn.setAttribute("aria-controls", pop.id);
755
+ place();
756
+ pop.addEventListener("click", (e) => {
757
+ const row =
758
+ e.target && e.target.closest ? e.target.closest(".den-mi") : null;
759
+ // A form's own button: the form posts (see submit below).
760
+ if (!row || row.type === "submit") return;
761
+ const kind = row.dataset.kind;
762
+ const value = row.dataset.value;
763
+ if (row.tagName !== "A") e.preventDefault();
764
+ close(true);
765
+ if (o.onPick) o.onPick(kind, value == null ? null : value, row);
766
+ });
767
+ pop.addEventListener("submit", (e) => {
768
+ if (o.onSubmit) o.onSubmit(e, e.target);
769
+ if (e.defaultPrevented) close(true);
770
+ });
771
+ pop.addEventListener("keydown", (e) => {
772
+ const list = rows();
773
+ const i = list.indexOf(document.activeElement);
774
+ const next =
775
+ e.key === "ArrowDown"
776
+ ? (i + 1) % list.length
777
+ : e.key === "ArrowUp"
778
+ ? (i - 1 + list.length) % list.length
779
+ : e.key === "Home"
780
+ ? 0
781
+ : e.key === "End"
782
+ ? list.length - 1
783
+ : -1;
784
+ if (next >= 0 && list.length) {
785
+ e.preventDefault();
786
+ list[next].focus();
787
+ } else if (e.key === "Escape") {
788
+ e.preventDefault();
789
+ e.stopPropagation();
790
+ close(true);
791
+ } else if (e.key === "Tab") close(true);
792
+ else if (e.key === " " && list[i] && list[i].tagName === "A") {
793
+ e.preventDefault();
794
+ list[i].click();
795
+ }
796
+ });
797
+ const list = rows();
798
+ const first =
799
+ at === "last"
800
+ ? list[list.length - 1]
801
+ : pop.querySelector('.den-mi[aria-checked="true"]') || list[0];
802
+ if (first) first.focus();
803
+ else pop.focus();
804
+ }
805
+ if (!o.delegated) {
806
+ btn.addEventListener("click", () => (pop ? close(false) : open()));
807
+ btn.addEventListener("keydown", (e) => {
808
+ if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
809
+ e.preventDefault();
810
+ if (!pop) open(e.key === "ArrowUp" ? "last" : null);
811
+ });
812
+ }
813
+ document.addEventListener("mousedown", (e) => {
814
+ if (!pop) return;
815
+ if (pop.contains(e.target) || btn.contains(e.target)) return;
816
+ close(false);
817
+ });
818
+ document.addEventListener("keydown", (e) => {
819
+ if (e.key === "Escape" && pop && !e.defaultPrevented) {
820
+ e.preventDefault();
821
+ close(true);
822
+ }
823
+ });
824
+ window.addEventListener("resize", () => close(false));
825
+ return {
826
+ open: (from) => open(null, from),
827
+ close: (r) => close(!!r),
828
+ isOpen,
829
+ // Delegated: a click on `from` opens it there, or closes it.
830
+ toggle: (from, at) =>
831
+ pop && btn === from ? close(false) : (close(false), open(at, from)),
832
+ };
833
+ }
834
+
835
+ /** The update-waiting pill after the version chip (see the top). */
836
+ export function setWaiting(o, root = document) {
837
+ const find = (id) =>
838
+ root.getElementById
839
+ ? root.getElementById(id)
840
+ : root.querySelector("#" + id);
841
+ const id = (o && o.id) || "stale-pill";
842
+ let pill = find(id);
843
+ if (!o) {
844
+ if (pill) pill.style.display = "none";
845
+ return null;
846
+ }
847
+ if (!pill) {
848
+ const chip = find("version-pill");
849
+ if (!chip || !chip.parentNode) return null;
850
+ const box = document.createElement("div");
851
+ box.innerHTML = waitingHtml(o);
852
+ pill = box.firstChild;
853
+ chip.parentNode.insertBefore(pill, chip.nextSibling);
854
+ pill.addEventListener("click", (e) => {
855
+ if (pill._onClick) pill._onClick(e);
856
+ });
857
+ }
858
+ pill._onClick = o.onClick || null;
859
+ pill.style.display = "";
860
+ return pill;
861
+ }