diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index d64d031668..d6ff89d6d4 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -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" > diff --git a/apps/web/src/components/chat/mobileComposerFocus.test.ts b/apps/web/src/components/chat/mobileComposerFocus.test.ts new file mode 100644 index 0000000000..b587e7e767 --- /dev/null +++ b/apps/web/src/components/chat/mobileComposerFocus.test.ts @@ -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", + ]); + }); +}); diff --git a/apps/web/src/components/chat/mobileComposerFocus.ts b/apps/web/src/components/chat/mobileComposerFocus.ts new file mode 100644 index 0000000000..6810b35dfc --- /dev/null +++ b/apps/web/src/components/chat/mobileComposerFocus.ts @@ -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(); +}