3.79 kBJavaScriptView Raw
1/*
2 * Copyright 2023 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { Classes, DISPLAYNAME_PREFIX } from "../../common";
20import { Popover } from "../popover/popover";
21import { Portal } from "../portal/portal";
22/**
23 * A floating popover which is positioned at a given target offset inside its parent element container.
24 * Used to display context menus. Note that this behaves differently from other popover components like
25 * Popover and Tooltip, which wrap their children with interaction handlers -- if you're looking for the whole
26 * interaction package, use ContextMenu instead.
27 *
28 * @see https://blueprintjs.com/docs/#core/components/context-menu-popover
29 */
30export var ContextMenuPopover = React.memo(function _ContextMenuPopover(props) {
31 var _a;
32 var content = props.content, popoverClassName = props.popoverClassName, onClose = props.onClose, _b = props.isDarkTheme, isDarkTheme = _b === void 0 ? false : _b, _c = props.rootBoundary, rootBoundary = _c === void 0 ? "viewport" : _c, targetOffset = props.targetOffset, _d = props.transitionDuration, transitionDuration = _d === void 0 ? 100 : _d, popoverProps = __rest(props, ["content", "popoverClassName", "onClose", "isDarkTheme", "rootBoundary", "targetOffset", "transitionDuration"]);
33 var cancelContextMenu = React.useCallback(function (e) { return e.preventDefault(); }, []);
34 // Popover should attach its ref to the virtual target we render inside a Portal, not the "inline" child target
35 var renderTarget = React.useCallback(function (_a) {
36 var ref = _a.ref;
37 return (React.createElement(Portal, null,
38 React.createElement("div", { className: Classes.CONTEXT_MENU_VIRTUAL_TARGET, style: targetOffset, ref: ref })));
39 }, [targetOffset]);
40 var handleInteraction = React.useCallback(function (nextOpenState) {
41 if (!nextOpenState) {
42 onClose === null || onClose === void 0 ? void 0 : onClose();
43 }
44 }, []);
45 return (React.createElement(Popover, __assign({ placement: "right-start", rootBoundary: rootBoundary, transitionDuration: transitionDuration }, popoverProps, { content:
46 // this prevents right-clicking inside our context menu
47 React.createElement("div", { onContextMenu: cancelContextMenu }, content), enforceFocus: false,
48 // Generate key based on offset so that a new Popover instance is created
49 // when offset changes, to force recomputing position.
50 key: getPopoverKey(targetOffset), hasBackdrop: true, backdropProps: { className: Classes.CONTEXT_MENU_BACKDROP }, minimal: true, onInteraction: handleInteraction, popoverClassName: classNames(Classes.CONTEXT_MENU_POPOVER, popoverClassName, (_a = {},
51 _a[Classes.DARK] = isDarkTheme,
52 _a)), positioningStrategy: "fixed", renderTarget: renderTarget })));
53});
54ContextMenuPopover.displayName = "".concat(DISPLAYNAME_PREFIX, ".ContextMenuPopover");
55function getPopoverKey(targetOffset) {
56 return targetOffset === undefined ? "default" : "".concat(targetOffset.left, "x").concat(targetOffset.top);
57}
58//# sourceMappingURL=contextMenuPopover.js.map
\No newline at end of file