Fix sidebar height & hide ScrollUpButton until scrolled (#384)

This commit is contained in:
Stewart Thomson 2021-01-24 14:58:50 -05:00 committed by GitHub
parent d0f47c738b
commit 1cbd273974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -3,11 +3,39 @@ import KeyboardArrowUp from '@material-ui/icons/KeyboardArrowUp';
import React, {Component} from 'react'; import React, {Component} from 'react';
class ScrollUpButton extends Component { class ScrollUpButton extends Component {
state = {
display: 'none',
opacity: 0,
};
componentDidMount() {
window.addEventListener('scroll', this.scrollHandler);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.scrollHandler);
}
scrollHandler = () => {
const currentScrollPos = window.pageYOffset;
const opacity = Math.min(currentScrollPos / 500, 1);
const nextState = {display: currentScrollPos > 0 ? 'inherit' : 'none', opacity};
if (this.state.display !== nextState.display || this.state.opacity !== nextState.opacity) {
this.setState(nextState);
}
};
public render() { public render() {
return ( return (
<Fab <Fab
color="primary" color="primary"
style={{position: 'fixed', bottom: '30px', right: '30px', zIndex: 100000}} style={{
position: 'fixed',
bottom: '30px',
right: '30px',
zIndex: 100000,
display: this.state.display,
opacity: this.state.opacity,
}}
onClick={this.scrollUp}> onClick={this.scrollUp}>
<KeyboardArrowUp /> <KeyboardArrowUp />
</Fab> </Fab>

View File

@ -12,7 +12,10 @@ import {Button, Hidden, IconButton, Typography} from '@material-ui/core';
import {DrawerProps} from '@material-ui/core/Drawer/Drawer'; import {DrawerProps} from '@material-ui/core/Drawer/Drawer';
import CloseIcon from '@material-ui/icons/Close'; import CloseIcon from '@material-ui/icons/Close';
const styles = (theme: Theme): StyleRules<'drawerPaper' | 'toolbar' | 'link'> => ({ const styles = (theme: Theme): StyleRules<'root' | 'drawerPaper' | 'toolbar' | 'link'> => ({
root: {
height: '100%',
},
drawerPaper: { drawerPaper: {
position: 'relative', position: 'relative',
width: 250, width: 250,
@ -26,7 +29,7 @@ const styles = (theme: Theme): StyleRules<'drawerPaper' | 'toolbar' | 'link'> =>
}, },
}); });
type Styles = WithStyles<'drawerPaper' | 'toolbar' | 'link'>; type Styles = WithStyles<'root' | 'drawerPaper' | 'toolbar' | 'link'>;
interface IProps { interface IProps {
loggedIn: boolean; loggedIn: boolean;
@ -74,7 +77,7 @@ class Navigation extends Component<
return ( return (
<ResponsiveDrawer <ResponsiveDrawer
classes={{paper: classes.drawerPaper}} classes={{root: classes.root, paper: classes.drawerPaper}}
navOpen={navOpen} navOpen={navOpen}
setNavOpen={setNavOpen} setNavOpen={setNavOpen}
id="message-navigation"> id="message-navigation">