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';
// TODO show buttons when applicable
// TODO caclulating height needs to take into account react renders later?
const scrollTop = () => {
if (document.body.clientHeight < document.documentElement.clientHeight) {
export default class ScrollTop extends React.Component {
scrollListener = ::this.scrollListener;
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 (
<div className="scroll-to-top">
<i
className="scroll-to-top__icon scroll-to-top__icon--top"
onClick={() => window.scrollTo(0, 0)}
/>
<i
className="scroll-to-top__icon scroll-to-top__icon--bottom"
onClick={() => window.scrollTo(0, document.body.scrollHeight)}
/>
{this.state.showTop && (
<i
className="scroll-to-top__icon scroll-to-top__icon--top"
onClick={() => window.scrollTo(0, 0)}
/>
)}
{this.state.showBottom && (
<i
className="scroll-to-top__icon scroll-to-top__icon--bottom"
onClick={() => window.scrollTo(0, document.body.scrollHeight)}
/>
)}
</div>
);
}
return null;
};
export default scrollTop;
}