diff --git a/.changeset/4230-untrusted-escape.md b/.changeset/4230-untrusted-escape.md new file mode 100644 index 0000000000..521f7381ae --- /dev/null +++ b/.changeset/4230-untrusted-escape.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-kit": patch +--- + +A dispatched (untrusted) Escape keydown closes an open panel again. A synthetic keydown raises no platform close request, so it never reached the panel's close watcher, and the panel stayed open. A real Escape and Android back still arrive through the watcher, once. diff --git a/packages/ui-kit/src/internal/close-request.ts b/packages/ui-kit/src/internal/close-request.ts index 8b1a890fa3..5f9f9e27a3 100644 --- a/packages/ui-kit/src/internal/close-request.ts +++ b/packages/ui-kit/src/internal/close-request.ts @@ -109,9 +109,11 @@ export function watchCloseRequests(document: Document, request: () => boolean, o } }; const keys = (event: KeyboardEvent) => { - // With an armed watcher, Escape is the platform's close request and - // arrives through the watcher; handling it here too would close twice. - if (watcher || destroyed || event.key !== "Escape" || event.defaultPrevented || event.isComposing || !topmost()) return; + // With an armed watcher, a real Escape is the platform's close request and + // arrives through the watcher; handling it here too would close twice. A + // dispatched (untrusted) Escape never raises a close request, so it is + // handled here whether or not a watcher is armed (#4230). + if ((watcher && event.isTrusted) || destroyed || event.key !== "Escape" || event.defaultPrevented || event.isComposing || !topmost()) return; // Prevented only when the surface closed. A refusal (a modal over it, // closedBy="none", a finishing take) leaves the keydown for whatever // takes it next: a dialog above needs it for its own cancel. diff --git a/packages/ui-kit/src/internal/overlay-stack.ts b/packages/ui-kit/src/internal/overlay-stack.ts index 021eee2cc8..925075e9bb 100644 --- a/packages/ui-kit/src/internal/overlay-stack.ts +++ b/packages/ui-kit/src/internal/overlay-stack.ts @@ -22,11 +22,13 @@ export function registerOverlay(document: Document, entry: Entry): () => void { const entries: Registered[] = []; const keys = (event: KeyboardEvent) => { const top = entries.at(-1); - // A watching top entry receives Escape as the platform's close request, - // through its watcher: taking it here as well would close twice. An - // entry with a surface opened above it (a dictation field over a - // drawer) is not the topmost surface, so the Escape is that surface's. - if (event.key !== "Escape" || event.defaultPrevented || event.isComposing || !top || top.modal || top.watch?.watching || top.watch?.topmost === false) return; + // A watching top entry receives a real Escape as the platform's close + // request, through its watcher: taking it here as well would close + // twice. A dispatched (untrusted) Escape raises no close request, so it + // is taken here (#4230). An entry with a surface opened above it (a + // dictation field over a drawer) is not the topmost surface, so the + // Escape is that surface's. + if (event.key !== "Escape" || event.defaultPrevented || event.isComposing || !top || top.modal || (top.watch?.watching && event.isTrusted) || top.watch?.topmost === false) return; event.preventDefault(); event.stopPropagation(); top.escape(); diff --git a/packages/ui-react/tests/browser/close-requests.pointer.tsx b/packages/ui-react/tests/browser/close-requests.pointer.tsx index 2330aaf6a3..96803fb33d 100644 --- a/packages/ui-react/tests/browser/close-requests.pointer.tsx +++ b/packages/ui-react/tests/browser/close-requests.pointer.tsx @@ -157,6 +157,23 @@ for (const surface of SURFACES) { }); } +for (const surface of SURFACES) test(`${surface.name}: a dispatched (untrusted) Escape still closes it once while its watcher is armed`, async ctx => { + // A synthetic keydown raises no close request, so the watcher never sees + // it; the keydown path must take it, once (#4230: the storybook play + // functions dispatch their Escape this way). + const seen = spyCloseWatcher(ctx); + const s = await mount(ctx, surface.width); + const opener = s.tab(surface.tab); + opener.focus(); await userEvent.click(opener); await frame(); + expect(seen.created, "armed").toBeGreaterThan(0); + const escape = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }); + expect(escape.isTrusted).toBe(false); + (document.activeElement ?? document.body).dispatchEvent(escape); await frame(); + expect(s.ui.stores.ui.getState()[surface.flag], `${surface.name} closed on the dispatched Escape`).toBe(false); + expect(seen.closed, "the watcher saw no close request").toBe(0); + await expect.poll(() => document.activeElement, { message: "focus returns to the opener" }).toBe(opener); +}); + test("a modal over a destination panel takes the first close request, the panel the next", async ctx => { const seen = spyCloseWatcher(ctx); const s = await mount(ctx, 390);