Skip to content

Instantly share code, notes, and snippets.

@tclain
Last active May 11, 2018 22:05
Show Gist options
  • Save tclain/8b701afa7be4b535ad9fc3e68332dac5 to your computer and use it in GitHub Desktop.
Save tclain/8b701afa7be4b535ad9fc3e68332dac5 to your computer and use it in GitHub Desktop.
styled-components utils
import styled, { css } from 'styled-components';
import at from 'lodash.at';
export const Blue = css`
border-bottom-color: blue;
`
export const Red = css`
border-top-color: red;
`
export const Yellow = css`
border-top-color: yellow;
`
export const Gray = css`
background-color: #cecece;
`
export const match = (contained, container) => {
return Object.keys(contained).every(key => {
return contained[key] === container[key]
})
}
/**
* if state match what's in props
*/
export const $if = (state, styleMatch, styleNoMatch=null) => props => (
match(state, props) ? styleMatch: styleNoMatch
)
/**
* extract value from upper theme
*/
export const theme = (themePath, defaults='') => ({theme}) => {
const value = at(theme, themePath)[0]
if (!value) return defaults;
return value
}
export const Button = styled.button`
border-radius: 3px;
padding: 0.25em 1em;
margin: 0 1em;
background: transparent;
color: ${theme('red', 'blue')};
border: 2px solid palevioletred;
${$if({
red: true
}, Red)};
${$if({
blue: true
}, Blue)};
${$if({
yellow: true
}, Yellow, Gray)};
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment