Public: anyone can read this; the crew can change it.

Escape closes the forge toolbar's open panel and returns focus to the pill #11347

Parked opened this
Closed without merging

scout/forge:scout/11337-escape-closes-the-forge-toolbar-s-open-p → main 6 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +44 −6
src/apps/toolbar.ts +8 −1
@@ -542,11 +542,18 @@ const toolbarSource = `(function () {
542542 set(panel.hidden);
543543 });
544544 ring(pill);
545 document.addEventListener("keydown", function (e) {
545 // Scoped to the toolbar's own element: an Escape the app handles on its page is left alone.
546 box.addEventListener("keydown", function (e) {
546547 if (e.key !== "Escape" || panel.hidden) return;
547548 set(false);
548549 pill.focus();
549550 });
551 // Focus tabbed or moved out of the toolbar to the page closes the panel, as a click outside does.
552 box.addEventListener("focusout", function (e) {
553 var to = e.relatedTarget;
554 if (panel.hidden || !to || to === host || box.contains(to)) return;
555 set(false);
556 });
550557 document.addEventListener("pointerdown", function (e) {
551558 if (panel.hidden) return;
552559 var path = e.composedPath ? e.composedPath() : [];
test/apps-toolbar.test.ts +22 −5
@@ -433,6 +433,7 @@ describe("the pill's keyboard and touch behaviour", () => {
433433 appendChild(c: El): El;
434434 addEventListener(t: string, f: (e?: unknown) => void): void;
435435 focus(): void;
436 contains(n: unknown): boolean;
436437 attachShadow(): El;
437438 }
438439 const make = (tag: string): El => ({
@@ -448,15 +449,18 @@ describe("the pill's keyboard and touch behaviour", () => {
448449 appendChild(c) { this.children.push(c); return c; },
449450 addEventListener(t, f) { (this.listeners[t] ??= []).push(f); },
450451 focus() { this.focused = true; },
452 contains(n) { return n === this || this.children.some((c) => c.contains(n)); },
451453 attachShadow() { return make("shadow"); },
452454 });
453455 const all = (el: El): El[] => [el, ...el.children.flatMap(all)];
454456 // The script keeps its tree in the shadow root; capture it.
455457 function tree(data: Record<string, string>) {
456458 let root!: El;
459 let host: El | undefined;
457460 const orig = make;
458461 const patched = (tag: string): El => {
459462 const el = orig(tag);
463 host ??= el;
460464 el.attachShadow = () => (root = make("shadow"));
461465 return el;
462466 };
@@ -471,7 +475,10 @@ describe("the pill's keyboard and touch behaviour", () => {
471475 const els = all(root);
472476 const pill = els.find((e) => e.tag === "button")!;
473477 const panel = els.find((e) => e.attrs.role === "region")!;
474 return { doc, pill, panel, root, lines: panel.children };
478 // The toolbar's own element inside the shadow root; `fire` runs every listener it has for an event.
479 const box = root.children[0]!;
480 const fire = (type: string, e: unknown = {}) => box.listeners[type]?.forEach((f) => f(e));
481 return { doc, pill, panel, root, host: host!, box, fire, lines: panel.children };
475482 }
476483
477484
@@ -490,7 +497,7 @@ describe("the pill's keyboard and touch behaviour", () => {
490497 });
491498
492499 test("the panel is a labelled region, opens with focus inside, and Escape returns focus to the pill", () => {
493 const { doc, pill, panel } = tree({ app: "notes", env: "production" });
500 const { pill, panel, fire } = tree({ app: "notes", env: "production" });
494501 expect(panel.attrs["aria-label"]).toBeTruthy();
495502 expect(panel.hidden).toBe(true);
496503 pill.listeners.click![0]!();
@@ -498,7 +505,7 @@ describe("the pill's keyboard and touch behaviour", () => {
498505 expect(panel.focused).toBe(true);
499506 expect(pill.attrs["aria-expanded"]).toBe("true");
500507 pill.focused = false;
501 doc.listeners.keydown![0]!({ key: "Escape" });
508 fire("keydown", { key: "Escape" });
502509 expect(panel.hidden).toBe(true);
503510 expect(pill.focused).toBe(true);
504511 });
@@ -532,7 +539,7 @@ describe("the pill's keyboard and touch behaviour", () => {
532539 });
533540
534541 test("display carries the state, because an inline display beats [hidden]", () => {
535 const { doc, pill, panel } = tree({ app: "notes" });
542 const { doc, pill, panel, host, fire } = tree({ app: "notes" });
536543 expect(panel.style.display).toBe("none");
537544 pill.listeners.click![0]!();
538545 expect(panel.style.display).toBe("flex");
@@ -542,7 +549,17 @@ describe("the pill's keyboard and touch behaviour", () => {
542549 doc.listeners.pointerdown![0]!({ composedPath: () => [] });
543550 expect(panel.style.display).toBe("none");
544551 pill.listeners.click![0]!();
545 doc.listeners.keydown![0]!({ key: "Escape" });
552 fire("keydown", { key: "Escape" });
553 expect(panel.style.display).toBe("none");
554 // Focus leaving for the page closes it; focus staying in the toolbar, or going nowhere, does not.
555 pill.listeners.click![0]!();
556 fire("focusout", { relatedTarget: null });
557 expect(panel.style.display).toBe("flex");
558 fire("focusout", { relatedTarget: host });
559 expect(panel.style.display).toBe("flex");
560 fire("focusout", { relatedTarget: panel.children[0] });
561 expect(panel.style.display).toBe("flex");
562 fire("focusout", { relatedTarget: {} });
546563 expect(panel.style.display).toBe("none");
547564 });
548565
test/toolbar-keyboard.test.ts +14
@@ -169,4 +169,18 @@ describe.skipIf(chromium === undefined)("the pill by keyboard", () => {
169169 await key("Escape", 27);
170170 expect(await expanded()).toBe("false");
171171 }, 30_000);
172
173 test("Escape on the app's own page leaves the open panel alone", async () => {
174 await evaluate(`document.getElementById("last").focus()`);
175 await tab();
176 await key("Enter", 13, "\r");
177 expect(await expanded()).toBe("true");
178 await evaluate(`document.getElementById("first").focus()`);
179 await key("Escape", 27);
180 expect(await expanded()).toBe("true");
181 await evaluate(`document.getElementById("last").focus()`);
182 await tab();
183 await key("Escape", 27);
184 expect(await expanded()).toBe("false");
185 }, 30_000);
172186 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs