mirror of
https://github.com/block/buzz.git
synced 2026-11-04 16:31:38 +00:00
Signed-off-by: Thomas Petersen <thomasp@squareup.com> Signed-off-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: npub14vtk7pvazqrq9639qu7e560wnqtl0d53ca4gjuvq6jzf3k2el23qqlwa7f <ab176f059d100602ea25073d9a69ee9817f7b691c76a897180d48498d959faa2@sprout-oss.stage.blox.sqprod.co>
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import * as React from "react";
|
|
|
|
export function useScrollBoundaryLock(
|
|
scrollRef: React.RefObject<HTMLElement | null>,
|
|
) {
|
|
React.useEffect(() => {
|
|
const scrollElement = scrollRef.current;
|
|
if (!scrollElement) return;
|
|
|
|
const handleWheel = (event: WheelEvent) => {
|
|
if (event.deltaY === 0) return;
|
|
|
|
const maxScrollTop =
|
|
scrollElement.scrollHeight - scrollElement.clientHeight;
|
|
if (maxScrollTop <= 0) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
return;
|
|
}
|
|
|
|
const atTop = scrollElement.scrollTop <= 0;
|
|
const atBottom = scrollElement.scrollTop >= maxScrollTop - 1;
|
|
const scrollingPastTop = event.deltaY < 0 && atTop;
|
|
const scrollingPastBottom = event.deltaY > 0 && atBottom;
|
|
|
|
if (scrollingPastTop || scrollingPastBottom) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
scrollElement.scrollTop = scrollingPastTop ? 0 : maxScrollTop;
|
|
}
|
|
};
|
|
|
|
scrollElement.addEventListener("wheel", handleWheel, {
|
|
capture: true,
|
|
passive: false,
|
|
});
|
|
return () => {
|
|
scrollElement.removeEventListener("wheel", handleWheel, {
|
|
capture: true,
|
|
});
|
|
};
|
|
}, [scrollRef]);
|
|
}
|