UNPKG

@arche-mc2/arche-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

118 lines 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var helpers_1 = require("../../../Common/utils/helpers"); var eventListener_1 = require("../../../Common/utils/eventListener"); var assign = require("object-assign"); var actions_1 = require("./actions"); var withTheme_1 = require("../../../Common/theming/withTheme"); var theming_1 = require("../../../Common/theming"); var styles_1 = require("./styles"); var UpContextMenu = (function (_super) { tslib_1.__extends(UpContextMenu, _super); function UpContextMenu(props) { var _this = _super.call(this, props) || this; _this.setMenu = function (element) { _this.menu = element; }; _this.getMenuPosition = function (x, y) { var _a = document.documentElement, scrollX = _a.scrollTop, scrollY = _a.scrollLeft; var innerWidth = window.innerWidth, innerHeight = window.innerHeight; var rect = _this.menu.getBoundingClientRect(); var menuStyles = { top: y + scrollY, left: x + scrollX }; if (y + rect.height > innerHeight) { menuStyles.top -= rect.height; } if (x + rect.width > innerWidth) { menuStyles.left -= rect.width; } if (menuStyles.top < 0) { menuStyles.top = (rect.height < innerHeight) ? (innerHeight - rect.height) / 2 : 0; } if (menuStyles.left < 0) { menuStyles.left = (rect.width < innerWidth) ? (innerWidth - rect.width) / 2 : 0; } return menuStyles; }; _this.handleShow = function (e) { if (e.detail.id !== _this.props.id) return; var _a = e.detail.position, x = _a.x, y = _a.y; _this.setState({ isVisible: true, x: x, y: y }); document.addEventListener('mousedown', _this.handleOutsideClick); document.addEventListener('ontouchstart', _this.handleOutsideClick); document.addEventListener('scroll', _this.handleHide); document.addEventListener('contextmenu', _this.handleHide); window.addEventListener('resize', _this.handleHide); helpers_1.callIfExists(_this.props.onShow, e); }; _this.handleHide = function (e) { document.removeEventListener('mousedown', _this.handleOutsideClick); document.removeEventListener('ontouchstart', _this.handleOutsideClick); document.removeEventListener('scroll', _this.handleHide); document.removeEventListener('contextmenu', _this.handleHide); window.removeEventListener('resize', _this.handleHide); _this.setState({ isVisible: false }); helpers_1.callIfExists(_this.props.onHide, e); }; _this.handleOutsideClick = function (e) { if (_this.state.isVisible && !_this.menu.contains(e.target)) { _this.setState(function (prevState) { return ({ isVisible: !prevState.isVisible }); }); _this.hideMenu({}, e.target); } }; _this.hideMenu = function (opts, target) { if (opts === void 0) { opts = {}; } eventListener_1.default.dispatchGlobalEvent(actions_1.MENU_HIDE, assign({}, opts, { type: actions_1.MENU_HIDE }), target); }; _this.state = { x: 0, y: 0, top: 0, left: 0, isVisible: false }; return _this; } UpContextMenu.prototype.componentDidMount = function () { var callbacks = {}; callbacks[actions_1.MENU_SHOW] = this.handleShow; callbacks[actions_1.MENU_HIDE] = this.handleHide; this.listenerId = eventListener_1.default.register(callbacks); }; UpContextMenu.prototype.componentDidUpdate = function () { var _this = this; if (this.state.isVisible) { var wrapper_1 = window.requestAnimationFrame || setTimeout; wrapper_1(function () { var _a = _this.state, x = _a.x, y = _a.y; var _b = _this.getMenuPosition(x, y), top = _b.top, left = _b.left; wrapper_1(function () { _this.menu.style.top = top + "px"; _this.menu.style.left = left + "px"; }); }); } }; UpContextMenu.prototype.componentWillUnmount = function () { if (this.listenerId) { eventListener_1.default.unregister(this.listenerId); } }; UpContextMenu.prototype.render = function () { var _a = this.props, children = _a.children, theme = _a.theme; return (React.createElement("nav", { ref: this.setMenu, onContextMenu: this.handleHide, className: styles_1.MenuStyle(theme, this.state) }, this.state.isVisible && children)); }; UpContextMenu.defaultProps = { theme: theming_1.default, }; return UpContextMenu; }(React.PureComponent)); exports.UpContextMenu = UpContextMenu; ; exports.default = withTheme_1.default(UpContextMenu); //# sourceMappingURL=UpContextMenu.js.map