@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
125 lines • 5.7 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
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';
import { OutsideClickTargetRefContext, withReactEditorViewOuterListeners } from '../../ui-react';
import DropdownList from '../../ui/DropList';
import Popup from '../../ui/Popup';
import { ArrowKeyNavigationProvider } from '../ArrowKeyNavigationProvider';
/**
* Wrapper around @atlaskit/droplist which uses Popup and Portal to render
* droplist outside of "overflow: hidden" containers when needed.
*
* Also it controls popper's placement.
*/
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
export var Dropdown = /*#__PURE__*/function (_PureComponent) {
function Dropdown(props) {
var _this;
_classCallCheck(this, Dropdown);
_this = _callSuper(this, Dropdown, [props]);
_defineProperty(_this, "handleRef", function (setOutsideClickTargetRef) {
return function (target) {
setOutsideClickTargetRef(target);
_this.setState({
target: target || undefined
});
};
});
_defineProperty(_this, "updatePopupPlacement", function (placement) {
_this.setState({
popupPlacement: placement
});
});
_defineProperty(_this, "handleCloseAndFocus", function (event) {
var _this$state$target;
(_this$state$target = _this.state.target) === null || _this$state$target === void 0 || (_this$state$target = _this$state$target.querySelector('button')) === null || _this$state$target === void 0 || _this$state$target.focus();
_this.handleClose(event);
});
_defineProperty(_this, "handleClose", function (event) {
if (_this.props.onOpenChange) {
_this.props.onOpenChange({
isOpen: false,
event: event
});
}
});
_this.state = {
popupPlacement: ['bottom', 'left']
};
return _this;
}
_inherits(Dropdown, _PureComponent);
return _createClass(Dropdown, [{
key: "renderDropdown",
value: function renderDropdown() {
var _this$state = this.state,
target = _this$state.target,
popupPlacement = _this$state.popupPlacement;
var _this$props = this.props,
children = _this$props.children,
mountTo = _this$props.mountTo,
boundariesElement = _this$props.boundariesElement,
scrollableElement = _this$props.scrollableElement,
onOpenChange = _this$props.onOpenChange,
fitHeight = _this$props.fitHeight,
fitWidth = _this$props.fitWidth,
zIndex = _this$props.zIndex,
arrowKeyNavigationProviderOptions = _this$props.arrowKeyNavigationProviderOptions,
dropdownListId = _this$props.dropdownListId,
alignDropdownWithParentElement = _this$props.alignDropdownWithParentElement;
return /*#__PURE__*/React.createElement(Popup, {
target: alignDropdownWithParentElement ? // Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
target === null || target === void 0 ? void 0 : target.closest("[data-testid='editor-floating-toolbar']") : target,
mountTo: mountTo,
boundariesElement: boundariesElement,
scrollableElement: scrollableElement,
onPlacementChanged: this.updatePopupPlacement,
fitHeight: fitHeight,
fitWidth: fitWidth,
zIndex: zIndex,
allowOutOfBounds: alignDropdownWithParentElement
}, /*#__PURE__*/React.createElement(ArrowKeyNavigationProvider
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
, _extends({}, arrowKeyNavigationProviderOptions, {
closeOnTab: true,
handleClose: this.handleCloseAndFocus
}), /*#__PURE__*/React.createElement("div", {
style: {
height: 0,
minWidth: fitWidth || 0
}
}, /*#__PURE__*/React.createElement(DropdownList, {
isOpen: true,
onOpenChange: onOpenChange,
position: popupPlacement.join(' '),
shouldFitContainer: true,
id: dropdownListId
}, children))));
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
trigger = _this$props2.trigger,
isOpen = _this$props2.isOpen;
return /*#__PURE__*/React.createElement(OutsideClickTargetRefContext.Consumer, null, function (setOutsideClickTargetRef) {
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
ref: _this2.handleRef(setOutsideClickTargetRef)
}, trigger), isOpen ? _this2.renderDropdown() : null);
});
}
}]);
}(PureComponent);
var DropdownWithOuterListeners = withReactEditorViewOuterListeners(Dropdown);
export default DropdownWithOuterListeners;