UNPKG

6.39 kBJavaScriptView Raw
1/*
2 * Copyright 2016 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, __extends, __rest } from "tslib";
17/**
18 * @fileoverview This component is DEPRECATED, and the code is frozen.
19 * All changes & bugfixes should be made to ContextMenu2 instead.
20 */
21/* eslint-disable deprecation/deprecation */
22import classNames from "classnames";
23import * as React from "react";
24import * as ReactDOM from "react-dom";
25import { AbstractPureComponent, Classes } from "../common";
26import { Popover } from "../components/popover/popover";
27var TRANSITION_DURATION = 100;
28/* istanbul ignore next */
29/** @deprecated use ContextMenu */
30var ContextMenuLegacy = /** @class */ (function (_super) {
31 __extends(ContextMenuLegacy, _super);
32 function ContextMenuLegacy() {
33 var _this = _super !== null && _super.apply(this, arguments) || this;
34 _this.state = {
35 isDarkTheme: false,
36 isOpen: false,
37 };
38 _this.cancelContextMenu = function (e) { return e.preventDefault(); };
39 _this.handleBackdropContextMenu = function (e) {
40 // React function to remove from the event pool, useful when using a event within a callback
41 e.persist();
42 e.preventDefault();
43 // wait for backdrop to disappear so we can find the "real" element at event coordinates.
44 // timeout duration is equivalent to transition duration so we know it's animated out.
45 _this.setTimeout(function () {
46 // retrigger context menu event at the element beneath the backdrop.
47 // if it has a `contextmenu` event handler then it'll be invoked.
48 // if it doesn't, no native menu will show (at least on OSX) :(
49 var newTarget = document.elementFromPoint(e.clientX, e.clientY);
50 var view = e.view, newEventInit = __rest(e, ["view"]);
51 newTarget === null || newTarget === void 0 ? void 0 : newTarget.dispatchEvent(new MouseEvent("contextmenu", newEventInit));
52 }, TRANSITION_DURATION);
53 };
54 _this.handlePopoverInteraction = function (nextOpenState) {
55 if (!nextOpenState) {
56 // delay the actual hiding till the event queue clears
57 // to avoid flicker of opening twice
58 _this.requestAnimationFrame(function () { return _this.hide(); });
59 }
60 };
61 return _this;
62 }
63 ContextMenuLegacy.prototype.render = function () {
64 var _a;
65 // prevent right-clicking in a context menu
66 var content = React.createElement("div", { onContextMenu: this.cancelContextMenu }, this.state.menu);
67 var popoverClassName = classNames((_a = {}, _a[Classes.DARK] = this.state.isDarkTheme, _a));
68 // HACKHACK: workaround until we have access to Popper#scheduleUpdate().
69 // https://github.com/palantir/blueprint/issues/692
70 // Generate key based on offset so a new Popover instance is created
71 // when offset changes, to force recomputing position.
72 var key = this.state.offset === undefined ? "" : "".concat(this.state.offset.left, "x").concat(this.state.offset.top);
73 // wrap the popover in a positioned div to make sure it is properly
74 // offset on the screen.
75 return (React.createElement("div", { className: Classes.CONTEXT_MENU, style: this.state.offset },
76 React.createElement(Popover, __assign({}, this.props, { backdropProps: { onContextMenu: this.handleBackdropContextMenu }, content: content, enforceFocus: false, key: key, hasBackdrop: true, isOpen: this.state.isOpen, minimal: true, rootBoundary: "viewport", onInteraction: this.handlePopoverInteraction, placement: "right-start", popoverClassName: popoverClassName, transitionDuration: TRANSITION_DURATION }),
77 React.createElement("div", null))));
78 };
79 ContextMenuLegacy.prototype.show = function (menu, offset, onClose, isDarkTheme) {
80 if (isDarkTheme === void 0) { isDarkTheme = false; }
81 this.setState({ isOpen: true, menu: menu, offset: offset, onClose: onClose, isDarkTheme: isDarkTheme });
82 };
83 ContextMenuLegacy.prototype.hide = function () {
84 var _a, _b;
85 (_b = (_a = this.state).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
86 this.setState({ isOpen: false, onClose: undefined });
87 };
88 return ContextMenuLegacy;
89}(AbstractPureComponent));
90var contextMenuElement;
91var contextMenu;
92/**
93 * Show the given menu element at the given offset from the top-left corner of the viewport.
94 * The menu will appear below-right of this point and will flip to below-left if there is not enough
95 * room onscreen. The optional callback will be invoked when this menu closes.
96 *
97 * @deprecated use ContextMenu2
98 */
99export function show(menu, offset, onClose, isDarkTheme) {
100 if (contextMenuElement === undefined) {
101 contextMenuElement = document.createElement("div");
102 contextMenuElement.classList.add(Classes.CONTEXT_MENU);
103 document.body.appendChild(contextMenuElement);
104 contextMenu = ReactDOM.render(React.createElement(ContextMenuLegacy, { onClosed: remove }), contextMenuElement);
105 }
106 contextMenu.show(menu, offset, onClose, isDarkTheme);
107}
108/** Hide the open context menu. */
109export function hide() {
110 contextMenu === null || contextMenu === void 0 ? void 0 : contextMenu.hide();
111}
112/** Return whether a context menu is currently open. */
113export function isOpen() {
114 return contextMenu != null && contextMenu.state.isOpen;
115}
116function remove() {
117 if (contextMenuElement != null) {
118 ReactDOM.unmountComponentAtNode(contextMenuElement);
119 contextMenuElement.remove();
120 contextMenuElement = undefined;
121 contextMenu = undefined;
122 }
123}
124//# sourceMappingURL=contextMenuLegacy.js.map
\No newline at end of file