From 699caa754c00115c9a1aacd04ce6fb0bd08e29d4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 4 May 2026 09:54:51 -0700 Subject: [PATCH] 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 --- apps/web/src/components/chat/ChatComposer.tsx | 372 ++++++++---------- .../chat/mobileComposerFocus.test.ts | 15 +- .../components/chat/mobileComposerFocus.ts | 10 +- 3 files changed, 163 insertions(+), 234 deletions(-) diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index d6ff89d6d4..6487e2285f 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -30,7 +30,6 @@ 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"; @@ -141,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, @@ -177,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; @@ -797,9 +785,13 @@ 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 isMobileViewport = useMediaQuery("max-sm"); - const isComposerCollapsedMobile = isMobileViewport && !isComposerFocused; + const mobileCollapsedOnlyClassName = + "sm:hidden max-sm:group-focus-within/composer:hidden max-sm:group-data-[mobile-focus-primed=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"; + const mobileExpandedFlexOnlyClassName = + "max-sm:hidden max-sm:group-focus-within/composer:flex max-sm:group-data-[mobile-focus-primed=true]/composer:flex"; // ------------------------------------------------------------------ // Refs @@ -812,10 +804,7 @@ export const ChatComposer = memo( const composerMenuOpenRef = useRef(false); const composerMenuItemsRef = useRef([]); const activeComposerMenuItemRef = useRef(null); - const composerBlurFrameRef = useRef(null); - const mobileComposerExpandFrameRef = useRef(null); - const mobileComposerExpandReleaseFrameRef = useRef(null); - const mobileComposerExpandInFlightRef = useRef(false); + const mobileComposerPrimeReleaseFrameRef = useRef(null); const dragDepthRef = useRef(0); // ------------------------------------------------------------------ @@ -960,8 +949,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); @@ -1057,8 +1045,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 @@ -1590,15 +1577,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(() => { @@ -1630,39 +1617,22 @@ export const ChatComposer = memo( ); const expandMobileComposer = useCallback(() => { 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; + if (mobileComposerPrimeReleaseFrameRef.current !== null) { + window.cancelAnimationFrame(mobileComposerPrimeReleaseFrameRef.current); + mobileComposerPrimeReleaseFrameRef.current = null; } }, - setExpandInFlight: (inFlight) => { - mobileComposerExpandInFlightRef.current = inFlight; - }, - commitExpandedState: () => { - flushSync(() => { - setIsComposerFocused(true); - }); + primeExpandedState: () => { + composerSurfaceRef.current?.setAttribute("data-mobile-focus-primed", "true"); }, focusEditorAtEnd: () => { composerEditorRef.current?.focusAtEnd(); }, scheduleRelease: () => { - mobileComposerExpandReleaseFrameRef.current = window.requestAnimationFrame(() => { - mobileComposerExpandReleaseFrameRef.current = null; - mobileComposerExpandInFlightRef.current = false; + mobileComposerPrimeReleaseFrameRef.current = window.requestAnimationFrame(() => { + mobileComposerPrimeReleaseFrameRef.current = null; + composerSurfaceRef.current?.setAttribute("data-mobile-focus-primed", "false"); }); }, }); @@ -1808,47 +1778,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); } }; }, []); @@ -1981,63 +1914,62 @@ export const ChatComposer = memo( >
{ - 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 ? ( -
- -
- ) : pendingUserInputs.length > 0 ? ( -
- -
- ) : showPlanFollowUpPrompt && activeProposedPlan ? ( -
- -
- ) : null)} - - {isComposerCollapsedMobile && activePendingApproval ? ( + {activePendingApproval ? (
+ +
+ ) : pendingUserInputs.length > 0 ? ( +
+ +
+ ) : showPlanFollowUpPrompt && activeProposedPlan ? ( +
+ +
+ ) : null} + + {activePendingApproval ? ( +
- ) : isComposerCollapsedMobile && pendingUserInputs.length > 0 ? ( + ) : pendingUserInputs.length > 0 ? (
+
)} - {!isComposerCollapsedMobile && - !isComposerApprovalState && - pendingUserInputs.length === 0 && - composerImages.length > 0 && ( -
- {composerImages.map((image) => ( -
- {image.previewUrl ? ( - - ) : ( -
- {image.name} -
- )} - {nonPersistedComposerImageIdSet.has(image.id) && ( - - - - - } - /> - - Draft attachment could not be saved locally and may be lost on - navigation. - - - )} - -
- ))} -
- )} + {image.name} + + ) : ( +
+ {image.name} +
+ )} + {nonPersistedComposerImageIdSet.has(image.id) && ( + + + + + } + /> + + Draft attachment could not be saved locally and may be lost on + navigation. + + + )} + +
+ ))} + + ) : null}
{/* Bottom toolbar */} - {isComposerCollapsedMobile ? null : activePendingApproval ? ( -
+ {activePendingApproval ? ( +
diff --git a/apps/web/src/components/chat/mobileComposerFocus.test.ts b/apps/web/src/components/chat/mobileComposerFocus.test.ts index b587e7e767..f0cb6b1b61 100644 --- a/apps/web/src/components/chat/mobileComposerFocus.test.ts +++ b/apps/web/src/components/chat/mobileComposerFocus.test.ts @@ -6,23 +6,12 @@ describe("expandMobileComposerForKeyboard", () => { 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")), + primeExpandedState: vi.fn(() => calls.push("prime-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", - ]); + expect(calls).toEqual(["cancel-release", "prime-expanded", "focus", "schedule-release"]); }); }); diff --git a/apps/web/src/components/chat/mobileComposerFocus.ts b/apps/web/src/components/chat/mobileComposerFocus.ts index 6810b35dfc..4641213ec5 100644 --- a/apps/web/src/components/chat/mobileComposerFocus.ts +++ b/apps/web/src/components/chat/mobileComposerFocus.ts @@ -1,19 +1,13 @@ export interface MobileComposerExpandOptions { - cancelPendingBlur: () => void; - cancelPendingExpandFocus: () => void; cancelPendingRelease: () => void; - setExpandInFlight: (inFlight: boolean) => void; - commitExpandedState: () => void; + primeExpandedState: () => void; focusEditorAtEnd: () => void; scheduleRelease: () => void; } export function expandMobileComposerForKeyboard(options: MobileComposerExpandOptions) { - options.cancelPendingBlur(); - options.cancelPendingExpandFocus(); options.cancelPendingRelease(); - options.setExpandInFlight(true); - options.commitExpandedState(); + options.primeExpandedState(); options.focusEditorAtEnd(); options.scheduleRelease(); }