@braineet/ui
Version:
Braineet design system
88 lines (86 loc) • 3.86 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"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/**
* The `PopOver` component.
*/
var 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
}))
}))]
});
});
exports.PopOver = PopOver;
PopOver.defaultProps = {};