Skip to content

Instantly share code, notes, and snippets.

@brigand

brigand/.js Secret

Created April 25, 2017 05:52
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 brigand/94c09476676a70e78e33ca84f0cc8ad3 to your computer and use it in GitHub Desktop.
Save brigand/94c09476676a70e78e33ca84f0cc8ad3 to your computer and use it in GitHub Desktop.
const h = (selector, props, ...children) => {
if (typeof selector !== 'string') {
return React.createElement(selector, props, ...children);
}
const classNames = [];
const tagName = selector.replace(/\.([^.]+)/g, (match, className) => {
classNames.push(className);
return '';
});
let newProps = props;
if (props && props.className) {
classNames.push(props.className);
}
if (classNames.length) {
if (!newProps) newProps = {};
newProps = Object.assign({}, props, {className: classNames.join(' ')});
}
return React.createElement(tagName, newProps, ...children);
};
module.exports = h;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment