UNPKG

@atlaskit/mention

Version:

A React component used to display user profiles in a list for 'Mention' functionality

116 lines 4.35 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React from 'react'; import ReactDOM from 'react-dom'; /* * Simple implementation of popup while waiting for ak-inline-dialog */ var Popup = /*#__PURE__*/function (_React$PureComponent) { function Popup() { _classCallCheck(this, Popup); return _callSuper(this, Popup, arguments); } _inherits(Popup, _React$PureComponent); return _createClass(Popup, [{ key: "componentDidMount", value: function componentDidMount() { this.popup = document.createElement('div'); document.body.appendChild(this.popup); this.popup.style.position = 'absolute'; this._applyAbsolutePosition(); this._renderContent(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this._renderContent(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.popup) { ReactDOM.unmountComponentAtNode(this.popup); document.body.removeChild(this.popup); } } }, { key: "_applyBelowPosition", value: function _applyBelowPosition() { var targetNode = this.props.target && document.getElementById(this.props.target); if (targetNode) { var box = targetNode.getBoundingClientRect(); var top = box.bottom + (this.props.offsetY || 0); var left = box.left + (this.props.offsetX || 0); if (this.popup) { this.popup.style.top = "".concat(top, "px"); this.popup.style.bottom = ''; this.popup.style.left = "".concat(left, "px"); } } } }, { key: "_applyAbovePosition", value: function _applyAbovePosition() { var targetNode = this.props.target && document.getElementById(this.props.target); if (targetNode) { var box = targetNode.getBoundingClientRect(); var bottom = window.innerHeight - box.top + (this.props.offsetY || 0); var left = box.left + (this.props.offsetX || 0); if (this.popup) { this.popup.style.top = ''; this.popup.style.bottom = "".concat(bottom, "px"); this.popup.style.left = "".concat(left, "px"); } } } }, { key: "_applyAbsolutePosition", value: function _applyAbsolutePosition() { if (this.props.relativePosition === 'above') { this._applyAbovePosition(); } else if (this.props.relativePosition === 'below') { this._applyBelowPosition(); } else { var targetNode = this.props.target && document.getElementById(this.props.target); if (targetNode) { var box = targetNode.getBoundingClientRect(); var viewPortHeight = window.innerHeight; if (box.top < viewPortHeight / 2) { this._applyBelowPosition(); } else { this._applyAbovePosition(); } } } if (this.props.zIndex && this.popup) { this.popup.style.zIndex = "".concat(this.props.zIndex); } } }, { key: "_renderContent", value: function _renderContent() { if (this.popup) { ReactDOM.render(this.props.children, this.popup); } } }, { key: "render", value: function render() { // Placeholder element for react to render inplace return /*#__PURE__*/React.createElement("div", null); } }]); }(React.PureComponent); _defineProperty(Popup, "defaultProps", { relativePosition: 'auto', offsetX: 0, offsetY: 0, zIndex: 0 }); export { Popup as default };