zepio/app/components/button.js

122 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-12-05 10:14:30 -08:00
// @flow
import React from 'react';
import styled from 'styled-components';
import { Link } from 'react-router-dom';
/* eslint-disable import/no-extraneous-dependencies */
2018-12-15 07:10:39 -08:00
/* eslint-disable max-len */
2018-12-05 10:14:30 -08:00
// $FlowFixMe
import { darken } from 'polished';
2018-12-20 06:10:25 -08:00
const DefaultButton = styled.button`
2018-12-05 10:14:30 -08:00
padding: 10px 30px;
font-family: ${props => props.theme.fontFamily};
font-weight: ${props => props.theme.fontWeight.bold};
font-size: ${props => `${props.theme.fontSize.regular}em`};
2018-12-05 10:14:30 -08:00
cursor: pointer;
outline: none;
min-width: 100px;
border-radius: 100px;
2019-01-12 09:55:05 -08:00
transition: background-color 0.1s
${props => props.theme.colors.transitionEase};
2018-12-20 06:10:25 -08:00
width: 100%;
2018-12-05 10:14:30 -08:00
`;
2018-12-20 06:10:25 -08:00
const Primary = styled(DefaultButton)`
2018-12-05 10:14:30 -08:00
background-color: ${props => props.theme.colors.primary};
color: ${props => props.theme.colors.secondary};
border: none;
&:hover {
background-color: ${props => darken(0.1, props.theme.colors.primary(props))};
}
&:disabled {
background-color: #3e3c42;
cursor: not-allowed;
opacity: 0.8;
}
`;
2018-12-20 06:10:25 -08:00
const Secondary = styled(DefaultButton)`
background-color: transparent;
2018-12-05 10:14:30 -08:00
color: ${props => props.theme.colors.secondary};
border: 2px solid #3e3c42;
&:hover {
border-color: ${props => props.theme.colors.primary};
}
&:disabled {
background-color: Transparent;
cursor: not-allowed;
color: #3e3c42;
&:hover {
border-color: #3e3c42;
}
}
`;
const Icon = styled.img`
height: 9px;
width: 12px;
margin-right: 10px;
`;
2018-12-05 10:14:30 -08:00
type Props = {
label: string,
onClick?: () => void,
2019-01-12 09:55:05 -08:00
to?: ?string,
2018-12-05 10:14:30 -08:00
variant?: 'primary' | 'secondary',
disabled?: boolean,
icon?: string | null,
2018-12-20 06:10:25 -08:00
className?: string,
2018-12-21 07:42:31 -08:00
isLoading?: boolean,
2018-12-05 10:14:30 -08:00
};
export const Button = ({
2018-12-20 06:10:25 -08:00
onClick,
label,
to,
variant,
disabled,
icon,
2018-12-20 06:10:25 -08:00
className,
2018-12-21 07:42:31 -08:00
isLoading,
2018-12-05 10:14:30 -08:00
}: Props) => {
if (to && onClick) throw new Error('Should define either "to" or "onClick"');
2018-12-05 10:14:30 -08:00
const props = {
onClick,
disabled: disabled || isLoading,
icon: null,
className,
};
2018-12-21 07:42:31 -08:00
const buttonLabel = isLoading ? 'Loading...' : label;
2018-12-05 10:14:30 -08:00
const component = variant === 'primary' ? (
<Primary {...props}>
{icon ? <Icon src={icon} /> : null}
{buttonLabel}
</Primary>
2018-12-05 10:14:30 -08:00
) : (
<Secondary {...props}>
{icon ? <Icon src={icon} /> : null}
{buttonLabel}
</Secondary>
2018-12-05 10:14:30 -08:00
);
return to ? <Link to={String(to)}>{component}</Link> : component;
2018-12-05 10:14:30 -08:00
};
Button.defaultProps = {
to: '',
icon: null,
2018-12-05 10:14:30 -08:00
variant: 'primary',
onClick: () => {},
2018-12-05 10:14:30 -08:00
disabled: false,
2018-12-20 06:10:25 -08:00
className: '',
2018-12-21 07:42:31 -08:00
isLoading: false,
2018-12-05 10:14:30 -08:00
};