Skip to content

Commit 18d5c97

Browse files
committed
Update ContextMenu.react.js
1 parent 10b2a5c commit 18d5c97

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/components/ContextMenu/ContextMenu.react.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import PropTypes from 'lib/PropTypes';
99
import React, { useState, useEffect, useRef } from 'react';
1010
import styles from 'components/ContextMenu/ContextMenu.scss';
1111

12-
const getPositionToFitVisibleScreen = (ref, offset = 0) => {
12+
const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, subItemCount = 0) => {
1313
if (ref.current) {
1414
const elBox = ref.current.getBoundingClientRect();
1515
let y = 0;
@@ -24,17 +24,24 @@ const getPositionToFitVisibleScreen = (ref, offset = 0) => {
2424
y = upperLimit - elBox.top;
2525
}
2626

27-
// Apply offset only if it doesn't push the element offscreen again
2827
const projectedTop = elBox.top + y + offset;
2928
const projectedBottom = projectedTop + elBox.height;
3029

31-
if (projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
30+
const shouldApplyOffset = subItemCount > mainItemCount;
31+
if (shouldApplyOffset && projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
3232
y += offset;
3333
}
3434

3535
const prevEl = ref.current.previousSibling;
3636
if (prevEl) {
3737
const prevElBox = prevEl.getBoundingClientRect();
38+
const prevElStyle = window.getComputedStyle(prevEl);
39+
const prevElTop = parseInt(prevElStyle.top, 10);
40+
41+
if (!shouldApplyOffset) {
42+
y = prevElTop + offset;
43+
}
44+
3845
const showOnRight = prevElBox.x + prevElBox.width + elBox.width < window.innerWidth;
3946
return {
4047
x: showOnRight ? prevElBox.width : -elBox.width,
@@ -46,23 +53,28 @@ const getPositionToFitVisibleScreen = (ref, offset = 0) => {
4653
}
4754
};
4855

49-
const MenuSection = ({ level, items, path, setPath, hide }) => {
56+
const MenuSection = ({ level, items, path, setPath, hide, parentItemCount = 0 }) => {
5057
const sectionRef = useRef(null);
5158
const [position, setPosition] = useState();
5259

5360
useEffect(() => {
54-
const newPosition = getPositionToFitVisibleScreen(sectionRef, path[level] * 30);
61+
const newPosition = getPositionToFitVisibleScreen(
62+
sectionRef,
63+
path[level] * 30,
64+
parentItemCount,
65+
items.length
66+
);
5567
newPosition && setPosition(newPosition);
5668
}, [sectionRef]);
5769

5870
const style = position
5971
? {
60-
left: position.x,
61-
top: position.y,
62-
maxHeight: '80vh',
63-
overflowY: 'scroll',
64-
opacity: 1,
65-
}
72+
left: position.x,
73+
top: position.y,
74+
maxHeight: '80vh',
75+
overflowY: 'scroll',
76+
opacity: 1,
77+
}
6678
: {};
6779

6880
return (
@@ -108,6 +120,8 @@ const MenuSection = ({ level, items, path, setPath, hide }) => {
108120
const ContextMenu = ({ x, y, items }) => {
109121
const [path, setPath] = useState([0]);
110122
const [visible, setVisible] = useState(true);
123+
const menuRef = useRef(null);
124+
111125
useEffect(() => {
112126
setVisible(true);
113127
}, [items]);
@@ -117,10 +131,6 @@ const ContextMenu = ({ x, y, items }) => {
117131
setPath([0]);
118132
};
119133

120-
//#region Closing menu after clicking outside it
121-
122-
const menuRef = useRef(null);
123-
124134
function handleClickOutside(event) {
125135
if (menuRef.current && !menuRef.current.contains(event.target)) {
126136
hide();
@@ -134,8 +144,6 @@ const ContextMenu = ({ x, y, items }) => {
134144
};
135145
});
136146

137-
//#endregion
138-
139147
if (!visible) {
140148
return null;
141149
}
@@ -158,14 +166,19 @@ const ContextMenu = ({ x, y, items }) => {
158166
}}
159167
>
160168
{path.map((position, level) => {
169+
const itemsForLevel = getItemsFromLevel(level);
170+
const parentItemCount =
171+
level === 0 ? items.length : getItemsFromLevel(level - 1).length;
172+
161173
return (
162174
<MenuSection
163175
key={`section-${position}-${level}`}
164176
path={path}
165177
setPath={setPath}
166178
level={level}
167-
items={getItemsFromLevel(level)}
179+
items={itemsForLevel}
168180
hide={hide}
181+
parentItemCount={parentItemCount}
169182
/>
170183
);
171184
})}

0 commit comments

Comments
 (0)