Skip to content

Instantly share code, notes, and snippets.

@Jimmydalecleveland
Created August 22, 2019 00:16
Show Gist options
  • Save Jimmydalecleveland/c00703350c7f635e1f00cf75b4df7de6 to your computer and use it in GitHub Desktop.
Save Jimmydalecleveland/c00703350c7f635e1f00cf75b4df7de6 to your computer and use it in GitHub Desktop.
function styled(css, ...variables) {
const theme = {
spacing: {
min: '2px',
max: '24px',
},
colors: {
primary: 'coral',
secondary: 'peachpuff',
},
}
const props = {
theme,
primary: true,
bigSpacing: true,
}
const computedCss = css
.map(
(chunk, index) =>
`${chunk}${variables[index] ? variables[index](props) : ''}`
)
.join('')
return computedCss
}
const Button = styled`
background: ${({ primary, theme }) =>
primary ? theme.colors.primary : theme.colors.secondary};
margin-bottom: ${({ bigSpacing, theme }) =>
bigSpacing ? theme.spacing.max : theme.spacing.min};
span {
padding: 0.25em 1em;
color: ${({ primary, theme }) =>
primary ? theme.colors.secondary : '#fff'};
}
`
/* Output:
background: coral; 
margin-bottom: 24px; 
span { 
padding: 0.25em 1em; 
color: peachpuff; 
} 
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment