Basic scroll to top button

This commit is contained in:
Daniel Schädler 2025-03-14 22:45:39 +01:00
parent 12bfa2eafe
commit 3b05195434
2 changed files with 60 additions and 11 deletions

View File

@ -0,0 +1,45 @@
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>
)}
</>
);
}

View File

@ -16,6 +16,7 @@ import {LinkButton} from '../js/components/LinkButton';
import {LoadingPage} from '../js/components/LoadingPage';
import {getTournament} from '../js/redux/tournamentApi';
import {FavoriteBar} from '../js/components/FavoriteBar';
import {ScrollToTopButton} from '../js/components/ScrollToTopButton';
class PrivateTournamentPage extends React.Component {
render() {
@ -23,18 +24,21 @@ class PrivateTournamentPage extends React.Component {
const {isSignedIn, username} = this.props;
const isOwner = username === ownerUsername;
return (<div className='pb-5'>
<TournamentBigImage {...this.props.tournament}/>
<StatusBar tournament={this.props.tournament} isOwner={isOwner} isSignedIn={isSignedIn}/>
<FavoriteBar teams={this.props.tournament.teams}/>
<div className='stages'>
{groupStage != null &&
<div><GroupStage groups={groupStage.groups} isSignedIn={isSignedIn} isOwner={isOwner}
showMatches={playoffStages !== null}/></div>}
<PlayoffStages playoffStages={playoffStages} isSignedIn={isSignedIn}
isOwner={isOwner}/>
return (
<div className='pb-5'>
<TournamentBigImage {...this.props.tournament}/>
<StatusBar tournament={this.props.tournament} isOwner={isOwner} isSignedIn={isSignedIn}/>
<FavoriteBar teams={this.props.tournament.teams}/>
<div className='stages'>
{groupStage != null &&
<div><GroupStage groups={groupStage.groups} isSignedIn={isSignedIn} isOwner={isOwner}
showMatches={playoffStages !== null}/></div>}
<PlayoffStages playoffStages={playoffStages} isSignedIn={isSignedIn}
isOwner={isOwner}/>
</div>
<ScrollToTopButton />
</div>
</div>);
);
}
}