react-elegant-ui
Version:
Elegant UI components, made by BEM best practices for react
119 lines • 4.33 kB
JavaScript
var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var __read = this && this.__read || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
} catch (error) {
e = {
error: error
};
} finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
};
var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useRef } from 'react';
import { createPortal } from 'react-dom';
import { useComponentRegistry } from '../../lib/di';
import { cn } from '@bem-react/classname';
import { canUseDOM } from '../../lib/canUseDOM';
import { useRefMix } from '../../hooks/useRefMix';
import { LayerManager } from '../LayerManager/LayerManager';
import './Popup.css';
export var cnPopup = cn('Popup');
/**
* Component to make pop-up windows
* @param {IPopupProps} props
*/
export var Popup = function (_a) {
var _b;
var visible = _a.visible,
keepMounted = _a.keepMounted,
scope = _a.scope,
hasTail = _a.hasTail,
tailRef = _a.tailRef,
zIndex = _a.zIndex,
style = _a.style,
className = _a.className,
innerRef = _a.innerRef,
children = _a.children,
addonAfter = _a.addonAfter,
addonBefore = _a.addonBefore,
onClick = _a.onClick,
onClose = _a.onClose,
UNSTABLE_onRenderTail = _a.UNSTABLE_onRenderTail,
_c = _a.essentialRefs,
essentialRefs = _c === void 0 ? [] : _c,
propsHostRef = _a.hostRef,
props = __rest(_a, ["visible", "keepMounted", "scope", "hasTail", "tailRef", "zIndex", "style", "className", "innerRef", "children", "addonAfter", "addonBefore", "onClick", "onClose", "UNSTABLE_onRenderTail", "essentialRefs", "hostRef"]);
var Tail = useComponentRegistry(cnPopup()).Tail;
var containerRef = useRef(null);
var hostRef = propsHostRef || containerRef;
var containerRefMix = useRefMix(containerRef, innerRef);
var scopeRef = (_b = scope === null || scope === void 0 ? void 0 : scope.current) !== null && _b !== void 0 ? _b : null;
// skip render on SSR
if (scopeRef !== null && !canUseDOM()) {
return null;
}
// skip render non visible component
if (!visible && !keepMounted) {
return null;
}
var renderedComponent = /*#__PURE__*/React.createElement(LayerManager, {
visible: visible,
onClose: onClose,
essentialRefs: __spreadArray([hostRef], __read(essentialRefs), false)
}, /*#__PURE__*/React.createElement("div", __assign({}, props, {
className: cnPopup({
visible: visible
}, [className]),
ref: containerRefMix,
style: __assign(__assign({}, style), {
zIndex: zIndex
}),
onClick: onClick
}), addonBefore, typeof children === 'function' ? children({
tailRef: tailRef
}) : children, addonAfter, UNSTABLE_onRenderTail && UNSTABLE_onRenderTail( /*#__PURE__*/React.createElement(Tail, {
innerRef: tailRef
})), !UNSTABLE_onRenderTail && hasTail && ( /*#__PURE__*/React.createElement(Tail, {
innerRef: tailRef
}))));
return scopeRef !== null ? /*#__PURE__*/createPortal(renderedComponent, scopeRef) : renderedComponent;
};
Popup.displayName = cnPopup();