Skip to content

Instantly share code, notes, and snippets.

@mdarens
Created June 19, 2015 20:59
Show Gist options
  • Save mdarens/c145aef92f028bb0fc4f to your computer and use it in GitHub Desktop.
Save mdarens/c145aef92f028bb0fc4f to your computer and use it in GitHub Desktop.
React.js highlight on change component
var React = require("react");
var cx = require("classnames");
var _ = require("lodash");
require("./index.less");
var HighlightOnChange = React.createClass({
propTypes: {
subscribeTo: React.PropTypes.any.isRequired,
component: React.PropTypes.any,
highlightDuration: React.PropTypes.number
},
getDefaultProps() {
return {
component: 'span'
};
},
getInitialState() {
return {
justChanged: false
};
},
componentWillReceiveProps(next) {
if (next.subscribeTo !== this.props.subscribeTo) {
this.setState({
justChanged: true
});
clearTimeout(this.coolOff);
this.coolOff = setTimeout(function() {
this.setState({
justChanged: false
});
}.bind(this), this.props.highlightDuration || 200);
}
},
render() {
var classNames = cx({
'highlight-no-change': !this.state.justChanged,
'highlight-just-changed': this.state.justChanged
});
return React.createElement(this.props.component, _.extend({}, this.props, { className: classNames }), this.props.children);
}
});
module.exports = HighlightOnChange;
var React = require("react");
var cx = require("classnames");
var _ = require("lodash");
require("./index.less");
var HighlightOnChange = React.createClass({
propTypes: {
subscribeTo: React.PropTypes.any.isRequired,
component: React.PropTypes.any,
highlightDuration: React.PropTypes.number
},
getDefaultProps() {
return {
component: 'span'
};
},
getInitialState() {
return {
justChanged: false
};
},
componentWillReceiveProps(next) {
if (next.subscribeTo !== this.props.subscribeTo) {
this.setState({
justChanged: true
});
clearTimeout(this.coolOff);
this.coolOff = setTimeout(function() {
this.setState({
justChanged: false
});
}.bind(this), this.props.highlightDuration || 200);
}
},
render() {
var classNames = cx({
'highlight-no-change': !this.state.justChanged,
'highlight-just-changed': this.state.justChanged
});
return React.createElement(this.props.component, _.extend({}, this.props, { className: classNames }), this.props.children);
}
});
module.exports = HighlightOnChange;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment