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
scout/forge:scout/11337-escape-closes-the-forge-toolbar-s-open-p → main 6 commits fork
3 files
changed +44
−6
src/apps/toolbar.ts +8 −1
| @@ -542,11 +542,18 @@ const toolbarSource = `(function () { | ||
| 542 | 542 | set(panel.hidden); |
| 543 | 543 | }); |
| 544 | 544 | 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) { | |
| 546 | 547 | if (e.key !== "Escape" || panel.hidden) return; |
| 547 | 548 | set(false); |
| 548 | 549 | pill.focus(); |
| 549 | 550 | }); |
| 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 | }); | |
| 550 | 557 | document.addEventListener("pointerdown", function (e) { |
| 551 | 558 | if (panel.hidden) return; |
| 552 | 559 | 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", () => { | ||
| 433 | 433 | appendChild(c: El): El; |
| 434 | 434 | addEventListener(t: string, f: (e?: unknown) => void): void; |
| 435 | 435 | focus(): void; |
| 436 | contains(n: unknown): boolean; | |
| 436 | 437 | attachShadow(): El; |
| 437 | 438 | } |
| 438 | 439 | const make = (tag: string): El => ({ |
| @@ -448,15 +449,18 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 448 | 449 | appendChild(c) { this.children.push(c); return c; }, |
| 449 | 450 | addEventListener(t, f) { (this.listeners[t] ??= []).push(f); }, |
| 450 | 451 | focus() { this.focused = true; }, |
| 452 | contains(n) { return n === this || this.children.some((c) => c.contains(n)); }, | |
| 451 | 453 | attachShadow() { return make("shadow"); }, |
| 452 | 454 | }); |
| 453 | 455 | const all = (el: El): El[] => [el, ...el.children.flatMap(all)]; |
| 454 | 456 | // The script keeps its tree in the shadow root; capture it. |
| 455 | 457 | function tree(data: Record<string, string>) { |
| 456 | 458 | let root!: El; |
| 459 | let host: El | undefined; | |
| 457 | 460 | const orig = make; |
| 458 | 461 | const patched = (tag: string): El => { |
| 459 | 462 | const el = orig(tag); |
| 463 | host ??= el; | |
| 460 | 464 | el.attachShadow = () => (root = make("shadow")); |
| 461 | 465 | return el; |
| 462 | 466 | }; |
| @@ -471,7 +475,10 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 471 | 475 | const els = all(root); |
| 472 | 476 | const pill = els.find((e) => e.tag === "button")!; |
| 473 | 477 | 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 }; | |
| 475 | 482 | } |
| 476 | 483 | |
| 477 | 484 | |
| @@ -490,7 +497,7 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 490 | 497 | }); |
| 491 | 498 | |
| 492 | 499 | 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" }); | |
| 494 | 501 | expect(panel.attrs["aria-label"]).toBeTruthy(); |
| 495 | 502 | expect(panel.hidden).toBe(true); |
| 496 | 503 | pill.listeners.click![0]!(); |
| @@ -498,7 +505,7 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 498 | 505 | expect(panel.focused).toBe(true); |
| 499 | 506 | expect(pill.attrs["aria-expanded"]).toBe("true"); |
| 500 | 507 | pill.focused = false; |
| 501 | doc.listeners.keydown![0]!({ key: "Escape" }); | |
| 508 | fire("keydown", { key: "Escape" }); | |
| 502 | 509 | expect(panel.hidden).toBe(true); |
| 503 | 510 | expect(pill.focused).toBe(true); |
| 504 | 511 | }); |
| @@ -532,7 +539,7 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 532 | 539 | }); |
| 533 | 540 | |
| 534 | 541 | 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" }); | |
| 536 | 543 | expect(panel.style.display).toBe("none"); |
| 537 | 544 | pill.listeners.click![0]!(); |
| 538 | 545 | expect(panel.style.display).toBe("flex"); |
| @@ -542,7 +549,17 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 542 | 549 | doc.listeners.pointerdown![0]!({ composedPath: () => [] }); |
| 543 | 550 | expect(panel.style.display).toBe("none"); |
| 544 | 551 | 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: {} }); | |
| 546 | 563 | expect(panel.style.display).toBe("none"); |
| 547 | 564 | }); |
| 548 | 565 |
test/toolbar-keyboard.test.ts +14
| @@ -169,4 +169,18 @@ describe.skipIf(chromium === undefined)("the pill by keyboard", () => { | ||
| 169 | 169 | await key("Escape", 27); |
| 170 | 170 | expect(await expanded()).toBe("false"); |
| 171 | 171 | }, 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); | |
| 172 | 186 | }); |
new above
offline