| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { Component, createRef, forwardRef } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Menu/menu.mjs'; |
| 5 |
import breadcrumbStyles from '@patternfly/react-styles/css/components/Breadcrumb/breadcrumb.mjs'; |
| 6 |
import { css } from '@patternfly/react-styles'; |
| 7 |
import { getOUIAProps } from '../../helpers'; |
| 8 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 9 |
import { MenuContext } from './MenuContext'; |
| 10 |
import { canUseDOM } from '../../helpers/util'; |
| 11 |
import { KeyboardHandler } from '../../helpers'; |
| 12 |
class MenuBase extends Component { |
| 13 |
constructor(props) { |
| 14 |
super(props); |
| 15 |
this.menuRef = createRef(); |
| 16 |
this.activeMenu = null; |
| 17 |
this.state = { |
| 18 |
transitionMoveTarget: null, |
| 19 |
flyoutRef: null, |
| 20 |
currentDrilldownMenuId: this.props.id |
| 21 |
}; |
| 22 |
this.handleDrilldownTransition = (event) => { |
| 23 |
const current = this.menuRef.current; |
| 24 |
if (!current || |
| 25 |
(current !== event.target.closest(`.${styles.menu}`) && |
| 26 |
!Array.from(current.getElementsByClassName(styles.menu)).includes(event.target.closest(`.${styles.menu}`)))) { |
| 27 |
return; |
| 28 |
} |
| 29 |
if (this.state.transitionMoveTarget) { |
| 30 |
this.state.transitionMoveTarget.focus(); |
| 31 |
this.setState({ transitionMoveTarget: null }); |
| 32 |
} |
| 33 |
else { |
| 34 |
const nextMenu = current.querySelector('#' + this.props.activeMenu) || current || null; |
| 35 |
const nextMenuLists = nextMenu.getElementsByTagName('UL'); |
| 36 |
if (nextMenuLists.length === 0) { |
| 37 |
return; |
| 38 |
} |
| 39 |
const nextMenuChildren = Array.from(nextMenuLists[0].children); |
| 40 |
if (!this.state.currentDrilldownMenuId || nextMenu.id !== this.state.currentDrilldownMenuId) { |
| 41 |
this.setState({ currentDrilldownMenuId: nextMenu.id }); |
| 42 |
} |
| 43 |
else { |
| 44 |
// if the drilldown transition ends on the same menu, do not focus the first item |
| 45 |
return; |
| 46 |
} |
| 47 |
const nextTarget = nextMenuChildren.filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider)))[0].firstChild; |
| 48 |
nextTarget.focus(); |
| 49 |
nextTarget.tabIndex = 0; |
| 50 |
} |
| 51 |
}; |
| 52 |
this.handleExtraKeys = (event) => { |
| 53 |
const isDrilldown = this.props.containsDrilldown; |
| 54 |
const activeElement = document.activeElement; |
| 55 |
if (event.target.closest(`.${styles.menu}`) !== this.activeMenu && |
| 56 |
!event.target.classList.contains(breadcrumbStyles.breadcrumbLink)) { |
| 57 |
this.activeMenu = event.target.closest(`.${styles.menu}`); |
| 58 |
} |
| 59 |
if (event.target.tagName === 'INPUT') { |
| 60 |
return; |
| 61 |
} |
| 62 |
const parentMenu = this.activeMenu; |
| 63 |
const key = event.key; |
| 64 |
const isFromBreadcrumb = activeElement.classList.contains(breadcrumbStyles.breadcrumbLink); |
| 65 |
if (key === ' ' || key === 'Enter') { |
| 66 |
event.preventDefault(); |
| 67 |
if (isDrilldown && !isFromBreadcrumb) { |
| 68 |
const isDrillingOut = activeElement.closest('li').classList.contains('pf-m-current-path'); |
| 69 |
if (isDrillingOut && parentMenu.parentElement.tagName === 'LI') { |
| 70 |
activeElement.tabIndex = -1; |
| 71 |
parentMenu.parentElement.firstChild.tabIndex = 0; |
| 72 |
this.setState({ transitionMoveTarget: parentMenu.parentElement.firstChild }); |
| 73 |
} |
| 74 |
else { |
| 75 |
if (activeElement.nextElementSibling && activeElement.nextElementSibling.classList.contains(styles.menu)) { |
| 76 |
const childItems = Array.from(activeElement.nextElementSibling.getElementsByTagName('UL')[0].children).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider))); |
| 77 |
activeElement.tabIndex = -1; |
| 78 |
childItems[0].firstChild.tabIndex = 0; |
| 79 |
this.setState({ transitionMoveTarget: childItems[0].firstChild }); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
document.activeElement.click(); |
| 84 |
} |
| 85 |
}; |
| 86 |
this.createNavigableElements = () => { |
| 87 |
const isDrilldown = this.props.containsDrilldown; |
| 88 |
if (isDrilldown) { |
| 89 |
return this.activeMenu |
| 90 |
? Array.from(this.activeMenu.getElementsByTagName('UL')[0].children).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider))) |
| 91 |
: []; |
| 92 |
} |
| 93 |
else { |
| 94 |
return this.menuRef.current |
| 95 |
? Array.from(this.menuRef.current.getElementsByTagName('LI')).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(styles.divider))) |
| 96 |
: []; |
| 97 |
} |
| 98 |
}; |
| 99 |
if (props.innerRef) { |
| 100 |
this.menuRef = props.innerRef; |
| 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)'); |
| 108 |
if (first) { |
| 109 |
first.tabIndex = 0; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
componentDidMount() { |
| 114 |
if (canUseDOM && this.props.containsDrilldown && this.props.isRootMenu) { |
| 115 |
window.addEventListener('transitionend', this.handleDrilldownTransition); |
| 116 |
} |
| 117 |
this.allowTabFirstItem(); |
| 118 |
} |
| 119 |
componentWillUnmount() { |
| 120 |
if (canUseDOM && this.props.containsDrilldown && this.props.isRootMenu) { |
| 121 |
window.removeEventListener('transitionend', this.handleDrilldownTransition); |
| 122 |
} |
| 123 |
} |
| 124 |
componentDidUpdate(prevProps) { |
| 125 |
if (prevProps.children !== this.props.children) { |
| 126 |
this.allowTabFirstItem(); |
| 127 |
} |
| 128 |
} |
| 129 |
render() { |
| 130 |
const _a = this.props, { id, children, className, onSelect, selected = null, onActionClick, ouiaId, ouiaSafe, containsFlyout, containsDrilldown, isMenuDrilledIn, isPlain, isScrollable, drilldownItemPath, drilledInMenus, onDrillIn, onDrillOut, onGetMenuHeight, parentMenu = null, activeItemId = null, |
| 131 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 132 |
innerRef, isRootMenu, activeMenu, role, isNavFlyout } = _a, |
| 133 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 134 |
props = __rest(_a, ["id", "children", "className", "onSelect", "selected", "onActionClick", "ouiaId", "ouiaSafe", "containsFlyout", "containsDrilldown", "isMenuDrilledIn", "isPlain", "isScrollable", "drilldownItemPath", "drilledInMenus", "onDrillIn", "onDrillOut", "onGetMenuHeight", "parentMenu", "activeItemId", "innerRef", "isRootMenu", "activeMenu", "role", "isNavFlyout"]); |
| 135 |
const _isMenuDrilledIn = isMenuDrilledIn || (drilledInMenus && drilledInMenus.includes(id)) || false; |
| 136 |
return (_jsx(SSRSafeIds, { prefix: "pf-", ouiaComponentType: Menu.displayName, children: (_, generatedOuiaId) => { |
| 137 |
var _a, _b; |
| 138 |
return (_jsxs(MenuContext.Provider, { value: { |
| 139 |
menuId: id, |
| 140 |
parentMenu: parentMenu || id, |
| 141 |
onSelect, |
| 142 |
onActionClick, |
| 143 |
activeItemId, |
| 144 |
selected, |
| 145 |
drilledInMenus, |
| 146 |
drilldownItemPath, |
| 147 |
onDrillIn, |
| 148 |
onDrillOut, |
| 149 |
onGetMenuHeight, |
| 150 |
flyoutRef: this.state.flyoutRef, |
| 151 |
setFlyoutRef: (flyoutRef) => this.setState({ flyoutRef }), |
| 152 |
disableHover: (_b = (_a = this.context) === null || _a === void 0 ? void 0 : _a.disableHover) !== null && _b !== void 0 ? _b : false, |
| 153 |
role |
| 154 |
}, children: [isRootMenu && (_jsx(KeyboardHandler, { containerRef: this.menuRef || null, additionalKeyHandler: this.handleExtraKeys, createNavigableElements: this.createNavigableElements, isActiveElement: (element) => document.activeElement.closest('li') === element || // if element is a basic MenuItem |
| 155 |
document.activeElement.parentElement === element || |
| 156 |
document.activeElement.closest(`.${styles.menuSearch}`) === element || // if element is a MenuSearch |
| 157 |
(document.activeElement.closest('ol') && document.activeElement.closest('ol').firstChild === element), getFocusableElement: (navigableElement) => { |
| 158 |
var _a, _b; |
| 159 |
return ((navigableElement === null || navigableElement === void 0 ? void 0 : navigableElement.tagName) === 'DIV' && navigableElement.querySelector('input')) || // for MenuSearchInput |
| 160 |
(((_a = navigableElement.firstChild) === null || _a === void 0 ? void 0 : _a.tagName) === 'LABEL' && |
| 161 |
navigableElement.querySelector('input')) || // for MenuItem checkboxes |
| 162 |
(((_b = navigableElement.firstChild) === null || _b === void 0 ? void 0 : _b.tagName) === 'DIV' && |
| 163 |
navigableElement.querySelector('a, button, input')) || // For aria-disabled element that is rendered inside a div with "display: contents" styling |
| 164 |
navigableElement.firstChild; |
| 165 |
}, noHorizontalArrowHandling: document.activeElement && |
| 166 |
(document.activeElement.classList.contains(breadcrumbStyles.breadcrumbLink) || |
| 167 |
document.activeElement.tagName === 'INPUT'), noEnterHandling: true, noSpaceHandling: true })), _jsx("div", Object.assign({ id: id, className: css(styles.menu, isPlain && styles.modifiers.plain, isScrollable && styles.modifiers.scrollable, containsFlyout && styles.modifiers.flyout, isNavFlyout && 'pf-m-nav', containsDrilldown && styles.modifiers.drilldown, _isMenuDrilledIn && styles.modifiers.drilledIn, className), ref: this.menuRef }, getOUIAProps(Menu.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe), props, { children: children }))] })); |
| 168 |
} })); |
| 169 |
} |
| 170 |
} |
| 171 |
MenuBase.displayName = 'Menu'; |
| 172 |
MenuBase.contextType = MenuContext; |
| 173 |
MenuBase.defaultProps = { |
| 174 |
ouiaSafe: true, |
| 175 |
isRootMenu: true, |
| 176 |
isPlain: false, |
| 177 |
isScrollable: false, |
| 178 |
role: 'menu' |
| 179 |
}; |
| 180 |
export const Menu = forwardRef((props, ref) => (_jsx(MenuBase, Object.assign({}, props, { innerRef: ref })))); |
| 181 |
Menu.displayName = 'Menu'; |
| 182 |
//# sourceMappingURL=Menu.js.map |