forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.js
More file actions
79 lines (68 loc) · 1.95 KB
/
Copy pathwrapper.js
File metadata and controls
79 lines (68 loc) · 1.95 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from "react";
const SubMenuWrapper = React.createClass({
displayName: "SubMenuWrapper",
propTypes: {
visible: React.PropTypes.bool
},
getInitialState() {
return {
position: {
top: true,
right: true
}
};
},
componentWillReceiveProps(nextProps) {
if (nextProps.visible) {
const wrapper = window.requestAnimationFrame || setTimeout;
wrapper(() => {
this.setState(this.getMenuPosition());
this.forceUpdate();
});
} else {
this.setState(this.getInitialState());
}
},
shouldComponentUpdate(nextProps) {
return this.props.visible !== nextProps.visible;
},
getMenuPosition() {
let { innerWidth, innerHeight } = window,
rect = this.menu.getBoundingClientRect(),
position = {};
if (rect.bottom > innerHeight) {
position.bottom = true;
} else {
position.top = true;
}
if (rect.right > innerWidth) {
position.left = true;
} else {
position.right = true;
}
return { position };
},
getPositionStyles() {
let style = {},
{ position } = this.state;
if (position.top) style.top = 0;
if (position.bottom) style.bottom = 0;
if (position.right) style.left = "100%";
if (position.left) style.right = "100%";
return style;
},
render() {
let { children, visible } = this.props;
const style = {
display: visible ? "block" : "none",
position: "absolute",
...this.getPositionStyles()
};
return (
<nav ref={(c) => (this.menu = c)} style={style} className="react-context-menu">
{children}
</nav>
);
}
});
export default SubMenuWrapper;