-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathContextMenu.react.js
201 lines (177 loc) · 5.11 KB
/
ContextMenu.react.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import PropTypes from 'lib/PropTypes';
import React, { useState, useEffect, useRef } from 'react';
import styles from 'components/ContextMenu/ContextMenu.scss';
const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, subItemCount = 0) => {
if (ref.current) {
const elBox = ref.current.getBoundingClientRect();
let y = 0;
const footerHeight = 50;
const lowerLimit = window.innerHeight - footerHeight;
const upperLimit = 0;
if (elBox.bottom > lowerLimit) {
y = lowerLimit - elBox.bottom;
} else if (elBox.top < upperLimit) {
y = upperLimit - elBox.top;
}
const projectedTop = elBox.top + y + offset;
const projectedBottom = projectedTop + elBox.height;
const shouldApplyOffset = subItemCount > mainItemCount;
if (shouldApplyOffset && projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
y += offset;
}
const prevEl = ref.current.previousSibling;
if (prevEl) {
const prevElBox = prevEl.getBoundingClientRect();
const prevElStyle = window.getComputedStyle(prevEl);
const prevElTop = parseInt(prevElStyle.top, 10);
if (!shouldApplyOffset) {
y = prevElTop + offset;
}
const showOnRight = prevElBox.x + prevElBox.width + elBox.width < window.innerWidth;
return {
x: showOnRight ? prevElBox.width : -elBox.width,
y
};
}
return { x: 0, y };
}
};
const MenuSection = ({ level, items, path, setPath, hide, parentItemCount = 0 }) => {
const sectionRef = useRef(null);
const [position, setPosition] = useState();
useEffect(() => {
const newPosition = getPositionToFitVisibleScreen(
sectionRef,
path[level] * 30,
parentItemCount,
items.length
);
newPosition && setPosition(newPosition);
}, [sectionRef]);
const style = position
? {
left: position.x,
top: position.y,
maxHeight: '80vh',
overflowY: 'scroll',
opacity: 1,
}
: {};
return (
<ul ref={sectionRef} className={styles.category} style={style}>
{items.map((item, index) => {
if (item.items) {
return (
<li
key={`menu-section-${level}-${index}`}
className={styles.item}
onMouseEnter={() => {
const newPath = path.slice(0, level + 1);
newPath.push(index);
setPath(newPath);
}}
>
{item.text}
</li>
);
}
return (
<li
key={`menu-section-${level}-${index}`}
className={styles.option}
style={item.disabled ? { opacity: 0.5, cursor: 'not-allowed' } : {}}
onClick={() => {
if (item.disabled === true) {
return;
}
item.callback && item.callback();
hide();
}}
onMouseEnter={() => {
const newPath = path.slice(0, level + 1);
setPath(newPath);
}}
>
{item.text}
{item.subtext && <span> - {item.subtext}</span>}
</li>
);
})}
</ul>
);
};
const ContextMenu = ({ x, y, items }) => {
const [path, setPath] = useState([0]);
const [visible, setVisible] = useState(true);
const menuRef = useRef(null);
useEffect(() => {
setVisible(true);
}, [items]);
const hide = () => {
setVisible(false);
setPath([0]);
};
function handleClickOutside(event) {
if (menuRef.current && !menuRef.current.contains(event.target)) {
hide();
}
}
useEffect(() => {
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
});
if (!visible) {
return null;
}
const getItemsFromLevel = level => {
let result = items;
for (let index = 1; index <= level; index++) {
result = result[path[index]].items;
}
return result;
};
return (
<div
className={styles.menu}
ref={menuRef}
style={{
left: x,
top: y,
}}
>
{path.map((position, level) => {
const itemsForLevel = getItemsFromLevel(level);
const parentItemCount =
level === 0 ? items.length : getItemsFromLevel(level - 1).length;
return (
<MenuSection
key={`section-${position}-${level}`}
path={path}
setPath={setPath}
level={level}
items={itemsForLevel}
hide={hide}
parentItemCount={parentItemCount}
/>
);
})}
</div>
);
};
ContextMenu.propTypes = {
x: PropTypes.number.isRequired.describe('X context menu position.'),
y: PropTypes.number.isRequired.describe('Y context menu position.'),
items: PropTypes.array.isRequired.describe(
'Array with tree representation of context menu items.'
),
};
export default ContextMenu;