-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
32 lines (31 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var ReactDOM = require('react-dom');
var elementResizeEvent = require('element-resize-event');
module.exports = {
getInitialState: function() {
if (this.props.initialComponentWidth !== undefined && this.props.initialComponentWidth !== null) {
return {
componentWidth: this.props.initialComponentWidth
};
} else {
return {};
}
},
// Add our resize sensor.
componentDidMount: function() {
this.setState({
componentWidth: ReactDOM.findDOMNode(this).getBoundingClientRect().width
});
elementResizeEvent(ReactDOM.findDOMNode(this), this.onResize);
},
// When the DOM updates, check that our resize sensor is still there.
componentDidUpdate: function() {
if (0 === ReactDOM.findDOMNode(this).getElementsByClassName('resize-sensor').length) {
elementResizeEvent(ReactDOM.findDOMNode(this), this.onResize);
}
},
onResize: function() {
this.setState({
componentWidth: ReactDOM.findDOMNode(this).getBoundingClientRect().width
});
}
};