@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
107 lines • 6.19 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;
};
import React from "react";
import { Classes as BlueprintClasses, Popover as BlueprintPopover, Utils as BlueprintUtils, } from "@blueprintjs/core";
import { CLASSPREFIX as eccgui } from "../../configuration/constants.js";
/**
* 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
*/
export var ContextOverlay = function (_a) {
var _b;
var children = _a.children, portalClassName = _a.portalClassName, preventTopPosition = _a.preventTopPosition, _c = _a.className, className = _c === void 0 ? "" : _c, _d = _a.usePlaceholder, usePlaceholder = _d === void 0 ? false : _d, otherPopoverProps = __rest(_a, ["children", "portalClassName", "preventTopPosition", "className", "usePlaceholder"]);
var placeholderRef = React.useRef(null);
var eventMemory = React.useRef(undefined);
var swapDelay = React.useRef(null);
var swapDelayTime = 15;
var _e = __read(React.useState(
// use placeholder only for "simple" overlays without special states
!otherPopoverProps.disabled &&
!otherPopoverProps.defaultIsOpen &&
!otherPopoverProps.isOpen &&
otherPopoverProps.renderTarget === undefined &&
usePlaceholder), 2), placeholder = _e[0], setPlaceholder = _e[1];
React.useEffect(function () {
if (placeholderRef.current) {
var swap_1 = function (ev) {
if (swapDelay.current) {
clearTimeout(swapDelay.current);
}
swapDelay.current = setTimeout(function () {
// we delay the swap to prevent unwanted effects
// (e.g. event hickup after replacing elements when it is not really necessary)
eventMemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
setPlaceholder(false);
}, swapDelayTime);
};
placeholderRef.current.addEventListener("mouseenter", swap_1);
placeholderRef.current.addEventListener("focusin", swap_1);
return function () {
if (placeholderRef.current) {
placeholderRef.current.removeEventListener("mouseenter", swap_1);
placeholderRef.current.removeEventListener("focusin", swap_1);
}
};
}
return function () { };
}, [!!placeholderRef.current]);
var refocus = React.useCallback(function (node) {
if (eventMemory.current === "afterfocus" && node) {
var target = node.targetRef.current.children[0];
if (target) {
target.focus();
}
}
}, []);
var targetClassName = "".concat(eccgui, "-contextoverlay") + (className ? " ".concat(className) : "");
var displayPlaceholder = function () {
var _a, _b, _c;
var 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");
var childTarget = BlueprintUtils.ensureElement(React.Children.toArray(children)[0]);
if (!childTarget) {
return null;
}
return React.createElement(PlaceholderElement, __assign(__assign({}, otherPopoverProps === null || otherPopoverProps === void 0 ? void 0 : otherPopoverProps.targetProps), { className: "".concat(BlueprintClasses.POPOVER_TARGET, " ").concat(targetClassName, " ").concat(eccgui, "-contextoverlay__wrapper--placeholder"), ref: placeholderRef }), React.cloneElement(childTarget, __assign(__assign({}, childTarget.props), { className: (_b = childTarget.props.className) !== null && _b !== void 0 ? _b : "" + (otherPopoverProps.fill ? " ".concat(BlueprintClasses.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) })));
};
var portalClassNameFinal = (preventTopPosition ? "".concat(eccgui, "-contextoverlay__portal--lowertop") : "") +
(portalClassName ? " ".concat(portalClassName) : "");
return placeholder ? (displayPlaceholder()) : (React.createElement(BlueprintPopover, __assign({ placement: "bottom" }, otherPopoverProps, { className: targetClassName, portalClassName: (_b = portalClassNameFinal.trim()) !== null && _b !== void 0 ? _b : undefined, ref: refocus }), children));
};
export default ContextOverlay;
//# sourceMappingURL=ContextOverlay.js.map