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
This commit is contained in:
1 parent
91a03e0747
commit
34f4646ccf
3 files changed
+93
-21
No files matched your search
@@ -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,
|
||||
@@ -1627,25 +1629,42 @@ 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;
|
||||
}
|
||||
},
|
||||
cancelPendingExpandFocus: () => {
|
||||
if (mobileComposerExpandFrameRef.current !== null) {
|
||||
window.cancelAnimationFrame(mobileComposerExpandFrameRef.current);
|
||||
mobileComposerExpandFrameRef.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;
|
||||
});
|
||||
},
|
||||
});
|
||||
}, []);
|
||||
|
||||
@@ -2063,7 +2082,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 +2124,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,28 @@
|
||||
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")),
|
||||
cancelPendingExpandFocus: vi.fn(() => calls.push("cancel-expand-focus")),
|
||||
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-expand-focus",
|
||||
"cancel-release",
|
||||
"in-flight:true",
|
||||
"commit-expanded",
|
||||
"focus",
|
||||
"schedule-release",
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
export interface MobileComposerExpandOptions {
|
||||
cancelPendingBlur: () => void;
|
||||
cancelPendingExpandFocus: () => void;
|
||||
cancelPendingRelease: () => void;
|
||||
setExpandInFlight: (inFlight: boolean) => void;
|
||||
commitExpandedState: () => void;
|
||||
focusEditorAtEnd: () => void;
|
||||
scheduleRelease: () => void;
|
||||
}
|
||||
|
||||
export function expandMobileComposerForKeyboard(options: MobileComposerExpandOptions) {
|
||||
options.cancelPendingBlur();
|
||||
options.cancelPendingExpandFocus();
|
||||
options.cancelPendingRelease();
|
||||
options.setExpandInFlight(true);
|
||||
options.commitExpandedState();
|
||||
options.focusEditorAtEnd();
|
||||
options.scheduleRelease();
|
||||
}
|
||||
Reference in new issue
Block a user