@braineet/ui
Version:
Braineet design system
87 lines (86 loc) • 3.79 kB
JavaScript
var _excluded = ["children", "content", "open", "options", "zIndex", "onOpen", "onClose", "anchorRef", "getAnchorRect"];
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; }
/* 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 } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { 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 = {};