1
1
import * as React from 'react' ;
2
- import { Dropdown , DropdownItem } from '@patternfly/react-core' ;
2
+ import { Menu , DropdownItem } from '@patternfly/react-core' ;
3
3
import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon' ;
4
4
import { css } from '@patternfly/react-styles' ;
5
5
import topologyStyles from '../../css/topology-components' ;
@@ -11,6 +11,11 @@ interface ContextSubMenuItemProps {
11
11
children : React . ReactNode [ ] ;
12
12
}
13
13
14
+ /** Check if an event target is also a node. Needed to stop runtime errors where the target does not implement Node. */
15
+ const isNode = ( node : any ) : boolean => {
16
+ return node && typeof node === 'object' && node . nodeType && node . nodeName ;
17
+ } ;
18
+
14
19
const ContextSubMenuItem : React . FunctionComponent < ContextSubMenuItemProps > = ( { label, children, ...other } ) => {
15
20
const nodeRef = React . useRef < HTMLButtonElement > ( null ) ;
16
21
const subMenuRef = React . useRef < HTMLDivElement > ( null ) ;
@@ -29,7 +34,10 @@ const ContextSubMenuItem: React.FunctionComponent<ContextSubMenuItemProps> = ({
29
34
onMouseEnter = { ( ) => setOpen ( true ) }
30
35
onMouseLeave = { ( e ) => {
31
36
// if the mouse leaves this item, close the sub menu only if the mouse did not enter the sub menu itself
32
- if ( ! subMenuRef . current || ! subMenuRef . current . contains ( e . relatedTarget as Node ) ) {
37
+ if (
38
+ ! subMenuRef . current ||
39
+ ( isNode ( e . relatedTarget ) && ! subMenuRef . current . contains ( e . relatedTarget as Node ) )
40
+ ) {
33
41
setOpen ( false ) ;
34
42
}
35
43
} }
@@ -51,22 +59,18 @@ const ContextSubMenuItem: React.FunctionComponent<ContextSubMenuItemProps> = ({
51
59
closeOnOutsideClick
52
60
onRequestClose = { ( e ) => {
53
61
// only close the sub menu if clicking anywhere outside the menu item that owns the sub menu
54
- if ( ! e || ! nodeRef . current || ! nodeRef . current . contains ( e . target as Node ) ) {
62
+ if ( ! e || ! nodeRef . current || ( isNode ( e . target ) && ! nodeRef . current . contains ( e . target as Node ) ) ) {
55
63
setOpen ( false ) ;
56
64
}
57
65
} }
58
66
reference = { referenceCb }
59
- // use the parent node to capture the li
60
- container = { nodeRef . current ? nodeRef . current . parentElement : nodeRef . current }
67
+ container = { nodeRef . current ?. closest ( '.pf-v6-c-menu__content' ) }
61
68
returnFocus
62
69
>
63
- < div
64
- ref = { subMenuRef }
65
- role = "presentation"
66
- className = "pf-v6-c-dropdown pf-m-expanded"
70
+ < Menu
67
71
onMouseLeave = { ( e ) => {
68
72
// only close the sub menu if the mouse does not enter the item
69
- if ( ! nodeRef . current || ! nodeRef . current . contains ( e . relatedTarget as Node ) ) {
73
+ if ( ! nodeRef . current || ( isNode ( e . relatedTarget ) && ! nodeRef . current . contains ( e . relatedTarget as Node ) ) ) {
70
74
setOpen ( false ) ;
71
75
}
72
76
} }
@@ -77,11 +81,12 @@ const ContextSubMenuItem: React.FunctionComponent<ContextSubMenuItemProps> = ({
77
81
e . stopPropagation ( ) ;
78
82
}
79
83
} }
84
+ ref = { subMenuRef }
85
+ role = "presentation"
86
+ isNavFlyout
80
87
>
81
- < Dropdown toggle = { ( ) => < > </ > } className = { css ( topologyStyles . topologyContextMenuCDropdownMenu ) } >
82
- { children }
83
- </ Dropdown >
84
- </ div >
88
+ { children }
89
+ </ Menu >
85
90
</ Popper >
86
91
</ >
87
92
) ;
0 commit comments