UNPKG

@blueprintjs/core

Version:
111 lines 6.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MenuItem = void 0; const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); /* * Copyright 2022 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const classnames_1 = tslib_1.__importDefault(require("classnames")); const react_1 = require("react"); const icons_1 = require("@blueprintjs/icons"); const common_1 = require("../../common"); const props_1 = require("../../common/props"); const utils_1 = require("../../common/utils"); const icon_1 = require("../icon/icon"); const popover_1 = require("../popover/popover"); const text_1 = require("../text/text"); const menu_1 = require("./menu"); /** * Menu item component. * * @see https://blueprintjs.com/docs/#core/components/menu.menu-item */ exports.MenuItem = (0, react_1.forwardRef)((props, ref) => { const { active = false, className, children, disabled = false, icon, intent, labelClassName, labelElement, multiline = false, popoverProps = {}, roleStructure = "menuitem", selected, shouldDismissPopover = true, submenuProps, text = "", textClassName, tagName = "a", htmlTitle, ...htmlProps } = props; const [liRole, targetRole, ariaSelected] = roleStructure === "listoption" // "listoption": parent has listbox role, or is a <select> ? [ "option", undefined, // target should have no role Boolean(selected), // aria-selected prop ] : roleStructure === "menuitem" // "menuitem": parent has menu role ? [ "none", "menuitem", undefined, // don't set aria-selected prop ] : roleStructure === "none" // "none": allows wrapping MenuItem in custom <li> ? [ "none", undefined, // target should have no role undefined, // don't set aria-selected prop ] : // roleStructure === "listitem" [ undefined, // needs no role prop, li is listitem by default undefined, undefined, // don't set aria-selected prop ]; const isSelectable = roleStructure === "listoption"; const isSelected = isSelectable && selected; const hasIcon = icon != null; const hasSubmenu = children != null; const intentClass = common_1.Classes.intentClass(intent); const anchorClasses = (0, classnames_1.default)(common_1.Classes.MENU_ITEM, intentClass, { [common_1.Classes.ACTIVE]: active, [common_1.Classes.DISABLED]: disabled, // prevent popover from closing when clicking on submenu trigger or disabled item [common_1.Classes.POPOVER_DISMISS]: shouldDismissPopover && !disabled && !hasSubmenu, [common_1.Classes.MENU_ITEM_IS_SELECTABLE]: isSelectable, [common_1.Classes.SELECTED]: isSelected, }, className); const maybeLabel = props.label == null && labelElement == null ? null : ((0, jsx_runtime_1.jsxs)("span", { className: (0, classnames_1.default)(common_1.Classes.MENU_ITEM_LABEL, labelClassName), children: [props.label, labelElement] })); const htmlPropsOnly = (0, props_1.removeNonHTMLProps)(htmlProps); const target = (0, react_1.createElement)(tagName, { // for menuitems, onClick when enter key pressed doesn't take effect like it does for a button-- fix this onKeyDown: (0, utils_1.clickElementOnKeyPress)(["Enter", " "]), // if hasSubmenu, must apply correct role and tabIndex to the outer popover target <span> instead of this target element role: hasSubmenu ? "none" : targetRole, ...htmlPropsOnly, tabIndex: hasSubmenu ? -1 : htmlPropsOnly.tabIndex != null ? htmlPropsOnly.tabIndex : 0, ...(disabled ? DISABLED_PROPS : {}), className: anchorClasses, }, isSelected ? (0, jsx_runtime_1.jsx)(icons_1.SmallTick, { className: common_1.Classes.MENU_ITEM_SELECTED_ICON }) : undefined, hasIcon ? ( // wrap icon in a <span> in case `icon` is a custom element rather than a built-in icon identifier, // so that we always render this class and hide it from a screen reader (0, jsx_runtime_1.jsx)("span", { className: common_1.Classes.MENU_ITEM_ICON, "aria-hidden": true, children: (0, jsx_runtime_1.jsx)(icon_1.Icon, { icon: icon, tabIndex: -1 }) })) : undefined, (0, jsx_runtime_1.jsx)(text_1.Text, { className: (0, classnames_1.default)(common_1.Classes.FILL, textClassName), ellipsize: !multiline, title: htmlTitle, children: text }), maybeLabel, hasSubmenu ? (0, jsx_runtime_1.jsx)(icons_1.CaretRight, { className: common_1.Classes.MENU_SUBMENU_ICON, title: "Open sub menu" }) : undefined); const liClasses = (0, classnames_1.default)({ [common_1.Classes.MENU_SUBMENU]: hasSubmenu }); return ((0, jsx_runtime_1.jsx)("li", { className: liClasses, ref: ref, role: liRole, "aria-selected": ariaSelected, children: children == null ? (target) : ((0, jsx_runtime_1.jsx)(popover_1.Popover, { autoFocus: false, captureDismiss: false, disabled: disabled, enforceFocus: false, hoverCloseDelay: 0, interactionKind: "hover", modifiers: SUBMENU_POPOVER_MODIFIERS, targetProps: { role: targetRole, tabIndex: 0 }, placement: "right-start", usePortal: false, ...popoverProps, content: (0, jsx_runtime_1.jsx)(menu_1.Menu, { ...submenuProps, children: children }), minimal: true, popoverClassName: (0, classnames_1.default)(common_1.Classes.MENU_SUBMENU, popoverProps?.popoverClassName), children: target })) })); }); exports.MenuItem.displayName = `${props_1.DISPLAYNAME_PREFIX}.MenuItem`; const SUBMENU_POPOVER_MODIFIERS = { // 20px padding - scrollbar width + a bit flip: { enabled: true, options: { padding: 20, rootBoundary: "viewport" } }, // shift popover up 5px so MenuItems align offset: { enabled: true, options: { offset: [-5, 0] } }, preventOverflow: { enabled: true, options: { padding: 20, rootBoundary: "viewport" } }, }; // props to ignore when disabled const DISABLED_PROPS = { "aria-disabled": true, href: undefined, onClick: undefined, onMouseDown: undefined, onMouseEnter: undefined, onMouseLeave: undefined, tabIndex: -1, }; //# sourceMappingURL=menuItem.js.map