Skip to content

Commit 003af8d

Browse files
authored
Merge pull request #1 from atlanhq/fix/modal-scroll-jump
fix: prevent modal scroll from jumping to bottom
2 parents 41f6522 + ad46825 commit 003af8d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

web/src/Modal.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { XMarkIcon } from '@heroicons/react/24/outline';
44
export function Modal(props) {
55
const { stopHandler, navBar, children } = props;
66
const logsEndRef = useRef(null);
7+
const scrollContainerRef = useRef(null);
8+
const isUserNearBottom = useRef(true);
79

810
useEffect(() => {
911
document.body.style.overflow = 'hidden';
@@ -13,9 +15,19 @@ export function Modal(props) {
1315
}, []);
1416

1517
useEffect(() => {
16-
logsEndRef.current.scrollIntoView();
18+
if (isUserNearBottom.current) {
19+
logsEndRef.current.scrollIntoView();
20+
}
1721
}, [children]);
1822

23+
const handleScroll = () => {
24+
const container = scrollContainerRef.current;
25+
if (!container) return;
26+
const threshold = 50;
27+
isUserNearBottom.current =
28+
container.scrollHeight - container.scrollTop - container.clientHeight < threshold;
29+
};
30+
1931
return (
2032
<div
2133
className="fixed flex inset-0 z-10 bg-gray-500 bg-opacity-75"
@@ -35,7 +47,7 @@ export function Modal(props) {
3547
</button>
3648
</div>
3749
<nav>{navBar}</nav>
38-
<div className="h-full relative overflow-y-auto p-4 bg-slate-800 rounded-b-lg font-normal">
50+
<div ref={scrollContainerRef} onScroll={handleScroll} className="h-full relative overflow-y-auto p-4 bg-slate-800 rounded-b-lg font-normal">
3951
{children}
4052
<p ref={logsEndRef} />
4153
</div>

0 commit comments

Comments
 (0)