- 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]>
25 lines
677 B
TypeScript
25 lines
677 B
TypeScript
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;
|
|
}
|