turniere-frontend/js/components/ScrollToTopButton.js

46 lines
1.3 KiB
JavaScript

import React, {useState, useEffect} from 'react';
import {Button} from 'reactstrap';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {faArrowUp} from '@fortawesome/free-solid-svg-icons';
export function ScrollToTopButton() {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 2 * window.innerHeight) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, []);
const scrollToTop = () => {
window.scrollTo({top: 0, behavior: 'smooth'});
};
return (
<>
{isVisible && (
<Button
onClick={scrollToTop}
style={{
position: 'fixed',
bottom: '20px',
right: '20px',
borderRadius: '50%',
width: '50px',
height: '50px',
zIndex: 999
}}
>
<FontAwesomeIcon icon={faArrowUp} />
</Button>
)}
</>
);
}