Skip to content

Commit a12589d

Browse files
t3dotggclaude
andauthored
fix(web): stop collapsing the composer when it loses focus (pingdotgg#10437)
Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 7112697 commit a12589d

14 files changed

Lines changed: 63 additions & 338 deletions

‎apps/desktop/src/settings/DesktopClientSettings.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const clientSettings: ClientSettings = {
2727
confirmThreadDelete: false,
2828
confirmThreadUnpin: false,
2929
contextWindowMeterEnabled: false,
30-
composerCollapseOnBlur: false,
3130
composerCollapseOnScroll: true,
3231
dismissedProviderUpdateNotificationKeys: [],
3332
diffIgnoreWhitespace: true,

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 12 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ import {
213213
resetComposerScrollGesture,
214214
suppressActiveComposerScrollGesture,
215215
} from "./composerScrollGesture";
216-
import { selectionHoldsComposerOpen } from "./composerSelectionHold";
217216
import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame";
218217

219218
function ComposerVideoThumbnail({ file }: { file: File }) {
@@ -1813,7 +1812,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18131812
isComposerScrollCollapsed,
18141813
setIsComposerScrollCollapsed,
18151814
restoreAfterTimelineReachedEnd,
1816-
} = useComposerFocusState(isMobileViewport);
1815+
} = useComposerFocusState();
18171816
const [composerSubmissionError, setComposerSubmissionError] = useState<string | null>(null);
18181817
const [providerInputSubmissionError, setProviderInputSubmissionError] = useState<string | null>(
18191818
null,
@@ -1847,8 +1846,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18471846
const mobileComposerExpandFrameRef = useRef<number | null>(null);
18481847
const mobileComposerExpandReleaseFrameRef = useRef<number | null>(null);
18491848
const mobileComposerExpandInFlightRef = useRef(false);
1850-
const desktopOutsidePointerInFlightRef = useRef(false);
1851-
const desktopOutsidePointerReleaseTimeoutRef = useRef<number | null>(null);
18521849
const composerScrollCollapseTimeoutRef = useRef<number | null>(null);
18531850
const composerScrollCollapseEligibleRef = useRef(false);
18541851
const windowRefocusInFlightRef = useRef(false);
@@ -3716,10 +3713,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
37163713
const isComposerResting = shouldUseRestingComposerLayout({
37173714
isExistingThread: routeKind === "server" && activeThreadId !== null,
37183715
isMobileViewport,
3719-
isFocused: isComposerFocused,
37203716
isScrollCollapsed: isComposerScrollCollapsed,
37213717
hasExpandedChrome: composerHasExpandedChrome,
3722-
collapseOnBlur: settings.composerCollapseOnBlur,
37233718
timelineOverflows,
37243719
});
37253720
// The relocated controls live in the context strip whenever the composer is
@@ -3804,9 +3799,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38043799
settings.composerCollapseOnScroll &&
38053800
!composerHasExpandedChrome &&
38063801
!showInlineTasksBadge;
3807-
// Scrolling only has something to collapse while the composer is expanded.
3808-
// With blur collapse off that includes an unfocused composer, so the wheel
3809-
// handler keys off this rather than editor focus.
3802+
// Scrolling only has something to collapse while the composer is expanded,
3803+
// focused or not, so the wheel handler keys off the resting state rather
3804+
// than editor focus.
38103805
composerScrollCollapseEligibleRef.current = canScrollCollapseComposer && !isComposerResting;
38113806

38123807
useEffect(() => {
@@ -4455,19 +4450,18 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
44554450
const handleImplementPlanInNewThreadPrimaryAction = useCallback(() => {
44564451
void onImplementPlanInNewThread();
44574452
}, [onImplementPlanInNewThread]);
4453+
// The phone composer collapses when the editor loses focus. Desktop only
4454+
// rests on a timeline scroll, so losing focus there changes nothing.
44584455
const scheduleComposerCollapseCheck = useCallback(() => {
4459-
if (isMobileViewport && mobileComposerExpandInFlightRef.current) {
4456+
if (!isMobileViewport || mobileComposerExpandInFlightRef.current) {
44604457
return;
44614458
}
44624459
if (composerBlurFrameRef.current !== null) {
44634460
window.cancelAnimationFrame(composerBlurFrameRef.current);
44644461
}
44654462
composerBlurFrameRef.current = window.requestAnimationFrame(() => {
44664463
composerBlurFrameRef.current = null;
4467-
if (isMobileViewport && mobileComposerExpandInFlightRef.current) {
4468-
return;
4469-
}
4470-
if (!isMobileViewport && desktopOutsidePointerInFlightRef.current) {
4464+
if (mobileComposerExpandInFlightRef.current) {
44714465
return;
44724466
}
44734467
const composerSurface = composerSurfaceRef.current;
@@ -4483,108 +4477,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
44834477
) {
44844478
return;
44854479
}
4486-
if (
4487-
!isMobileViewport &&
4488-
selectionHoldsComposerOpen(window.getSelection(), getTimelineScrollableNode())
4489-
) {
4490-
// The check runs again once the selection clears.
4491-
return;
4492-
}
44934480
setIsComposerFocused(false);
44944481
});
4495-
}, [getTimelineScrollableNode, isMobileViewport, setIsComposerFocused]);
4496-
4497-
// A held collapse settles when the selection goes away, whether the user
4498-
// clicked elsewhere, pressed Escape, or used the selection toolbar.
4499-
useEffect(() => {
4500-
if (isMobileViewport || !isComposerFocused) return;
4501-
let wasHolding = false;
4502-
const handleSelectionChange = () => {
4503-
const holding = selectionHoldsComposerOpen(
4504-
window.getSelection(),
4505-
getTimelineScrollableNode(),
4506-
);
4507-
if (wasHolding && !holding) {
4508-
scheduleComposerCollapseCheck();
4509-
}
4510-
wasHolding = holding;
4511-
};
4512-
document.addEventListener("selectionchange", handleSelectionChange);
4513-
return () => {
4514-
document.removeEventListener("selectionchange", handleSelectionChange);
4515-
};
4516-
}, [
4517-
getTimelineScrollableNode,
4518-
isComposerFocused,
4519-
isMobileViewport,
4520-
scheduleComposerCollapseCheck,
4521-
]);
4522-
4523-
useEffect(() => {
4524-
if (isMobileViewport || !isComposerFocused) return;
4525-
4526-
const isInsideDesktopComposerFocusScope = (target: EventTarget | null) =>
4527-
Boolean(
4528-
target instanceof Node &&
4529-
(composerFormRef.current?.contains(target) || isInsideRestingComposerControlScope(target)),
4530-
);
4531-
const handleFocusIn = (event: FocusEvent) => {
4532-
if (!isInsideDesktopComposerFocusScope(event.target)) {
4533-
if (desktopOutsidePointerInFlightRef.current) {
4534-
return;
4535-
}
4536-
setIsComposerFocused(false);
4537-
}
4538-
};
4539-
const handlePointerDown = (event: PointerEvent) => {
4540-
if (!isInsideDesktopComposerFocusScope(event.target)) {
4541-
desktopOutsidePointerInFlightRef.current = true;
4542-
if (desktopOutsidePointerReleaseTimeoutRef.current !== null) {
4543-
window.clearTimeout(desktopOutsidePointerReleaseTimeoutRef.current);
4544-
desktopOutsidePointerReleaseTimeoutRef.current = null;
4545-
}
4546-
}
4547-
};
4548-
const finishOutsidePointerInteraction = () => {
4549-
desktopOutsidePointerInFlightRef.current = false;
4550-
if (desktopOutsidePointerReleaseTimeoutRef.current !== null) {
4551-
window.clearTimeout(desktopOutsidePointerReleaseTimeoutRef.current);
4552-
desktopOutsidePointerReleaseTimeoutRef.current = null;
4553-
}
4554-
scheduleComposerCollapseCheck();
4555-
};
4556-
const handlePointerUp = () => {
4557-
if (!desktopOutsidePointerInFlightRef.current) return;
4558-
desktopOutsidePointerReleaseTimeoutRef.current = window.setTimeout(() => {
4559-
if (desktopOutsidePointerInFlightRef.current) {
4560-
finishOutsidePointerInteraction();
4561-
}
4562-
}, 0);
4563-
};
4564-
const handleClick = () => {
4565-
if (desktopOutsidePointerInFlightRef.current) {
4566-
finishOutsidePointerInteraction();
4567-
}
4568-
};
4569-
4570-
document.addEventListener("focusin", handleFocusIn, true);
4571-
document.addEventListener("pointerdown", handlePointerDown, true);
4572-
document.addEventListener("pointerup", handlePointerUp, true);
4573-
document.addEventListener("pointercancel", handlePointerUp, true);
4574-
document.addEventListener("click", handleClick);
4575-
return () => {
4576-
document.removeEventListener("focusin", handleFocusIn, true);
4577-
document.removeEventListener("pointerdown", handlePointerDown, true);
4578-
document.removeEventListener("pointerup", handlePointerUp, true);
4579-
document.removeEventListener("pointercancel", handlePointerUp, true);
4580-
document.removeEventListener("click", handleClick);
4581-
if (desktopOutsidePointerReleaseTimeoutRef.current !== null) {
4582-
window.clearTimeout(desktopOutsidePointerReleaseTimeoutRef.current);
4583-
desktopOutsidePointerReleaseTimeoutRef.current = null;
4584-
}
4585-
desktopOutsidePointerInFlightRef.current = false;
4586-
};
4587-
}, [isComposerFocused, isMobileViewport, scheduleComposerCollapseCheck, setIsComposerFocused]);
4482+
}, [isMobileViewport, setIsComposerFocused]);
45884483

45894484
useEffect(() => {
45904485
return () => {
@@ -4790,10 +4685,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
47904685

47914686
setIsComposerScrollCollapsed(false);
47924687
if (isComposerResting && !target.closest('[data-testid="composer-editor"]')) {
4793-
// Clicking resting-surface padding would otherwise blur the still
4794-
// focused editor after pointerdown: expansion starts, the blur check
4795-
// runs, and it immediately collapses again. Treat that padding like
4796-
// the editor without stealing native caret placement from text.
4688+
// Clicking resting-surface padding would otherwise blur the editor.
4689+
// Treat that padding like the editor without stealing native caret
4690+
// placement from text.
47974691
event.preventDefault();
47984692
setIsComposerFocused(true);
47994693
scheduleComposerFocus();

‎apps/web/src/components/chat/MessagesTimeline.test.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,17 @@ describe("MessagesTimeline", () => {
267267
getState: () => ({ isAtEnd: timelineIsAtEnd }),
268268
getScrollableNode: () => null,
269269
} as unknown as LegendListRef;
270-
let isResting = true;
270+
let isResting = false;
271+
let composerState: ReturnType<typeof useComposerFocusState> | undefined;
271272
function ThreadProbe() {
272-
const composer = useComposerFocusState(false);
273+
const composer = useComposerFocusState();
273274
useLayoutEffect(() => {
275+
composerState = composer;
274276
isResting = shouldUseRestingComposerLayout({
275277
isExistingThread: true,
276278
isMobileViewport: false,
277-
isFocused: composer.isComposerFocused,
278279
isScrollCollapsed: composer.isComposerScrollCollapsed,
279280
hasExpandedChrome: false,
280-
collapseOnBlur: true,
281281
timelineOverflows: true,
282282
});
283283
});
@@ -309,6 +309,8 @@ describe("MessagesTimeline", () => {
309309
await act(() => {
310310
renderer = create(<ThreadProbe />);
311311
});
312+
// The user scrolled up to read, so the composer is resting.
313+
await act(() => composerState!.setIsComposerScrollCollapsed(true));
312314
const toggle = renderer!.root.findByProps({ "aria-expanded": false });
313315
await act(() => toggle.props.onClick());
314316
await flushFrame();

‎apps/web/src/components/chat/composerSelectionHold.test.ts‎

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎apps/web/src/components/chat/composerSelectionHold.ts‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎apps/web/src/components/chat/useComposerFocusState.test.tsx‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ let root: Root;
99
let composer: ReturnType<typeof useComposerFocusState>;
1010
let isResting: boolean;
1111

12-
function ComposerProbe({ isMobileViewport = false }: { isMobileViewport?: boolean }) {
13-
const state = useComposerFocusState(isMobileViewport);
12+
function ComposerProbe() {
13+
const state = useComposerFocusState();
1414
useLayoutEffect(() => {
1515
composer = state;
1616
isResting = shouldUseRestingComposerLayout({
1717
isExistingThread: true,
18-
isMobileViewport,
19-
isFocused: state.isComposerFocused,
18+
isMobileViewport: false,
2019
isScrollCollapsed: state.isComposerScrollCollapsed,
2120
hasExpandedChrome: false,
22-
collapseOnBlur: true,
2321
timelineOverflows: true,
2422
});
2523
});
@@ -54,19 +52,13 @@ afterEach(async () => {
5452
});
5553

5654
describe("composer focus state", () => {
57-
it("expands at the timeline end after a tool call takes focus", async () => {
55+
it("stays expanded when the composer loses focus", async () => {
5856
await act(() => composer.setIsComposerFocused(true));
5957
expect(isResting).toBe(false);
6058

61-
// A tool disclosure takes focus before the user scrolls through its output.
59+
// A tool disclosure takes focus away from the editor.
6260
await act(() => composer.setIsComposerFocused(false));
63-
expect(isResting).toBe(true);
64-
65-
await act(() => composer.restoreAfterTimelineReachedEnd());
6661
expect(isResting).toBe(false);
67-
68-
await act(() => composer.setIsComposerFocused(false));
69-
expect(isResting).toBe(true);
7062
});
7163

7264
it("can collapse again on the next scroll after returning to the end", async () => {
@@ -80,9 +72,10 @@ describe("composer focus state", () => {
8072
expect(isResting).toBe(true);
8173
});
8274

83-
it("does not expand the phone composer when the timeline reaches the end", async () => {
84-
await act(() => root.render(<ComposerProbe isMobileViewport />));
75+
it("does not move focus into the composer when the timeline reaches the end", async () => {
76+
await act(() => composer.setIsComposerScrollCollapsed(true));
8577
await act(() => composer.restoreAfterTimelineReachedEnd());
78+
expect(isResting).toBe(false);
8679
expect(composer.isComposerFocused).toBe(false);
8780
});
8881
});

‎apps/web/src/components/chat/useComposerFocusState.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { useCallback, useState } from "react";
22

3-
export function useComposerFocusState(isMobileViewport: boolean) {
3+
export function useComposerFocusState() {
44
const [isComposerFocused, setIsComposerFocused] = useState(false);
55
const [isComposerScrollCollapsed, setIsComposerScrollCollapsed] = useState(false);
66

7+
// Reaching the end of the timeline lifts a scroll collapse without moving
8+
// DOM focus to the editor.
79
const restoreAfterTimelineReachedEnd = useCallback(() => {
810
setIsComposerScrollCollapsed(false);
9-
// Restore the expanded layout after a timeline control takes focus too.
10-
// This state holds the layout open without moving DOM focus to the editor.
11-
if (!isMobileViewport) {
12-
setIsComposerFocused(true);
13-
}
14-
}, [isMobileViewport]);
11+
}, []);
1512

1613
return {
1714
isComposerFocused,

0 commit comments

Comments
 (0)