turniere-frontend/js/components/ScrollToTopButton.js

44 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 > 1.5 * 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 (
<Button
onClick={scrollToTop}
style={{
position: 'fixed',
bottom: '20px',
right: '20px',
borderRadius: '50%',
width: '50px',
height: '50px',
zIndex: 999,
transition: 'opacity 0.5s ease-in-out',
opacity: isVisible ? 0.7 : 0,
pointerEvents: isVisible ? 'auto' : 'none'
}}
>
<FontAwesomeIcon icon={faArrowUp} />
</Button>
);
}