Skip to content

Instantly share code, notes, and snippets.

@lzimm
Created December 31, 2015 05:25
Show Gist options
  • Save lzimm/a7d382d492d9ddb06aa7 to your computer and use it in GitHub Desktop.
Save lzimm/a7d382d492d9ddb06aa7 to your computer and use it in GitHub Desktop.
lol @ psuedo-selector inline styles
// lol
var StyleNode = React.createClass({
getInitialState: function() {
return {
hover: false,
active: false,
focus: false
};
},
styles: function() {
var child = React.Children.map(this.props.children, function(child) {
return child;
}).first();
return _.extend.apply(null, [{}].concat((child.props.styles || []).concat([
child.props.style,
this.props.style,
this.state.hover && this.props.hover,
this.state.active && this.props.active,
this.state.focus && this.props.focus
])));
},
render: function() {
return React.Children.map(this.props.children, function(child) {
return React.cloneElement(child, {
onMouseEnter: function(ev) { this.setState({ hover: true }); }.bind(this),
onMouseLeave: function(ev) { this.setState({ hover: false, active: false }); }.bind(this),
onMouseDown: function(ev) { this.setState({ active: true }); }.bind(this),
onMouseUp: function(ev) { this.setState({ active: false }); }.bind(this),
onFocus: function(ev) { this.setState({ focus: true }); }.bind(this),
onBlur: function(ev) { this.setState({ focus: false }); }.bind(this),
style: _.extend.apply(null, [{}].concat(this.styles()))
});
}.bind(this)).first();
}
});
// hello world
<StyleNode style={{ ... }} hover={{ ... }}><a>Hello World</a></StyleNode>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment