|
| 1 | +import { makeStyles } from '@material-ui/core'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import Icon from '../../icons/components/Icon'; |
| 6 | +import { BUTTON_TYPES } from '../../react/constants'; |
| 7 | +import { withPixelLineHeight } from '../../styles/functions.web'; |
| 8 | + |
| 9 | +import { ButtonProps } from './types'; |
| 10 | + |
| 11 | +interface IButtonProps extends ButtonProps { |
| 12 | + |
| 13 | + /** |
| 14 | + * Whether or not the button should be full width. |
| 15 | + */ |
| 16 | + fullWidth?: boolean, |
| 17 | + |
| 18 | + /** |
| 19 | + * The id of the button. |
| 20 | + */ |
| 21 | + id?: string; |
| 22 | + |
| 23 | + /** |
| 24 | + * Click callback. |
| 25 | + */ |
| 26 | + onClick: () => void; |
| 27 | + |
| 28 | + /** |
| 29 | + * Which size the button should be. |
| 30 | + */ |
| 31 | + size?: 'small' | 'medium' | 'large'; |
| 32 | +} |
| 33 | + |
| 34 | +const useStyles = makeStyles((theme: any) => { |
| 35 | + return { |
| 36 | + button: { |
| 37 | + backgroundColor: theme.palette.action01, |
| 38 | + color: theme.palette.text01, |
| 39 | + borderRadius: theme.shape.borderRadius, |
| 40 | + padding: '10px 16px', |
| 41 | + display: 'flex', |
| 42 | + alignItems: 'center', |
| 43 | + justifyContent: 'center', |
| 44 | + border: 0, |
| 45 | + ...withPixelLineHeight(theme.typography.bodyShortBold), |
| 46 | + transition: 'background .2s', |
| 47 | + cursor: 'pointer', |
| 48 | + |
| 49 | + '&:hover': { |
| 50 | + backgroundColor: theme.palette.action01Hover |
| 51 | + }, |
| 52 | + |
| 53 | + '&:active': { |
| 54 | + backgroundColor: theme.palette.action01Active |
| 55 | + }, |
| 56 | + |
| 57 | + '&:focus': { |
| 58 | + outline: 0, |
| 59 | + boxShadow: `0px 0px 0px 2px ${theme.palette.focus01}` |
| 60 | + }, |
| 61 | + |
| 62 | + '& svg': { |
| 63 | + fill: theme.palette.icon01 |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + primary: {}, |
| 68 | + |
| 69 | + secondary: { |
| 70 | + backgroundColor: theme.palette.action02, |
| 71 | + color: theme.palette.text04, |
| 72 | + |
| 73 | + '&:hover': { |
| 74 | + backgroundColor: theme.palette.action02Hover |
| 75 | + }, |
| 76 | + |
| 77 | + '&:active': { |
| 78 | + backgroundColor: theme.palette.action02Active |
| 79 | + }, |
| 80 | + |
| 81 | + '& svg': { |
| 82 | + fill: theme.palette.icon04 |
| 83 | + } |
| 84 | + }, |
| 85 | + |
| 86 | + tertiary: { |
| 87 | + backgroundColor: theme.palette.action03, |
| 88 | + |
| 89 | + '&:hover': { |
| 90 | + backgroundColor: theme.palette.action03Hover |
| 91 | + }, |
| 92 | + |
| 93 | + '&:active': { |
| 94 | + backgroundColor: theme.palette.action03Active |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + destructive: { |
| 99 | + backgroundColor: theme.palette.actionDanger, |
| 100 | + |
| 101 | + '&:hover': { |
| 102 | + backgroundColor: theme.palette.actionDangerHover |
| 103 | + }, |
| 104 | + |
| 105 | + '&:active': { |
| 106 | + backgroundColor: theme.palette.actionDangerActive |
| 107 | + } |
| 108 | + }, |
| 109 | + |
| 110 | + disabled: { |
| 111 | + backgroundColor: theme.palette.disabled01, |
| 112 | + color: theme.palette.text03, |
| 113 | + |
| 114 | + '&:hover': { |
| 115 | + backgroundColor: theme.palette.disabled01, |
| 116 | + color: theme.palette.text03 |
| 117 | + }, |
| 118 | + |
| 119 | + '&:active': { |
| 120 | + backgroundColor: theme.palette.disabled01, |
| 121 | + color: theme.palette.text03 |
| 122 | + }, |
| 123 | + |
| 124 | + '& svg': { |
| 125 | + fill: theme.palette.icon03 |
| 126 | + } |
| 127 | + }, |
| 128 | + |
| 129 | + iconButton: { |
| 130 | + padding: '10px' |
| 131 | + }, |
| 132 | + |
| 133 | + textWithIcon: { |
| 134 | + marginLeft: `${theme.spacing(2)}px` |
| 135 | + }, |
| 136 | + |
| 137 | + small: { |
| 138 | + padding: '8px 16px', |
| 139 | + ...withPixelLineHeight(theme.typography.labelBold), |
| 140 | + |
| 141 | + '&.iconButton': { |
| 142 | + padding: '6px' |
| 143 | + } |
| 144 | + }, |
| 145 | + |
| 146 | + medium: {}, |
| 147 | + |
| 148 | + large: { |
| 149 | + padding: '13px 16px', |
| 150 | + ...withPixelLineHeight(theme.typography.bodyShortBoldLarge), |
| 151 | + |
| 152 | + '&.iconButton': { |
| 153 | + padding: '14px' |
| 154 | + } |
| 155 | + }, |
| 156 | + |
| 157 | + fullWidth: { |
| 158 | + width: '100%' |
| 159 | + } |
| 160 | + }; |
| 161 | +}); |
| 162 | + |
| 163 | +const Button = ({ |
| 164 | + accessibilityLabel, |
| 165 | + disabled, |
| 166 | + fullWidth, |
| 167 | + icon, |
| 168 | + id, |
| 169 | + label, |
| 170 | + onClick, |
| 171 | + size = 'medium', |
| 172 | + type = BUTTON_TYPES.PRIMARY |
| 173 | +}: IButtonProps) => { |
| 174 | + const styles = useStyles(); |
| 175 | + |
| 176 | + return ( |
| 177 | + <button |
| 178 | + aria-label = { accessibilityLabel } |
| 179 | + className = { clsx(styles.button, styles[type], |
| 180 | + disabled && styles.disabled, |
| 181 | + icon && !label && `${styles.iconButton} iconButton`, |
| 182 | + styles[size], fullWidth && styles.fullWidth) } |
| 183 | + disabled = { disabled } |
| 184 | + { ...(id ? { id } : {}) } |
| 185 | + onClick = { onClick } |
| 186 | + type = 'button'> |
| 187 | + {icon && <Icon |
| 188 | + size = { 20 } |
| 189 | + src = { icon } />} |
| 190 | + {label && <span className = { icon ? styles.textWithIcon : '' }>{label}</span>} |
| 191 | + </button> |
| 192 | + ); |
| 193 | +}; |
| 194 | + |
| 195 | +export default Button; |
0 commit comments