{"version":3,"file":"Tip.mjs","sources":["../../../src/lib/components/Tip/Tip.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, { useState, useRef, useEffect, useCallback, useMemo } from 'react'\nimport PropTypes from 'prop-types'\nimport { CSSTransition } from 'react-transition-group'\nimport classnames from 'classnames'\nimport { createPortal } from 'react-dom'\n\nimport { getScssVariableValue } from '../../utils/common.util'\n\nimport QuestionMarkIcon from '../../images/question-mark.svg?react'\nimport ExclamationMarkIcon from '../../images/exclamation-mark.svg?react'\n\nimport './tip.scss'\n\nconst Tip = ({ className = '', text, withExclamationMark = false }) => {\n  const [isShow, setIsShow] = useState(false)\n  const [tipClassName, setTipClassName] = useState('tip_top tip_left')\n\n  const arrowLength = useMemo(() => parseInt(getScssVariableValue('--tipArrowLength')), [])\n  const arrowOffset = useMemo(() => parseInt(getScssVariableValue('--tipArrowOffset')), [])\n  const iconLength = useMemo(() => parseInt(getScssVariableValue('--tipIconLength')), [])\n\n  const minTextLength = 40\n\n  const iconRef = useRef()\n  const tipBodyRef = useRef()\n\n  const tipContainerClassNames = classnames(className, 'tip-container')\n  const tipClassNames = classnames(\n    'tip',\n    tipClassName,\n    text.length <= minTextLength ? 'tip_small' : 'tip_big'\n  )\n\n  const handleMouseEnter = useCallback(() => {\n    setIsShow(true)\n  }, [])\n\n  useEffect(() => {\n    if (isShow) {\n      const iconRect = iconRef.current.getBoundingClientRect()\n      const tipRect = tipBodyRef.current.getBoundingClientRect()\n      const widthPosition = iconRect.left > tipRect.width - arrowOffset ? 'tip_left' : 'tip_right'\n      const heightPosition = iconRect.top > tipRect.height + arrowLength ? 'tip_top' : 'tip_bottom'\n\n      setTipClassName(`${heightPosition} ${widthPosition}`)\n\n      if (widthPosition === 'tip_left') {\n        const computedArrowOffset = arrowOffset + (iconLength + arrowLength) / 2\n        tipBodyRef.current.style.left = `${iconRect.left - (tipRect.width - computedArrowOffset)}px`\n      } else {\n        const computedArrowOffset = arrowOffset - (iconLength - arrowLength) / 2\n        tipBodyRef.current.style.left = `${iconRect.left - computedArrowOffset}px`\n      }\n\n      tipBodyRef.current.style.top =\n        heightPosition === 'tip_top'\n          ? `${iconRect.top - tipRect.height - arrowLength}px`\n          : `${iconRect.bottom + arrowLength}px`\n    }\n  }, [arrowLength, arrowOffset, iconLength, isShow])\n\n  const handleMouseLeave = () => {\n    setIsShow(false)\n  }\n\n  useEffect(() => {\n    const node = iconRef.current\n\n    if (iconRef.current) {\n      node.addEventListener('mouseenter', handleMouseEnter)\n      node.addEventListener('mouseleave', handleMouseLeave)\n\n      return () => {\n        node.removeEventListener('mouseenter', handleMouseEnter)\n        node.removeEventListener('mouseleave', handleMouseLeave)\n      }\n    }\n  }, [handleMouseEnter, isShow])\n\n  return (\n    <div data-testid=\"tip\" className={tipContainerClassNames}>\n      <div ref={iconRef} className=\"tip-wrapper\">\n        {withExclamationMark ? (\n          <ExclamationMarkIcon data-testid=\"tip-icon\" />\n        ) : (\n          <QuestionMarkIcon data-testid=\"tip-icon\" />\n        )}\n      </div>\n      {createPortal(\n        <CSSTransition\n          nodeRef={tipBodyRef}\n          in={isShow}\n          timeout={200}\n          classNames=\"fade\"\n          unmountOnExit\n        >\n          <div ref={tipBodyRef} data-testid=\"tip-text\" className={tipClassNames}>\n            {text}\n          </div>\n        </CSSTransition>,\n        document.getElementById('overlay_container')\n      )}\n    </div>\n  )\n}\n\nTip.propTypes = {\n  className: PropTypes.string,\n  text: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).isRequired,\n  withExclamationMark: PropTypes.bool\n}\n\nexport default Tip\n"],"names":["Tip","className","text","withExclamationMark","isShow","setIsShow","useState","tipClassName","setTipClassName","arrowLength","useMemo","getScssVariableValue","arrowOffset","iconLength","minTextLength","iconRef","useRef","tipBodyRef","tipContainerClassNames","classnames","tipClassNames","handleMouseEnter","useCallback","useEffect","iconRect","tipRect","widthPosition","heightPosition","computedArrowOffset","handleMouseLeave","node","jsxs","jsx","ExclamationMarkIcon","QuestionMarkIcon","createPortal","CSSTransition","PropTypes"],"mappings":";;;;;;;;;;AA6BA,MAAMA,IAAM,CAAC,EAAE,WAAAC,IAAY,IAAI,MAAAC,GAAM,qBAAAC,IAAsB,SAAY;AACrE,QAAM,CAACC,GAAQC,CAAS,IAAIC,EAAS,EAAK,GACpC,CAACC,GAAcC,CAAe,IAAIF,EAAS,kBAAkB,GAE7DG,IAAcC,EAAQ,MAAM,SAASC,EAAqB,kBAAkB,CAAC,GAAG,EAAE,GAClFC,IAAcF,EAAQ,MAAM,SAASC,EAAqB,kBAAkB,CAAC,GAAG,EAAE,GAClFE,IAAaH,EAAQ,MAAM,SAASC,EAAqB,iBAAiB,CAAC,GAAG,EAAE,GAEhFG,IAAgB,IAEhBC,IAAUC,EAAA,GACVC,IAAaD,EAAA,GAEbE,IAAyBC,EAAWlB,GAAW,eAAe,GAC9DmB,IAAgBD;AAAA,IACpB;AAAA,IACAZ;AAAA,IACAL,EAAK,UAAUY,IAAgB,cAAc;AAAA,EAAA,GAGzCO,IAAmBC,EAAY,MAAM;AACzC,IAAAjB,EAAU,EAAI;AAAA,EAChB,GAAG,CAAA,CAAE;AAEL,EAAAkB,EAAU,MAAM;AACd,QAAInB,GAAQ;AACV,YAAMoB,IAAWT,EAAQ,QAAQ,sBAAA,GAC3BU,IAAUR,EAAW,QAAQ,sBAAA,GAC7BS,IAAgBF,EAAS,OAAOC,EAAQ,QAAQb,IAA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