Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Menu/MenuContainer.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.MenuContainer = void 0;
4 const jsx_runtime_1 = require("react/jsx-runtime");
5 const react_1 = require("react");
6 const helpers_1 = require("../../helpers");
7 /**
8 * Container that links a menu and menu toggle together, to handle basic keyboard input and control the opening and closing of a menu.
9 */
10 const MenuContainer = ({ menu, menuRef, isOpen, toggle, toggleRef, onOpenChange, onToggleKeydown, zIndex = 9999, popperProps, onOpenChangeKeys = ['Escape', 'Tab'], shouldFocusFirstItemOnOpen = false, shouldPreventScrollOnItemFocus = true, focusTimeoutDelay = 0 }) => {
11 const prevIsOpen = (0, react_1.useRef)(isOpen);
12 (0, react_1.useEffect)(() => {
13 // menu was opened, focus on first menu item
14 if (prevIsOpen.current === false && isOpen === true && shouldFocusFirstItemOnOpen) {
15 setTimeout(() => {
16 var _a;
17 const firstElement = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('li button:not(:disabled),li input:not(:disabled),li a:not([aria-disabled="true"])');
18 firstElement && firstElement.focus({ preventScroll: shouldPreventScrollOnItemFocus });
19 }, focusTimeoutDelay);
20 }
21 prevIsOpen.current = isOpen;
22 // eslint-disable-next-line react-hooks/exhaustive-deps
23 }, [isOpen]);
24 (0, react_1.useEffect)(() => {
25 const handleMenuKeys = (event) => {
26 var _a, _b, _c, _d;
27 // Close the menu on tab or escape if onOpenChange is provided
28 if ((isOpen && onOpenChange && ((_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) ||
29 ((_b = toggleRef.current) === null || _b === void 0 ? void 0 : _b.contains(event.target))) {
30 if (onOpenChangeKeys.includes(event.key)) {
31 onOpenChange(false);
32 (_c = toggleRef.current) === null || _c === void 0 ? void 0 : _c.focus();
33 }
34 }
35 if ((_d = toggleRef.current) === null || _d === void 0 ? void 0 : _d.contains(event.target)) {
36 if (onToggleKeydown) {
37 onToggleKeydown(event);
38 }
39 else if (isOpen) {
40 (0, helpers_1.onToggleArrowKeydownDefault)(event, menuRef);
41 }
42 }
43 };
44 const handleClick = (event) => {
45 var _a, _b;
46 // If the event is not on the toggle and onOpenChange callback is provided, close the menu
47 if (isOpen && onOpenChange && !((_a = toggleRef === null || toggleRef === void 0 ? void 0 : toggleRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) {
48 if (isOpen && !((_b = menuRef.current) === null || _b === void 0 ? void 0 : _b.contains(event.target))) {
49 onOpenChange(false);
50 }
51 }
52 };
53 window.addEventListener('keydown', handleMenuKeys);
54 window.addEventListener('click', handleClick);
55 return () => {
56 window.removeEventListener('keydown', handleMenuKeys);
57 window.removeEventListener('click', handleClick);
58 };
59 }, [
60 focusTimeoutDelay,
61 isOpen,
62 menuRef,
63 onOpenChange,
64 onOpenChangeKeys,
65 onToggleKeydown,
66 shouldPreventScrollOnItemFocus,
67 toggleRef
68 ]);
69 return ((0, jsx_runtime_1.jsx)(helpers_1.Popper, Object.assign({ trigger: toggle, triggerRef: toggleRef, popper: menu, popperRef: menuRef, isVisible: isOpen, zIndex: zIndex }, popperProps)));
70 };
71 exports.MenuContainer = MenuContainer;
72 exports.MenuContainer.displayName = 'MenuContainer';
73 //# sourceMappingURL=MenuContainer.js.map