|
1 |
| -import styled from 'vue-styled-components' |
2 |
| -// import { colors } from '../../lib/theme' |
| 1 | +import { addOpacity } from '../../lib/utils' |
| 2 | +const baseStyles = { |
| 3 | + display: 'inline-flex', |
| 4 | + appearance: 'none', |
| 5 | + alignItems: 'center', |
| 6 | + justifyContent: 'center', |
| 7 | + transition: 'all 250ms', |
| 8 | + userSelect: 'none', |
| 9 | + position: 'relative', |
| 10 | + whiteSpace: 'nowrap', |
| 11 | + verticalAlign: 'middle', |
| 12 | + lineHeight: '1.2', |
| 13 | + outline: 'none' |
| 14 | +} |
3 | 15 |
|
4 |
| -const buttonProps = { |
5 |
| - variant: String |
| 16 | +/** |
| 17 | + * Size values |
| 18 | + */ |
| 19 | +const sizes = { |
| 20 | + lg: { |
| 21 | + height: 12, |
| 22 | + minWidth: 12, |
| 23 | + fontSize: 'lg', |
| 24 | + px: 6, |
| 25 | + py: 4 |
| 26 | + }, |
| 27 | + md: { |
| 28 | + height: 10, |
| 29 | + minWidth: 10, |
| 30 | + fontSize: 'md', |
| 31 | + px: 4, |
| 32 | + py: 3 |
| 33 | + }, |
| 34 | + sm: { |
| 35 | + height: 8, |
| 36 | + minWidth: 8, |
| 37 | + fontSize: 'sm', |
| 38 | + px: 3, |
| 39 | + py: 2 |
| 40 | + }, |
| 41 | + xs: { |
| 42 | + height: 6, |
| 43 | + minWidth: 6, |
| 44 | + fontSize: 'xs', |
| 45 | + px: 2, |
| 46 | + py: 1 |
| 47 | + } |
6 | 48 | }
|
7 | 49 |
|
8 | 50 | /**
|
9 |
| - * @description Determines style colors for solid variant |
10 |
| - * @param {{color:String, colorMode:String, _theme:Object }} params |
| 51 | + * @description Determines size props |
| 52 | + * @param {Object} param0 Props object |
| 53 | + * @returns {Object} Size style props |
11 | 54 | */
|
12 |
| -const solidVariantProps = ({ color, colorMode = 'light', _theme }) => { |
| 55 | +const sizeProps = ({ size }) => sizes[size] |
| 56 | + |
| 57 | +/** |
| 58 | + * @description Get solid button style values |
| 59 | + * @param {Object} props - Style props object |
| 60 | + * @returns {Object} - Solid styles object |
| 61 | + */ |
| 62 | +const getSolidStyles = ({ color, colorMode }) => { |
13 | 63 | let style = {
|
14 | 64 | light: {
|
15 | 65 | bg: `${color}.400`,
|
16 |
| - color: 'white', |
| 66 | + color: `${color}.50`, |
17 | 67 | _hover: {
|
18 |
| - bg: `${color}.600` |
| 68 | + bg: `${color}.500` |
19 | 69 | },
|
20 | 70 | _active: {
|
21 |
| - bg: `${color}.700` |
| 71 | + bg: `${color}.600` |
22 | 72 | }
|
23 | 73 | }
|
24 | 74 | }
|
25 | 75 | return style[colorMode]
|
26 | 76 | }
|
27 | 77 |
|
28 | 78 | /**
|
29 |
| - * @description Generates styled for variants |
30 |
| - * @param {Object} props |
31 |
| - * @returns {Object} Variant styles object |
| 79 | + * @description Get ghost button style values |
| 80 | + * @param {Object} props - Style Props |
| 81 | + * @returns {Object} - Ghost styles object |
| 82 | + */ |
| 83 | +const getGhostStyles = ({ color, colorMode, theme }) => { |
| 84 | + const _theme = theme() |
| 85 | + const _color = _theme.colors[color] && _theme.colors[color][200] |
| 86 | + let result = { |
| 87 | + light: { |
| 88 | + color: `${color}.400`, |
| 89 | + bg: `${color}.50`, |
| 90 | + _hover: { |
| 91 | + bg: `${color}.100` |
| 92 | + }, |
| 93 | + _active: { |
| 94 | + bg: `${color}.200` |
| 95 | + } |
| 96 | + }, |
| 97 | + dark: { |
| 98 | + color: `${color}.200`, |
| 99 | + bg: 'transparent', |
| 100 | + _hover: { |
| 101 | + bg: addOpacity(_color, 0.12) |
| 102 | + }, |
| 103 | + _active: { |
| 104 | + bg: addOpacity(_color, 0.24) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return result[colorMode] |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * @description Get flat button style values |
| 113 | + * @param {Object} props - Style Props |
| 114 | + * @returns {Object} - Ghost styles object |
| 115 | + */ |
| 116 | +const getFlatStyles = ({ color, colorMode, theme }) => { |
| 117 | + const _theme = theme() |
| 118 | + const _color = _theme.colors[color] && _theme.colors[color][200] |
| 119 | + let result = { |
| 120 | + light: { |
| 121 | + color: `${color}.400`, |
| 122 | + bg: 'transparent', |
| 123 | + _hover: { |
| 124 | + bg: `${color}.50` |
| 125 | + }, |
| 126 | + _active: { |
| 127 | + bg: `${color}.100` |
| 128 | + } |
| 129 | + }, |
| 130 | + dark: { |
| 131 | + color: `${color}.200`, |
| 132 | + bg: 'transparent', |
| 133 | + _hover: { |
| 134 | + bg: addOpacity(_color, 0.12) |
| 135 | + }, |
| 136 | + _active: { |
| 137 | + bg: addOpacity(_color, 0.24) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return result[colorMode] |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * @description Get outline button style values |
| 146 | + * @param {Object} props - Style props object |
| 147 | + * @returns {Object} - Solid styles object |
| 148 | + */ |
| 149 | +const getOutlineStyles = props => { |
| 150 | + const { color, colorMode } = props |
| 151 | + const borderColor = { light: 'gray.200', dark: 'whiteAlpha.300' } |
| 152 | + |
| 153 | + return { |
| 154 | + border: '1px', |
| 155 | + borderColor: color === 'gray' ? borderColor[colorMode] : 'current', |
| 156 | + ...getFlatStyles(props) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * @description Get link button style values |
| 162 | + * @param {Object} props - Style props object |
| 163 | + * @returns {Object} - Solid styles object |
| 164 | + */ |
| 165 | +const getLinkStyles = ({ color, colorMode }) => { |
| 166 | + const _color = { light: `${color}.400`, dark: `${color}.200` } |
| 167 | + const _activeColor = { light: `${color}.700`, dark: `${color}.500` } |
| 168 | + return { |
| 169 | + p: 0, |
| 170 | + height: 'auto', |
| 171 | + lineHeight: 'normal', |
| 172 | + color: _color[colorMode], |
| 173 | + _hover: { |
| 174 | + textDecoration: 'underline' |
| 175 | + }, |
| 176 | + _active: { |
| 177 | + color: _activeColor[colorMode] |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * @description Determines styles for a given v |
| 184 | + * @param {Object} props - Props Object |
| 185 | + * @returns {Object} - Variant styles object |
32 | 186 | */
|
33 |
| -const variantProps = props => { |
| 187 | +const getVariantStyles = (props) => { |
34 | 188 | switch (props.variant) {
|
35 | 189 | case 'solid':
|
36 |
| - return solidVariantProps(props) |
37 |
| - } |
38 |
| -} |
39 |
| - |
40 |
| -// const baseProps = { |
41 |
| -// display: 'inline-flex', |
42 |
| -// appearance: 'none', |
43 |
| -// alignItems: 'center', |
44 |
| -// justifyContent: 'center', |
45 |
| -// transition: 'all 250ms', |
46 |
| -// userSelect: 'none', |
47 |
| -// position: 'relative', |
48 |
| -// whiteSpace: 'nowrap', |
49 |
| -// verticalAlign: 'middle', |
50 |
| -// lineHeight: '1.2', |
51 |
| -// outline: 'none' |
52 |
| -// } |
53 |
| - |
54 |
| -const Button = styled('button', buttonProps)` |
55 |
| - font-size: 1em; |
56 |
| - text-align: center; |
57 |
| - padding: 10px 15px; |
58 |
| - border-radius: ${({ radius }) => radius ? '6px' : null}; |
59 |
| - ${(props) => variantProps(props)} |
60 |
| -` |
61 |
| - |
62 |
| -export default Button |
| 190 | + return getSolidStyles(props) |
| 191 | + case 'outline': |
| 192 | + return getOutlineStyles(props) |
| 193 | + case 'ghost': |
| 194 | + return getGhostStyles(props) |
| 195 | + case 'flat': |
| 196 | + return getFlatStyles(props) |
| 197 | + case 'link': |
| 198 | + return getLinkStyles(props) |
| 199 | + default: |
| 200 | + return {} |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +/** |
| 205 | + * Button focus styles |
| 206 | + */ |
| 207 | +const focusStyles = { |
| 208 | + _focus: { |
| 209 | + outline: 'none', |
| 210 | + boxShadow: 'outline' |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/** |
| 215 | + * @description Generates Button styles based on passed variant props and theme colors. |
| 216 | + * @param {{color: String|Array<String>, theme: Object, colorMode: String, size: String|Array<String>}} props - Style props object |
| 217 | + * @returns {Object} Style object to be passed to styled component |
| 218 | + * @todo Pass the `theme` from the ThemeProvider context. Will need to create a context provider for theme. |
| 219 | + */ |
| 220 | +const createButtonStyles = (props) => { |
| 221 | + return { |
| 222 | + ...baseStyles, |
| 223 | + ...focusStyles, |
| 224 | + ...sizeProps(props), |
| 225 | + ...getVariantStyles(props) |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +export default createButtonStyles |
0 commit comments