@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
125 lines • 7.53 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 index_1 = require("../../index");
/**
* 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, _c;
var { children, portalClassName, preventTopPosition, className = "", usePlaceholder = false, paddingSize, content } = _a, otherPopoverProps = __rest(_a, ["children", "portalClassName", "preventTopPosition", "className", "usePlaceholder", "paddingSize", "content"]);
const placeholderRef = react_1.default.useRef(null);
const eventMemory = react_1.default.useRef(undefined);
const swapDelay = react_1.default.useRef(null);
const interactionKind = react_1.default.useRef((_b = otherPopoverProps.interactionKind) !== null && _b !== void 0 ? _b : core_1.PopoverInteractionKind.CLICK);
const swapDelayTime = 15;
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);
const swap = (ev) => {
const waitForClick = interactionKind.current === core_1.PopoverInteractionKind.CLICK ||
interactionKind.current === core_1.PopoverInteractionKind.CLICK_TARGET_ONLY;
if (swapDelay.current) {
clearTimeout(swapDelay.current);
}
const replacePlaceholder = () => {
eventMemory.current = ev.type;
setPlaceholder(false);
};
if (waitForClick) {
ev.stopImmediatePropagation();
replacePlaceholder();
return;
}
swapDelay.current = setTimeout(replacePlaceholder,
// we delay the swap for hover/focus to prevent unwanted effects
// (e.g. event hickup after replacing elements when it is not really necessary)
swapDelayTime);
};
react_1.default.useEffect(() => {
var _a;
interactionKind.current = (_a = otherPopoverProps.interactionKind) !== null && _a !== void 0 ? _a : core_1.PopoverInteractionKind.CLICK;
const waitForClick = interactionKind.current === core_1.PopoverInteractionKind.CLICK ||
interactionKind.current === core_1.PopoverInteractionKind.CLICK_TARGET_ONLY;
const removeEvents = () => {
if (placeholderRef.current) {
placeholderRef.current.removeEventListener("click", swap);
placeholderRef.current.removeEventListener("mouseenter", swap);
placeholderRef.current.removeEventListener("focusin", swap);
}
return;
};
if (placeholderRef.current) {
removeEvents(); // remove events in case of interaction kind changed during existence
if (waitForClick) {
placeholderRef.current.addEventListener("click", swap);
}
else {
placeholderRef.current.addEventListener("mouseenter", swap);
placeholderRef.current.addEventListener("focusin", swap);
}
return () => {
removeEvents();
};
}
return () => { };
}, [!!placeholderRef.current, otherPopoverProps.interactionKind]);
const refocus = react_1.default.useCallback((node) => {
const target = node === null || node === void 0 ? void 0 : node.targetRef.current.children[0];
if (!eventMemory.current || !target) {
return;
}
switch (eventMemory.current) {
case "focusin":
target.focus();
break;
case "click":
target.click();
break;
case "mouseenter":
// re-check if the cursor is still over the element after swapping the placeholder before triggering the event to bubble up
target.addEventListener("mouseover", () => target.dispatchEvent(new MouseEvent("mouseover", { bubbles: true })), {
capture: true,
once: true,
});
break;
}
}, []);
const targetClassName = `${index_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} ${index_1.CLASSPREFIX}-contextoverlay__wrapper--placeholder`, 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 ? `${index_1.CLASSPREFIX}-contextoverlay__portal--lowertop` : "") +
(portalClassName ? ` ${portalClassName}` : "");
return placeholder ? (displayPlaceholder()) : (react_1.default.createElement(core_1.Popover, Object.assign({ placement: "bottom", content: content ? (paddingSize ? (react_1.default.createElement(index_1.WhiteSpaceContainer, { paddingTop: paddingSize, paddingRight: paddingSize, paddingBottom: paddingSize, paddingLeft: paddingSize }, content)) : content) : undefined }, otherPopoverProps, { className: targetClassName, portalClassName: (_c = portalClassNameFinal.trim()) !== null && _c !== void 0 ? _c : undefined, ref: refocus }), children));
};
exports.ContextOverlay = ContextOverlay;
exports.default = exports.ContextOverlay;
//# sourceMappingURL=ContextOverlay.js.map