Skip to content

Commit 8ca3861

Browse files
author
Techno911
committed
fix: keep a narrow follow-up composer open when its model picker is pressed
In a narrow pane the follow-up composer is a single row until it is focused, and it collapses again when focus leaves. An overlay trigger inside the composer (model picker, attachments) blurs the editor on pointerdown, and a real click holds the pointer for longer than the frame in which the focus-loss check runs, so the trigger was not yet reported as expanded and the composer collapsed under the picker. An instant synthetic click never showed it. A pointerdown on an overlay trigger inside the composer now marks the gesture, and the focus-loss check leaves the composer expanded while that mark is set; it clears a tick after pointerup, so a later real focus loss still collapses. Test reproduces the sequence with a not-yet-open trigger and asserts both halves.
1 parent 4eab288 commit 8ca3861

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎apps/app/src/components/promptbox/FollowUpPromptBox.test.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,50 @@ describe("FollowUpPromptBox", () => {
994994
);
995995
});
996996

997+
it("stays expanded when an overlay trigger inside the composer takes the pointer before it opens", async () => {
998+
mocks.isCompactViewport = true;
999+
render(
1000+
<>
1001+
<FollowUpPromptBox
1002+
{...createFollowUpPromptBoxProps({ kind: "ready" })}
1003+
/>
1004+
<button type="button">Outside composer</button>
1005+
</>,
1006+
);
1007+
const input = screen.getByRole("textbox", { name: "Follow-up prompt" });
1008+
const trigger = screen.getByRole("button", { name: "Submit" });
1009+
const outside = screen.getByRole("button", { name: "Outside composer" });
1010+
// A picker trigger that has not opened yet: a real click holds the
1011+
// pointer for longer than a frame, and the trigger blurs the editor on
1012+
// pointerdown before it reports itself expanded.
1013+
trigger.setAttribute("aria-haspopup", "menu");
1014+
trigger.setAttribute("aria-expanded", "false");
1015+
act(() => input.focus());
1016+
1017+
fireEvent.pointerDown(trigger);
1018+
act(() => outside.focus());
1019+
await act(async () => {
1020+
await new Promise((resolve) => window.requestAnimationFrame(resolve));
1021+
});
1022+
1023+
expect(screen.getByTestId("prompt-box").getAttribute("data-compact")).toBe(
1024+
"false",
1025+
);
1026+
1027+
// Once the pointer is released and focus leaves for real, it collapses.
1028+
fireEvent.pointerUp(trigger);
1029+
await act(async () => {
1030+
await new Promise((resolve) => window.setTimeout(resolve, 0));
1031+
});
1032+
act(() => input.focus());
1033+
act(() => outside.focus());
1034+
await waitFor(() =>
1035+
expect(
1036+
screen.getByTestId("prompt-box").getAttribute("data-compact"),
1037+
).toBe("true"),
1038+
);
1039+
});
1040+
9971041
it("stays expanded while a composer-owned overlay is open", async () => {
9981042
mocks.isCompactViewport = true;
9991043
render(

‎apps/app/src/components/promptbox/FollowUpPromptBox.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type FocusEvent as ReactFocusEvent,
1212
type ReactNode,
1313
type RefObject,
14+
type PointerEvent as ReactPointerEvent,
1415
} from "react";
1516
import type {
1617
PromptTextMention,
@@ -113,6 +114,7 @@ const FOLLOW_UP_PROMPT_BOX_DEFAULT_MIN_HEIGHT = 68;
113114
const FOLLOW_UP_PROMPT_BOX_ELASTIC_TARGET_HEIGHT =
114115
FOLLOW_UP_PROMPT_BOX_DEFAULT_MIN_HEIGHT +
115116
THREAD_PROMPT_CONTEXT_BANNER_ROW_HEIGHT;
117+
const COMPOSER_OVERLAY_TRIGGER_SELECTOR = "[aria-haspopup]";
116118
const OPEN_COMPOSER_OVERLAY_TRIGGER_SELECTOR =
117119
'[aria-haspopup][aria-expanded="true"]';
118120
const MOBILE_KEYBOARD_VIEWPORT_MIN_DELTA_PX = 80;
@@ -303,6 +305,11 @@ function FollowUpPromptBoxWithComposer({
303305
const interactionExpandedRef = useRef(false);
304306
const pendingFocusExpansionCleanupRef = useRef<(() => void) | null>(null);
305307
const pendingFocusLossCleanupRef = useRef<(() => void) | null>(null);
308+
// Set while a pointer is down on an overlay trigger inside the composer
309+
// (model picker, attachments menu). The trigger blurs the editor on
310+
// pointerdown, before the overlay reports itself open, so the focus-loss
311+
// check must not read that blur as the user leaving the composer.
312+
const overlayTriggerPointerDownRef = useRef(false);
306313
const [isInteractionExpanded, setIsInteractionExpanded] = useState(false);
307314
const [widePromptBoxCollapsedFor, setWidePromptBoxCollapsedFor] = useState<
308315
string | number | null
@@ -426,6 +433,12 @@ function FollowUpPromptBoxWithComposer({
426433
) {
427434
return;
428435
}
436+
if (overlayTriggerPointerDownRef.current) {
437+
// A real click holds the pointer for longer than a frame: the
438+
// overlay opens on pointerup, after this check runs. Keep the
439+
// composer expanded; a later focus loss still collapses it.
440+
return;
441+
}
429442

430443
const collapse = () => {
431444
cancelPendingFocusExpansion();
@@ -492,6 +505,22 @@ function FollowUpPromptBoxWithComposer({
492505
setInteractionExpanded,
493506
],
494507
);
508+
const handleComposerPointerDown = useCallback(
509+
(event: ReactPointerEvent<HTMLDivElement>) => {
510+
const target = event.target;
511+
overlayTriggerPointerDownRef.current =
512+
target instanceof Element &&
513+
target.closest(COMPOSER_OVERLAY_TRIGGER_SELECTOR) !== null;
514+
},
515+
[],
516+
);
517+
const releaseOverlayTriggerPointer = useCallback(() => {
518+
// Let the overlay finish opening before the flag drops, so the blur
519+
// check that follows pointerup still sees the trigger as engaged.
520+
window.setTimeout(() => {
521+
overlayTriggerPointerDownRef.current = false;
522+
}, 0);
523+
}, []);
495524
const collapseWidePromptBox = useCallback(() => {
496525
cancelPendingFocusExpansion();
497526
cancelPendingFocusLoss();
@@ -620,6 +649,9 @@ function FollowUpPromptBoxWithComposer({
620649
hidden={hasPendingInteraction}
621650
onBlurCapture={scheduleCollapseAfterFocusLoss}
622651
onFocusCapture={handleComposerFocus}
652+
onPointerDownCapture={handleComposerPointerDown}
653+
onPointerUpCapture={releaseOverlayTriggerPointer}
654+
onPointerCancelCapture={releaseOverlayTriggerPointer}
623655
>
624656
<PromptBoxWithScrollAnchor
625657
id={id}

0 commit comments

Comments
 (0)