Skip to content

Instantly share code, notes, and snippets.

@bobpace
Created March 19, 2015 04:47
Show Gist options
  • Save bobpace/bd24c14d5fc774231529 to your computer and use it in GitHub Desktop.
Save bobpace/bd24c14d5fc774231529 to your computer and use it in GitHub Desktop.
React Resize Width Mixin
var Rx = require('rx');
var _ = require('lodash');
var defaultOptions = {
minWidth: 1050,
containerRefName: 'container'
};
module.exports = function(options) {
return {
getDefaultProps() {
return _.extend({}, defaultOptions, options);
},
setWidth() {
var container = this.refs[this.props.containerRefName].getDOMNode();
var width = Math.max(container.clientWidth, this.props.minWidth);
this.setState({ width });
},
componentDidMount() {
this.setWidth();
this.resizeSubscription = Rx.Observable.fromEvent(window, 'resize')
.debounce(250)
.subscribe(this.setWidth);
},
componentWillUnmount() {
this.resizeSubscription.dispose();
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment