Skip to content

Instantly share code, notes, and snippets.

@markmur
Created January 28, 2018 23:17
Show Gist options
  • Save markmur/80f7459bcd36d534fab1f43ac5408e9c to your computer and use it in GitHub Desktop.
Save markmur/80f7459bcd36d534fab1f43ac5408e9c to your computer and use it in GitHub Desktop.
Omit props from styled component
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