-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopupMenu.jsx
45 lines (44 loc) · 1.46 KB
/
PopupMenu.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
39
40
41
42
43
44
45
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
export default class PopupMenu extends React.Component {
static propTypes = {
children: PropTypes.array,
className: PropTypes.string,
onClose: PropTypes.func,
width: PropTypes.number,
x: PropTypes.number,
y: PropTypes.number
};
constructor(props) {
super(props);
this.container = document.createElement("div");
this.container.id = 'popup-container';
this.container.style.position = 'fixed';
this.container.style.left = 0;
this.container.style.right = 0;
this.container.style.top = 0;
this.container.style.bottom = 0;
this.container.style.zIndex = 100000;
this.container.addEventListener('click', this.props.onClose);
document.body.appendChild(this.container);
}
componentWillUnmount() {
document.body.removeChild(this.container);
}
render() {
const style = {
position: 'absolute',
left: this.props.x + 'px',
top: this.props.y + 'px',
minWidth: this.props.width + 'px',
maxHeight: (window.innerHeight - this.props.y) + 'px',
overflowY: 'auto'
};
return ReactDOM.createPortal((
<div className={this.props.className} style={style}>
{this.props.children}
</div>
), this.container);
}
}