Debian Codegraph @

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

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