Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Nav/NavItem.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.NavItem = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const nav_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Nav/nav"));
8 const menu_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Menu/menu"));
9 const divider_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Divider/divider"));
10 const react_styles_1 = require("@patternfly/react-styles");
11 const Nav_1 = require("./Nav");
12 const PageSidebar_1 = require("../Page/PageSidebar");
13 const helpers_1 = require("../../helpers");
14 const Popper_1 = require("../../helpers/Popper/Popper");
15 const rh_microns_caret_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-right-icon'));
16 const NavItemBase = (_a) => {
17 var { children, styleChildren = true, className, to, isActive = false, groupId = null, itemId = null, preventDefault = false, onClick, component = 'a', flyout, onShowFlyout, ouiaId, ouiaSafe, zIndex = 9999, icon, innerRef, anchorRef } = _a, props = tslib_1.__rest(_a, ["children", "styleChildren", "className", "to", "isActive", "groupId", "itemId", "preventDefault", "onClick", "component", "flyout", "onShowFlyout", "ouiaId", "ouiaSafe", "zIndex", "icon", "innerRef", "anchorRef"]);
18 const { flyoutRef, setFlyoutRef, navRef } = (0, react_1.useContext)(Nav_1.NavContext);
19 const { isSidebarOpen } = (0, react_1.useContext)(PageSidebar_1.PageSidebarContext);
20 const [flyoutTarget, setFlyoutTarget] = (0, react_1.useState)(null);
21 const [isHovered, setIsHovered] = (0, react_1.useState)(false);
22 const _ref = (0, react_1.useRef)(undefined);
23 const ref = innerRef || _ref;
24 const flyoutVisible = ref === flyoutRef;
25 const popperRef = (0, react_1.useRef)(undefined);
26 const hasFlyout = flyout !== undefined;
27 const Component = hasFlyout ? 'button' : component;
28 // A NavItem should not be both a link and a flyout
29 if (to && hasFlyout) {
30 // eslint-disable-next-line no-console
31 console.error('NavItem cannot have both "to" and "flyout" props.');
32 }
33 const showFlyout = (show, override) => {
34 if ((!flyoutVisible || override) && show) {
35 setFlyoutRef(ref);
36 }
37 else if ((flyoutVisible || override) && !show) {
38 setFlyoutRef(null);
39 }
40 onShowFlyout && show && onShowFlyout();
41 };
42 const onMouseOver = (event) => {
43 const evtContainedInFlyout = event.target.closest(`.${nav_1.default.navItem}.pf-m-flyout`);
44 if (hasFlyout && !flyoutVisible) {
45 showFlyout(true);
46 }
47 else if (flyoutRef !== null && !evtContainedInFlyout) {
48 setFlyoutRef(null);
49 }
50 };
51 const onFlyoutClick = (event) => {
52 const target = event.target;
53 const closestItem = target.closest('.pf-m-flyout');
54 if (!closestItem) {
55 if (hasFlyout) {
56 showFlyout(false, true);
57 }
58 else if (flyoutRef !== null) {
59 setFlyoutRef(null);
60 }
61 }
62 };
63 const handleFlyout = (event) => {
64 var _a, _b;
65 const key = event.key;
66 const target = event.target;
67 if ((key === ' ' || key === 'Enter' || key === 'ArrowRight') && hasFlyout && ((_a = ref === null || ref === void 0 ? void 0 : ref.current) === null || _a === void 0 ? void 0 : _a.contains(target))) {
68 event.stopPropagation();
69 event.preventDefault();
70 if (!flyoutVisible) {
71 showFlyout(true);
72 setFlyoutTarget(target);
73 }
74 }
75 // We only want the NavItem to handle closing a flyout menu if only the first level flyout is open.
76 // Otherwise, MenuItem should handle closing its flyouts
77 if ((key === 'Escape' || key === 'ArrowLeft') &&
78 ((_b = popperRef === null || popperRef === void 0 ? void 0 : popperRef.current) === null || _b === void 0 ? void 0 : _b.querySelectorAll(`.${menu_1.default.menu}`).length) === 1) {
79 if (flyoutVisible) {
80 event.stopPropagation();
81 event.preventDefault();
82 showFlyout(false);
83 }
84 }
85 };
86 (0, react_1.useEffect)(() => {
87 if (hasFlyout) {
88 window.addEventListener('click', onFlyoutClick);
89 }
90 return () => {
91 if (hasFlyout) {
92 window.removeEventListener('click', onFlyoutClick);
93 }
94 };
95 }, []);
96 (0, react_1.useEffect)(() => {
97 if (flyoutTarget) {
98 if (flyoutVisible) {
99 const flyoutItems = Array.from(popperRef.current.getElementsByTagName('UL')[0].children).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(divider_1.default.divider)));
100 flyoutItems[0].firstChild.focus();
101 }
102 else {
103 flyoutTarget.focus();
104 }
105 }
106 }, [flyoutVisible, flyoutTarget]);
107 const flyoutButton = ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(nav_1.default.navToggle), children: (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(nav_1.default.navToggleIcon), children: (0, jsx_runtime_1.jsx)(rh_microns_caret_right_icon_1.default, {}) }) }));
108 const ariaFlyoutProps = {
109 'aria-haspopup': 'menu',
110 'aria-expanded': flyoutVisible
111 };
112 const tabIndex = isSidebarOpen ? null : -1;
113 const renderDefaultLink = (context) => {
114 const preventLinkDefault = preventDefault || !to;
115 return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: anchorRef, href: to, onClick: (e) => context.onSelect(e, groupId, itemId, to, preventLinkDefault, onClick), className: (0, react_styles_1.css)(nav_1.default.navLink, isActive && nav_1.default.modifiers.current, isHovered && nav_1.default.modifiers.hover, className), "aria-current": isActive ? 'page' : null, tabIndex: tabIndex }, (hasFlyout && Object.assign({}, ariaFlyoutProps)), props, { children: [icon && (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(nav_1.default.navLinkIcon), children: icon }), (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(`${nav_1.default.nav}__link-text`), children: children }), flyout && flyoutButton] })));
116 };
117 const renderClonedChild = (context, child) => (0, react_1.cloneElement)(child, Object.assign(Object.assign({ onClick: (e) => context.onSelect(e, groupId, itemId, to, preventDefault, onClick), 'aria-current': isActive ? 'page' : null }, (styleChildren && {
118 className: (0, react_styles_1.css)(nav_1.default.navLink, isActive && nav_1.default.modifiers.current, child.props && child.props.className)
119 })), { tabIndex: child.props.tabIndex || tabIndex, ref: anchorRef, children: hasFlyout ? ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [child.props.children, flyoutButton] })) : (child.props.children) }));
120 const ouiaProps = (0, helpers_1.useOUIAProps)(exports.NavItem.displayName, ouiaId, ouiaSafe);
121 const handleMouseEnter = () => {
122 setIsHovered(true);
123 };
124 const handleMouseLeave = () => {
125 setIsHovered(false);
126 };
127 const flyoutPopper = ((0, jsx_runtime_1.jsx)(Popper_1.Popper, { triggerRef: ref, popper: (0, jsx_runtime_1.jsx)("div", { ref: popperRef, onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, children: flyout }), popperRef: popperRef, placement: "right-start", isVisible: flyoutVisible, onDocumentKeyDown: handleFlyout, zIndex: zIndex, appendTo: navRef === null || navRef === void 0 ? void 0 : navRef.current }));
128 const navItem = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("li", Object.assign({ onMouseOver: onMouseOver, className: (0, react_styles_1.css)(nav_1.default.navItem, hasFlyout && nav_1.default.modifiers.flyout, className), ref: ref }, ouiaProps, { children: (0, jsx_runtime_1.jsx)(Nav_1.NavContext.Consumer, { children: (context) => (0, react_1.isValidElement)(children)
129 ? renderClonedChild(context, children)
130 : renderDefaultLink(context) }) })), flyout && flyoutPopper] }));
131 return navItem;
132 };
133 exports.NavItem = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(NavItemBase, Object.assign({}, props, { innerRef: ref }))));
134 exports.NavItem.displayName = 'NavItem';
135 //# sourceMappingURL=NavItem.js.map