Skip to content

Commit 77f08e6

Browse files
fix(web): app shortcuts wait while a confirmation dialog is open (#27157)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7e83902 commit 77f08e6

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎apps/web/src/shortcuts/dispatcher.browser.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ describe("command dispatcher", () => {
111111
expect(calls).toEqual(["command", "observe:true", "observe:false"]);
112112
});
113113

114+
it("consumes a command chord pressed inside a confirmation without running its handler", () => {
115+
const command = vi.fn();
116+
const dialog = document.createElement("div");
117+
dialog.setAttribute("role", "alertdialog");
118+
const cancel = document.createElement("button");
119+
dialog.append(cancel);
120+
document.body.append(dialog);
121+
using stack = new DisposableStack();
122+
stack.defer(() => dialog.remove());
123+
stack.defer(registerCommands({ "chat.new": command }, { keybindings }));
124+
cancel.focus();
125+
126+
const event = new KeyboardEvent("keydown", { key: "k", cancelable: true, bubbles: true });
127+
cancel.dispatchEvent(event);
128+
129+
expect(command).not.toHaveBeenCalled();
130+
expect(event.defaultPrevented).toBe(true);
131+
});
132+
114133
it("routes a menu action to its newest handler", () => {
115134
const older = vi.fn();
116135
const newer = vi.fn();

‎apps/web/src/shortcuts/dispatcher.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ function readFocusContext(event: KeyboardEvent): Partial<ShortcutMatchContext> {
5353
};
5454
}
5555

56+
function isInsideConfirmation(target: EventTarget | null): boolean {
57+
return target instanceof Element && target.closest('[role="alertdialog"]') !== null;
58+
}
59+
5660
function runScope(scope: CommandScope, event: KeyboardEvent): boolean {
5761
const { commands, options } = scope;
5862
if (options.when && !options.when(event)) return false;
@@ -63,7 +67,12 @@ function runScope(scope: CommandScope, event: KeyboardEvent): boolean {
6367
{ context: { ...readFocusContext(event), ...options.context?.() } },
6468
);
6569
const handler = command === null ? undefined : commands[command];
66-
return handler !== undefined && handler(event) !== false;
70+
if (handler === undefined) return false;
71+
if (isInsideConfirmation(event.target)) {
72+
event.preventDefault();
73+
return true;
74+
}
75+
return handler(event) !== false;
6776
}
6877

6978
function runScopes(scopes: readonly CommandScope[], event: KeyboardEvent): boolean {

0 commit comments

Comments
 (0)