Skip to content

Commit f59dbf2

Browse files
juliusmarmingesandersonstaboclaudecodex
committed
fix(web): composer polish — focus ring, send/stop buttons, command menu, context meter, answer panel
Ported from #2451. Co-authored-by: ss <[email protected]> Co-Authored-By: Claude Opus 4.8 <[email protected]> Co-authored-by: codex <[email protected]>
1 parent 3efabdc commit f59dbf2

11 files changed

Lines changed: 437 additions & 172 deletions

‎apps/web/src/components/ChatView.browser.tsx‎

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,13 +1416,15 @@ async function waitForButtonByText(text: string): Promise<HTMLButtonElement> {
14161416
return waitForElement(() => findButtonByText(text), `Unable to find "${text}" button.`);
14171417
}
14181418

1419-
function findButtonContainingText(text: string): HTMLButtonElement | null {
1420-
return (Array.from(document.querySelectorAll("button")).find((button) =>
1421-
button.textContent?.includes(text),
1422-
) ?? null) as HTMLButtonElement | null;
1419+
function findButtonContainingText(text: string): HTMLElement | null {
1420+
return (
1421+
Array.from(document.querySelectorAll<HTMLElement>('button, [role="button"]')).find((button) =>
1422+
button.textContent?.includes(text),
1423+
) ?? null
1424+
);
14231425
}
14241426

1425-
async function waitForButtonContainingText(text: string): Promise<HTMLButtonElement> {
1427+
async function waitForButtonContainingText(text: string): Promise<HTMLElement> {
14261428
return waitForElement(
14271429
() => findButtonContainingText(text),
14281430
`Unable to find button containing "${text}".`,
@@ -3219,6 +3221,89 @@ describe("ChatView timeline estimator parity (full app)", () => {
32193221
}
32203222
});
32213223

3224+
it("focuses the composer and inserts printable text typed from the page background", async () => {
3225+
const mounted = await mountChatView({
3226+
viewport: DEFAULT_VIEWPORT,
3227+
snapshot: createSnapshotForTargetUser({
3228+
targetMessageId: "msg-user-target-type-to-focus" as MessageId,
3229+
targetText: "type-to-focus target",
3230+
}),
3231+
});
3232+
3233+
const backgroundTarget = document.createElement("div");
3234+
backgroundTarget.tabIndex = -1;
3235+
document.body.append(backgroundTarget);
3236+
3237+
try {
3238+
const composerEditor = await waitForComposerEditor();
3239+
backgroundTarget.focus();
3240+
expect(document.activeElement).not.toBe(composerEditor);
3241+
3242+
const event = new KeyboardEvent("keydown", {
3243+
key: "h",
3244+
bubbles: true,
3245+
cancelable: true,
3246+
});
3247+
backgroundTarget.dispatchEvent(event);
3248+
3249+
await waitForComposerText("h");
3250+
expect(event.defaultPrevented).toBe(true);
3251+
expect(document.activeElement).toBe(composerEditor);
3252+
3253+
window.dispatchEvent(
3254+
new KeyboardEvent("keydown", {
3255+
key: "i",
3256+
bubbles: true,
3257+
cancelable: true,
3258+
}),
3259+
);
3260+
3261+
await waitForComposerText("hi");
3262+
} finally {
3263+
backgroundTarget.remove();
3264+
await mounted.cleanup();
3265+
}
3266+
});
3267+
3268+
it("does not steal printable keys from editable targets or shortcut modifiers", async () => {
3269+
const mounted = await mountChatView({
3270+
viewport: DEFAULT_VIEWPORT,
3271+
snapshot: createSnapshotForTargetUser({
3272+
targetMessageId: "msg-user-target-type-to-focus-guards" as MessageId,
3273+
targetText: "type-to-focus guards target",
3274+
}),
3275+
});
3276+
const input = document.createElement("input");
3277+
document.body.append(input);
3278+
3279+
try {
3280+
input.focus();
3281+
input.dispatchEvent(
3282+
new KeyboardEvent("keydown", {
3283+
key: "x",
3284+
bubbles: true,
3285+
cancelable: true,
3286+
}),
3287+
);
3288+
await waitForLayout();
3289+
expect(useComposerDraftStore.getState().draftsByThreadKey[THREAD_KEY]?.prompt ?? "").toBe("");
3290+
3291+
window.dispatchEvent(
3292+
new KeyboardEvent("keydown", {
3293+
key: "k",
3294+
metaKey: true,
3295+
bubbles: true,
3296+
cancelable: true,
3297+
}),
3298+
);
3299+
await waitForLayout();
3300+
expect(useComposerDraftStore.getState().draftsByThreadKey[THREAD_KEY]?.prompt ?? "").toBe("");
3301+
} finally {
3302+
input.remove();
3303+
await mounted.cleanup();
3304+
}
3305+
});
3306+
32223307
it("uses the active draft route session when changing the base branch", async () => {
32233308
const staleDraftId = draftIdFromPath("/draft/draft-stale-branch-session");
32243309
const activeDraftId = draftIdFromPath("/draft/draft-active-branch-session");

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,35 @@ const EMPTY_PROPOSED_PLANS: Thread["proposedPlans"] = [];
203203
const EMPTY_PROVIDERS: ServerProvider[] = [];
204204
const EMPTY_PROVIDER_SKILLS: ServerProvider["skills"] = [];
205205
const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {};
206+
const TYPE_TO_FOCUS_EDITABLE_SELECTOR = [
207+
"input",
208+
"textarea",
209+
"select",
210+
'[contenteditable="true"]',
211+
'[contenteditable="plaintext-only"]',
212+
'[role="textbox"]',
213+
].join(",");
214+
const TYPE_TO_FOCUS_INTERACTIVE_SELECTOR = [
215+
"button",
216+
"a[href]",
217+
"summary",
218+
'[role="button"]',
219+
'[role="checkbox"]',
220+
'[role="menuitem"]',
221+
'[role="option"]',
222+
'[role="radio"]',
223+
'[role="switch"]',
224+
'[role="tab"]',
225+
].join(",");
226+
const TYPE_TO_FOCUS_FLOATING_LAYER_SELECTOR = [
227+
'[data-slot="dialog"]',
228+
'[data-slot="menu-popup"]',
229+
'[data-slot="select-popup"]',
230+
'[data-slot="popover-popup"]',
231+
'[data-slot="combobox-popup"]',
232+
'[data-slot="autocomplete-popup"]',
233+
].join(",");
234+
206235
type EnvironmentUnavailableState = {
207236
readonly environmentId: EnvironmentId;
208237
readonly label: string;
@@ -211,6 +240,25 @@ type EnvironmentUnavailableState = {
211240

212241
type ThreadPlanCatalogEntry = Pick<Thread, "id" | "proposedPlans">;
213242

243+
function eventTargetElement(target: EventTarget | null): Element | null {
244+
if (target instanceof Element) return target;
245+
if (target instanceof Node) return target.parentElement;
246+
return null;
247+
}
248+
249+
function shouldTypeToFocusComposer(event: KeyboardEvent): boolean {
250+
if (event.defaultPrevented || event.isComposing) return false;
251+
if (event.metaKey || event.ctrlKey || event.altKey) return false;
252+
if (event.key.length !== 1) return false;
253+
254+
const target = eventTargetElement(event.target);
255+
if (target?.closest(TYPE_TO_FOCUS_EDITABLE_SELECTOR)) return false;
256+
if (target?.closest(TYPE_TO_FOCUS_INTERACTIVE_SELECTOR)) return false;
257+
if (document.querySelector(TYPE_TO_FOCUS_FLOATING_LAYER_SELECTOR)) return false;
258+
259+
return true;
260+
}
261+
214262
function useThreadPlanCatalog(threadIds: readonly ThreadId[]): ThreadPlanCatalogEntry[] {
215263
return useStore(
216264
useMemo(() => {
@@ -2702,6 +2750,18 @@ export default function ChatView(props: ChatViewProps) {
27022750
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
27032751
};
27042752

2753+
if (
2754+
!shortcutContext.terminalFocus &&
2755+
!shortcutContext.modelPickerOpen &&
2756+
shouldTypeToFocusComposer(event)
2757+
) {
2758+
if (composerRef.current?.insertTextAtEnd(event.key)) {
2759+
event.preventDefault();
2760+
event.stopPropagation();
2761+
return;
2762+
}
2763+
}
2764+
27052765
const command = resolveShortcutCommand(event, keybindings, {
27062766
context: shortcutContext,
27072767
});

0 commit comments

Comments
 (0)