Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/4230-untrusted-escape.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 5 additions & 3 deletions packages/ui-kit/src/internal/close-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 7 additions & 5 deletions packages/ui-kit/src/internal/overlay-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions packages/ui-react/tests/browser/close-requests.pointer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading