{"version":3,"file":"PopUpDialog.mjs","sources":["../../../src/lib/components/PopUpDialog/PopUpDialog.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, { forwardRef, useCallback, useEffect, useLayoutEffect, useRef } from 'react'\nimport PropTypes from 'prop-types'\nimport classnames from 'classnames'\nimport { createPortal } from 'react-dom'\nimport { throttle } from 'lodash'\n\nimport RoundedIcon from '../RoundedIcon/RoundedIcon'\nimport Tooltip from '../Tooltip/Tooltip'\nimport TextTooltipTemplate from '../TooltipTemplate/TextTooltipTemplate'\n\nimport { POP_UP_CUSTOM_POSITION } from '../../types'\nimport CloseIcon from '../../images/close.svg?react'\n\nimport './popUpDialog.scss'\n\nlet PopUpDialog = (\n  {\n    children,\n    className = '',\n    closePopUp = null,\n    customPosition = {},\n    headerIsHidden = false,\n    headerText = '',\n    isOpen = true,\n    onResolve = null,\n    style = {},\n    tooltipText = ''\n  },\n  ref\n) => {\n  const popUpOverlayRef = useRef(null)\n  ref ??= popUpOverlayRef\n  const popUpClassNames = classnames(\n    className,\n    'pop-up-dialog__overlay',\n    customPosition.element && 'custom-position'\n  )\n\n  const handleClosePopUp = useCallback(() => {\n    closePopUp && closePopUp()\n    onResolve && onResolve()\n  }, [closePopUp, onResolve])\n\n  const calculateCustomPopUpPosition = useCallback(() => {\n    if (customPosition?.element?.current && ref?.current) {\n      const elementRect = customPosition.element.current.getBoundingClientRect()\n      const popUpRect = ref.current.getBoundingClientRect()\n      const [verticalPosition, horizontalPosition] = customPosition.position.split('-')\n      const popupMargin = 15\n      const elementMargin = 5\n      const isEnoughSpaceFromLeft = elementRect.right >= popUpRect.width + popupMargin\n      const isEnoughSpaceFromRight =\n        window.innerWidth - elementRect.left >= popUpRect.width + popupMargin\n      const isEnoughSpaceFromTop = elementRect.top > popUpRect.height + popupMargin + elementMargin\n      const isEnoughSpaceFromBottom =\n        elementRect.bottom + popUpRect.height + popupMargin + elementMargin <= window.innerHeight\n      let leftPosition =\n        horizontalPosition === 'left' ? elementRect.right - popUpRect.width : elementRect.left\n\n      let topPosition\n\n      if (verticalPosition === 'top') {\n        topPosition = isEnoughSpaceFromTop\n          ? elementRect.top - popUpRect.height - elementMargin\n          : popupMargin\n      } else {\n        topPosition = isEnoughSpaceFromBottom\n          ? elementRect.bottom + elementMargin\n          : window.innerHeight - popUpRect.height - popupMargin\n      }\n\n      if (customPosition.autoVerticalPosition) {\n        if (verticalPosition === 'top') {\n          if (!isEnoughSpaceFromTop && isEnoughSpaceFromBottom) {\n            topPosition = elementRect.bottom + elementMargin\n          }\n        } else {\n          if (isEnoughSpaceFromTop && !isEnoughSpaceFromBottom) {\n            topPosition = elementRect.top - popUpRect.height - elementMargin\n          }\n        }\n      }\n\n      if (customPosition.autoHorizontalPosition) {\n        if (verticalPosition === 'left') {\n          if (!isEnoughSpaceFromLeft && isEnoughSpaceFromRight) {\n            leftPosition = elementRect.left\n          } else if (!isEnoughSpaceFromLeft && !isEnoughSpaceFromRight) {\n            leftPosition = popupMargin\n          }\n        } else {\n          if (isEnoughSpaceFromLeft && !isEnoughSpaceFromRight) {\n            leftPosition = elementRect.right - popUpRect.width\n          } else if (!isEnoughSpaceFromLeft && !isEnoughSpaceFromRight) {\n            leftPosition = window.innerWidth - popUpRect.width - popupMargin\n          }\n        }\n      }\n\n      ref.current.style.top = `${topPosition}px`\n\n      if (style.left && !(customPosition.autoHorizontalPosition && isEnoughSpaceFromRight)) {\n        ref.current.style.left = `calc(${leftPosition}px + ${style.left})`\n      } else {\n        ref.current.style.left = `${leftPosition}px`\n      }\n    }\n  }, [customPosition, style.left, ref])\n\n  useLayoutEffect(() => {\n    calculateCustomPopUpPosition()\n  }, [calculateCustomPopUpPosition])\n\n  useEffect(() => {\n    if (isOpen) {\n      const throttledCalculatedCustomPopUpPosition = throttle(calculateCustomPopUpPosition, 100, {\n        trailing: true,\n        leading: true\n      })\n      const popupObserver = new ResizeObserver(throttledCalculatedCustomPopUpPosition)\n      const popupElement = ref.current\n\n      popupObserver.observe(popupElement)\n      window.addEventListener('resize', throttledCalculatedCustomPopUpPosition)\n\n      return () => {\n        popupObserver.unobserve(popupElement)\n        window.removeEventListener('resize', throttledCalculatedCustomPopUpPosition)\n      }\n    }\n  }, [calculateCustomPopUpPosition, ref, isOpen])\n\n  return isOpen\n    ? createPortal(\n        <div ref={ref} className={popUpClassNames} style={style}>\n          <div data-testid=\"pop-up-dialog\" className=\"pop-up-dialog\">\n            {!headerIsHidden && (\n              <div className=\"pop-up-dialog__header\">\n                {headerText && (\n                  <div data-testid=\"pop-up-dialog-header\" className=\"pop-up-dialog__header-text\">\n                    <Tooltip template={<TextTooltipTemplate text={tooltipText || headerText} />}>\n                      <span>{headerText}</span>\n                    </Tooltip>\n                  </div>\n                )}\n                <RoundedIcon\n                  className=\"pop-up-dialog__btn_close\"\n                  onClick={handleClosePopUp}\n                  tooltipText=\"Close\"\n                  data-testid=\"pop-up-close-btn\"\n                >\n                  <CloseIcon />\n                </RoundedIcon>\n              </div>\n            )}\n            {children}\n          </div>\n        </div>,\n        document.getElementById('overlay_container')\n      )\n    : null\n}\n\nPopUpDialog = forwardRef(PopUpDialog)\n\nPopUpDialog.displayName = 'PopUpDialog'\n\nPopUpDialog.propTypes = {\n  children: PropTypes.node.isRequired,\n  className: PropTypes.string,\n  closePopUp: PropTypes.func,\n  customPosition: POP_UP_CUSTOM_POSITION,\n  isOpen: PropTypes.bool,\n  headerIsHidden: PropTypes.bool,\n  headerText: PropTypes.string,\n  onResolve: PropTypes.func,\n  showPopUpDialog: PropTypes.bool,\n  style: PropTypes.object,\n  tooltipText: PropTypes.string\n}\n\nexport default PopUpDialog\n"],"names":["PopUpDialog","children","className","closePopUp","customPosition","headerIsHidden","headerText","isOpen","onResolve","style","tooltipText","ref","popUpOverlayRef","useRef","popUpClassNames","classnames","handleClosePopUp","useCallback","calculateCustomPopUpPosition","_a","elementRect","popUpRect","verticalPosition","horizontalPosition","popupMargin","elementMargin","isEnoughSpaceFromLeft","isEnoughSpaceFromRight","isEnoughSpaceFromTop","isEnoughSpaceFromBottom","leftPosition","topPosition","useLayoutEffect","useEffect","throttledCalculatedCustomPopUpPosition","throttle","popupObserver","popupElement","createPortal","jsx","jsxs","Tooltip","TextTooltipTemplate","RoundedIcon","CloseIcon","forwardRef","PropTypes","POP_UP_CUSTOM_POSITION","PopUpDialog$1"],"mappings":";;;;;;;;;;;;AA+BA,IAAIA,IAAc,CAChB;AAAA,EACE,UAAAC;AAAA,EACA,WAAAC,IAAY;AAAA,EACZ,YAAAC,IAAa;AAAA,EACb,gBAAAC,IAAiB,CAAA;AAAA,EACjB,gBAAAC,IAAiB;AAAA,EACjB,YAAAC,IAAa;AAAA,EACb,QAAAC,IAAS;AAAA,EACT,WAAAC,IAAY;AAAA,EACZ,OAAAC,IAAQ,CAAA;AAAA,EACR,aAAAC,IAAc;AAChB,GACAC,MACG;AACH,QAAMC,IAAkBC,EAAO,IAAI;AACnC,EAAAF,UAAQC;AACR,QAAME,IAAkBC;AAAA,IACtBb;AAAA,IACA;AAAA,IACAE,EAAe,WAAW;AAAA,EAAA,GAGtBY,IAAmBC,EAAY,MAAM;AACzC,IAAAd,KAAcA,EAAA,GACdK,KAAaA,EAAA;AAAA,EACf,GAAG,CAACL,GAAYK,CAAS,CAAC,GAEpBU,IAA+BD,EAAY,MAAM;;AACrD,SAAIE,IAAAf,KAAA,gBAAAA,EAAgB,YAAhB,QAAAe,EAAyB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