| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.MenuItem = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const menu_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Menu/menu")); |
| 8 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 9 |
const c_menu_m_flyout__menu_top_offset_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_menu_m_flyout__menu_top_offset')); |
| 10 |
const c_menu_m_flyout__menu_m_left_right_offset_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_menu_m_flyout__menu_m_left_right_offset')); |
| 11 |
const c_menu_m_flyout__menu_left_offset_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_menu_m_flyout__menu_left_offset')); |
| 12 |
const rh_microns_external_link_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-external-link-icon')); |
| 13 |
const rh_microns_caret_left_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-left-icon')); |
| 14 |
const rh_microns_caret_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-right-icon')); |
| 15 |
const check_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/check-icon')); |
| 16 |
const Checkbox_1 = require("../Checkbox"); |
| 17 |
const MenuContext_1 = require("./MenuContext"); |
| 18 |
const MenuItemAction_1 = require("./MenuItemAction"); |
| 19 |
const Tooltip_1 = require("../Tooltip"); |
| 20 |
const util_1 = require("../../helpers/util"); |
| 21 |
const useIsomorphicLayout_1 = require("../../helpers/useIsomorphicLayout"); |
| 22 |
const helpers_1 = require("../../helpers"); |
| 23 |
const FlyoutContext = (0, react_1.createContext)({ |
| 24 |
direction: 'right' |
| 25 |
}); |
| 26 |
const MenuItemBase = (_a) => { |
| 27 |
var { children, className, itemId = null, to, hasCheckbox = false, isActive = null, isFavorited = null, isLoadButton = false, isLoading = false, flyoutMenu, direction, description = null, onClick = () => { }, component = 'button', isDisabled = false, isAriaDisabled = false, isExternalLink = false, isSelected = null, isFocused, isDanger = false, icon, actions, onShowFlyout, drilldownMenu, isOnPath, innerRef, id, 'aria-label': ariaLabel, tooltipProps, rel, target, download } = _a, props = tslib_1.__rest(_a, ["children", "className", "itemId", "to", "hasCheckbox", "isActive", "isFavorited", "isLoadButton", "isLoading", "flyoutMenu", "direction", "description", "onClick", "component", "isDisabled", "isAriaDisabled", "isExternalLink", "isSelected", "isFocused", "isDanger", "icon", "actions", "onShowFlyout", "drilldownMenu", "isOnPath", "innerRef", "id", 'aria-label', "tooltipProps", "rel", "target", "download"]); |
| 28 |
const { menuId, parentMenu, onSelect, onActionClick, activeItemId, selected, drilldownItemPath, onDrillIn, onDrillOut, flyoutRef, setFlyoutRef, disableHover, role: menuRole } = (0, react_1.useContext)(MenuContext_1.MenuContext); |
| 29 |
let Component = (to ? 'a' : component); |
| 30 |
if (hasCheckbox && !to) { |
| 31 |
Component = 'label'; |
| 32 |
} |
| 33 |
const [flyoutTarget, setFlyoutTarget] = (0, react_1.useState)(null); |
| 34 |
const flyoutContext = (0, react_1.useContext)(FlyoutContext); |
| 35 |
const [flyoutXDirection, setFlyoutXDirection] = (0, react_1.useState)(flyoutContext.direction); |
| 36 |
const ref = (0, react_1.useRef)(undefined); |
| 37 |
const privateRef = (0, react_1.useRef)(undefined); |
| 38 |
const innerComponentRef = innerRef || privateRef; |
| 39 |
const flyoutVisible = ref === flyoutRef; |
| 40 |
const randomId = (0, helpers_1.useSSRSafeId)(); |
| 41 |
const hasFlyout = flyoutMenu !== undefined; |
| 42 |
const showFlyout = (show) => { |
| 43 |
if (!flyoutVisible && show) { |
| 44 |
setFlyoutRef(ref); |
| 45 |
} |
| 46 |
else if (flyoutVisible && !show) { |
| 47 |
setFlyoutRef(null); |
| 48 |
} |
| 49 |
onShowFlyout && show && onShowFlyout(); |
| 50 |
}; |
| 51 |
(0, useIsomorphicLayout_1.useIsomorphicLayoutEffect)(() => { |
| 52 |
if (hasFlyout && ref.current && util_1.canUseDOM) { |
| 53 |
const flyoutMenu = ref.current.lastElementChild; |
| 54 |
if (flyoutMenu && flyoutMenu.classList.contains(menu_1.default.menu)) { |
| 55 |
const origin = ref.current.getClientRects()[0]; |
| 56 |
const rect = flyoutMenu.getClientRects()[0]; |
| 57 |
if (origin && rect) { |
| 58 |
const spaceLeftLeft = origin.x - rect.width; |
| 59 |
const spaceLeftRight = window.innerWidth - origin.x - origin.width - rect.width; |
| 60 |
let xDir = flyoutXDirection; |
| 61 |
if (spaceLeftRight < 0 && xDir !== 'left') { |
| 62 |
setFlyoutXDirection('left'); |
| 63 |
xDir = 'left'; |
| 64 |
} |
| 65 |
else if (spaceLeftLeft < 0 && xDir !== 'right') { |
| 66 |
setFlyoutXDirection('right'); |
| 67 |
xDir = 'right'; |
| 68 |
} |
| 69 |
let xOffset = 0; |
| 70 |
if (spaceLeftLeft < 0 && spaceLeftRight < 0) { |
| 71 |
xOffset = xDir === 'right' ? -spaceLeftRight : -spaceLeftLeft; |
| 72 |
} |
| 73 |
if (xDir === 'left') { |
| 74 |
flyoutMenu.classList.add(menu_1.default.modifiers.left); |
| 75 |
flyoutMenu.style.setProperty(c_menu_m_flyout__menu_m_left_right_offset_1.default.name, `-${xOffset}px`); |
| 76 |
} |
| 77 |
else { |
| 78 |
flyoutMenu.style.setProperty(c_menu_m_flyout__menu_left_offset_1.default.name, `-${xOffset}px`); |
| 79 |
} |
| 80 |
const spaceLeftBot = window.innerHeight - origin.y - rect.height; |
| 81 |
const spaceLeftTop = window.innerHeight - rect.height; |
| 82 |
if (spaceLeftTop < 0 && spaceLeftBot < 0) { |
| 83 |
// working idea: page can usually scroll down, but not up |
| 84 |
// TODO: proper scroll buttons |
| 85 |
} |
| 86 |
else if (spaceLeftBot < 0) { |
| 87 |
flyoutMenu.style.setProperty(c_menu_m_flyout__menu_top_offset_1.default.name, `${spaceLeftBot}px`); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
}, [flyoutVisible, flyoutMenu]); |
| 93 |
(0, react_1.useEffect)(() => { |
| 94 |
setFlyoutXDirection(flyoutContext.direction); |
| 95 |
}, [flyoutContext]); |
| 96 |
(0, react_1.useEffect)(() => { |
| 97 |
var _a, _b, _c; |
| 98 |
if (flyoutTarget) { |
| 99 |
if (flyoutVisible) { |
| 100 |
const flyoutMenu = flyoutTarget.nextElementSibling; |
| 101 |
const flyoutItems = Array.from(((_a = flyoutMenu === null || flyoutMenu === void 0 ? void 0 : flyoutMenu.getElementsByTagName('UL')[0]) === null || _a === void 0 ? void 0 : _a.children) || []).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(menu_1.default.divider))); |
| 102 |
(_c = (_b = flyoutItems[0]) === null || _b === void 0 ? void 0 : _b.firstChild) === null || _c === void 0 ? void 0 : _c.focus(); |
| 103 |
} |
| 104 |
else { |
| 105 |
flyoutTarget.focus(); |
| 106 |
} |
| 107 |
} |
| 108 |
}, [flyoutVisible, flyoutTarget]); |
| 109 |
const handleFlyout = (event) => { |
| 110 |
const key = event.key; |
| 111 |
const target = event.target; |
| 112 |
const type = event.type; |
| 113 |
if (key === ' ' || key === 'Enter' || key === 'ArrowRight' || type === 'click') { |
| 114 |
event.stopPropagation(); |
| 115 |
event.preventDefault(); |
| 116 |
if (!flyoutVisible) { |
| 117 |
showFlyout(true); |
| 118 |
setFlyoutTarget(target); |
| 119 |
} |
| 120 |
} |
| 121 |
if (key === 'Escape' || key === 'ArrowLeft') { |
| 122 |
if (flyoutVisible) { |
| 123 |
event.stopPropagation(); |
| 124 |
showFlyout(false); |
| 125 |
} |
| 126 |
} |
| 127 |
}; |
| 128 |
const onItemSelect = (event, onSelect) => { |
| 129 |
if (!isAriaDisabled) { |
| 130 |
// Trigger callback for Menu onSelect |
| 131 |
onSelect && onSelect(event, itemId); |
| 132 |
// Trigger callback for item onClick |
| 133 |
onClick && onClick(event); |
| 134 |
} |
| 135 |
}; |
| 136 |
const _isOnPath = (isOnPath && isOnPath) || (drilldownItemPath && drilldownItemPath.includes(itemId)) || false; |
| 137 |
let drill; |
| 138 |
if (direction) { |
| 139 |
if (direction === 'down') { |
| 140 |
drill = (event) => onDrillIn && |
| 141 |
onDrillIn(event, menuId, typeof drilldownMenu === 'function' |
| 142 |
? drilldownMenu().props.id |
| 143 |
: drilldownMenu.props.id, itemId); |
| 144 |
} |
| 145 |
else { |
| 146 |
drill = (event) => onDrillOut && onDrillOut(event, parentMenu, itemId); |
| 147 |
} |
| 148 |
} |
| 149 |
let additionalProps = {}; |
| 150 |
if (Component === 'a') { |
| 151 |
additionalProps = { |
| 152 |
href: to, |
| 153 |
'aria-disabled': isDisabled || isAriaDisabled ? true : null, |
| 154 |
// prevent invalid 'disabled' attribute on <a> tags |
| 155 |
disabled: null, |
| 156 |
target: isExternalLink ? '_blank' : target, |
| 157 |
rel, |
| 158 |
download |
| 159 |
}; |
| 160 |
} |
| 161 |
else if (Component === 'button') { |
| 162 |
additionalProps = { |
| 163 |
type: 'button', |
| 164 |
'aria-disabled': isAriaDisabled ? true : null |
| 165 |
}; |
| 166 |
} |
| 167 |
if (isOnPath) { |
| 168 |
additionalProps['aria-expanded'] = true; |
| 169 |
} |
| 170 |
else if (hasFlyout) { |
| 171 |
additionalProps['aria-haspopup'] = 'menu'; |
| 172 |
additionalProps['aria-expanded'] = flyoutVisible; |
| 173 |
} |
| 174 |
const getAriaCurrent = () => { |
| 175 |
if (isActive !== null) { |
| 176 |
if (isActive) { |
| 177 |
return 'page'; |
| 178 |
} |
| 179 |
else { |
| 180 |
return null; |
| 181 |
} |
| 182 |
} |
| 183 |
else if (itemId !== null && activeItemId !== null) { |
| 184 |
return itemId === activeItemId; |
| 185 |
} |
| 186 |
return null; |
| 187 |
}; |
| 188 |
const getIsSelected = () => { |
| 189 |
if (isSelected !== null) { |
| 190 |
return isSelected; |
| 191 |
} |
| 192 |
else if (selected !== null && itemId !== null) { |
| 193 |
return (Array.isArray(selected) && selected.includes(itemId)) || itemId === selected; |
| 194 |
} |
| 195 |
return false; |
| 196 |
}; |
| 197 |
const onMouseOver = () => { |
| 198 |
if (disableHover) { |
| 199 |
return; |
| 200 |
} |
| 201 |
if (hasFlyout) { |
| 202 |
showFlyout(true); |
| 203 |
} |
| 204 |
else { |
| 205 |
setFlyoutRef(null); |
| 206 |
} |
| 207 |
}; |
| 208 |
(0, react_1.useEffect)(() => { |
| 209 |
if (isFocused && ref.current) { |
| 210 |
const itemEl = ref.current; |
| 211 |
const parentListEl = itemEl.parentElement; |
| 212 |
if (parentListEl) { |
| 213 |
const isAboveTop = itemEl.offsetTop - parentListEl.offsetTop < parentListEl.scrollTop; |
| 214 |
const isBelowBottom = itemEl.offsetTop - parentListEl.offsetTop + itemEl.clientHeight; |
| 215 |
if (isAboveTop || isBelowBottom) { |
| 216 |
itemEl.scrollIntoView({ behavior: 'auto', block: 'nearest' }); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
}, [isFocused]); |
| 221 |
const isSelectMenu = menuRole === 'listbox'; |
| 222 |
const renderItem = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Component, Object.assign({ id: id, tabIndex: -1, className: (0, react_styles_1.css)(menu_1.default.menuItem, getIsSelected() && !hasCheckbox && menu_1.default.modifiers.selected, className), "aria-current": getAriaCurrent() }, (!hasCheckbox && { disabled: isDisabled, 'aria-label': ariaLabel }), (!hasCheckbox && !flyoutMenu && { role: isSelectMenu ? 'option' : 'menuitem' }), (!hasCheckbox && !flyoutMenu && isSelectMenu && { 'aria-selected': getIsSelected() }), { ref: innerComponentRef }, (!hasCheckbox && { |
| 223 |
onClick: (event) => { |
| 224 |
if (!isAriaDisabled) { |
| 225 |
onItemSelect(event, onSelect); |
| 226 |
drill && drill(event); |
| 227 |
flyoutMenu && handleFlyout(event); |
| 228 |
} |
| 229 |
else { |
| 230 |
event.preventDefault(); |
| 231 |
} |
| 232 |
} |
| 233 |
}), (hasCheckbox && { htmlFor: randomId }), additionalProps, { children: [(0, jsx_runtime_1.jsxs)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemMain), children: [direction === 'up' && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemToggleIcon), children: (0, jsx_runtime_1.jsx)(rh_microns_caret_left_icon_1.default, {}) })), icon && (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemIcon), children: icon }), hasCheckbox && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemCheck), children: (0, jsx_runtime_1.jsx)(Checkbox_1.Checkbox, { id: randomId, component: "span", isChecked: isSelected || false, onChange: (event) => onItemSelect(event, onSelect), isDisabled: isDisabled, "aria-disabled": isAriaDisabled }) })), (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemText), children: children }), isExternalLink && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemExternalIcon), children: (0, jsx_runtime_1.jsx)(rh_microns_external_link_icon_1.default, {}) })), (flyoutMenu || direction === 'down') && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemToggleIcon), children: (0, jsx_runtime_1.jsx)(rh_microns_caret_right_icon_1.default, {}) })), getIsSelected() && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemSelectIcon), children: (0, jsx_runtime_1.jsx)(check_icon_1.default, {}) }))] }), description && direction !== 'up' && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(menu_1.default.menuItemDescription), children: (0, jsx_runtime_1.jsx)("span", { children: description }) }))] })), flyoutVisible && ((0, jsx_runtime_1.jsx)(MenuContext_1.MenuContext.Provider, { value: { disableHover }, children: (0, jsx_runtime_1.jsx)(FlyoutContext.Provider, { value: { direction: flyoutXDirection }, children: flyoutMenu }) })), typeof drilldownMenu === 'function' ? drilldownMenu() : drilldownMenu, (0, jsx_runtime_1.jsxs)(MenuContext_1.MenuItemContext.Provider, { value: { itemId, isDisabled }, children: [actions, isFavorited !== null && ((0, jsx_runtime_1.jsx)(MenuItemAction_1.MenuItemAction, { icon: "favorites", isFavorited: isFavorited, "aria-label": isFavorited ? 'starred' : 'not starred', onClick: (event) => onActionClick(event, itemId), tabIndex: -1, actionId: "fav" }))] })] })); |
| 234 |
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("li", Object.assign({ className: (0, react_styles_1.css)(menu_1.default.menuListItem, isDisabled && menu_1.default.modifiers.disabled, isAriaDisabled && menu_1.default.modifiers.ariaDisabled, _isOnPath && menu_1.default.modifiers.currentPath, isLoadButton && menu_1.default.modifiers.load, isLoading && menu_1.default.modifiers.loading, isFocused && 'pf-m-focus', isDanger && menu_1.default.modifiers.danger, className), onMouseOver: () => { |
| 235 |
if (!isAriaDisabled) { |
| 236 |
onMouseOver(); |
| 237 |
} |
| 238 |
} }, (flyoutMenu && !isAriaDisabled && { onKeyDown: handleFlyout }), { ref: ref, role: !hasCheckbox ? 'none' : 'menuitem' }, (hasCheckbox && { 'aria-label': ariaLabel }), props, { children: renderItem })), tooltipProps && (0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, Object.assign({}, tooltipProps, { triggerRef: innerComponentRef }))] })); |
| 239 |
}; |
| 240 |
exports.MenuItem = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(MenuItemBase, Object.assign({}, props, { innerRef: ref })))); |
| 241 |
exports.MenuItem.displayName = 'MenuItem'; |
| 242 |
//# sourceMappingURL=MenuItem.js.map |