Created
December 31, 2018 04:53
-
-
Save Zodiase/7a994bca5476729d96f0678867f15a28 to your computer and use it in GitHub Desktop.
Allow some props to be only used in styling but not passed to DOM (or underlying components).
This file contains 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 React from 'react'; | |
const wrapReactComponentToIgnoreSomeProps = (component, propsToOmit = {}) => { | |
const newComponent = (props) => { | |
const newProps = Object.entries(props).reduce( | |
(acc, [propName, propValue]) => { | |
if (propName in propsToOmit && propsToOmit[propName] === true) { | |
return acc; | |
} | |
return { | |
...acc, | |
[propName]: propValue | |
}; | |
}, | |
{} | |
); | |
return React.createElement(component, newProps); | |
}; | |
return newComponent; | |
}; | |
/** | |
* Usage: sansProps(styled, { propNotInDom: true })('div')``; | |
* @param {Function} styled | |
* @param {Object} propsToOmit | |
* @return {Function} | |
*/ | |
export const sansProps = (styled, propsToOmit = {}) => { | |
const _styled = (component, ...args) => { | |
const componentSansProps = wrapReactComponentToIgnoreSomeProps( | |
component, | |
propsToOmit | |
); | |
return styled(componentSansProps, ...args); | |
}; | |
return _styled; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment