Skip to content

Commit a5594d7

Browse files
Merge pull request #502 from AndreWohnsland/dev
Hide jump to top button if at top
2 parents a968af8 + f7dc172 commit a5594d7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

web_client/src/components/common/JumpToTopButton.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
1+
import { useEffect, useState } from 'react';
12
import { FaArrowUp } from 'react-icons/fa';
23

34
export const JumpToTopButton: React.FC = () => {
5+
const [isVisible, setIsVisible] = useState(false);
6+
7+
useEffect(() => {
8+
const updateVisibility = () => {
9+
const root = document.documentElement;
10+
const canScroll = root.scrollHeight > root.clientHeight;
11+
const isAtTop = window.scrollY <= 0;
12+
setIsVisible(canScroll && !isAtTop);
13+
};
14+
15+
updateVisibility();
16+
window.addEventListener('scroll', updateVisibility, { passive: true });
17+
window.addEventListener('resize', updateVisibility);
18+
19+
return () => {
20+
window.removeEventListener('scroll', updateVisibility);
21+
window.removeEventListener('resize', updateVisibility);
22+
};
23+
}, []);
24+
425
const scrollToTop = () => {
526
window.scrollTo({ top: 0, behavior: 'smooth' });
627
};
28+
29+
if (!isVisible) {
30+
return null;
31+
}
32+
733
return (
834
<div className='sticky-bottom'>
935
<button

0 commit comments

Comments
 (0)