| 1 |
import { Component, createRef, forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 3 |
import breadcrumbStyles from '@patternfly/react-styles/css/components/Breadcrumb/breadcrumb'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 6 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 7 |
import { MenuContext } from './MenuContext'; |
| 8 |
import type { MenuItemProps } from './MenuItem'; |
| 9 |
import { canUseDOM } from '../../helpers/util'; |
| 10 |
import { KeyboardHandler } from '../../helpers'; |
| 11 |
export interface MenuProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'ref' | 'onSelect'>, OUIAProps { |
| 12 |
/** Anything that can be rendered inside of the Menu */ |
| 13 |
children?: React.ReactNode; |
| 14 |
/** Additional classes added to the Menu */ |
| 15 |
className?: string; |
| 16 |
/** ID of the menu */ |
| 17 |
id?: string; |
| 18 |
/** Callback for updating when item selection changes. You can also specify onClick on the MenuItem. */ |
| 19 |
onSelect?: (event?: React.MouseEvent, itemId?: MenuItemProps['itemId']) => void; |
| 20 |
/** Single itemId for single select menus, or array of itemIds for multi select. You can also specify isSelected on the MenuItem. */ |
| 21 |
selected?: any | any[]; |
| 22 |
/** Callback called when an MenuItems's action button is clicked. You can also specify it within a MenuItemAction. */ |
| 23 |
onActionClick?: (event?: any, itemId?: any, actionId?: any) => void; |
| 24 |
/** Indicates if menu contains a flyout menu */ |
| 25 |
containsFlyout?: boolean; |
| 26 |
/** Indicating that the menu should have nav flyout styling */ |
| 27 |
isNavFlyout?: boolean; |
| 28 |
/** Indicates if menu contains a drilldown menu */ |
| 29 |
containsDrilldown?: boolean; |
| 30 |
/** Indicates if a menu is drilled into */ |
| 31 |
isMenuDrilledIn?: boolean; |
| 32 |
/** Indicates the path of drilled in menu itemIds */ |
| 33 |
drilldownItemPath?: string[]; |
| 34 |
/** Array of menus that are drilled in */ |
| 35 |
drilledInMenus?: string[]; |
| 36 |
/** Callback for drilling into a submenu */ |
| 37 |
onDrillIn?: ( |
| 38 |
event: React.KeyboardEvent | React.MouseEvent, |
| 39 |
fromItemId: string, |
| 40 |
toItemId: string, |
| 41 |
itemId: string |
| 42 |
) => void; |
| 43 |
/** Callback for drilling out of a submenu */ |
| 44 |
onDrillOut?: (event: React.KeyboardEvent | React.MouseEvent, toItemId: string, itemId: string) => void; |
| 45 |
/** Callback for collecting menu heights */ |
| 46 |
onGetMenuHeight?: (menuId: string, height: number) => void; |
| 47 |
/** ID of parent menu for drilldown menus */ |
| 48 |
parentMenu?: string; |
| 49 |
/** ID of the currently active menu for the drilldown variant */ |
| 50 |
activeMenu?: string; |
| 51 |
/** itemId of the currently active item. You can also specify isActive on the MenuItem. */ |
| 52 |
activeItemId?: string | number; |
| 53 |
/** @hide Forwarded ref */ |
| 54 |
innerRef?: React.Ref<HTMLDivElement>; |
| 55 |
/** Internal flag indicating if the Menu is the root of a menu tree */ |
| 56 |
isRootMenu?: boolean; |
| 57 |
/** Indicates if the menu should be without the outer box-shadow */ |
| 58 |
isPlain?: boolean; |
| 59 |
/** Indicates if the menu should be srollable */ |
| 60 |
isScrollable?: boolean; |
| 61 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 62 |
ouiaId?: number | string; |
| 63 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 64 |
ouiaSafe?: boolean; |
| 65 |
/** Determines the accessible role of the menu. For a non-checkbox menu that can have |
| 66 |
* one or more items selected, pass in "listbox". */ |
| 67 |
role?: string; |
| 68 |
} |
| 69 |
|
| 70 |
export interface MenuState { |
| 71 |
transitionMoveTarget: HTMLElement; |
| 72 |
flyoutRef: React.Ref<HTMLLIElement> | null; |
| 73 |
currentDrilldownMenuId: string; |
| 74 |
} |
| 75 |
|
| 76 |
class MenuBase extends Component<MenuProps, MenuState> { |
| 77 |
static displayName = 'Menu'; |
| 78 |
static contextType = MenuContext; |
| 79 |
context!: React.ContextType<typeof MenuContext>; |
| 80 |
private menuRef = createRef<HTMLDivElement>(); |
| 81 |
private activeMenu = null as Element; |
| 82 |
static defaultProps: MenuProps = { |
| 83 |
ouiaSafe: true, |
| 84 |
isRootMenu: true, |
| 85 |
isPlain: false, |
| 86 |
isScrollable: false, |
| 87 |
role: 'menu' |
| 88 |
}; |
| 89 |
|
| 90 |
constructor(props: MenuProps) { |
| 91 |
super(props); |
| 92 |
if (props.innerRef) { |
| 93 |
this.menuRef = props.innerRef as React.RefObject<HTMLDivElement | null>; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
state: MenuState = { |
| 98 |
transitionMoveTarget: null, |
| 99 |
flyoutRef: null, |
| 100 |
currentDrilldownMenuId: this.props.id |
| 101 |
}; |
| 102 |
|
| 103 |
allowTabFirstItem() { |
| 104 |
// Allow tabbing to first menu item |
| 105 |
const current = this.menuRef.current; |
| 106 |
if (current) { |
| 107 |
const first = current.querySelector('ul button:not(:disabled), ul a:not(:disabled)') as |
| 108 |
| HTMLButtonElement |
| 109 |
| HTMLAnchorElement; |
| 110 |
if (first) { |
| 111 |
first.tabIndex = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
componentDidMount() { |
| 117 |
if (canUseDOM && this.props.containsDrilldown && this.props.isRootMenu) { |
| 118 |
window.addEventListener('transitionend', this.handleDrilldownTransition); |
| 119 |
} |
| 120 |
|
| 121 |
this.allowTabFirstItem(); |
| 122 |
} |
| 123 |
|
| 124 |
componentWillUnmount() { |
| 125 |
if (canUseDOM && this.props.containsDrilldown && this.props.isRootMenu) { |
| 126 |
window.removeEventListener('transitionend', this.handleDrilldownTransition); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
componentDidUpdate(prevProps: MenuProps) { |
| 131 |
if (prevProps.children !== this.props.children) { |
| 132 |
this.allowTabFirstItem(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
handleDrilldownTransition = (event: TransitionEvent) => { |
| 137 |
const current = this.menuRef.current; |
| 138 |
if ( |
| 139 |
!current || |
| 140 |
(current !== (event.target as HTMLElement).closest(`.${styles.menu}`) && |
| 141 |
!Array.from(current.getElementsByClassName(styles.menu)).includes( |
| 142 |
(event.target as HTMLElement).closest(`.${styles.menu}`) |
| 143 |
)) |
| 144 |
) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if (this.state.transitionMoveTarget) { |
| 149 |
this.state.transitionMoveTarget.focus(); |
| 150 |
this.setState({ transitionMoveTarget: null }); |
| 151 |
} else { |
| 152 |
const nextMenu = current.querySelector('#' + this.props.activeMenu) || current || null; |
| 153 |
const nextMenuLists = nextMenu.getElementsByTagName('UL'); |
| 154 |
if (nextMenuLists.length === 0) { |
| 155 |
return; |
| 156 |
} |
| 157 |
const nextMenuChildren = Array.from(nextMenuLists[0].children); |
| 158 |
if (!this.state.currentDrilldownMenuId || nextMenu.id !== this.state.currentDrilldownMenuId) { |
| 159 |
this.setState({ currentDrilldownMenuId: nextMenu.id }); |
| 160 |
} else { |
| 161 |
// if the drilldown transition ends on the same menu, do not focus the first item |
| 162 |
return; |
| 163 |
} |
| 164 |
const nextTarget = nextMenuChildren.filter( |
| 165 |
(el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider)) |
| 166 |
)[0].firstChild; |
| 167 |
(nextTarget as HTMLElement).focus(); |
| 168 |
(nextTarget as HTMLElement).tabIndex = 0; |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
handleExtraKeys = (event: KeyboardEvent) => { |
| 173 |
const isDrilldown = this.props.containsDrilldown; |
| 174 |
const activeElement = document.activeElement; |
| 175 |
|
| 176 |
if ( |
| 177 |
(event.target as HTMLElement).closest(`.${styles.menu}`) !== this.activeMenu && |
| 178 |
!(event.target as HTMLElement).classList.contains(breadcrumbStyles.breadcrumbLink) |
| 179 |
) { |
| 180 |
this.activeMenu = (event.target as HTMLElement).closest(`.${styles.menu}`); |
| 181 |
} |
| 182 |
|
| 183 |
if ((event.target as HTMLElement).tagName === 'INPUT') { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
const parentMenu = this.activeMenu; |
| 188 |
const key = event.key; |
| 189 |
const isFromBreadcrumb = activeElement.classList.contains(breadcrumbStyles.breadcrumbLink); |
| 190 |
|
| 191 |
if (key === ' ' || key === 'Enter') { |
| 192 |
event.preventDefault(); |
| 193 |
if (isDrilldown && !isFromBreadcrumb) { |
| 194 |
const isDrillingOut = activeElement.closest('li').classList.contains('pf-m-current-path'); |
| 195 |
if (isDrillingOut && parentMenu.parentElement.tagName === 'LI') { |
| 196 |
(activeElement as HTMLElement).tabIndex = -1; |
| 197 |
(parentMenu.parentElement.firstChild as HTMLElement).tabIndex = 0; |
| 198 |
this.setState({ transitionMoveTarget: parentMenu.parentElement.firstChild as HTMLElement }); |
| 199 |
} else { |
| 200 |
if (activeElement.nextElementSibling && activeElement.nextElementSibling.classList.contains(styles.menu)) { |
| 201 |
const childItems = Array.from( |
| 202 |
activeElement.nextElementSibling.getElementsByTagName('UL')[0].children |
| 203 |
).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider))); |
| 204 |
|
| 205 |
(activeElement as HTMLElement).tabIndex = -1; |
| 206 |
(childItems[0].firstChild as HTMLElement).tabIndex = 0; |
| 207 |
this.setState({ transitionMoveTarget: childItems[0].firstChild as HTMLElement }); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
(document.activeElement as HTMLElement).click(); |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
createNavigableElements = () => { |
| 216 |
const isDrilldown = this.props.containsDrilldown; |
| 217 |
|
| 218 |
if (isDrilldown) { |
| 219 |
return this.activeMenu |
| 220 |
? Array.from(this.activeMenu.getElementsByTagName('UL')[0].children).filter( |
| 221 |
(el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider)) |
| 222 |
) |
| 223 |
: []; |
| 224 |
} else { |
| 225 |
return this.menuRef.current |
| 226 |
? Array.from(this.menuRef.current.getElementsByTagName('LI')).filter( |
| 227 |
(el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider)) |
| 228 |
) |
| 229 |
: []; |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
render() { |
| 234 |
const { |
| 235 |
id, |
| 236 |
children, |
| 237 |
className, |
| 238 |
onSelect, |
| 239 |
selected = null, |
| 240 |
onActionClick, |
| 241 |
ouiaId, |
| 242 |
ouiaSafe, |
| 243 |
containsFlyout, |
| 244 |
containsDrilldown, |
| 245 |
isMenuDrilledIn, |
| 246 |
isPlain, |
| 247 |
isScrollable, |
| 248 |
drilldownItemPath, |
| 249 |
drilledInMenus, |
| 250 |
onDrillIn, |
| 251 |
onDrillOut, |
| 252 |
onGetMenuHeight, |
| 253 |
parentMenu = null, |
| 254 |
activeItemId = null, |
| 255 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 256 |
innerRef, |
| 257 |
isRootMenu, |
| 258 |
activeMenu, |
| 259 |
role, |
| 260 |
isNavFlyout, |
| 261 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 262 |
...props |
| 263 |
} = this.props; |
| 264 |
const _isMenuDrilledIn = isMenuDrilledIn || (drilledInMenus && drilledInMenus.includes(id)) || false; |
| 265 |
return ( |
| 266 |
<SSRSafeIds prefix="pf-" ouiaComponentType={Menu.displayName}> |
| 267 |
{(_, generatedOuiaId) => ( |
| 268 |
<MenuContext.Provider |
| 269 |
value={{ |
| 270 |
menuId: id, |
| 271 |
parentMenu: parentMenu || id, |
| 272 |
onSelect, |
| 273 |
onActionClick, |
| 274 |
activeItemId, |
| 275 |
selected, |
| 276 |
drilledInMenus, |
| 277 |
drilldownItemPath, |
| 278 |
onDrillIn, |
| 279 |
onDrillOut, |
| 280 |
onGetMenuHeight, |
| 281 |
flyoutRef: this.state.flyoutRef, |
| 282 |
setFlyoutRef: (flyoutRef) => this.setState({ flyoutRef }), |
| 283 |
disableHover: this.context?.disableHover ?? false, |
| 284 |
role |
| 285 |
}} |
| 286 |
> |
| 287 |
{isRootMenu && ( |
| 288 |
<KeyboardHandler |
| 289 |
containerRef={(this.menuRef as React.RefObject<HTMLDivElement | null>) || null} |
| 290 |
additionalKeyHandler={this.handleExtraKeys} |
| 291 |
createNavigableElements={this.createNavigableElements} |
| 292 |
isActiveElement={(element: Element) => |
| 293 |
document.activeElement.closest('li') === element || // if element is a basic MenuItem |
| 294 |
document.activeElement.parentElement === element || |
| 295 |
document.activeElement.closest(`.${styles.menuSearch}`) === element || // if element is a MenuSearch |
| 296 |
(document.activeElement.closest('ol') && document.activeElement.closest('ol').firstChild === element) |
| 297 |
} |
| 298 |
getFocusableElement={(navigableElement: Element) => |
| 299 |
(navigableElement?.tagName === 'DIV' && navigableElement.querySelector('input')) || // for MenuSearchInput |
| 300 |
((navigableElement.firstChild as Element)?.tagName === 'LABEL' && |
| 301 |
navigableElement.querySelector('input')) || // for MenuItem checkboxes |
| 302 |
((navigableElement.firstChild as Element)?.tagName === 'DIV' && |
| 303 |
navigableElement.querySelector('a, button, input')) || // For aria-disabled element that is rendered inside a div with "display: contents" styling |
| 304 |
(navigableElement.firstChild as Element) |
| 305 |
} |
| 306 |
noHorizontalArrowHandling={ |
| 307 |
document.activeElement && |
| 308 |
(document.activeElement.classList.contains(breadcrumbStyles.breadcrumbLink) || |
| 309 |
document.activeElement.tagName === 'INPUT') |
| 310 |
} |
| 311 |
noEnterHandling |
| 312 |
noSpaceHandling |
| 313 |
/> |
| 314 |
)} |
| 315 |
<div |
| 316 |
id={id} |
| 317 |
className={css( |
| 318 |
styles.menu, |
| 319 |
isPlain && styles.modifiers.plain, |
| 320 |
isScrollable && styles.modifiers.scrollable, |
| 321 |
containsFlyout && styles.modifiers.flyout, |
| 322 |
isNavFlyout && 'pf-m-nav', |
| 323 |
containsDrilldown && styles.modifiers.drilldown, |
| 324 |
_isMenuDrilledIn && styles.modifiers.drilledIn, |
| 325 |
className |
| 326 |
)} |
| 327 |
ref={this.menuRef} |
| 328 |
{...getOUIAProps(Menu.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)} |
| 329 |
{...props} |
| 330 |
> |
| 331 |
{children} |
| 332 |
</div> |
| 333 |
</MenuContext.Provider> |
| 334 |
)} |
| 335 |
</SSRSafeIds> |
| 336 |
); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
export const Menu = forwardRef((props: MenuProps, ref: React.Ref<HTMLDivElement>) => ( |
| 341 |
<MenuBase {...props} innerRef={ref} /> |
| 342 |
)); |
| 343 |
Menu.displayName = 'Menu'; |