Debian Codegraph @

node/@patternfly/react-core/src/components/Tabs/OverflowTab.tsx

raw
1 import { Fragment, useContext, useEffect, useRef, useState } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Tabs/tabs';
3 import { css } from '@patternfly/react-styles';
4 import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
5 import { Popper, PopperOptions } from '../../helpers';
6 import { Menu, MenuContent, MenuList, MenuItem } from '../Menu';
7 import { TabsContext } from './TabsContext';
8 import { TabProps } from './Tab';
9 import { TabTitleText } from './TabTitleText';
10
11 /** @deprecated Use PopperOptions instead */
12 export type HorizontalOverflowPopperProps = PopperOptions;
13
14 export interface OverflowTabProps extends React.HTMLProps<HTMLLIElement> {
15 /** Additional classes added to the overflow tab */
16 className?: string;
17 /** The tabs that should be displayed in the menu */
18 overflowingTabs?: TabProps[];
19 /** Flag which shows the count of overflowing tabs when enabled */
20 showTabCount?: boolean;
21 /** The text which displays when an overflowing tab isn't selected */
22 defaultTitleText?: string;
23 /** The aria label applied to the button which toggles the tab overflow menu */
24 toggleAriaLabel?: string;
25 /** z-index of the overflow tab */
26 zIndex?: number;
27 /** Flag indicating if scroll on focus of the first menu item should occur. */
28 shouldPreventScrollOnItemFocus?: boolean;
29 /** Time in ms to wait before firing the toggles' focus event. Defaults to 0 */
30 focusTimeoutDelay?: number;
31 /** Additional props to spread to the popper menu. */
32 popperProps?: PopperOptions;
33 }
34
35 export const OverflowTab: React.FunctionComponent<OverflowTabProps> = ({
36 className,
37 overflowingTabs = [],
38 showTabCount,
39 defaultTitleText = 'More',
40 toggleAriaLabel,
41 zIndex = 9999,
42 shouldPreventScrollOnItemFocus = true,
43 focusTimeoutDelay = 0,
44 popperProps,
45 ...props
46 }: OverflowTabProps) => {
47 const menuRef = useRef<HTMLDivElement>(undefined);
48 const overflowTabRef = useRef<HTMLButtonElement>(undefined);
49 const overflowLIRef = useRef<HTMLLIElement>(undefined);
50
51 const [isExpanded, setIsExpanded] = useState(false);
52
53 const { localActiveKey, handleTabClick } = useContext(TabsContext);
54
55 const closeMenu = () => {
56 setIsExpanded(false);
57 overflowTabRef.current.focus();
58 };
59
60 const handleMenuKeys = (ev: KeyboardEvent) => {
61 const menuContainsEventTarget = menuRef?.current?.contains(ev.target as Node);
62 if (isExpanded && menuContainsEventTarget && ev.key === 'Escape') {
63 closeMenu();
64 }
65 };
66
67 const handleClick = (ev: MouseEvent) => {
68 const clickIsOutsideMenu = !menuRef?.current?.contains(ev.target as Node);
69 const clickIsOutsideOverflowTab = !overflowTabRef?.current?.contains(ev.target as Node);
70 if (isExpanded && clickIsOutsideMenu && clickIsOutsideOverflowTab) {
71 closeMenu();
72 }
73 };
74
75 useEffect(() => {
76 window.addEventListener('click', handleClick);
77 window.addEventListener('keydown', handleMenuKeys);
78
79 return () => {
80 window.removeEventListener('click', handleClick);
81 window.removeEventListener('keydown', handleMenuKeys);
82 };
83 }, [isExpanded, menuRef, overflowTabRef]);
84
85 const selectedTab = overflowingTabs.find((tab) => tab.eventKey === localActiveKey);
86 const tabTitle = selectedTab?.title ? selectedTab.title : defaultTitleText;
87
88 const toggleMenu = () => {
89 setIsExpanded((prevIsExpanded) => !prevIsExpanded);
90 setTimeout(() => {
91 if (menuRef?.current) {
92 const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
93 firstElement && (firstElement as HTMLElement).focus({ preventScroll: shouldPreventScrollOnItemFocus });
94 }
95 }, focusTimeoutDelay);
96 };
97
98 const overflowTab = (
99 <li
100 className={css(styles.tabsItem, styles.modifiers.overflow, selectedTab && styles.modifiers.current, className)}
101 role="presentation"
102 ref={overflowLIRef}
103 {...props}
104 >
105 <button
106 type="button"
107 className={css(styles.tabsLink, isExpanded && styles.modifiers.expanded)}
108 onClick={() => toggleMenu()}
109 aria-label={toggleAriaLabel}
110 aria-haspopup="menu"
111 aria-expanded={isExpanded}
112 role="tab"
113 ref={overflowTabRef}
114 >
115 <TabTitleText>
116 {tabTitle}
117 {showTabCount && tabTitle === defaultTitleText && ` (${overflowingTabs.length})`}
118 </TabTitleText>
119 <span className={styles.tabsLinkToggleIcon}>
120 <RhMicronsCaretDownIcon />
121 </span>
122 </button>
123 </li>
124 );
125
126 const tabs = overflowingTabs.map((tab) => (
127 <MenuItem key={tab.eventKey} itemId={tab.eventKey} isSelected={localActiveKey === tab.eventKey}>
128 {tab.title}
129 </MenuItem>
130 ));
131
132 const onTabSelect = (event: React.MouseEvent<HTMLElement, MouseEvent>, key: number | string) => {
133 closeMenu();
134 const selectedTabRef = overflowingTabs.find((tab) => tab.eventKey === key).tabContentRef;
135 handleTabClick(event, key, selectedTabRef);
136 };
137
138 const overflowMenu = (
139 <Menu ref={menuRef} onSelect={(ev, itemId) => onTabSelect(ev as React.MouseEvent<HTMLElement, MouseEvent>, itemId)}>
140 <MenuContent>
141 <MenuList>{tabs}</MenuList>
142 </MenuContent>
143 </Menu>
144 );
145
146 return (
147 <Fragment>
148 {overflowTab}
149 <Popper
150 triggerRef={overflowTabRef}
151 popper={overflowMenu}
152 popperRef={menuRef}
153 isVisible={isExpanded}
154 minWidth="revert"
155 appendTo={overflowLIRef.current}
156 zIndex={zIndex}
157 {...popperProps}
158 />
159 </Fragment>
160 );
161 };
162
163 OverflowTab.displayName = 'OverflowTab';