@@ -9,20 +9,36 @@ import PropTypes from 'lib/PropTypes';
9
9
import React , { useState , useEffect , useRef } from 'react' ;
10
10
import styles from 'components/ContextMenu/ContextMenu.scss' ;
11
11
12
- const getPositionToFitVisibleScreen = ref => {
12
+ const getPositionToFitVisibleScreen = ( ref , offset = 0 ) => {
13
13
if ( ref . current ) {
14
14
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
+ }
16
34
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.
19
35
const prevEl = ref . current . previousSibling ;
20
36
if ( prevEl ) {
21
37
const prevElBox = prevEl . getBoundingClientRect ( ) ;
22
38
const showOnRight = prevElBox . x + prevElBox . width + elBox . width < window . innerWidth ;
23
39
return {
24
40
x : showOnRight ? prevElBox . width : - elBox . width ,
25
- y,
41
+ y
26
42
} ;
27
43
}
28
44
@@ -35,14 +51,14 @@ const MenuSection = ({ level, items, path, setPath, hide }) => {
35
51
const [ position , setPosition ] = useState ( ) ;
36
52
37
53
useEffect ( ( ) => {
38
- const newPosition = getPositionToFitVisibleScreen ( sectionRef ) ;
54
+ const newPosition = getPositionToFitVisibleScreen ( sectionRef , path [ level ] * 30 ) ;
39
55
newPosition && setPosition ( newPosition ) ;
40
56
} , [ sectionRef ] ) ;
41
57
42
58
const style = position
43
59
? {
44
60
left : position . x ,
45
- top : position . y + path [ level ] * 30 ,
61
+ top : position . y ,
46
62
maxHeight : '80vh' ,
47
63
overflowY : 'scroll' ,
48
64
opacity : 1 ,
0 commit comments