UNPKG

@eccenca/gui-elements

Version:

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

119 lines 7.42 kB
"use strict"; 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.Tooltip = void 0; const react_1 = __importDefault(require("react")); const core_1 = require("@blueprintjs/core"); const constants_1 = require("../../configuration/constants"); const Markdown_1 = require("./../../cmem/markdown/Markdown"); const Tooltip = (_a) => { var { children, content, className = "", size = "medium", addIndicator = false, markdownEnabler = "\n\n", markdownProps, usePlaceholder, swapPlaceholderDelay = 100, hoverOpenDelay = 450 } = _a, otherTooltipProps = __rest(_a, ["children", "content", "className", "size", "addIndicator", "markdownEnabler", "markdownProps", "usePlaceholder", "swapPlaceholderDelay", "hoverOpenDelay"]); const placeholderRef = react_1.default.useRef(null); const eventMemory = react_1.default.useRef(null); const searchId = react_1.default.useRef(null); const swapDelay = react_1.default.useRef(null); const swapDelayTime = swapPlaceholderDelay; const [placeholder, setPlaceholder] = react_1.default.useState(!otherTooltipProps.disabled && !otherTooltipProps.defaultIsOpen && !otherTooltipProps.isOpen && otherTooltipProps.renderTarget === undefined && swapDelayTime > 0 && hoverOpenDelay > swapDelayTime && (usePlaceholder === true || (typeof content === "string" && usePlaceholder !== false))); const targetClassName = `${constants_1.CLASSPREFIX}-tooltip__wrapper` + (className ? " " + className : "") + (addIndicator === true ? " " + core_1.Classes.TOOLTIP_INDICATOR : ""); react_1.default.useEffect(() => { if (placeholderRef.current !== null) { const swap = (ev) => { if (swapDelay.current) { clearTimeout(swapDelay.current); } swapDelay.current = setTimeout(() => { // we delay the swap to prevent unwanted effects // (e.g. forced mouseover after the swap but the cursor is already somewhere else) eventMemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover"; searchId.current = Date.now().toString(16) + Math.random().toString(16).slice(2); setPlaceholder(false); }, swapDelayTime); if (placeholderRef.current !== null) { const eventType = ev.type === "focusin" ? "focusout" : "mouseleave"; placeholderRef.current.addEventListener(eventType, () => { if ((eventType === "focusout" && eventMemory.current === "afterfocus") || (eventType === "mouseleave" && eventMemory.current === "afterhover")) { eventMemory.current = null; } clearTimeout(swapDelay.current); }); } }; 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) => { var _a; if (eventMemory.current && node) { // we do not have a `targetRef` here, so we need to workaround it // const target = node.targetRef.current.children[0]; const target = (_a = document.body.querySelector(`[data-postplaceholder=id${eventMemory.current}${searchId.current}]`)) === null || _a === void 0 ? void 0 : _a.children[0]; if (target) { switch (eventMemory.current) { case "afterfocus": target.focus(); break; case "afterhover": // 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 displayPlaceholder = () => { var _a, _b; const PlaceholderElement = (_a = otherTooltipProps === null || otherTooltipProps === void 0 ? void 0 : otherTooltipProps.targetTagName) !== null && _a !== void 0 ? _a : ((otherTooltipProps === null || otherTooltipProps === void 0 ? void 0 : otherTooltipProps.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({}, otherTooltipProps === null || otherTooltipProps === void 0 ? void 0 : otherTooltipProps.targetProps), { className: `${core_1.Classes.POPOVER_TARGET} ${targetClassName} ${constants_1.CLASSPREFIX}-tooltip__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 : "" + (otherTooltipProps.fill ? ` ${core_1.Classes.FILL}` : ""), tabIndex: 0 }))); }; let tooltipContent = content; if (typeof content === "string" && typeof markdownEnabler === "string" && new RegExp(markdownEnabler).test(content)) { tooltipContent = react_1.default.createElement(Markdown_1.Markdown, Object.assign({}, markdownProps), content); } return placeholder ? (displayPlaceholder()) : (react_1.default.createElement(core_1.Tooltip, Object.assign({ lazy: true, hoverOpenDelay: hoverOpenDelay }, otherTooltipProps, { content: tooltipContent, className: targetClassName, popoverClassName: `${constants_1.CLASSPREFIX}-tooltip__content` + ` ${constants_1.CLASSPREFIX}-tooltip--${size}` + (className ? " " + className + "__content" : ""), ref: refocus, targetProps: Object.assign(Object.assign({}, otherTooltipProps.targetProps), { "data-postplaceholder": eventMemory.current ? `id${eventMemory.current}${searchId.current}` : undefined }) }), children)); }; exports.Tooltip = Tooltip; exports.default = exports.Tooltip; //# sourceMappingURL=Tooltip.js.map