summaryrefslogtreecommitdiff
path: root/src/components/Button.tsx
blob: dbac20dd9c5c180978691642ceb15950ad3385da (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
import React, { useCallback } from 'react';
import { useHistory } from 'react-router-dom';

export interface Props extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  onClick?: () => void;
  route?: string;
  variant?: 'contained' | 'outlined';
  size?: 'sm' | 'md' | 'lg';
  children?: string;
}

const variants = {
  contained: 'bg-black text-white hover:underline ring-gray-400',
  outlined: 'bg-white hover:shadow border border-gray-300 ring-gray-200',
};

const sizes = {
  lg: 'p-5',
  md: 'p-4',
  sm: 'p-3',
};

const style = 'm-2 rounded-sm font-semibold focus:outline-none active:ring-2';

const Button: React.FC<Props> = ({ onClick, route, variant = 'contained', size = 'md', type = 'button', ...props }) => {
  const history = useHistory();
  const navigateRoute = useCallback(() => history.push(route || '/'), [route, history]);

  return (
    <button
      type={type}
      onClick={route ? navigateRoute : onClick}
      className={`${style} ${variants[variant]} ${sizes[size]}`}
      {...props}
    />
  );
};

export default Button;