@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
78 lines • 4.92 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextOverlay = void 0;
const react_1 = __importDefault(require("react"));
const core_1 = require("@blueprintjs/core");
const constants_1 = require("../../configuration/constants");
/**
* Element displays connected content by interacting with a target element.
* Full list of available option can be seen at https://blueprintjs.com/docs/#popover2-package/popover2
*/
const ContextOverlay = (_a) => {
var _b;
var { children, portalClassName, preventTopPosition, className = "", usePlaceholder = false } = _a, otherPopoverProps = __rest(_a, ["children", "portalClassName", "preventTopPosition", "className", "usePlaceholder"]);
const placeholderRef = react_1.default.useRef(null);
const eventMemory = react_1.default.useRef(undefined);
const [placeholder, setPlaceholder] = react_1.default.useState(
// use placeholder only for "simple" overlays without special states
!otherPopoverProps.disabled &&
!otherPopoverProps.defaultIsOpen &&
!otherPopoverProps.isOpen &&
otherPopoverProps.renderTarget === undefined &&
usePlaceholder);
react_1.default.useEffect(() => {
if (placeholderRef.current) {
const swap = (ev) => {
eventMemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
setPlaceholder(false);
};
placeholderRef.current.addEventListener("mouseenter", swap);
placeholderRef.current.addEventListener("focusin", swap);
return () => {
if (placeholderRef.current) {
placeholderRef.current.removeEventListener("mouseenter", swap);
placeholderRef.current.removeEventListener("focusin", swap);
}
};
}
return () => { };
}, [!!placeholderRef.current]);
const refocus = react_1.default.useCallback((node) => {
if (eventMemory.current === "afterfocus" && node) {
const target = node.targetRef.current.children[0];
if (target) {
target.focus();
}
}
}, []);
const targetClassName = `${constants_1.CLASSPREFIX}-contextoverlay` + (className ? ` ${className}` : "");
const displayPlaceholder = () => {
var _a, _b, _c;
const PlaceholderElement = (_a = otherPopoverProps === null || otherPopoverProps === void 0 ? void 0 : otherPopoverProps.targetTagName) !== null && _a !== void 0 ? _a : ((otherPopoverProps === null || otherPopoverProps === void 0 ? void 0 : otherPopoverProps.fill) ? "div" : "span");
const childTarget = core_1.Utils.ensureElement(react_1.default.Children.toArray(children)[0]);
if (!childTarget) {
return null;
}
return react_1.default.createElement(PlaceholderElement, Object.assign(Object.assign({}, otherPopoverProps === null || otherPopoverProps === void 0 ? void 0 : otherPopoverProps.targetProps), { className: `${core_1.Classes.POPOVER_TARGET} ${targetClassName}`, ref: placeholderRef }), react_1.default.cloneElement(childTarget, Object.assign(Object.assign({}, childTarget.props), { className: (_b = childTarget.props.className) !== null && _b !== void 0 ? _b : "" + (otherPopoverProps.fill ? ` ${core_1.Classes.FILL}` : ""), tabIndex: (_c = childTarget.props.tabIndex) !== null && _c !== void 0 ? _c : (!(otherPopoverProps === null || otherPopoverProps === void 0 ? void 0 : otherPopoverProps.disabled) && (otherPopoverProps === null || otherPopoverProps === void 0 ? void 0 : otherPopoverProps.openOnTargetFocus) ? 0 : undefined) })));
};
const portalClassNameFinal = (preventTopPosition ? `${constants_1.CLASSPREFIX}-contextoverlay__portal--lowertop` : "") +
(portalClassName ? ` ${portalClassName}` : "");
return placeholder ? (displayPlaceholder()) : (react_1.default.createElement(core_1.Popover, Object.assign({ placement: "bottom" }, otherPopoverProps, { className: targetClassName, portalClassName: (_b = portalClassNameFinal.trim()) !== null && _b !== void 0 ? _b : undefined, ref: refocus }), children));
};
exports.ContextOverlay = ContextOverlay;
exports.default = exports.ContextOverlay;
//# sourceMappingURL=ContextOverlay.js.map