Compare commits

...
Author SHA1 Message Date
Cursor Agent 12084242b0 Remove dead mobileComposerExpandFrameRef and cancelPendingExpandFocus
The ref was never assigned a non-null value after the refactoring to
synchronous focus. This made cancelPendingExpandFocus always a no-op
and the cleanup effect for this ref dead code. Remove the ref, callback,
interface member, and test assertion.
2026-05-04 16:48:45 +00: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
3 changed files with 83 additions and 25 deletions

No files matched your search

+40 -25
View File
@@ -30,6 +30,7 @@ import {
useRef,
useState,
} from "react";
import { flushSync } from "react-dom";
import { useQuery } from "@tanstack/react-query";
import { useDebouncedValue } from "@tanstack/react-pacer";
import { projectSearchEntriesQueryOptions } from "~/lib/projectReactQuery";
@@ -71,6 +72,7 @@ import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
import { ComposerPlanFollowUpBanner } from "./ComposerPlanFollowUpBanner";
import { resolveComposerMenuActiveItemId } from "./composerMenuHighlight";
import { searchSlashCommandItems } from "./composerSlashCommandSearch";
import { expandMobileComposerForKeyboard } from "./mobileComposerFocus";
import {
getComposerProviderState,
renderProviderTraitsMenuContent,
@@ -811,7 +813,6 @@ export const ChatComposer = memo(
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 dragDepthRef = useRef(0);
@@ -1627,25 +1628,36 @@ 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({
cancelPendingBlur: () => {
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
composerBlurFrameRef.current = null;
}
},
cancelPendingRelease: () => {
if (mobileComposerExpandReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandReleaseFrameRef.current);
mobileComposerExpandReleaseFrameRef.current = null;
}
},
setExpandInFlight: (inFlight) => {
mobileComposerExpandInFlightRef.current = inFlight;
},
commitExpandedState: () => {
flushSync(() => {
setIsComposerFocused(true);
});
},
focusEditorAtEnd: () => {
composerEditorRef.current?.focusAtEnd();
},
scheduleRelease: () => {
mobileComposerExpandReleaseFrameRef.current = window.requestAnimationFrame(() => {
mobileComposerExpandReleaseFrameRef.current = null;
mobileComposerExpandInFlightRef.current = false;
});
},
});
}, []);
@@ -1825,9 +1837,6 @@ export const ChatComposer = memo(
if (composerBlurFrameRef.current !== null) {
window.cancelAnimationFrame(composerBlurFrameRef.current);
}
if (mobileComposerExpandFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandFrameRef.current);
}
if (mobileComposerExpandReleaseFrameRef.current !== null) {
window.cancelAnimationFrame(mobileComposerExpandReleaseFrameRef.current);
}
@@ -2063,7 +2072,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"
>
@@ -2102,7 +2114,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"
>
@@ -0,0 +1,26 @@
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({
cancelPendingBlur: vi.fn(() => calls.push("cancel-blur")),
cancelPendingRelease: vi.fn(() => calls.push("cancel-release")),
setExpandInFlight: vi.fn((inFlight) => calls.push(`in-flight:${inFlight}`)),
commitExpandedState: vi.fn(() => calls.push("commit-expanded")),
focusEditorAtEnd: vi.fn(() => calls.push("focus")),
scheduleRelease: vi.fn(() => calls.push("schedule-release")),
});
expect(calls).toEqual([
"cancel-blur",
"cancel-release",
"in-flight:true",
"commit-expanded",
"focus",
"schedule-release",
]);
});
});
@@ -0,0 +1,17 @@
export interface MobileComposerExpandOptions {
cancelPendingBlur: () => void;
cancelPendingRelease: () => void;
setExpandInFlight: (inFlight: boolean) => void;
commitExpandedState: () => void;
focusEditorAtEnd: () => void;
scheduleRelease: () => void;
}
export function expandMobileComposerForKeyboard(options: MobileComposerExpandOptions) {
options.cancelPendingBlur();
options.cancelPendingRelease();
options.setExpandInFlight(true);
options.commitExpandedState();
options.focusEditorAtEnd();
options.scheduleRelease();
}