Skip to content

Instantly share code, notes, and snippets.

@Zodiase
Created December 31, 2018 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zodiase/7a994bca5476729d96f0678867f15a28 to your computer and use it in GitHub Desktop.
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).
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