Skip to content

Instantly share code, notes, and snippets.

@n3tr
Created August 21, 2016 08:11
Show Gist options
  • Save n3tr/b100efb0a3fef65d33a19019bc1381cd to your computer and use it in GitHub Desktop.
Save n3tr/b100efb0a3fef65d33a19019bc1381cd to your computer and use it in GitHub Desktop.
import React from 'react'
const Button = (props) => {
return <button className="button" {...props} />
}
function isStateless(component) {
return !(component.prototype && component.prototype.render);
}
function applyStyleAndClassName(component) {
let ComposedComponent = component
// Check if stateless function component
if (isStateless(component)) {
ComposedComponent = class extends React.Component {
render() {
return component()
}
}
}
const StylableComponent = class extends ComposedComponent {
render() {
const element = super.render()
const clonedElement = React.cloneElement(element, {
...this.props,
style: {
...(element.props.style || {}),
...(this.props.style || {})
},
className: [element.props.className || "", this.props.className || ""].join(" "),
})
return clonedElement
}
}
StylableComponent.displayName = component.displayName || component.name;
return StylableComponent
}
export default applyStyleAndClassName(Button)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment