Skip to content

Commit b54b999

Browse files
NikaCholadzeclaude
andcommitted
fix(web): composer menu chevrons open while the composer is resting
The "Send options" and "Implementation actions" triggers cancelled pointerdown to keep editor focus. Chromium then skips mousedown, which is the event Base UI opens menus on, so the chevron did nothing and "Send with full history" could not be reached. Cancel mousedown instead: focus stays in the editor and the menu still opens. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30cc788 commit b54b999

2 files changed

Lines changed: 104 additions & 4 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// @vitest-environment jsdom
2+
3+
import { act, type ComponentProps } from "react";
4+
import { createRoot } from "react-dom/client";
5+
import { expect, it, vi } from "vite-plus/test";
6+
7+
vi.mock("~/hooks/useSettings", () => ({
8+
useEnvironmentIdentificationMode: () => "none",
9+
}));
10+
vi.mock("../SidebarStageBackdrop", () => ({
11+
StageBackdropButtonArt: () => null,
12+
useSidebarStageBackdropVariant: () => null,
13+
}));
14+
15+
import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
16+
17+
const baseProps: ComponentProps<typeof ComposerPrimaryActions> = {
18+
compact: false,
19+
canOperateThread: true,
20+
pendingAction: null,
21+
isRunning: false,
22+
canInterrupt: false,
23+
showPlanFollowUpPrompt: false,
24+
promptHasText: true,
25+
isSendBusy: false,
26+
sendDisabledReason: null,
27+
isConnecting: false,
28+
isEnvironmentUnavailable: false,
29+
isPreparingWorktree: false,
30+
hasSendableContent: true,
31+
preserveComposerFocusOnPointerDown: true,
32+
onPreviousPendingQuestion: () => {},
33+
onInterrupt: () => {},
34+
onImplementPlanInNewThread: () => {},
35+
};
36+
37+
// Chromium skips the compatibility mouse events after a canceled pointerdown
38+
// but still fires click. jsdom does not, so the press is spelled out here.
39+
function pressWithMouse(target: Element) {
40+
const init = { bubbles: true, cancelable: true, button: 0 };
41+
const pointerDown = new MouseEvent("pointerdown", init);
42+
Object.defineProperty(pointerDown, "pointerType", { value: "mouse" });
43+
target.dispatchEvent(pointerDown);
44+
if (!pointerDown.defaultPrevented) target.dispatchEvent(new MouseEvent("mousedown", init));
45+
const pointerUp = new MouseEvent("pointerup", init);
46+
Object.defineProperty(pointerUp, "pointerType", { value: "mouse" });
47+
target.dispatchEvent(pointerUp);
48+
if (!pointerDown.defaultPrevented) target.dispatchEvent(new MouseEvent("mouseup", init));
49+
target.dispatchEvent(new MouseEvent("click", init));
50+
}
51+
52+
it.each([
53+
{
54+
trigger: "Send options",
55+
item: "Send with full history (180k tokens)",
56+
props: { compactBeforeSendTokens: 180_000, onSendWithFullHistory: vi.fn() },
57+
handler: "onSendWithFullHistory",
58+
},
59+
{
60+
trigger: "Implementation actions",
61+
item: "Implement in a new thread",
62+
props: {
63+
showPlanFollowUpPrompt: true,
64+
promptHasText: false,
65+
hasSendableContent: false,
66+
onImplementPlanInNewThread: vi.fn(),
67+
},
68+
handler: "onImplementPlanInNewThread",
69+
},
70+
] as const)(
71+
"opens the $trigger menu with a mouse while the composer is resting",
72+
async ({ trigger, item, props, handler }) => {
73+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
74+
const container = document.createElement("div");
75+
document.body.append(container);
76+
const root = createRoot(container);
77+
try {
78+
await act(async () => root.render(<ComposerPrimaryActions {...baseProps} {...props} />));
79+
const button = container.querySelector(`[aria-label="${trigger}"]`)!;
80+
await act(async () => pressWithMouse(button));
81+
// Base UI opens a mousedown-triggered menu on the next animation frame.
82+
await act(async () => new Promise((resolve) => requestAnimationFrame(resolve)));
83+
const menuItem = Array.from(document.querySelectorAll<HTMLElement>('[role="menuitem"]')).find(
84+
(element) => element.textContent === item,
85+
);
86+
expect(menuItem).toBeDefined();
87+
await act(async () => menuItem!.click());
88+
expect(props[handler as keyof typeof props]).toHaveBeenCalledTimes(1);
89+
} finally {
90+
await act(async () => root.unmount());
91+
container.remove();
92+
vi.unstubAllGlobals();
93+
}
94+
},
95+
);

‎apps/web/src/components/chat/ComposerPrimaryActions.tsx‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, type MouseEventHandler, type PointerEventHandler } from "react";
1+
import { memo, type MouseEventHandler } from "react";
22
import { CheckIcon, ChevronDownIcon, ChevronLeftIcon, PlayIcon } from "lucide-react";
33
import { CornerUpRight, ListPlus } from "lucide";
44
import { MorphIcon } from "~/components/MorphIcon";
@@ -79,7 +79,7 @@ const formatPendingPrimaryActionLabel = (input: {
7979
const messageActionPillClassName =
8080
"inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-full bg-message-action font-medium text-base text-message-action-foreground shadow-xs shadow-message-action/24 outline-none hover:bg-message-action-hover focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 disabled:shadow-none sm:text-sm";
8181

82-
const preventPointerFocus: PointerEventHandler<HTMLElement> = (event) => {
82+
const preventPointerFocus: MouseEventHandler<HTMLElement> = (event) => {
8383
event.preventDefault();
8484
};
8585

@@ -113,6 +113,11 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
113113
const pointerFocusProps = preserveComposerFocusOnPointerDown
114114
? { onPointerDown: preventPointerFocus }
115115
: undefined;
116+
// Menu triggers open on mousedown, which a canceled pointerdown suppresses,
117+
// so they keep editor focus by canceling mousedown instead.
118+
const menuTriggerFocusProps = preserveComposerFocusOnPointerDown
119+
? { onMouseDown: preventPointerFocus }
120+
: undefined;
116121
const environmentIdentificationMode = useEnvironmentIdentificationMode();
117122
const shortcutModifiers = useShortcutModifierState();
118123
const isQueuing =
@@ -239,7 +244,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
239244
"h-9 rounded-l-none border-l border-message-action-foreground/20 px-2 sm:h-8",
240245
)}
241246
aria-label="Implementation actions"
242-
{...pointerFocusProps}
247+
{...menuTriggerFocusProps}
243248
disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
244249
/>
245250
}
@@ -302,7 +307,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
302307
"h-9 rounded-l-none border-l border-message-action-foreground/20 px-2 sm:h-8",
303308
)}
304309
aria-label="Send options"
305-
{...pointerFocusProps}
310+
{...menuTriggerFocusProps}
306311
disabled={sendBlocked || !hasSendableContent}
307312
/>
308313
}

0 commit comments

Comments
 (0)