Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/Menu/MenuItem.js

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