Created
January 28, 2018 23:17
-
-
Save markmur/80f7459bcd36d534fab1f43ac5408e9c to your computer and use it in GitHub Desktop.
Omit props from styled component
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
import pickBy from 'lodash.pickby'; | |
import { createElement } from 'react'; | |
export const createComponent = ({ | |
tag: defaultTag = 'div', | |
prop = 'tag', | |
omit = [] | |
} = {}) => { | |
return ({ children, ...otherProps }) => { | |
const tag = otherProps[prop] || defaultTag; | |
const omitPropsKeys = [prop, ...omit]; | |
const props = pickBy(otherProps, (value, key) => { | |
return omitPropsKeys.indexOf(key) === -1; | |
}); | |
return createElement(tag, props, children); | |
}; | |
}; | |
const ButtonComponent = createComponent({ | |
tag: 'button', | |
omit: [ | |
'block', | |
'ghost', | |
'primary', | |
'secondary', | |
'tertiary', | |
'shadow', | |
'animate', | |
'subtle', | |
'compact', | |
'rounded' | |
] | |
}); | |
const StyledButton = styled(ButtonComponent)` | |
${buttonStyles}; | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment