Add scroll to top/bottom component

This commit is contained in:
Sonny Bakker 2021-07-21 21:13:22 +02:00
parent e6cfef8d96
commit da05b3ac2e
6 changed files with 80 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import { isEqual } from 'lodash';
import { fetchCategories } from './actions/categories'; import { fetchCategories } from './actions/categories';
import ScrollTop from './components/ScrollTop.js';
import Sidebar from './components/sidebar/Sidebar.js'; import Sidebar from './components/sidebar/Sidebar.js';
import PostList from './components/postlist/PostList.js'; import PostList from './components/postlist/PostList.js';
import PostModal from './components/PostModal.js'; import PostModal from './components/PostModal.js';
@ -41,6 +42,8 @@ class App extends React.Component {
/> />
)} )}
<ScrollTop />
{this.props.error && ( {this.props.error && (
<Messages messages={[{ type: 'error', text: this.props.error.message }]} /> <Messages messages={[{ type: 'error', text: this.props.error.message }]} />
)} )}

View file

@ -0,0 +1,40 @@
import React from 'react';
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">
{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>
);
}
}

View file

@ -26,3 +26,4 @@
@import './post-message/index'; @import './post-message/index';
@import './posts/index'; @import './posts/index';
@import './posts-info/index'; @import './posts-info/index';
@import './scroll-to-top/index';

View file

@ -5,4 +5,6 @@
padding: 0; padding: 0;
cursor: pointer; cursor: pointer;
z-index: 1000;
} }

View file

@ -0,0 +1,33 @@
.scroll-to-top {
display: flex;
gap: 10px;
position: fixed;
right: 15%;
bottom: 0;
margin: 0 0 20px 0;
&:hover {
cursor: pointer;
}
&__icon {
font-style: initial;
padding: 10px;
background-color: var(--lightest-accent-color);
&--top:before {
@include font-awesome;
content: "\f062";
}
&--bottom:before {
@include font-awesome;
content: "\f063";
}
}
}

View file

@ -0,0 +1 @@
@import './scroll-to-top';