Created
March 19, 2015 04:47
-
-
Save bobpace/bd24c14d5fc774231529 to your computer and use it in GitHub Desktop.
React Resize Width Mixin
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
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