{"version":3,"file":"useHiddenChipsBlock.hook.mjs","sources":["../../src/lib/hooks/useHiddenChipsBlock.hook.js"],"sourcesContent":["/*\nCopyright 2019 Iguazio Systems Ltd.\n\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.\n\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.\n\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 { useCallback, useEffect, useMemo, useState } from 'react'\nimport classnames from 'classnames'\n\nimport { getTransitionEndEventName } from '../utils/common.util'\n\nexport const useHiddenChipsBlock = (hiddenChipsCounterRef, hiddenChipsPopUpRef) => {\n  const [isTop, setIsTop] = useState(false)\n  const [isLeft, setIsLeft] = useState(false)\n  const [isVisible, setIsVisible] = useState(false)\n  const transitionEndEventName = useMemo(() => getTransitionEndEventName(), [])\n\n  const hiddenChipsBlockClassNames = classnames(\n    'chip-block-hidden',\n    isTop ? 'chip-block-hidden_top' : 'chip-block-hidden_bottom',\n    isLeft ? 'chip-block-hidden_left' : 'chip-block-hidden_right',\n    isVisible && 'chip-block-hidden_visible'\n  )\n\n  const resizePopUp = useCallback(() => {\n    if (hiddenChipsPopUpRef?.current && hiddenChipsCounterRef?.current) {\n      const offset = 10\n      const offsetMargin = 20\n      const elementRect = hiddenChipsCounterRef.current.getBoundingClientRect()\n\n      // Calculate the distance from the right edge of the window to the element's right edge\n      const elementRectRight = Math.floor(window.innerWidth - elementRect.left - elementRect.width)\n\n      // Calculate the distance from the bottom edge of the window to the element's bottom edge\n      const elementRectBottom = Math.floor(\n        window.innerHeight - elementRect.top - elementRect.height\n      )\n      let isLeftPosition = false\n      let isTopPosition = false\n      hiddenChipsPopUpRef.current.style.maxWidth = '100%'\n      hiddenChipsPopUpRef.current.style.maxHeight = '100%'\n\n      // Determine if the left position is preferred based on the element's position and available width\n      if (elementRect.left > hiddenChipsPopUpRef.current.clientWidth) {\n        isLeftPosition = true\n      } else if (elementRectRight > hiddenChipsPopUpRef.current.clientWidth) {\n        isLeftPosition = false\n      } else {\n        // Compare elementRect.left and elementRectRight to choose the larger value as the max width\n        isLeftPosition = elementRect.left > elementRectRight\n        const popUpMaxWidth = Math.max(elementRect.left, elementRectRight)\n        hiddenChipsPopUpRef.current.style.maxWidth = `${popUpMaxWidth}px`\n      }\n\n      hiddenChipsPopUpRef.current.style.right = isLeftPosition ? `${elementRectRight}px` : 'unset'\n      hiddenChipsPopUpRef.current.style.left = isLeftPosition ? 'unset' : `${elementRect.left}px`\n\n      // Determine if the top position is preferred based on the element's position and available height\n      if (elementRect.top > hiddenChipsPopUpRef.current.clientHeight + offset + offsetMargin) {\n        isTopPosition = true\n      } else if (\n        elementRectBottom >\n        hiddenChipsPopUpRef.current.clientHeight + offset + offsetMargin\n      ) {\n        isTopPosition = false\n      } else {\n        // Compare elementRect.top and elementRectBottom to choose the larger value as the max height\n        isTopPosition = elementRect.top > elementRectBottom + offset\n        const popUpMaxHeight = Math.max(elementRect.top, elementRectBottom) - offset - offsetMargin\n        hiddenChipsPopUpRef.current.style.maxHeight = `${popUpMaxHeight}px`\n      }\n\n      hiddenChipsPopUpRef.current.style.bottom = isTopPosition\n        ? `${elementRectBottom + elementRect.height + offset}px`\n        : 'unset'\n      hiddenChipsPopUpRef.current.style.top = isTopPosition\n        ? 'unset'\n        : `${elementRect.bottom + offset}px`\n\n      setIsTop(isTopPosition)\n      setIsLeft(isLeftPosition)\n      setIsVisible(true)\n    }\n  }, [hiddenChipsCounterRef, hiddenChipsPopUpRef])\n\n  useEffect(() => {\n    if (hiddenChipsPopUpRef?.current && hiddenChipsCounterRef?.current) {\n      window.addEventListener('resize', resizePopUp)\n      window.addEventListener(transitionEndEventName, resizePopUp)\n\n      return () => {\n        window.removeEventListener('resize', resizePopUp)\n        window.removeEventListener(transitionEndEventName, resizePopUp)\n      }\n    }\n  }, [hiddenChipsPopUpRef, hiddenChipsCounterRef, resizePopUp, transitionEndEventName])\n\n  useEffect(() => {\n    resizePopUp()\n  }, [resizePopUp])\n\n  return {\n    hiddenChipsBlockClassNames\n  }\n}\n"],"names":["useHiddenChipsBlock","hiddenChipsCounterRef","hiddenChipsPopUpRef","isTop","setIsTop","useState","isLeft","setIsLeft","isVisible","setIsVisible","transitionEndEventName","useMemo","getTransitionEndEventName","hiddenChipsBlockClassNames","classnames","resizePopUp","useCallback","elementRect","elementRectRight","elementRectBottom","isLeftPosition","isTopPosition","popUpMaxWidth","popUpMaxHeight","useEffect"],"mappings":";;;AAwBY,MAACA,IAAsB,CAACC,GAAuBC,MAAwB;AACjF,QAAM,CAACC,GAAOC,CAAQ,IAAIC,EAAS,EAAK,GAClC,CAACC,GAAQC,CAAS,IAAIF,EAAS,EAAK,GACpC,CAACG,GAAWC,CAAY,IAAIJ,EAAS,EAAK,GAC1CK,IAAyBC,EAAQ,MAAMC,EAAyB,GAAI,CAAA,CAAE,GAEtEC,IAA6BC;AAAA,IACjC;AAAA,IACAX,IAAQ,0BAA0B;AAAA,IAClCG,IAAS,2BAA2B;AAAA,IACpCE,KAAa;AAAA,EACjB,GAEQO,IAAcC,EAAY,MAAM;AACpC,QAAId,KAAA,QAAAA,EAAqB,YAAWD,KAAA,QAAAA,EAAuB,UAAS;AAGlE,YAAMgB,IAAchB,EAAsB,QAAQ,sBAAqB,GAGjEiB,IAAmB,KAAK,MAAM,OAAO,aAAaD,EAAY,OAAOA,EAAY,KAAK,GAGtFE,IAAoB,KAAK;AAAA,QAC7B,OAAO,cAAcF,EAAY,MAAMA,EAAY;AAAA,MAC3D;AACM,UAAIG,IAAiB,IACjBC,IAAgB;AAKpB,UAJAnB,EAAoB,QAAQ,MAAM,WAAW,QAC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