Skip to content

Commit 53f4b8c

Browse files
committed
Update ContextMenu.react.js
1 parent 950191c commit 53f4b8c

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/components/ContextMenu/ContextMenu.react.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,36 @@ 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 => {
12+
const getPositionToFitVisibleScreen = (ref, offset = 0) => {
1313
if (ref.current) {
1414
const elBox = ref.current.getBoundingClientRect();
15-
const y = elBox.y + elBox.height < window.innerHeight ? 0 : 0 - elBox.y + 100;
15+
let y = 0;
16+
17+
const footerHeight = 50;
18+
const lowerLimit = window.innerHeight - footerHeight;
19+
const upperLimit = 0;
20+
21+
if (elBox.bottom > lowerLimit) {
22+
y = lowerLimit - elBox.bottom;
23+
} else if (elBox.top < upperLimit) {
24+
y = upperLimit - elBox.top;
25+
}
26+
27+
// Apply offset only if it doesn't push the element offscreen again
28+
const projectedTop = elBox.top + y + offset;
29+
const projectedBottom = projectedTop + elBox.height;
30+
31+
if (projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
32+
y += offset;
33+
}
1634

17-
// If there's a previous element show current next to it.
18-
// Try on right side first, then on left if there's no place.
1935
const prevEl = ref.current.previousSibling;
2036
if (prevEl) {
2137
const prevElBox = prevEl.getBoundingClientRect();
2238
const showOnRight = prevElBox.x + prevElBox.width + elBox.width < window.innerWidth;
2339
return {
2440
x: showOnRight ? prevElBox.width : -elBox.width,
25-
y,
41+
y
2642
};
2743
}
2844

@@ -35,14 +51,14 @@ const MenuSection = ({ level, items, path, setPath, hide }) => {
3551
const [position, setPosition] = useState();
3652

3753
useEffect(() => {
38-
const newPosition = getPositionToFitVisibleScreen(sectionRef);
54+
const newPosition = getPositionToFitVisibleScreen(sectionRef, path[level] * 30);
3955
newPosition && setPosition(newPosition);
4056
}, [sectionRef]);
4157

4258
const style = position
4359
? {
4460
left: position.x,
45-
top: position.y + path[level] * 30,
61+
top: position.y,
4662
maxHeight: '80vh',
4763
overflowY: 'scroll',
4864
opacity: 1,

0 commit comments

Comments
 (0)