Created
July 12, 2018 14:56
-
-
Save jxnblk/78b117d1c598d3833daee405a8578b3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demo for using styled-system variants for button styles | |
// and mapping props for size | |
import React from 'react' | |
import styled from 'styled-components' | |
import { | |
color, | |
space, | |
fontSize, | |
buttonStyle, | |
} from 'styled-system' | |
export const colors = { | |
blue: '#0067ee', | |
green: '#00cc10', | |
red: '#ee1000' | |
} | |
export const theme = { | |
colors, | |
buttons: { | |
primary: { | |
color: 'white', | |
backgroundColor: colors.blue | |
}, | |
outline: { | |
color: colors.blue, | |
backgroundColor: 'transparent', | |
borderColor: colors.blue | |
}, | |
secondary: { | |
color: 'white', | |
backgroundColor: colors.green | |
}, | |
error: { | |
color: 'white', | |
backgroundColor: colors.red | |
} | |
}, | |
} | |
const buttonSizes = { | |
small: { | |
px: 2, | |
py: 1, | |
}, | |
medium: { | |
px: 3, | |
py: 2, | |
}, | |
large: { | |
px: 4, | |
py: 3 | |
} | |
} | |
const withSize = Component => ({ | |
size, | |
...props | |
}) => | |
<Component | |
{...props} | |
{...(buttonSizes[size])} | |
/> | |
export const Button = withSize(styled.button([], { | |
fontFamily: 'inherit', | |
fontWeight: 'bold', | |
lineHeight: 16/14, | |
WebkitFontSmoothing: 'antialiased', | |
display: 'inline-block', | |
verticalAlign: 'middle', | |
textAlign: 'center', | |
textDecoration: 'none', | |
appearance: 'none', | |
borderWidth: '2px', | |
borderStyle: 'solid', | |
borderColor: 'transparent', | |
borderRadius: '4px', | |
'&:disabled': { | |
opacity: 1/4 | |
}, | |
}, | |
buttonStyle, | |
color, | |
space, | |
fontSize | |
)) | |
Button.displayName = 'Button' | |
Button.defaultProps = { | |
variant: 'primary', | |
m: 0, | |
px: 3, | |
py: 2, | |
fontSize: 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment