Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/Tabs/OverflowTab.js

raw
1 import { __rest } from "tslib";
2 import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
3 import { Fragment, useContext, useEffect, useRef, useState } from 'react';
4 import styles from '@patternfly/react-styles/css/components/Tabs/tabs.mjs';
5 import { css } from '@patternfly/react-styles';
6 import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
7 import { Popper } from '../../helpers';
8 import { Menu, MenuContent, MenuList, MenuItem } from '../Menu';
9 import { TabsContext } from './TabsContext';
10 import { TabTitleText } from './TabTitleText';
11 export const OverflowTab = (_a) => {
12 var { className, overflowingTabs = [], showTabCount, defaultTitleText = 'More', toggleAriaLabel, zIndex = 9999, shouldPreventScrollOnItemFocus = true, focusTimeoutDelay = 0, popperProps } = _a, props = __rest(_a, ["className", "overflowingTabs", "showTabCount", "defaultTitleText", "toggleAriaLabel", "zIndex", "shouldPreventScrollOnItemFocus", "focusTimeoutDelay", "popperProps"]);
13 const menuRef = useRef(undefined);
14 const overflowTabRef = useRef(undefined);
15 const overflowLIRef = useRef(undefined);
16 const [isExpanded, setIsExpanded] = useState(false);
17 const { localActiveKey, handleTabClick } = useContext(TabsContext);
18 const closeMenu = () => {
19 setIsExpanded(false);
20 overflowTabRef.current.focus();
21 };
22 const handleMenuKeys = (ev) => {
23 var _a;
24 const menuContainsEventTarget = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(ev.target);
25 if (isExpanded && menuContainsEventTarget && ev.key === 'Escape') {
26 closeMenu();
27 }
28 };
29 const handleClick = (ev) => {
30 var _a, _b;
31 const clickIsOutsideMenu = !((_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(ev.target));
32 const clickIsOutsideOverflowTab = !((_b = overflowTabRef === null || overflowTabRef === void 0 ? void 0 : overflowTabRef.current) === null || _b === void 0 ? void 0 : _b.contains(ev.target));
33 if (isExpanded && clickIsOutsideMenu && clickIsOutsideOverflowTab) {
34 closeMenu();
35 }
36 };
37 useEffect(() => {
38 window.addEventListener('click', handleClick);
39 window.addEventListener('keydown', handleMenuKeys);
40 return () => {
41 window.removeEventListener('click', handleClick);
42 window.removeEventListener('keydown', handleMenuKeys);
43 };
44 }, [isExpanded, menuRef, overflowTabRef]);
45 const selectedTab = overflowingTabs.find((tab) => tab.eventKey === localActiveKey);
46 const tabTitle = (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.title) ? selectedTab.title : defaultTitleText;
47 const toggleMenu = () => {
48 setIsExpanded((prevIsExpanded) => !prevIsExpanded);
49 setTimeout(() => {
50 if (menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) {
51 const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
52 firstElement && firstElement.focus({ preventScroll: shouldPreventScrollOnItemFocus });
53 }
54 }, focusTimeoutDelay);
55 };
56 const overflowTab = (_jsx("li", Object.assign({ className: css(styles.tabsItem, styles.modifiers.overflow, selectedTab && styles.modifiers.current, className), role: "presentation", ref: overflowLIRef }, props, { children: _jsxs("button", { type: "button", className: css(styles.tabsLink, isExpanded && styles.modifiers.expanded), onClick: () => toggleMenu(), "aria-label": toggleAriaLabel, "aria-haspopup": "menu", "aria-expanded": isExpanded, role: "tab", ref: overflowTabRef, children: [_jsxs(TabTitleText, { children: [tabTitle, showTabCount && tabTitle === defaultTitleText && ` (${overflowingTabs.length})`] }), _jsx("span", { className: styles.tabsLinkToggleIcon, children: _jsx(RhMicronsCaretDownIcon, {}) })] }) })));
57 const tabs = overflowingTabs.map((tab) => (_jsx(MenuItem, { itemId: tab.eventKey, isSelected: localActiveKey === tab.eventKey, children: tab.title }, tab.eventKey)));
58 const onTabSelect = (event, key) => {
59 closeMenu();
60 const selectedTabRef = overflowingTabs.find((tab) => tab.eventKey === key).tabContentRef;
61 handleTabClick(event, key, selectedTabRef);
62 };
63 const overflowMenu = (_jsx(Menu, { ref: menuRef, onSelect: (ev, itemId) => onTabSelect(ev, itemId), children: _jsx(MenuContent, { children: _jsx(MenuList, { children: tabs }) }) }));
64 return (_jsxs(Fragment, { children: [overflowTab, _jsx(Popper, Object.assign({ triggerRef: overflowTabRef, popper: overflowMenu, popperRef: menuRef, isVisible: isExpanded, minWidth: "revert", appendTo: overflowLIRef.current, zIndex: zIndex }, popperProps))] }));
65 };
66 OverflowTab.displayName = 'OverflowTab';
67 //# sourceMappingURL=OverflowTab.js.map