@braineet/ui
Version:
Braineet design system
85 lines (84 loc) • 3.55 kB
JavaScript
var _excluded = ["children", "content", "open", "options", "zIndex", "onOpen", "onClose", "anchorRef", "getAnchorRect"];
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; }
/* eslint-disable no-plusplus */
/* eslint-disable no-underscore-dangle */
/* eslint-disable jsx-a11y/mouse-events-have-key-events */
import React from 'react';
import { usePopoverDisclosure, usePopoverState } from 'ariakit/popover';
import { useModalContext } from '../modal';
import { PopoverStyled } from './styles';
/**
* The `PopOver` component.
*/
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
export var PopOver = /*#__PURE__*/React.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.useRef(null);
var modalContext = useModalContext();
var defaultOptions = {
portal: true,
placement: 'bottom-start',
gutter: 4
};
var 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 = usePopoverDisclosure(_extends({
state: popover
}, children == null ? void 0 : children.props));
React.useImperativeHandle(ref, function () {
return popover == null ? void 0 : popover.disclosureRef;
});
return /*#__PURE__*/_jsxs(_Fragment, {
children: [children ? (/*#__PURE__*/React.cloneElement(children, _extends({}, popoverDisoclureProps, {
ref: popover == null ? void 0 : popover.disclosureRef
}))) : /*#__PURE__*/_jsx("span", {
ref: fallbackRef,
style: {
position: 'fixed'
}
}), popover.open && /*#__PURE__*/_jsx(PopoverStyled, _extends({
className: "popover-content",
state: popover,
$zIndex: zIndex,
portalElement: (_modalContext$modalIn = modalContext.modalInnerRef) == null ? void 0 : _modalContext$modalIn.current
}, props, {
children: /*#__PURE__*/React.cloneElement(content, _extends({}, content == null ? void 0 : content.props, {
popover: popover
}))
}))]
});
});
PopOver.defaultProps = {};