UNPKG

@eccenca/gui-elements

Version:

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

139 lines 8.39 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, Tooltip as BlueprintTooltip, Utils as BlueprintUtils, } from "@blueprintjs/core"; import { CLASSPREFIX as eccgui } from "../../configuration/constants.js"; import { Markdown } from "./../../cmem/markdown/Markdown.js"; export var Tooltip = function (_a) { var children = _a.children, content = _a.content, _b = _a.className, className = _b === void 0 ? "" : _b, _c = _a.size, size = _c === void 0 ? "medium" : _c, _d = _a.addIndicator, addIndicator = _d === void 0 ? false : _d, _e = _a.markdownEnabler, markdownEnabler = _e === void 0 ? "\n\n" : _e, markdownProps = _a.markdownProps, usePlaceholder = _a.usePlaceholder, _f = _a.swapPlaceholderDelay, swapPlaceholderDelay = _f === void 0 ? 100 : _f, _g = _a.hoverOpenDelay, hoverOpenDelay = _g === void 0 ? 450 : _g, otherTooltipProps = __rest(_a, ["children", "content", "className", "size", "addIndicator", "markdownEnabler", "markdownProps", "usePlaceholder", "swapPlaceholderDelay", "hoverOpenDelay"]); var placeholderRef = React.useRef(null); var eventMemory = React.useRef(null); var searchId = React.useRef(null); var swapDelay = React.useRef(null); var swapDelayTime = swapPlaceholderDelay; var _h = __read(React.useState(!otherTooltipProps.disabled && !otherTooltipProps.defaultIsOpen && !otherTooltipProps.isOpen && otherTooltipProps.renderTarget === undefined && swapDelayTime > 0 && hoverOpenDelay > swapDelayTime && (usePlaceholder === true || (typeof content === "string" && usePlaceholder !== false))), 2), placeholder = _h[0], setPlaceholder = _h[1]; var targetClassName = "".concat(eccgui, "-tooltip__wrapper") + (className ? " " + className : "") + (addIndicator === true ? " " + BlueprintClasses.TOOLTIP_INDICATOR : ""); React.useEffect(function () { if (placeholderRef.current !== null) { 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. 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) { var eventType_1 = ev.type === "focusin" ? "focusout" : "mouseleave"; placeholderRef.current.addEventListener(eventType_1, function () { if ((eventType_1 === "focusout" && eventMemory.current === "afterfocus") || (eventType_1 === "mouseleave" && eventMemory.current === "afterhover")) { eventMemory.current = null; } clearTimeout(swapDelay.current); }); } }; 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) { 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]; var target_1 = (_a = document.body.querySelector("[data-postplaceholder=id".concat(eventMemory.current).concat(searchId.current, "]"))) === null || _a === void 0 ? void 0 : _a.children[0]; if (target_1) { switch (eventMemory.current) { case "afterfocus": target_1.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_1.addEventListener("mouseover", function () { return target_1.dispatchEvent(new MouseEvent("mouseover", { bubbles: true })); }, { capture: true, once: true, }); break; } } } }, []); var displayPlaceholder = function () { var _a, _b; var 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"); var childTarget = BlueprintUtils.ensureElement(React.Children.toArray(children)[0]); if (!childTarget) { return null; } return React.createElement(PlaceholderElement, __assign(__assign({}, otherTooltipProps === null || otherTooltipProps === void 0 ? void 0 : otherTooltipProps.targetProps), { className: "".concat(BlueprintClasses.POPOVER_TARGET, " ").concat(targetClassName, " ").concat(eccgui, "-tooltip__wrapper--placeholder"), ref: placeholderRef }), React.cloneElement(childTarget, __assign(__assign({}, childTarget.props), { className: (_b = childTarget.props.className) !== null && _b !== void 0 ? _b : "" + (otherTooltipProps.fill ? " ".concat(BlueprintClasses.FILL) : ""), tabIndex: 0 }))); }; var tooltipContent = content; if (typeof content === "string" && typeof markdownEnabler === "string" && new RegExp(markdownEnabler).test(content)) { tooltipContent = React.createElement(Markdown, __assign({}, markdownProps), content); } return placeholder ? (displayPlaceholder()) : (React.createElement(BlueprintTooltip, __assign({ lazy: true, hoverOpenDelay: hoverOpenDelay }, otherTooltipProps, { content: tooltipContent, className: targetClassName, popoverClassName: "".concat(eccgui, "-tooltip__content") + " ".concat(eccgui, "-tooltip--").concat(size) + (className ? " " + className + "__content" : ""), ref: refocus, targetProps: __assign(__assign({}, otherTooltipProps.targetProps), { "data-postplaceholder": eventMemory.current && searchId.current ? "id".concat(eventMemory.current).concat(searchId.current) : undefined }) }), children)); }; export default Tooltip; //# sourceMappingURL=Tooltip.js.map