main
js 58 lines 1.14 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import * as React from 'react';
11 import {useImperativeHandle} from 'react';
12
13 import ContextMenu from './ContextMenu';
14 import useContextMenu from './useContextMenu';
15
16 import type {ContextMenuItem, ContextMenuRef} from './types';
17
18 type Props = {
19 anchorElementRef: {
20 current: React.ElementRef<any> | null,
21 },
22 items: ContextMenuItem[],
23 closedMenuStub?: React.Node | null,
24 ref?: ContextMenuRef,
25 };
26
27 export default function ContextMenuContainer({
28 anchorElementRef,
29 items,
30 closedMenuStub = null,
31 ref,
32 }: Props): React.Node {
33 const {shouldShow, position, hide} = useContextMenu(anchorElementRef);
34
35 useImperativeHandle(
36 ref,
37 () => ({
38 isShown() {
39 return shouldShow;
40 },
41 hide,
42 }),
43 [shouldShow, hide],
44 );
45
46 if (!shouldShow) {
47 return closedMenuStub;
48 }
49
50 return (
51 <ContextMenu
52 anchorElementRef={anchorElementRef}
53 position={position}
54 hide={hide}
55 items={items}
56 />
57 );
58 }