@grafana/ui
Version:
Grafana Components Library
89 lines (82 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactUse = require('react-use');
var e2eSelectors = require('@grafana/e2e-selectors');
var Menu = require('../Menu/Menu.cjs');
var Portal = require('../Portal/Portal.cjs');
function _interopNamespaceCompat(e) {
if (e && typeof e === 'object' && 'default' in e) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
;
const ContextMenu = React__namespace.memo(
({ x, y, onClose, focusOnOpen = true, renderMenuItems, renderHeader }) => {
const menuRef = React.useRef(null);
const [positionStyles, setPositionStyles] = React.useState({});
React.useLayoutEffect(() => {
const menuElement = menuRef.current;
if (menuElement) {
const rect = menuElement.getBoundingClientRect();
const OFFSET = 5;
const collisions = {
right: window.innerWidth < x + rect.width,
bottom: window.innerHeight < y + rect.height + OFFSET
};
setPositionStyles({
position: "fixed",
left: collisions.right ? x - rect.width - OFFSET : x - OFFSET,
top: Math.max(0, collisions.bottom ? y - rect.height - OFFSET : y + OFFSET)
});
}
}, [x, y]);
reactUse.useClickAway(menuRef, () => {
onClose == null ? void 0 : onClose();
});
const header = renderHeader == null ? void 0 : renderHeader();
const menuItems = renderMenuItems == null ? void 0 : renderMenuItems();
const onOpen = (setFocusedItem) => {
if (focusOnOpen) {
setFocusedItem(0);
}
};
const onKeyDown = (e) => {
if (e.key === "Escape") {
e.preventDefault();
e.stopPropagation();
onClose == null ? void 0 : onClose();
}
};
return /* @__PURE__ */ jsxRuntime.jsx(Portal.Portal, { children: /* @__PURE__ */ jsxRuntime.jsx(
Menu.Menu,
{
header,
ref: menuRef,
style: positionStyles,
ariaLabel: e2eSelectors.selectors.components.Menu.MenuComponent("Context"),
onOpen,
onClick: onClose,
onKeyDown,
children: menuItems
}
) });
}
);
ContextMenu.displayName = "ContextMenu";
exports.ContextMenu = ContextMenu;
//# sourceMappingURL=ContextMenu.cjs.map