Compare commits

...
Author SHA1 Message Date
Julius Marmingeandcodex 0476e50b12 Respect chat scroll position during streaming updates
- only auto-scroll when the user is already near the bottom
- centralize near-bottom detection in `chat-scroll.ts` with a 64px threshold
- add unit tests covering threshold and edge-case behavior

Co-authored-by: codex <[email protected]>
2026-02-15 10:52:27 -08:00
3 changed files with 115 additions and 9 deletions

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
import { describe, expect, it } from "vitest";
import {
AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
isScrollContainerNearBottom,
} from "./chat-scroll";
describe("isScrollContainerNearBottom", () => {
it("returns true when already at bottom", () => {
expect(
isScrollContainerNearBottom({
scrollTop: 600,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(true);
});
it("returns true when within the auto-scroll threshold", () => {
expect(
isScrollContainerNearBottom({
scrollTop: 540,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(true);
});
it("returns false when the user is meaningfully above the bottom", () => {
expect(
isScrollContainerNearBottom({
scrollTop: 520,
clientHeight: 400,
scrollHeight: 1_000,
}),
).toBe(false);
});
it("clamps negative thresholds to zero", () => {
expect(
isScrollContainerNearBottom(
{
scrollTop: 539,
clientHeight: 400,
scrollHeight: 1_000,
},
-1,
),
).toBe(false);
});
it("falls back to the default threshold for non-finite values", () => {
expect(
isScrollContainerNearBottom(
{
scrollTop: 540,
clientHeight: 400,
scrollHeight: 1_000,
},
Number.NaN,
),
).toBe(true);
expect(AUTO_SCROLL_BOTTOM_THRESHOLD_PX).toBe(64);
});
});
+24
View File
@@ -0,0 +1,24 @@
export const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 64;
interface ScrollPosition {
scrollTop: number;
clientHeight: number;
scrollHeight: number;
}
export function isScrollContainerNearBottom(
position: ScrollPosition,
thresholdPx = AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
): boolean {
const threshold = Number.isFinite(thresholdPx)
? Math.max(0, thresholdPx)
: AUTO_SCROLL_BOTTOM_THRESHOLD_PX;
const { scrollTop, clientHeight, scrollHeight } = position;
if (![scrollTop, clientHeight, scrollHeight].every(Number.isFinite)) {
return true;
}
const distanceFromBottom = scrollHeight - clientHeight - scrollTop;
return distanceFromBottom <= threshold;
}
+26 -9
View File
@@ -45,6 +45,7 @@ import {
formatElapsed,
formatTimestamp,
} from "../session-logic";
import { isScrollContainerNearBottom } from "../chat-scroll";
import { useStore } from "../store";
import type { ChatImageAttachment } from "../types";
import BranchToolbar from "./BranchToolbar";
@@ -145,6 +146,7 @@ export default function ChatView() {
const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
const messagesScrollRef = useRef<HTMLDivElement>(null);
const messagesEndRef = useRef<HTMLDivElement>(null);
const shouldAutoScrollRef = useRef(true);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
const dragDepthRef = useRef(0);
@@ -366,19 +368,30 @@ export default function ChatView() {
// Auto-scroll on new messages
const messageCount = activeThread?.messages.length ?? 0;
const workLogCount = workLogEntries.length;
useLayoutEffect(() => {
if (!activeThread?.id) return;
const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
scrollContainer.scrollTop = scrollContainer.scrollHeight;
}, [activeThread?.id]);
scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
shouldAutoScrollRef.current = true;
}, []);
const onMessagesScroll = useCallback(() => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
shouldAutoScrollRef.current = isScrollContainerNearBottom(scrollContainer);
}, []);
useLayoutEffect(() => {
if (!activeThread?.id) return;
scrollMessagesToBottom();
}, [activeThread?.id, scrollMessagesToBottom]);
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messageCount]);
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom("smooth");
}, [messageCount, scrollMessagesToBottom]);
useEffect(() => {
if (phase !== "running") return;
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [phase, workLogCount]);
if (!shouldAutoScrollRef.current) return;
scrollMessagesToBottom("smooth");
}, [phase, workLogCount, scrollMessagesToBottom]);
useEffect(() => {
setExpandedWorkGroups({});
@@ -943,7 +956,11 @@ export default function ChatView() {
)}
{/* Messages */}
<div ref={messagesScrollRef} className="min-h-0 flex-1 overflow-y-auto px-5 py-4">
<div
ref={messagesScrollRef}
className="min-h-0 flex-1 overflow-y-auto px-5 py-4"
onScroll={onMessagesScroll}
>
{activeThread.messages.length === 0 && !isWorking ? (
<div className="flex h-full items-center justify-center">
<p className="text-sm text-muted-foreground/30">