@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
82 lines • 4.44 kB
JavaScript
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, { PureComponent } from 'react';
// Use this context to pass in the reference of the element that should be considered as the outside click target
// The outside click target is the element that should be clicked outside of to trigger the `handleClickOutside` event
export var PlainOutsideClickTargetRefContext = /*#__PURE__*/React.createContext(function () {});
export default function withOuterListeners(Component) {
return /*#__PURE__*/function (_PureComponent) {
function WithOutsideClick() {
var _this;
_classCallCheck(this, WithOutsideClick);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, WithOutsideClick, [].concat(args));
_defineProperty(_this, "outsideClickTargetRef", /*#__PURE__*/React.createRef());
_defineProperty(_this, "handleClick", function (evt) {
var _this$outsideClickTar;
var domNode = (_this$outsideClickTar = _this.outsideClickTargetRef.current) === null || _this$outsideClickTar === void 0 ? void 0 : _this$outsideClickTar.deref();
if (!domNode || evt.target instanceof Node && !domNode.contains(evt.target)) {
var _this$props$handleCli, _this$props;
(_this$props$handleCli = (_this$props = _this.props).handleClickOutside) === null || _this$props$handleCli === void 0 || _this$props$handleCli.call(_this$props, evt);
}
});
_defineProperty(_this, "handleKeydown", function (evt) {
if (evt.code === 'Escape' && _this.props.handleEscapeKeydown) {
_this.props.handleEscapeKeydown(evt);
}
});
_defineProperty(_this, "setOutsideClickTargetRef", function (el) {
_this.outsideClickTargetRef.current = el && new WeakRef(el);
});
return _this;
}
_inherits(WithOutsideClick, _PureComponent);
return _createClass(WithOutsideClick, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.handleClickOutside) {
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
document.addEventListener('click', this.handleClick, false);
}
if (this.props.handleEscapeKeydown) {
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
document.addEventListener('keydown', this.handleKeydown, false);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.props.handleClickOutside) {
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
document.removeEventListener('click', this.handleClick, false);
}
if (this.props.handleEscapeKeydown) {
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
document.removeEventListener('keydown', this.handleKeydown, false);
}
}
}, {
key: "render",
value: function render() {
return /*#__PURE__*/React.createElement(PlainOutsideClickTargetRefContext.Provider, {
value: this.setOutsideClickTargetRef
}, /*#__PURE__*/React.createElement(Component
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
, this.props));
}
}]);
}(PureComponent);
}