Only show buttons when needed

This commit is contained in:
Sonny Bakker 2021-07-21 21:06:46 +02:00
parent b9c749d4e4
commit 38b2d91f18

View file

@ -1,24 +1,40 @@
import React from 'react'; import React from 'react';
// TODO show buttons when applicable export default class ScrollTop extends React.Component {
// TODO caclulating height needs to take into account react renders later? scrollListener = ::this.scrollListener;
const scrollTop = () => {
if (document.body.clientHeight < document.documentElement.clientHeight) { state = { showTop: false, showBottom: false };
componentDidMount() {
window.addEventListener('scroll', this.scrollListener);
}
scrollListener() {
const showBottom = window.innerHeight + window.scrollY < document.body.offsetHeight;
this.setState({
showTop: window.pageYOffset > 0 ? true : false,
showBottom: showBottom,
});
}
render() {
return ( return (
<div className="scroll-to-top"> <div className="scroll-to-top">
{this.state.showTop && (
<i <i
className="scroll-to-top__icon scroll-to-top__icon--top" className="scroll-to-top__icon scroll-to-top__icon--top"
onClick={() => window.scrollTo(0, 0)} onClick={() => window.scrollTo(0, 0)}
/> />
)}
{this.state.showBottom && (
<i <i
className="scroll-to-top__icon scroll-to-top__icon--bottom" className="scroll-to-top__icon scroll-to-top__icon--bottom"
onClick={() => window.scrollTo(0, document.body.scrollHeight)} onClick={() => window.scrollTo(0, document.body.scrollHeight)}
/> />
)}
</div> </div>
); );
} }
}
return null;
};
export default scrollTop;