@atlaskit/mention
Version:
A React component used to display user profiles in a list for 'Mention' functionality
122 lines (121 loc) • 4.89 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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; })(); }
/*
* Simple implementation of popup while waiting for ak-inline-dialog
*/
var Popup = exports.default = /*#__PURE__*/function (_React$PureComponent) {
function Popup() {
(0, _classCallCheck2.default)(this, Popup);
return _callSuper(this, Popup, arguments);
}
(0, _inherits2.default)(Popup, _React$PureComponent);
return (0, _createClass2.default)(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.default.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.default.render(this.props.children, this.popup);
}
}
}, {
key: "render",
value: function render() {
// Placeholder element for react to render inplace
return /*#__PURE__*/_react.default.createElement("div", null);
}
}]);
}(_react.default.PureComponent);
(0, _defineProperty2.default)(Popup, "defaultProps", {
relativePosition: 'auto',
offsetX: 0,
offsetY: 0,
zIndex: 0
});