Compare commits

...
Author SHA1 Message Date
Cursor Agent ee9cabf430 fix: prevent mobile composer collapse when portaled floating layers are open
Track open state of CompactComposerControlsMenu and ProviderModelPicker
to set a data-mobile-floating-open attribute on the composer surface.
CSS conditions now include group-data-[mobile-floating-open=true]/composer
so the composer stays expanded while portaled menus/popovers are active.
2026-05-04 17:29:42 +00:00
Julius Marminge 699caa754c Fix mobile composer keyboard focus race
- Replace JS-driven collapse state with a primed mobile focus attribute
- Simplify keyboard expansion flow and update focus sequencing tests
2026-05-04 09:54:51 -07:00
Julius Marminge 34f4646ccf Fix mobile composer focus race on expand
- Extract keyboard expand flow into a helper
- Use flushSync to commit focus state before focusing the editor
- Add a regression test for the expand sequence
2026-05-04 09:36:30 -07:00
4 changed files with 215 additions and 209 deletions

No files matched your search

+183 -208
View File
@@ -71,6 +71,7 @@ import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
import { ComposerPlanFollowUpBanner } from "./ComposerPlanFollowUpBanner";
import { resolveComposerMenuActiveItemId } from "./composerMenuHighlight";
import { searchSlashCommandItems } from "./composerSlashCommandSearch";
import { expandMobileComposerForKeyboard } from "./mobileComposerFocus";
import {
getComposerProviderState,
renderProviderTraitsMenuContent,
@@ -139,13 +140,6 @@ const runtimeModeConfig: Record<
const runtimeModeOptions = Object.keys(runtimeModeConfig) as RuntimeMode[];
const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;
const EMPTY_PROJECT_ENTRIES: ProjectEntry[] = [];
const COMPOSER_FLOATING_LAYER_SELECTOR = [
'[data-slot="popover-popup"]',
'[data-slot="menu-popup"]',
'[data-slot="select-popup"]',
'[data-slot="combobox-popup"]',
'[data-slot="autocomplete-popup"]',
].join(",");
const extendReplacementRangeForTrailingSpace = (
text: string,
@@ -175,10 +169,6 @@ const terminalContextIdListsEqual = (
): boolean =>
contexts.length === ids.length && contexts.every((context, index) => context.id === ids[index]);
function isInsideComposerFloatingLayer(element: Element): boolean {
return element.closest(COMPOSER_FLOATING_LAYER_SELECTOR) !== null;
}
const ComposerFooterModeControls = memo(function ComposerFooterModeControls(props: {
showInteractionModeToggle: boolean;
interactionMode: ProviderInteractionMode;
@@ -795,9 +785,15 @@ export const ChatComposer = memo(
const [isComposerFooterCompact, setIsComposerFooterCompact] = useState(false);
const [isComposerPrimaryActionsCompact, setIsComposerPrimaryActionsCompact] = useState(false);
const [isComposerModelPickerOpen, setIsComposerModelPickerOpen] = useState(false);
const [isComposerFocused, setIsComposerFocused] = useState(false);
const [isComposerCompactMenuOpen, setIsComposerCompactMenuOpen] = useState(false);
const isMobileViewport = useMediaQuery("max-sm");
const isComposerCollapsedMobile = isMobileViewport && !isComposerFocused;
const isMobileFloatingLayerOpen = isComposerModelPickerOpen || isComposerCompactMenuOpen;
const mobileCollapsedOnlyClassName =
"sm:hidden max-sm:group-focus-within/composer:hidden max-sm:group-data-[mobile-focus-primed=true]/composer:hidden max-sm:group-data-[mobile-floating-open=true]/composer:hidden";
const mobileExpandedOnlyClassName =
"max-sm:hidden max-sm:group-focus-within/composer:block max-sm:group-data-[mobile-focus-primed=true]/composer:block max-sm:group-data-[mobile-floating-open=true]/composer:block";
const mobileExpandedFlexOnlyClassName =
"max-sm:hidden max-sm:group-focus-within/composer:flex max-sm:group-data-[mobile-focus-primed=true]/composer:flex max-sm:group-data-[mobile-floating-open=true]/composer:flex";
// ------------------------------------------------------------------
// Refs
@@ -810,10 +806,7 @@ export const ChatComposer = memo(
const composerMenuOpenRef = useRef(false);
const composerMenuItemsRef = useRef<ComposerCommandItem[]>([]);
const activeComposerMenuItemRef = useRef<ComposerCommandItem | null>(null);
const composerBlurFrameRef = useRef<number | null>(null);
const mobileComposerExpandFrameRef = useRef<number | null>(null);
const mobileComposerExpandReleaseFrameRef = useRef<number | null>(null);
const mobileComposerExpandInFlightRef = useRef(false);
const mobileComposerPrimeReleaseFrameRef = useRef<number | null>(null);
const dragDepthRef = useRef(0);
// ------------------------------------------------------------------
@@ -958,8 +951,7 @@ export const ChatComposer = memo(
isComposerApprovalState ||
pendingUserInputs.length > 0 ||
(showPlanFollowUpPrompt && activeProposedPlan !== null);
const showCollapsedMobilePromptRow =
isComposerCollapsedMobile && !isComposerApprovalState && pendingUserInputs.length === 0;
const showCollapsedMobilePromptRow = !isComposerApprovalState && pendingUserInputs.length === 0;
const composerFooterHasWideActions = showPlanFollowUpPrompt || activePendingProgress !== null;
const showPlanSidebarToggle = Boolean(activePlan || sidebarProposedPlan || planSidebarOpen);
@@ -1055,8 +1047,7 @@ export const ChatComposer = memo(
const collapsedComposerPrimaryActionDisabled =
phase === "running" || isSendBusy || isConnecting || !composerSendState.hasSendableContent;
const collapsedComposerPrimaryActionLabel = "Send message";
const showMobilePendingAnswerActions =
isMobileViewport && !isComposerCollapsedMobile && pendingPrimaryAction !== null;
const showMobilePendingAnswerActions = isMobileViewport && pendingPrimaryAction !== null;
// ------------------------------------------------------------------
// Prompt helpers
@@ -1588,15 +1579,15 @@ export const ChatComposer = memo(
const blurMobileComposerAfterSend = useCallback(() => {
if (!isMobileViewport) return;
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
composerBlurFrameRef.current = null;
if (mobileComposerPrimeReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerPrimeReleaseFrameRef.current);
mobileComposerPrimeReleaseFrameRef.current = null;
}
composerSurfaceRef.current?.setAttribute("data-mobile-focus-primed", "false");
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement) {
activeElement.blur();
}
setIsComposerFocused(false);
}, [isMobileViewport]);
const shouldBlurMobileComposerOnSubmit = useCallback(() => {
@@ -1627,25 +1618,25 @@ export const ChatComposer = memo(
[blurMobileComposerAfterSend, onSend, shouldBlurMobileComposerOnSubmit],
);
const expandMobileComposer = useCallback(() => {
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
composerBlurFrameRef.current = null;
}
if (mobileComposerExpandFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandFrameRef.current);
}
if (mobileComposerExpandReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandReleaseFrameRef.current);
}
mobileComposerExpandInFlightRef.current = true;
setIsComposerFocused(true);
mobileComposerExpandFrameRef.current = window.requestAnimationFrame(() => {
mobileComposerExpandFrameRef.current = null;
composerEditorRef.current?.focusAtEnd();
mobileComposerExpandReleaseFrameRef.current = window.requestAnimationFrame(() => {
mobileComposerExpandReleaseFrameRef.current = null;
mobileComposerExpandInFlightRef.current = false;
});
expandMobileComposerForKeyboard({
cancelPendingRelease: () => {
if (mobileComposerPrimeReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerPrimeReleaseFrameRef.current);
mobileComposerPrimeReleaseFrameRef.current = null;
}
},
primeExpandedState: () => {
composerSurfaceRef.current?.setAttribute("data-mobile-focus-primed", "true");
},
focusEditorAtEnd: () => {
composerEditorRef.current?.focusAtEnd();
},
scheduleRelease: () => {
mobileComposerPrimeReleaseFrameRef.current = window.requestAnimationFrame(() => {
mobileComposerPrimeReleaseFrameRef.current = null;
composerSurfaceRef.current?.setAttribute("data-mobile-focus-primed", "false");
});
},
});
}, []);
@@ -1789,47 +1780,10 @@ export const ChatComposer = memo(
const handleImplementPlanInNewThreadPrimaryAction = useCallback(() => {
void onImplementPlanInNewThread();
}, [onImplementPlanInNewThread]);
const scheduleComposerCollapseCheck = useCallback(() => {
if (!isMobileViewport) {
return;
}
if (mobileComposerExpandInFlightRef.current) {
return;
}
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
}
composerBlurFrameRef.current = window.requestAnimationFrame(() => {
composerBlurFrameRef.current = null;
if (mobileComposerExpandInFlightRef.current) {
return;
}
const composerSurface = composerSurfaceRef.current;
const activeElement = document.activeElement;
if (activeElement instanceof Element && isInsideComposerFloatingLayer(activeElement)) {
return;
}
if (
composerSurface &&
activeElement instanceof Node &&
composerSurface.contains(activeElement)
) {
return;
}
setIsComposerFocused(false);
});
}, [isMobileViewport]);
useEffect(() => {
return () => {
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
}
if (mobileComposerExpandFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandFrameRef.current);
}
if (mobileComposerExpandReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandReleaseFrameRef.current);
if (mobileComposerPrimeReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerPrimeReleaseFrameRef.current);
}
};
}, []);
@@ -1962,63 +1916,63 @@ export const ChatComposer = memo(
>
<div
ref={composerSurfaceRef}
data-chat-composer-mobile-collapsed={isComposerCollapsedMobile ? "true" : "false"}
data-mobile-focus-primed="false"
data-mobile-floating-open={isMobileFloatingLayerOpen ? "true" : "false"}
className={cn(
"rounded-[20px] border bg-card transition-colors duration-200 has-focus-visible:border-ring/45",
"group/composer rounded-[20px] border bg-card transition-colors duration-200 has-focus-visible:border-ring/45",
isDragOverComposer ? "border-primary/70 bg-accent/30" : "border-border",
environmentUnavailable ? "opacity-75" : null,
composerProviderState.composerSurfaceClassName,
)}
onFocusCapture={(event) => {
const activeElement = event.target;
if (
isComposerCollapsedMobile &&
activeElement instanceof HTMLElement &&
activeElement.closest('[data-chat-composer-collapsed-controls="true"]')
) {
return;
}
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
composerBlurFrameRef.current = null;
}
setIsComposerFocused(true);
}}
onBlurCapture={() => {
scheduleComposerCollapseCheck();
}}
>
{!isComposerCollapsedMobile &&
(activePendingApproval ? (
<div className="rounded-t-[19px] border-b border-border/65 bg-muted/20">
<ComposerPendingApprovalPanel
approval={activePendingApproval}
pendingCount={pendingApprovals.length}
/>
</div>
) : pendingUserInputs.length > 0 ? (
<div className="rounded-t-[19px] border-b border-border/65 bg-muted/20">
<ComposerPendingUserInputPanel
pendingUserInputs={pendingUserInputs}
respondingRequestIds={respondingRequestIds}
answers={activePendingDraftAnswers}
questionIndex={activePendingQuestionIndex}
onToggleOption={onSelectActivePendingUserInputOption}
onAdvance={onAdvanceActivePendingUserInput}
/>
</div>
) : showPlanFollowUpPrompt && activeProposedPlan ? (
<div className="rounded-t-[19px] border-b border-border/65 bg-muted/20">
<ComposerPlanFollowUpBanner
key={activeProposedPlan.id}
planTitle={proposedPlanTitle(activeProposedPlan.planMarkdown) ?? null}
/>
</div>
) : null)}
{isComposerCollapsedMobile && activePendingApproval ? (
{activePendingApproval ? (
<div
className="rounded-t-[19px] border-b border-border/65 bg-muted/20"
className={cn(
"rounded-t-[19px] border-b border-border/65 bg-muted/20",
mobileExpandedOnlyClassName,
)}
>
<ComposerPendingApprovalPanel
approval={activePendingApproval}
pendingCount={pendingApprovals.length}
/>
</div>
) : pendingUserInputs.length > 0 ? (
<div
className={cn(
"rounded-t-[19px] border-b border-border/65 bg-muted/20",
mobileExpandedOnlyClassName,
)}
>
<ComposerPendingUserInputPanel
pendingUserInputs={pendingUserInputs}
respondingRequestIds={respondingRequestIds}
answers={activePendingDraftAnswers}
questionIndex={activePendingQuestionIndex}
onToggleOption={onSelectActivePendingUserInputOption}
onAdvance={onAdvanceActivePendingUserInput}
/>
</div>
) : showPlanFollowUpPrompt && activeProposedPlan ? (
<div
className={cn(
"rounded-t-[19px] border-b border-border/65 bg-muted/20",
mobileExpandedOnlyClassName,
)}
>
<ComposerPlanFollowUpBanner
key={activeProposedPlan.id}
planTitle={proposedPlanTitle(activeProposedPlan.planMarkdown) ?? null}
/>
</div>
) : null}
{activePendingApproval ? (
<div
className={cn(
"rounded-t-[19px] border-b border-border/65 bg-muted/20",
mobileCollapsedOnlyClassName,
)}
data-chat-composer-collapsed-controls="true"
>
<ComposerPendingApprovalPanel
@@ -2033,9 +1987,12 @@ export const ChatComposer = memo(
/>
</div>
</div>
) : isComposerCollapsedMobile && pendingUserInputs.length > 0 ? (
) : pendingUserInputs.length > 0 ? (
<div
className="rounded-t-[19px] border-b border-border/65 bg-muted/20"
className={cn(
"rounded-t-[19px] border-b border-border/65 bg-muted/20",
mobileCollapsedOnlyClassName,
)}
data-chat-composer-collapsed-controls="true"
>
<ComposerPendingUserInputPanel
@@ -2063,7 +2020,10 @@ export const ChatComposer = memo(
: "text-muted-foreground/60",
!activePendingProgress?.activeQuestion?.multiSelect && "px-3 py-2",
)}
onPointerDown={(event) => event.preventDefault()}
onPointerDown={(event) => {
event.preventDefault();
expandMobileComposer();
}}
onClick={expandMobileComposer}
aria-label="Write custom answer"
>
@@ -2093,7 +2053,12 @@ export const ChatComposer = memo(
) : null}
{showCollapsedMobilePromptRow ? (
<div className="flex items-center justify-between gap-2 px-3 py-2">
<div
className={cn(
"flex items-center justify-between gap-2 px-3 py-2",
mobileCollapsedOnlyClassName,
)}
>
<button
type="button"
className={cn(
@@ -2102,7 +2067,10 @@ export const ChatComposer = memo(
? "text-foreground"
: "text-muted-foreground/35",
)}
onPointerDown={(event) => event.preventDefault()}
onPointerDown={(event) => {
event.preventDefault();
expandMobileComposer();
}}
onClick={expandMobileComposer}
aria-label="Expand composer"
>
@@ -2139,7 +2107,7 @@ export const ChatComposer = memo(
className={cn(
"relative px-3 pb-2 sm:px-4",
hasComposerHeader ? "pt-2.5 sm:pt-3" : "pt-3.5 sm:pt-4",
isComposerCollapsedMobile && "hidden",
mobileExpandedOnlyClassName,
)}
>
{composerMenuOpen && !isComposerApprovalState && (
@@ -2161,73 +2129,72 @@ export const ChatComposer = memo(
</div>
)}
{!isComposerCollapsedMobile &&
!isComposerApprovalState &&
pendingUserInputs.length === 0 &&
composerImages.length > 0 && (
<div className="mb-3 flex flex-wrap gap-2">
{composerImages.map((image) => (
<div
key={image.id}
className="relative h-16 w-16 overflow-hidden rounded-lg border border-border/80 bg-background"
>
{image.previewUrl ? (
<button
type="button"
className="h-full w-full cursor-zoom-in"
aria-label={`Preview ${image.name}`}
onClick={() => {
const preview = buildExpandedImagePreview(composerImages, image.id);
if (!preview) return;
onExpandImage(preview);
}}
>
<img
src={image.previewUrl}
alt={image.name}
className="h-full w-full object-cover"
/>
</button>
) : (
<div className="flex h-full w-full items-center justify-center px-1 text-center text-[10px] text-muted-foreground/70">
{image.name}
</div>
)}
{nonPersistedComposerImageIdSet.has(image.id) && (
<Tooltip>
<TooltipTrigger
render={
<span
role="img"
aria-label="Draft attachment may not persist"
className="absolute left-1 top-1 inline-flex items-center justify-center rounded bg-background/85 p-0.5 text-amber-600"
>
<CircleAlertIcon className="size-3" />
</span>
}
/>
<TooltipPopup
side="top"
className="max-w-64 whitespace-normal leading-tight"
>
Draft attachment could not be saved locally and may be lost on
navigation.
</TooltipPopup>
</Tooltip>
)}
<Button
variant="ghost"
size="icon-xs"
className="absolute right-1 top-1 bg-background/80 hover:bg-background/90"
onClick={() => removeComposerImage(image.id)}
aria-label={`Remove ${image.name}`}
{!isComposerApprovalState &&
pendingUserInputs.length === 0 &&
composerImages.length > 0 ? (
<div className="mb-3 flex flex-wrap gap-2">
{composerImages.map((image) => (
<div
key={image.id}
className="relative h-16 w-16 overflow-hidden rounded-lg border border-border/80 bg-background"
>
{image.previewUrl ? (
<button
type="button"
className="h-full w-full cursor-zoom-in"
aria-label={`Preview ${image.name}`}
onClick={() => {
const preview = buildExpandedImagePreview(composerImages, image.id);
if (!preview) return;
onExpandImage(preview);
}}
>
<XIcon />
</Button>
</div>
))}
</div>
)}
<img
src={image.previewUrl}
alt={image.name}
className="h-full w-full object-cover"
/>
</button>
) : (
<div className="flex h-full w-full items-center justify-center px-1 text-center text-[10px] text-muted-foreground/70">
{image.name}
</div>
)}
{nonPersistedComposerImageIdSet.has(image.id) && (
<Tooltip>
<TooltipTrigger
render={
<span
role="img"
aria-label="Draft attachment may not persist"
className="absolute left-1 top-1 inline-flex items-center justify-center rounded bg-background/85 p-0.5 text-amber-600"
>
<CircleAlertIcon className="size-3" />
</span>
}
/>
<TooltipPopup
side="top"
className="max-w-64 whitespace-normal leading-tight"
>
Draft attachment could not be saved locally and may be lost on
navigation.
</TooltipPopup>
</Tooltip>
)}
<Button
variant="ghost"
size="icon-xs"
className="absolute right-1 top-1 bg-background/80 hover:bg-background/90"
onClick={() => removeComposerImage(image.id)}
aria-label={`Remove ${image.name}`}
>
<XIcon />
</Button>
</div>
))}
</div>
) : null}
<div className="relative">
<ComposerPromptEditor
@@ -2302,8 +2269,13 @@ export const ChatComposer = memo(
</div>
{/* Bottom toolbar */}
{isComposerCollapsedMobile ? null : activePendingApproval ? (
<div className="flex items-center justify-end gap-2 px-2.5 pb-2.5 sm:px-3 sm:pb-3">
{activePendingApproval ? (
<div
className={cn(
"flex items-center justify-end gap-2 px-2.5 pb-2.5 sm:px-3 sm:pb-3",
mobileExpandedFlexOnlyClassName,
)}
>
<ComposerPendingApprovalActions
requestId={activePendingApproval.requestId}
isResponding={respondingRequestIds.includes(activePendingApproval.requestId)}
@@ -2317,7 +2289,9 @@ export const ChatComposer = memo(
className={cn(
"flex min-w-0 flex-nowrap items-center justify-between gap-2 overflow-visible px-2.5 pb-2.5 sm:px-3 sm:pb-3",
isComposerFooterCompact ? "gap-1.5" : "gap-2 sm:gap-0",
showMobilePendingAnswerActions && "hidden sm:flex",
showMobilePendingAnswerActions
? "max-sm:hidden"
: mobileExpandedFlexOnlyClassName,
)}
>
<div className="-m-1 flex min-w-0 flex-1 items-center gap-1 overflow-x-auto p-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
@@ -2353,6 +2327,7 @@ export const ChatComposer = memo(
runtimeMode={runtimeMode}
showInteractionModeToggle={composerProviderControls.showInteractionModeToggle}
traitsMenuContent={providerTraitsMenuContent}
onOpenChange={setIsComposerCompactMenuOpen}
onToggleInteractionMode={toggleInteractionMode}
onTogglePlanSidebar={togglePlanSidebar}
onRuntimeModeChange={handleRuntimeModeChange}
@@ -20,12 +20,13 @@ export const CompactComposerControlsMenu = memo(function CompactComposerControls
runtimeMode: RuntimeMode;
showInteractionModeToggle: boolean;
traitsMenuContent?: ReactNode;
onOpenChange?: (open: boolean) => void;
onToggleInteractionMode: () => void;
onTogglePlanSidebar: () => void;
onRuntimeModeChange: (mode: RuntimeMode) => void;
}) {
return (
<Menu>
<Menu onOpenChange={(open) => props.onOpenChange?.(open)}>
<MenuTrigger
render={
<Button
@@ -0,0 +1,17 @@
import { describe, expect, it, vi } from "vitest";
import { expandMobileComposerForKeyboard } from "./mobileComposerFocus";
describe("expandMobileComposerForKeyboard", () => {
it("focuses after the expanded composer is committed and before release is scheduled", () => {
const calls: string[] = [];
expandMobileComposerForKeyboard({
cancelPendingRelease: vi.fn(() => calls.push("cancel-release")),
primeExpandedState: vi.fn(() => calls.push("prime-expanded")),
focusEditorAtEnd: vi.fn(() => calls.push("focus")),
scheduleRelease: vi.fn(() => calls.push("schedule-release")),
});
expect(calls).toEqual(["cancel-release", "prime-expanded", "focus", "schedule-release"]);
});
});
@@ -0,0 +1,13 @@
export interface MobileComposerExpandOptions {
cancelPendingRelease: () => void;
primeExpandedState: () => void;
focusEditorAtEnd: () => void;
scheduleRelease: () => void;
}
export function expandMobileComposerForKeyboard(options: MobileComposerExpandOptions) {
options.cancelPendingRelease();
options.primeExpandedState();
options.focusEditorAtEnd();
options.scheduleRelease();
}