UNPKG

kwikid-components-react

Version:

KwikID's Component Library in React

170 lines (168 loc) 7.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _kwikidToolkit = require("kwikid-toolkit"); var _react = _interopRequireWildcard(require("react")); var _Dialog = _interopRequireDefault(require("../dialogs/Dialog")); var _Tooltip = require("./Tooltip.defaults"); var _Tooltip2 = require("./Tooltip.definition"); var _Tooltip3 = require("./Tooltip.style"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } // Define constants to avoid magic numbers const PERCENTAGE_MULTIPLIER = 100; const KwikUITooltip = _ref => { let { children = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.children, closeOnClickOutside = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.closeOnClickOutside, customStyles = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.customStyles, enlargeFactor = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.enlargeFactor, id = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.id, isExternalCheckCondition = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.isExternalCheckCondition, isInternalZoom = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.isInternalZoom, open = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.open, position = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.position, variant = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.variant, scalingFactor = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.scalingFactor, showCloseButton = _Tooltip.KWIKUI_TOOLTIP_DEFAULTS.showCloseButton } = _ref; const childRef = (0, _react.useRef)(null); const [tooltipPosition, setTooltipPosition] = (0, _react.useState)(position); const [isOpenTooltip, setIsOpenTooltip] = (0, _react.useState)(false); const [isOpenTooltipDialog, setIsOpenTooltipDialog] = (0, _react.useState)(false); const tooltipRef = (0, _react.useRef)(null); const refBasedChildren = _react.default.Children.map(children, child => { return /*#__PURE__*/_react.default.cloneElement(child, { ref: childRef, "aria-describedby": isOpenTooltip ? id : undefined }); }); const enhanchedChildren = _react.default.Children.map(children, child => { var _child$props; return /*#__PURE__*/_react.default.cloneElement(child, { style: { ...(((_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.style) || {}), zoom: enlargeFactor } }); }); const initialTooltipLensStyle = { width: "100%", height: "100%", position: "absolute", zIndex: 2, top: 0, left: 0, cursor: isInternalZoom ? "zoom-in" : "default" }; const [tooltipLensStyle, setTooltipLensStyle] = (0, _react.useState)(initialTooltipLensStyle); (0, _react.useEffect)(() => { const handleTooltipPosition = () => { var _tooltipRef$current; if ((0, _kwikidToolkit.isEmptyValue)(tooltipRef.current)) { return; } const tooltipRect = (_tooltipRef$current = tooltipRef.current) === null || _tooltipRef$current === void 0 ? void 0 : _tooltipRef$current.getBoundingClientRect(); const windowHeight = window.innerHeight; const windowWidth = window.innerWidth; if (!tooltipRect) { return; } if (tooltipRect.left < 0 && (tooltipRect.top < 0 || tooltipRect.bottom > windowHeight)) { setTooltipPosition(_Tooltip2.IKwikUITooltipPosition.RIGHT); } else if (tooltipRect.right > windowWidth && (tooltipRect.top < 0 || tooltipRect.bottom > windowHeight)) { setTooltipPosition(_Tooltip2.IKwikUITooltipPosition.LEFT); } else if (tooltipRect.top < 0 || tooltipRect.right > windowWidth && tooltipRect.left < 0 && tooltipRect.top < 0) { setTooltipPosition(_Tooltip2.IKwikUITooltipPosition.BOTTOM); } else if (tooltipRect.bottom > windowHeight || tooltipRect.right > windowWidth && tooltipRect.left < 0 && tooltipRect.bottom > windowHeight) { setTooltipPosition(_Tooltip2.IKwikUITooltipPosition.TOP); } }; handleTooltipPosition(); }, []); const handleOnClickClose = () => { setIsOpenTooltipDialog(false); }; const handleOnMouseEnter = () => { setIsOpenTooltip(true); setTooltipPosition(position); }; const handleOnMouseLeave = () => { setIsOpenTooltip(false); }; const handleOnInternalZoomOut = () => { setTooltipLensStyle(initialTooltipLensStyle); }; const handleOnClickTooltipDialog = () => { if (!isInternalZoom) { return; } setIsOpenTooltipDialog(true); }; const handleOnMouseMove = e => { var _refBasedChildren$, _refBasedChildren$$re; if (!isInternalZoom || !scalingFactor) { return; } const imgElement = (_refBasedChildren$ = refBasedChildren[0]) === null || _refBasedChildren$ === void 0 ? void 0 : (_refBasedChildren$$re = _refBasedChildren$.ref) === null || _refBasedChildren$$re === void 0 ? void 0 : _refBasedChildren$$re.current; const imgSrc = imgElement === null || imgElement === void 0 ? void 0 : imgElement.src; const imgHeight = imgElement === null || imgElement === void 0 ? void 0 : imgElement.height; const { left, top, width, height } = e.target.getBoundingClientRect(); const x = (e.pageX - left) / width * PERCENTAGE_MULTIPLIER; const y = (e.pageY - top) / height * PERCENTAGE_MULTIPLIER; setTooltipLensStyle(prevStyle => ({ ...prevStyle, cursor: "zoom-in", backgroundImage: imgSrc ? `url(${imgSrc})` : "none", backgroundPosition: `${x}% ${y}%`, backgroundSize: `auto ${imgHeight * scalingFactor}px`, backgroundRepeat: "no-repeat" })); }; // Ensure customStyles is never undefined const safeCustomStyles = customStyles || {}; return /*#__PURE__*/_react.default.createElement(_Tooltip3.KwikUIStyleTooltipParentContainer, { onMouseLeave: handleOnMouseLeave, style: safeCustomStyles.container }, /*#__PURE__*/_react.default.createElement("div", { onMouseOver: handleOnMouseEnter, onMouseOut: handleOnMouseLeave, onFocus: handleOnMouseEnter, onBlur: handleOnMouseLeave, style: safeCustomStyles.childWrapper }, refBasedChildren), open && isOpenTooltip && isExternalCheckCondition && /*#__PURE__*/_react.default.createElement(_Tooltip3.KwikUIStyleTooltipContainer, { id: id, ref: tooltipRef, onMouseEnter: handleOnMouseEnter, onMouseLeave: handleOnMouseLeave, position: tooltipPosition, style: safeCustomStyles.tooltipContainerWrapper, role: "tooltip", "aria-hidden": !isOpenTooltip }, /*#__PURE__*/_react.default.createElement(_Tooltip3.KwikUIStyleTooltipChildren, { onMouseMove: handleOnMouseMove, onMouseLeave: handleOnInternalZoomOut, style: safeCustomStyles.tooltipWrapper }, enhanchedChildren, isInternalZoom && /*#__PURE__*/_react.default.createElement("div", { onClick: handleOnClickTooltipDialog, style: tooltipLensStyle, "aria-label": "Click to zoom", role: "button", tabIndex: 0 }))), /*#__PURE__*/_react.default.createElement(_Dialog.default, { closeOnClickOutside: closeOnClickOutside, onClose: handleOnClickClose, open: isOpenTooltipDialog, showCloseButton: showCloseButton, variant: variant }, enhanchedChildren)); }; var _default = exports.default = KwikUITooltip;