|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import styled from 'styled-components'; |
| 4 | +import { Link } from 'react-router'; |
| 5 | + |
| 6 | +import { remSize, prop } from '../theme'; |
| 7 | + |
| 8 | +// The '&&&' will increase the specificity of the |
| 9 | +// component's CSS so that it overrides the more |
| 10 | +// general global styles |
| 11 | +const StyledButton = styled.button` |
| 12 | + &&& { |
| 13 | + display: flex; |
| 14 | + justify-content: center; |
| 15 | + align-items: center; |
| 16 | +
|
| 17 | + width: max-content; |
| 18 | + text-decoration: none; |
| 19 | +
|
| 20 | + background-color: ${prop('buttonColorBackground')}; |
| 21 | + color: ${prop('buttonColor')}; |
| 22 | + cursor: pointer; |
| 23 | + border: 2px solid ${prop('buttonBorderColor')}; |
| 24 | + border-radius: 2px; |
| 25 | + padding: ${remSize(8)} ${remSize(25)}; |
| 26 | + line-height: 1; |
| 27 | + |
| 28 | + &:hover:not(:disabled) { |
| 29 | + color: ${prop('buttonHoverColor')}; |
| 30 | + background-color: ${prop('buttonHoverColorBackground')}; |
| 31 | + } |
| 32 | +
|
| 33 | + &:disabled { |
| 34 | + color: ${prop('buttonDisabledColor')}; |
| 35 | + background-color: ${prop('buttonDisabledColorBackground')}; |
| 36 | + cursor: not-allowed; |
| 37 | + } |
| 38 | + } |
| 39 | +`; |
| 40 | + |
| 41 | +/** |
| 42 | + * A Button performs an primary action |
| 43 | + */ |
| 44 | +const Button = ({ |
| 45 | + children, href, label, to, type, ...props |
| 46 | +}) => { |
| 47 | + if (href) { |
| 48 | + return <StyledButton as="a" aria-label={label} href={href} {...props}>{children}</StyledButton>; |
| 49 | + } |
| 50 | + |
| 51 | + if (to) { |
| 52 | + return <StyledButton as={Link} aria-label={label} to={to} {...props}>{children}</StyledButton>; |
| 53 | + } |
| 54 | + |
| 55 | + return <StyledButton aria-label={label} type={type} {...props}>{children}</StyledButton>; |
| 56 | +}; |
| 57 | + |
| 58 | +Button.defaultProps = { |
| 59 | + disabled: false, |
| 60 | + href: null, |
| 61 | + to: null, |
| 62 | + type: 'button', |
| 63 | +}; |
| 64 | + |
| 65 | +Button.propTypes = { |
| 66 | + /** |
| 67 | + * The visible part of the button, telling the user what |
| 68 | + * the action is |
| 69 | + */ |
| 70 | + children: PropTypes.element.isRequired, |
| 71 | + /** |
| 72 | + If the button can be activated or not |
| 73 | + */ |
| 74 | + disabled: PropTypes.bool, |
| 75 | + /** |
| 76 | + * Specifying an href will use an <a> to link to the URL |
| 77 | + */ |
| 78 | + href: PropTypes.string, |
| 79 | + /* |
| 80 | + * An ARIA Label used for accessibility |
| 81 | + */ |
| 82 | + label: PropTypes.string.isRequired, |
| 83 | + /** |
| 84 | + * Specifying a to URL will use a react-router Link |
| 85 | + */ |
| 86 | + to: PropTypes.string, |
| 87 | + /** |
| 88 | + * If using a button, then type is defines the type of button |
| 89 | + */ |
| 90 | + type: PropTypes.oneOf(['button', 'submit']), |
| 91 | +}; |
| 92 | + |
| 93 | +export default Button; |
0 commit comments