TypeScript
fix(web): app shortcuts wait while a confirmation dialog is open (#27157)
Seryözha Dmitriev77f08e6b01e2
diff --git a/apps/web/src/shortcuts/dispatcher.browser.test.ts b/apps/web/src/shortcuts/dispatcher.browser.test.tsindex 377f3d3f107..b9bf6c4e4b7 100644--- a/apps/web/src/shortcuts/dispatcher.browser.test.ts+++ b/apps/web/src/shortcuts/dispatcher.browser.test.ts@@ -111,6 +111,25 @@ describe("command dispatcher", () => {expect(calls).toEqual(["command", "observe:true", "observe:false"]);});+ it("consumes a command chord pressed inside a confirmation without running its handler", () => {+ const command = vi.fn();+ const dialog = document.createElement("div");+ dialog.setAttribute("role", "alertdialog");+ const cancel = document.createElement("button");+ dialog.append(cancel);+ document.body.append(dialog);+ using stack = new DisposableStack();+ stack.defer(() => dialog.remove());+ stack.defer(registerCommands({ "chat.new": command }, { keybindings }));+ cancel.focus();++ const event = new KeyboardEvent("keydown", { key: "k", cancelable: true, bubbles: true });+ cancel.dispatchEvent(event);++ expect(command).not.toHaveBeenCalled();+ expect(event.defaultPrevented).toBe(true);+ });+it("routes a menu action to its newest handler", () => {const older = vi.fn();const newer = vi.fn();diff --git a/apps/web/src/shortcuts/dispatcher.ts b/apps/web/src/shortcuts/dispatcher.tsindex c5ef4274aed..5ce799cf482 100644--- a/apps/web/src/shortcuts/dispatcher.ts+++ b/apps/web/src/shortcuts/dispatcher.ts@@ -53,6 +53,10 @@ function readFocusContext(event: KeyboardEvent): Partial<ShortcutMatchContext> {};}+function isInsideConfirmation(target: EventTarget | null): boolean {+ return target instanceof Element && target.closest('[role="alertdialog"]') !== null;+}+function runScope(scope: CommandScope, event: KeyboardEvent): boolean {const { commands, options } = scope;if (options.when && !options.when(event)) return false;@@ -63,7 +67,12 @@ function runScope(scope: CommandScope, event: KeyboardEvent): boolean {{ context: { ...readFocusContext(event), ...options.context?.() } },);const handler = command === null ? undefined : commands[command];- return handler !== undefined && handler(event) !== false;+ if (handler === undefined) return false;+ if (isInsideConfirmation(event.target)) {+ event.preventDefault();+ return true;+ }+ return handler(event) !== false;}function runScopes(scopes: readonly CommandScope[], event: KeyboardEvent): boolean {