Skip to content

Commit ca2d081

Browse files
committed
fix: use preventScroll on focus calls so autoFocus opt-in doesn't scroll page
When users explicitly set autoFocus={true}, the textarea now focuses with { preventScroll: true } so the page doesn't jump. Also applies to the modal-open focus path. Added tests verifying default no-focus behavior and that opt-in focus uses preventScroll. https://claude.ai/code/session_01LPiJ8GcUNtWdS2W6MYSE4b
1 parent f41133d commit ca2d081

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

packages/react-core/src/v2/components/chat/CopilotChatInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export function CopilotChatInput({
252252
}
253253

254254
if (config?.isModalOpen && !previousModalStateRef.current) {
255-
inputRef.current?.focus();
255+
inputRef.current?.focus({ preventScroll: true });
256256
}
257257

258258
previousModalStateRef.current = config?.isModalOpen;
@@ -1224,7 +1224,7 @@ export namespace CopilotChatInput {
12241224

12251225
useEffect(() => {
12261226
if (autoFocus) {
1227-
internalTextareaRef.current?.focus();
1227+
internalTextareaRef.current?.focus({ preventScroll: true });
12281228
}
12291229
}, [autoFocus]);
12301230

packages/react-core/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,5 +1006,27 @@ describe("CopilotChatInput", () => {
10061006
// Clean up
10071007
delete (HTMLElement.prototype as any).scrollIntoView;
10081008
});
1009+
1010+
it("does not auto-focus the textarea by default", () => {
1011+
const focusSpy = vi.spyOn(HTMLElement.prototype, "focus");
1012+
1013+
renderWithProvider(
1014+
<CopilotChatInput onSubmitMessage={mockOnSubmitMessage} />,
1015+
);
1016+
1017+
expect(focusSpy).not.toHaveBeenCalled();
1018+
focusSpy.mockRestore();
1019+
});
1020+
1021+
it("auto-focuses with preventScroll when autoFocus is true", () => {
1022+
const focusSpy = vi.spyOn(HTMLElement.prototype, "focus");
1023+
1024+
renderWithProvider(
1025+
<CopilotChatInput autoFocus onSubmitMessage={mockOnSubmitMessage} />,
1026+
);
1027+
1028+
expect(focusSpy).toHaveBeenCalledWith({ preventScroll: true });
1029+
focusSpy.mockRestore();
1030+
});
10091031
});
10101032
});

0 commit comments

Comments
 (0)