UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

145 lines 8.26 kB
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, PopoverInteractionKind as InteractionKind, Utils as BlueprintUtils, } from "@blueprintjs/core"; import { CLASSPREFIX as eccgui, WhiteSpaceContainer } from "../../index.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, _c; var children = _a.children, portalClassName = _a.portalClassName, preventTopPosition = _a.preventTopPosition, _d = _a.className, className = _d === void 0 ? "" : _d, _e = _a.usePlaceholder, usePlaceholder = _e === void 0 ? false : _e, paddingSize = _a.paddingSize, content = _a.content, otherPopoverProps = __rest(_a, ["children", "portalClassName", "preventTopPosition", "className", "usePlaceholder", "paddingSize", "content"]); var placeholderRef = React.useRef(null); var eventMemory = React.useRef(undefined); var swapDelay = React.useRef(null); var interactionKind = React.useRef((_b = otherPopoverProps.interactionKind) !== null && _b !== void 0 ? _b : InteractionKind.CLICK); var swapDelayTime = 15; var _f = __read(React.useState( // use placeholder only for "simple" overlays without special states !otherPopoverProps.disabled && !otherPopoverProps.defaultIsOpen && !otherPopoverProps.isOpen && otherPopoverProps.renderTarget === undefined && usePlaceholder), 2), placeholder = _f[0], setPlaceholder = _f[1]; var swap = function (ev) { var waitForClick = interactionKind.current === InteractionKind.CLICK || interactionKind.current === InteractionKind.CLICK_TARGET_ONLY; if (swapDelay.current) { clearTimeout(swapDelay.current); } var replacePlaceholder = function () { 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.useEffect(function () { var _a; interactionKind.current = (_a = otherPopoverProps.interactionKind) !== null && _a !== void 0 ? _a : InteractionKind.CLICK; var waitForClick = interactionKind.current === InteractionKind.CLICK || interactionKind.current === InteractionKind.CLICK_TARGET_ONLY; var removeEvents = function () { 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 function () { removeEvents(); }; } return function () { }; }, [!!placeholderRef.current, otherPopoverProps.interactionKind]); var refocus = React.useCallback(function (node) { var 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", function () { return target.dispatchEvent(new MouseEvent("mouseover", { bubbles: true })); }, { capture: true, once: true, }); break; } }, []); 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", content: content ? (paddingSize ? (React.createElement(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)); }; export default ContextOverlay; //# sourceMappingURL=ContextOverlay.js.map