{"version":3,"file":"Tooltip.mjs","sources":["../../../src/lib/components/Tooltip/Tooltip.jsx"],"sourcesContent":["/*\nCopyright 2022 Iguazio Systems Ltd.\nLicensed under the Apache License, Version 2.0 (the \"License\") with\nan addition restriction as set forth herein. You may not use this\nfile except in compliance with the License. You may obtain a copy of\nthe License at http://www.apache.org/licenses/LICENSE-2.0.\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\nimplied. See the License for the specific language governing\npermissions and limitations under the License.\nIn addition, you may not use the software for any purposes that are\nillegal under applicable law, and the grant of the foregoing license\nunder the Apache 2.0 license is conditioned upon your compliance with\nsuch restriction.\n*/\nimport React, { useRef, useState, useEffect, useCallback } from 'react'\nimport { createPortal } from 'react-dom'\nimport PropTypes from 'prop-types'\nimport { CSSTransition } from 'react-transition-group'\nimport classnames from 'classnames'\nimport { debounce } from 'lodash'\n\nimport { isEveryObjectValueEmpty } from '../../utils/common.util'\n\nimport './tooltip.scss'\n\nlet Tooltip = ({\n  children = '',\n  className = '',\n  tooltipBodyClassName = '',\n  hidden = false,\n  id = '',\n  renderChildAsHtml = false,\n  template,\n  textShow = false\n}) => {\n  const [show, setShow] = useState(false)\n  const [style, setStyle] = useState({})\n\n  const tooltipWrapperClassNames = classnames('data-ellipsis', 'tooltip-wrapper', className)\n  const tooltipBodyClassNames = classnames('tooltip', tooltipBodyClassName)\n  const duration = 200\n  const parentRef = useRef()\n  const tooltipRef = useRef()\n  const offset = 10\n\n  const handleScroll = () => {\n    setShow(false)\n  }\n\n  const handleMouseLeave = useCallback(\n    event => {\n      if (\n        !tooltipRef.current ||\n        hidden ||\n        (tooltipRef.current &&\n          !tooltipRef.current.contains(event.relatedTarget) &&\n          parentRef.current &&\n          !parentRef.current.contains(event.relatedTarget))\n      ) {\n        setShow(false)\n      }\n    },\n    [hidden]\n  )\n\n  const handleMouseEnter = useCallback(\n    event => {\n      if (!show) {\n        const [child] = parentRef.current.childNodes\n        let show =\n          !hidden &&\n          (textShow\n            ? true\n            : !child\n              ? false\n              : (child.nodeType !== Node.TEXT_NODE &&\n                  child.childNodes?.[0]?.nodeType !== Node.TEXT_NODE) ||\n                /*\n                                    If the child node is a text node and the text of the child node inside the container is greater than the width of the container, then show tooltip.\n                                  */\n                ((child.nodeType === Node.TEXT_NODE ||\n                  child.childNodes?.[0]?.nodeType === Node.TEXT_NODE) &&\n                  parentRef.current.scrollWidth > parentRef.current.offsetWidth))\n\n        setShow(show)\n\n        setTimeout(() => {\n          if (show) {\n            let { height, top, bottom } = parentRef?.current?.getBoundingClientRect() ?? {}\n            const { height: tooltipHeight, width: tooltipWidth } =\n              tooltipRef.current?.getBoundingClientRect() ?? {\n                height: 0,\n                width: 0\n              }\n            const leftPosition = event.x - (event.x + tooltipWidth - window.innerWidth + offset)\n            const left =\n              event.x + tooltipWidth + offset > window.innerWidth\n                ? leftPosition > offset\n                  ? leftPosition\n                  : offset\n                : event.x + offset\n\n            if (top + height + offset + tooltipHeight >= window.innerHeight) {\n              const topPosition = bottom - height - offset - tooltipHeight\n\n              setStyle({\n                top: topPosition > 0 ? topPosition : offset,\n                left\n              })\n            } else {\n              setStyle({\n                top: top + height + offset,\n                left\n              })\n            }\n          }\n        }, 0)\n      }\n    },\n    [hidden, textShow, show]\n  )\n\n  const clearStyles = debounce(() => {\n    if (!isEveryObjectValueEmpty(style)) {\n      setStyle({})\n    }\n  }, 100)\n\n  useEffect(() => {\n    const parentNode = parentRef.current\n\n    if (parentNode) {\n      parentNode.addEventListener('mouseenter', handleMouseEnter)\n      parentNode.addEventListener('mouseleave', handleMouseLeave)\n\n      return () => {\n        parentNode.removeEventListener('mouseenter', handleMouseEnter)\n        parentNode.removeEventListener('mouseleave', handleMouseLeave)\n      }\n    }\n  }, [parentRef, handleMouseEnter, handleMouseLeave])\n\n  useEffect(() => {\n    const tooltipNode = tooltipRef.current\n\n    if (tooltipNode && show) {\n      tooltipNode.addEventListener('mouseleave', handleMouseLeave)\n\n      return () => {\n        tooltipNode.removeEventListener('mouseleave', handleMouseLeave)\n      }\n    }\n  }, [tooltipRef, handleMouseEnter, handleMouseLeave, show])\n\n  useEffect(() => {\n    if (show) {\n      window.addEventListener('scroll', handleScroll, true)\n    }\n\n    return () => window.removeEventListener('scroll', handleScroll, true)\n  }, [show])\n\n  useEffect(() => {\n    window.addEventListener('resize', clearStyles)\n\n    return () => {\n      window.removeEventListener('resize', clearStyles)\n    }\n  }, [clearStyles, style])\n\n  return (\n    <>\n      {renderChildAsHtml ? (\n        <div\n          data-testid={id ? `${id}-tooltip-wrapper` : 'tooltip-wrapper'}\n          ref={parentRef}\n          className={tooltipWrapperClassNames}\n          dangerouslySetInnerHTML={{ __html: children }}\n          onClick={handleMouseLeave}\n        />\n      ) : (\n        <div\n          data-testid={id ? `${id}-tooltip-wrapper` : 'tooltip-wrapper'}\n          ref={parentRef}\n          className={tooltipWrapperClassNames}\n          onClick={handleMouseLeave}\n        >\n          {children}\n        </div>\n      )}\n      {!hidden &&\n        createPortal(\n          <CSSTransition\n            nodeRef={tooltipRef}\n            classNames=\"fade\"\n            in={show}\n            timeout={duration}\n            unmountOnExit\n          >\n            <div\n              data-testid={id ? `${id}-tooltip` : 'tooltip'}\n              ref={tooltipRef}\n              style={{\n                ...style\n              }}\n              className={tooltipBodyClassNames}\n            >\n              {template}\n            </div>\n          </CSSTransition>,\n          document.getElementById('overlay_container')\n        )}\n    </>\n  )\n}\n\nTooltip.propTypes = {\n  children: PropTypes.any,\n  className: PropTypes.string,\n  tooltipBodyClassName: PropTypes.string,\n  hidden: PropTypes.bool,\n  id: PropTypes.string,\n  renderChildAsHtml: PropTypes.bool,\n  template: PropTypes.element.isRequired,\n  textShow: PropTypes.bool\n}\n\nTooltip = React.memo(Tooltip)\n\nexport default Tooltip\n"],"names":["Tooltip","children","className","tooltipBodyClassName","hidden","id","renderChildAsHtml","template","textShow","show","setShow","useState","style","setStyle","tooltipWrapperClassNames","classnames","tooltipBodyClassNames","duration","parentRef","useRef","tooltipRef","offset","handleScroll","handleMouseLeave","useCallback","event","handleMouseEnter","child","_b","_a","_d","_c","height","top","bottom","tooltipHeight","tooltipWidth","leftPosition","left","topPosition","clearStyles","debounce","isEveryObjectValueEmpty","useEffect","parentNode","tooltipNode","jsxs","Fragment","jsx","createPortal","CSSTransition","PropTypes","React"],"mappings":";;;;;;;;;AA2BA,IAAIA,IAAU,CAAC;AAAA,EACb,UAAAC,IAAW;AAAA,EACX,WAAAC,IAAY;AAAA,EACZ,sBAAAC,IAAuB;AAAA,EACvB,QAAAC,IAAS;AAAA,EACT,IAAAC,IAAK;AAAA,EACL,mBAAAC,IAAoB;AAAA,EACpB,UAAAC;AAAA,EACA,UAAAC,IAAW;AACb,MAAM;AACJ,QAAM,CAACC,GAAMC,CAAO,IAAIC,EAAS,EAAK,GAChC,CAACC,GAAOC,CAAQ,IAAIF,EAAS,CAAA,CAAE,GAE/BG,IAA2BC,EAAW,iBAAiB,mBAAmBb,CAAS,GACnFc,IAAwBD,EAAW,WAAWZ,CAAoB,GAClEc,IAAW,KACXC,IAAYC,EAAA,GACZC,IAAaD,EAAA,GACbE,IAAS,IAETC,IAAe,MAAM;AACzB,IAAAZ,EAAQ,EAAK;AAAA,EACf,GAEMa,IAAmBC;AAAA,IACvB,CAAAC,MAAS;AACP,OACE,CAACL,EAAW,WACZhB,KACCgB,EAAW,WACV,CAACA,EAAW,QAAQ,SAASK,EAAM,aAAa,KAChDP,EAAU,WACV,CAACA,EAAU,QAAQ,SAASO,EAAM,aAAa,MAEjDf,EAAQ,EAAK;AAAA,IAEjB;AAAA,IACA,CAACN,CAAM;AAAA,EAAA,GAGHsB,IAAmBF;AAAA,IACvB,CAAAC,MAAS;;AACP,UAAI,CAAChB,GAAM;AACT,cAAM,CAACkB,CAAK,IAAIT,EAAU,QAAQ;AAClC,YAAIT,IACF,CAACL,MACAI,IACG,KACCmB,IAEEA,EAAM,aAAa,KAAK,eACvBC,KAA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