-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 1.51 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var React = require('react');
var Dom = require('react-dom');
var elementResizeEvent = require('element-resize-event');
module.exports = React.createClass({
getInitialState: function() {
if (this.props.initialComponentWidth !== undefined && this.props.initialComponentWidth !== null) {
return {
componentWidth: this.props.initialComponentWidth,
componentHeight: this.props.initialComponentHeight
};
} else {
return {};
}
},
// Add our resize sensor.
componentDidMount: function() {
this.setState({
componentWidth: Dom.findDOMNode(this).getBoundingClientRect().width,
componentHeight: Dom.findDOMNode(this).getBoundingClientRect().height
});
elementResizeEvent(Dom.findDOMNode(this), this.onResize);
this.props.handleResize(Dom.findDOMNode(this).getBoundingClientRect());
},
// When the DOM updates, check that our resize sensor is still there.
componentDidUpdate: function() {
if (0 === Dom.findDOMNode(this).getElementsByClassName('resize-sensor').length) {
elementResizeEvent(Dom.findDOMNode(this), this.onResize);
}
},
onResize: function() {
this.setState({
componentWidth: Dom.findDOMNode(this).getBoundingClientRect().width,
componentHeight: Dom.findDOMNode(this).getBoundingClientRect().height
});
this.props.handleResize(Dom.findDOMNode(this).getBoundingClientRect());
},
render: function() {
return React.createElement(
"div",
null,
this.props.children
);
}
});