forked from react-grid-layout/react-grid-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidthProvider.jsx
38 lines (34 loc) · 1.03 KB
/
WidthProvider.jsx
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
// @flow
import React from "react";
import ReactDOM from 'react-dom';
/*
* A simple HOC that provides facility for listening to container resizes.
*/
export default ComposedComponent => class extends React.Component {
constructor() {
super();
this.state = {
width: 1280,
offsetY: 0,
offsetX: 0
};
}
componentDidMount() {
const node = ReactDOM.findDOMNode(this);
window.addEventListener('resize', () => this.onWindowResize(node));
// This is intentional. Once to properly set the breakpoint and resize the elements,
// and again to compensate for any scrollbar that appeared because of the first step.
this.onWindowResize(node);
this.onWindowResize(node);
}
componentWillUnmount() {
window.removeEventListener('resize', this.onWindowResize);
}
onWindowResize(node) {
const {top, left, width} = node.getBoundingClientRect();
this.setState({offsetY: top, offsetX: left, width: width});
}
render() {
return <ComposedComponent {...this.props} {...this.state} />;
}
};