2017-05-18 21:21:33 +03:00
|
|
|
import React from 'react';
|
|
|
|
import remcalc from 'remcalc';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import is, { isOr } from 'styled-is';
|
|
|
|
import styled, { css } from 'styled-components';
|
|
|
|
import { A, Button as NButton } from 'normalized-styled-components';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import { bottomShaddow, borderRadius } from '../boxes';
|
|
|
|
import paperEffect from '../paper-effect';
|
|
|
|
import typography from '../typography';
|
|
|
|
import Baseline from '../baseline';
|
2017-08-08 13:07:18 +03:00
|
|
|
import StatusLoader from '../status-loader';
|
2017-05-18 21:21:33 +03:00
|
|
|
|
|
|
|
// Based on bootstrap 4
|
|
|
|
const style = css`
|
|
|
|
box-sizing: border-box;
|
|
|
|
display: inline-block;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
margin: 0;
|
|
|
|
padding: ${remcalc(15)} ${remcalc(18)};
|
|
|
|
position: relative;
|
|
|
|
|
|
|
|
${typography.fontFamily};
|
|
|
|
${typography.normal};
|
|
|
|
|
|
|
|
font-size: ${remcalc(15)};
|
|
|
|
text-align: center;
|
|
|
|
font-style: normal;
|
|
|
|
font-stretch: normal;
|
|
|
|
line-height: normal;
|
|
|
|
letter-spacing: normal;
|
|
|
|
text-decoration: none;
|
|
|
|
|
|
|
|
white-space: nowrap;
|
|
|
|
vertical-align: middle;
|
|
|
|
touch-action: manipulation;
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
color: ${props => props.theme.white};
|
|
|
|
background-image: none;
|
|
|
|
background-color: ${props => props.theme.primary};
|
|
|
|
border-radius: ${borderRadius};
|
|
|
|
border: solid ${remcalc(1)} ${props => props.theme.primaryDesaturated};
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
outline: 0;
|
|
|
|
text-decoration: none;
|
|
|
|
background-color: ${props => props.theme.primary};
|
|
|
|
border-color: ${props => props.theme.primaryDesaturated};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background-color: ${props => props.theme.primaryHover};
|
2017-07-11 17:11:52 +03:00
|
|
|
border: solid ${remcalc(1)} ${props => props.theme.primaryActive};
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&:active,
|
|
|
|
&:active:hover,
|
|
|
|
&:active:focus {
|
|
|
|
background-image: none;
|
|
|
|
outline: 0;
|
|
|
|
background-color: ${props => props.theme.primaryActive};
|
2017-07-11 17:11:52 +03:00
|
|
|
border-color: ${props => props.theme.primaryActive};
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&[disabled] {
|
|
|
|
cursor: not-allowed;
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
${is('secondary')`
|
|
|
|
color: ${props => props.theme.secondary};
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background-color: ${props => props.theme.whiteHover};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active,
|
|
|
|
&:active:hover,
|
|
|
|
&:active:focus {
|
|
|
|
background-color: ${props => props.theme.whiteActive};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
|
|
|
|
${is('tertiary')`
|
|
|
|
color: ${props => props.theme.secondary};
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
box-shadow: ${paperEffect};
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background-color: ${props => props.theme.whiteHover};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active,
|
|
|
|
&:active:hover,
|
|
|
|
&:active:focus {
|
|
|
|
background-color: ${props => props.theme.whiteActive};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
box-shadow: ${bottomShaddow};
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
|
|
|
|
${is('disabled')`
|
|
|
|
color: ${props => props.theme.grey};
|
|
|
|
background-color: ${props => props.theme.disabled};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
box-shadow: ${bottomShaddow};
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
background-color: ${props => props.theme.disabled};
|
|
|
|
border-color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background-color: ${props => props.theme.disabled};
|
|
|
|
border-color: ${props => props.theme.disabled};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active,
|
|
|
|
&:active:hover,
|
|
|
|
&:active:focus {
|
|
|
|
background-color: ${props => props.theme.disabled};
|
|
|
|
border-color: ${props => props.theme.disabled};
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
|
|
|
|
${isOr('rect', 'tertiary')`
|
|
|
|
border-radius: 0;
|
|
|
|
`}
|
|
|
|
`;
|
|
|
|
|
2017-05-25 17:59:58 +03:00
|
|
|
const StyledButton = NButton.extend`
|
2017-05-18 21:21:33 +03:00
|
|
|
min-width: ${remcalc(120)};
|
|
|
|
${style}
|
|
|
|
|
|
|
|
& + button {
|
|
|
|
margin-left: ${remcalc(20)};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2017-05-25 17:59:58 +03:00
|
|
|
const StyledAnchor = A.extend`
|
2017-05-18 21:21:33 +03:00
|
|
|
display: inline-block;
|
2017-07-26 15:50:49 +03:00
|
|
|
${style};
|
2017-05-18 21:21:33 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledLink = styled(Link)`
|
|
|
|
display: inline-block;
|
2017-08-30 03:22:35 +03:00
|
|
|
${style};
|
2017-05-18 21:21:33 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @example ./usage.md
|
|
|
|
*/
|
|
|
|
const Button = props => {
|
2017-08-08 13:07:18 +03:00
|
|
|
const { href = '', to = '', loading = false, secondary, tertiary } = props;
|
2017-05-18 21:21:33 +03:00
|
|
|
|
|
|
|
const Views = [
|
|
|
|
() => (to ? StyledLink : null),
|
|
|
|
() => (href ? StyledAnchor : null),
|
|
|
|
() => StyledButton
|
|
|
|
];
|
|
|
|
|
|
|
|
const View = Views.reduce((sel, view) => (sel ? sel : view()), null);
|
|
|
|
|
2017-08-28 22:21:08 +03:00
|
|
|
const children = loading ? (
|
|
|
|
<StatusLoader secondary={!secondary} tertiary={tertiary} />
|
|
|
|
) : (
|
|
|
|
props.children
|
2017-05-18 21:21:33 +03:00
|
|
|
);
|
2017-08-28 22:21:08 +03:00
|
|
|
|
|
|
|
return <View {...props}>{children}</View>;
|
2017-05-18 21:21:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Button.propTypes = {
|
|
|
|
/**
|
|
|
|
* The `<button>`/`<a>`/`<Link>` text
|
|
|
|
*/
|
|
|
|
children: PropTypes.node,
|
|
|
|
/**
|
|
|
|
* When used, it will render an `<a>` with the givern href
|
|
|
|
*/
|
|
|
|
href: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* When used, it will render a `<Link>` with the givern to
|
|
|
|
*/
|
|
|
|
to: PropTypes.string,
|
|
|
|
primary: PropTypes.bool,
|
|
|
|
secondary: PropTypes.bool,
|
|
|
|
tertiary: PropTypes.bool
|
|
|
|
};
|
|
|
|
|
|
|
|
Button.defaultProps = {
|
|
|
|
primary: true
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Baseline(Button);
|