aboutsummaryrefslogtreecommitdiff
path: root/src/components/Fab/Fab.tsx
blob: 7ca28931f08987fc83633e41b061dda81f353d97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React from 'react';
import { useHistory } from 'react-router-dom';
import { Fab as FabBase, Slide, useScrollTrigger } from '@material-ui/core/';
import { makeStyles } from '@material-ui/core/styles';
import PlusIcon from '@material-ui/icons/Add';

interface PropTypes {
  hideOnScroll?: boolean;
}

const useStyles = makeStyles(theme => ({
  root: {
    zIndex: 1000,
    position: 'fixed',

    [theme.breakpoints.down('sm')]: {
      right: theme.spacing(2),
      bottom: theme.spacing(8)
    },
    [theme.breakpoints.up('md')]: {
      right: theme.spacing(5),
      bottom: theme.spacing(5)
    }
  }
}));

const Fab: React.FC<PropTypes> = ({ hideOnScroll = false }) => {
  const classes = useStyles();
  const history = useHistory();
  const trigger = useScrollTrigger();

  const handleClick = () => {
    history.push('/new');
  };

  return (
    <Slide appear={false} direction="up" in={(!hideOnScroll) || !trigger}>
      <FabBase
        onClick={handleClick}
        className={classes.root}
        color="secondary"
      >
        <PlusIcon />
      </FabBase>
    </Slide>
  );
};

export default Fab;