Open History to see the last commit
1export const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 64;
2
3interface ScrollPosition {
4 scrollTop: number;
5 clientHeight: number;
6 scrollHeight: number;
7}
8
9export function isScrollContainerNearBottom(
10 position: ScrollPosition,
11 thresholdPx = AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
12): boolean {
13 const threshold = Number.isFinite(thresholdPx)
14 ? Math.max(0, thresholdPx)
15 : AUTO_SCROLL_BOTTOM_THRESHOLD_PX;
16
17 const { scrollTop, clientHeight, scrollHeight } = position;
18 if (![scrollTop, clientHeight, scrollHeight].every(Number.isFinite)) {
19 return true;
20 }
21
22 const distanceFromBottom = scrollHeight - clientHeight - scrollTop;
23 return distanceFromBottom <= threshold;
24}