diff --git a/apps/web/src/components/chat/ComposerPendingUserInputPanel.browser.test.tsx b/apps/web/src/components/chat/ComposerPendingUserInputPanel.browser.test.tsx index 40022b45bc845..846a6be5b9e89 100644 --- a/apps/web/src/components/chat/ComposerPendingUserInputPanel.browser.test.tsx +++ b/apps/web/src/components/chat/ComposerPendingUserInputPanel.browser.test.tsx @@ -101,3 +101,38 @@ it.each<{ focus: string; control: ReactNode; toggles: boolean }>([ expect(onToggleOption.mock.calls).toEqual(toggles ? [["question", "Incremental"]] : []); }, ); + +it.each<{ keys: string; toggles: boolean }>([ + { keys: "{Alt>}1{/Alt}", toggles: true }, + { keys: "1", toggles: false }, + { keys: "{Meta>}1{/Meta}", toggles: false }, + { keys: "{Alt>}{Shift>}1{/Shift}{/Alt}", toggles: false }, + { keys: "{Alt>}2{/Alt}", toggles: false }, +])("$keys in the answer box toggles an option: $toggles", async ({ keys, toggles }) => { + const onToggleOption = vi.fn(); + await using screen = await render( + <> +
+ + , + ); + await expect.element(screen.getByText("Incremental")).toBeVisible(); + screen.getByLabelText("Answer").element().focus(); + + await userEvent.keyboard(keys); + + expect(onToggleOption.mock.calls).toEqual(toggles ? [["question", "Incremental"]] : []); + if (toggles) { + await expect.element(screen.getByLabelText("Answer")).toHaveTextContent(""); + } +}); diff --git a/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx b/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx index ba811c37b73f8..f6b8a8f8715af 100644 --- a/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx +++ b/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx @@ -9,7 +9,7 @@ import { derivePendingUserInputProgress } from "../../pendingUserInput"; import { CheckboxMark } from "../ui/checkbox"; import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; import { Kbd } from "../ui/kbd"; -import { cn } from "~/lib/utils"; +import { cn, isMacPlatform } from "~/lib/utils"; import { isInSideChatPane } from "~/lib/sideChatPane"; import { ComposerBanner } from "./ComposerBanner"; import ChatMarkdown from "../ChatMarkdown"; @@ -151,21 +151,29 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard( useEffect(() => { if (!activeQuestion || responseDisabled || isCollapsed || backgroundThread) return; const handler = (event: globalThis.KeyboardEvent) => { - if (event.metaKey || event.ctrlKey || event.altKey) return; const target = event.target; if (isInSideChatPane(target)) return; - if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) { - return; - } - if ( - target instanceof HTMLElement && - target.closest( - 'select, [contenteditable]:not([contenteditable="false"]), [role="dialog"], [aria-modal="true"], [role="menu"], [role="combobox"], [data-slot$="popup"]', - ) - ) { - return; + // Focus starts in the answer box, where a bare digit types, so a modifier selects there. + const inAnswerBox = + target instanceof Element && target.closest('[data-testid="composer-editor"]') !== null; + let digit: number; + if (inAnswerBox) { + digit = answerBoxShortcutDigit(event); + } else { + if (event.metaKey || event.ctrlKey || event.altKey) return; + if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) { + return; + } + if ( + target instanceof HTMLElement && + target.closest( + 'select, [contenteditable]:not([contenteditable="false"]), [role="dialog"], [aria-modal="true"], [role="menu"], [role="combobox"], [data-slot$="popup"]', + ) + ) { + return; + } + digit = Number.parseInt(event.key, 10); } - const digit = Number.parseInt(event.key, 10); if (Number.isNaN(digit) || digit < 1 || digit > 9) return; const optionIndex = digit - 1; if (optionIndex >= activeQuestion.options.length) return; @@ -364,3 +372,14 @@ function OptionRadio({ checked }: { checked: boolean }) { ); } + +// Option+digit everywhere, plus Control+digit on macOS. Mod+digit (Cmd on macOS, Ctrl elsewhere) +// jumps between threads. Option rewrites event.key on macOS, so the digit comes from event.code. +function answerBoxShortcutDigit(event: globalThis.KeyboardEvent): number { + if (event.metaKey || event.shiftKey) return Number.NaN; + const optionDigit = event.altKey && !event.ctrlKey; + const controlDigit = event.ctrlKey && !event.altKey && isMacPlatform(navigator.platform); + if (!optionDigit && !controlDigit) return Number.NaN; + const match = /^(?:Digit|Numpad)([1-9])$/.exec(event.code); + return match ? Number(match[1]) : Number.NaN; +}