-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathContextMenu.tsx
More file actions
45 lines (39 loc) · 1.36 KB
/
ContextMenu.tsx
File metadata and controls
45 lines (39 loc) · 1.36 KB
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
import { useState, useCallback, useEffect } from 'react';
import { Menu, MenuContent } from '@patternfly/react-core';
import { css } from '@patternfly/react-styles';
import topologyStyles from '../../css/topology-components';
// FIXME fully qualified due to the effect of long build times on storybook
import Popper from '../popper/Popper';
type ContextMenuProps = Pick<
React.ComponentProps<typeof Popper>,
'children' | 'container' | 'className' | 'open' | 'reference' | 'onRequestClose'
>;
const ContextMenu: React.FunctionComponent<ContextMenuProps> = ({
children,
open = true,
onRequestClose,
...other
}) => {
const [isOpen, setOpen] = useState(!!open);
useEffect(() => {
setOpen(open);
}, [open]);
const handleOnRequestClose = useCallback(() => {
onRequestClose ? onRequestClose() : setOpen(false);
}, [onRequestClose]);
useEffect(() => {
if (isOpen) {
setOpen(false);
requestAnimationFrame(() => setOpen(true));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [children]);
return (
<Popper {...other} closeOnEsc closeOnOutsideClick open={isOpen} onRequestClose={handleOnRequestClose}>
<Menu onSelect={handleOnRequestClose} className={css(topologyStyles.topologyContextMenuCDropdownMenu)}>
<MenuContent>{children}</MenuContent>
</Menu>
</Popper>
);
};
export default ContextMenu;