Created
May 15, 2017 20:50
-
-
Save iMoses/f2800181794a483ffea94a2f9950bf54 to your computer and use it in GitHub Desktop.
Automatically pass React elements 'className' into the 'classnames' library after binding with css-modules styles
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 classNames from 'classnames/bind'; | |
import React from 'react'; | |
export default styles => Component => isStateless(Component) | |
? props => transformElement(Component(props), classNames.bind(styles)) | |
: class extends Component { render() { return transformElement(super.render(), classNames.bind(styles)); } }; | |
function transformElement(el, cx) { | |
let className = el && el.props.className; | |
if (className) { | |
className = cx(splitStrings(className)); | |
} | |
return el && React.cloneElement(el, {...el.props, className}, recursiveTransform(el.props.children, cx)); | |
} | |
function splitStrings(className) { | |
if (Array.isArray(className)) { | |
return className.map(splitStrings); | |
} | |
if (typeof className === 'string') { | |
return className.split(/\s+/g); | |
} | |
return className; | |
} | |
function recursiveTransform(el, cx) { | |
if (React.isValidElement(el)) { | |
return transformElement(el, cx); | |
} | |
if (Array.isArray(el)) { | |
return React.Children.map(el, child => recursiveTransform(child, cx)); | |
} | |
return el; | |
} | |
function isStateless(Component) { | |
return !('prototype' in Component && typeof Component.prototype.render === 'function'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment