@braineet/ui
Version:
Braineet design system
89 lines (88 loc) • 3.81 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.PopOver = void 0;
var _react = _interopRequireDefault(require("react"));
var _popover = require("ariakit/popover");
var _modal = require("../modal");
var _styles = require("./styles");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["children", "content", "open", "options", "zIndex", "onOpen", "onClose", "anchorRef", "getAnchorRect"];
/* eslint-disable no-plusplus */
/* eslint-disable no-underscore-dangle */
/* eslint-disable jsx-a11y/mouse-events-have-key-events */
/**
* The `PopOver` component.
*/
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
var PopOver = exports.PopOver = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
var _modalContext$modalIn;
var children = _ref.children,
content = _ref.content,
open = _ref.open,
_ref$options = _ref.options,
options = _ref$options === void 0 ? {} : _ref$options,
zIndex = _ref.zIndex,
onOpen = _ref.onOpen,
onClose = _ref.onClose,
anchorRef = _ref.anchorRef,
_getAnchorRect = _ref.getAnchorRect,
props = _objectWithoutPropertiesLoose(_ref, _excluded);
var fallbackRef = _react.default.useRef(null);
var modalContext = (0, _modal.useModalContext)();
var defaultOptions = {
portal: true,
placement: 'bottom-start',
gutter: 4
};
var popover = (0, _popover.usePopoverState)(_extends({
open: open,
setOpen: function setOpen(o) {
if (!o && onClose) onClose();
if (o && onOpen) onOpen();
return o;
},
getAnchorRect: function getAnchorRect() {
var _fallbackRef$current;
if (_getAnchorRect) {
return _getAnchorRect();
}
if (anchorRef) {
var _anchorRef$current;
return ((_anchorRef$current = anchorRef.current) == null ? void 0 : _anchorRef$current.getBoundingClientRect()) || null;
}
var parentElement = (_fallbackRef$current = fallbackRef.current) == null ? void 0 : _fallbackRef$current.parentElement;
if (parentElement) {
return parentElement.getBoundingClientRect();
}
return null;
}
}, defaultOptions, options));
var popoverDisoclureProps = (0, _popover.usePopoverDisclosure)(_extends({
state: popover
}, children == null ? void 0 : children.props));
_react.default.useImperativeHandle(ref, function () {
return popover == null ? void 0 : popover.disclosureRef;
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [children ? (/*#__PURE__*/_react.default.cloneElement(children, _extends({}, popoverDisoclureProps, {
ref: popover == null ? void 0 : popover.disclosureRef
}))) : /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
ref: fallbackRef,
style: {
position: 'fixed'
}
}), popover.open && /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.PopoverStyled, _extends({
className: "popover-content",
state: popover,
$zIndex: zIndex,
portalElement: (_modalContext$modalIn = modalContext.modalInnerRef) == null ? void 0 : _modalContext$modalIn.current
}, props, {
children: /*#__PURE__*/_react.default.cloneElement(content, _extends({}, content == null ? void 0 : content.props, {
popover: popover
}))
}))]
});
});
PopOver.defaultProps = {};