{"version":3,"file":"useScrollState.cjs","names":["_react","require","getIsScrollHeightElement","containerElement","element","containerElementHeight","getBoundingClientRect","height","elementHeight","exports","getIsYAxisScrollContainer","overflowY","window","getComputedStyle","getIsYAxisScrolling","scrollHeight","clientHeight","getNestedScrollContainers","Array","from","querySelectorAll","filter","scrollHeightElement","fakeDefaultContainerElement","document","createElement","useScrollState","isScrolled","setIsScrolled","useState","requestedAnimationFrameIdRef","useRef","scrollableElementsRef","updateScrollState","useCallback","cancelAnimationFrame","current","requestAnimationFrame","reduce","scrollableElement","scrollTop","addScrollEventListeners","forEach","addEventListener","removeScrollEventListeners","removeEventListener","updateScrollableElements","computedContainerElement","concat","updateScrollListeners","useEffect","mutationObserver","MutationObserver","observe","attributes","attributeFilter","childList","subtree","disconnect","isContentScrolled"],"sources":["../../../src/ui-shell/useScrollState.ts"],"sourcesContent":["/*!\n * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nimport { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport const getIsScrollHeightElement = ({\n  containerElement,\n  element,\n}: {\n  containerElement: HTMLElement;\n  element: HTMLElement;\n}) => {\n  const containerElementHeight =\n    containerElement.getBoundingClientRect().height;\n  const elementHeight = element.getBoundingClientRect().height;\n\n  return elementHeight - containerElementHeight >= 0;\n};\n\nexport const getIsYAxisScrollContainer = (element: HTMLElement) => {\n  const overflowY = window.getComputedStyle(element).overflowY;\n\n  return overflowY === \"auto\" || overflowY === \"scroll\";\n};\n\nexport const getIsYAxisScrolling = (element: HTMLElement) =>\n  element.scrollHeight > element.clientHeight\n    ? getIsYAxisScrollContainer(element)\n    : false;\n\nexport const getNestedScrollContainers = (containerElement: HTMLElement) =>\n  Array.from(containerElement.querySelectorAll<HTMLElement>(\"*\"))\n    .filter((element) =>\n      getIsScrollHeightElement({\n        containerElement,\n        element,\n      }),\n    )\n    .filter((scrollHeightElement) =>\n      getIsYAxisScrollContainer(scrollHeightElement),\n    );\n\nexport const fakeDefaultContainerElement =\n  typeof window === \"undefined\" ? null : document.createElement(\"div\");\n\nexport const useScrollState = <\n  ContainerElement extends HTMLElement = HTMLDivElement,\n>(\n  /**\n   * The element containing a scroll area.\n   */\n  containerElement: ContainerElement | null,\n) => {\n  const [isScrolled, setIsScrolled] = useState(false);\n\n  const requestedAnimationFrameIdRef = useRef(0);\n  const scrollableElementsRef = useRef<HTMLElement[]>([]);\n\n  const updateScrollState = useCallback(() => {\n    cancelAnimationFrame(requestedAnimationFrameIdRef.current);\n\n    requestedAnimationFrameIdRef.current = requestAnimationFrame(() => {\n      setIsScrolled(\n        scrollableElementsRef.current.reduce(\n          (isScrolled, scrollableElement) =>\n            isScrolled || scrollableElement.scrollTop > 0,\n          false,\n        ),\n      );\n    });\n  }, []);\n\n  const addScrollEventListeners = useCallback(() => {\n    scrollableElementsRef.current.forEach((scrollableElement) => {\n      scrollableElement.addEventListener(\"scroll\", updateScrollState);\n    });\n  }, [updateScrollState]);\n\n  const removeScrollEventListeners = useCallback(() => {\n    scrollableElementsRef.current.forEach((scrollableElement) => {\n      scrollableElement.removeEventListener(\"scroll\", updateScrollState);\n    });\n  }, [updateScrollState]);\n\n  const updateScrollableElements = useCallback(() => {\n    const computedContainerElement =\n      containerElement || fakeDefaultContainerElement;\n\n    if (computedContainerElement) {\n      scrollableElementsRef.current = getNestedScrollContainers(\n        computedContainerElement,\n      ).concat(computedContainerElement);\n    }\n  }, [containerElement]);\n\n  const updateScrollListeners = useCallback(() => {\n    removeScrollEventListeners();\n    updateScrollableElements();\n    addScrollEventListeners();\n    updateScrollState();\n  }, [\n    addScrollEventListeners,\n    removeScrollEventListeners,\n    updateScrollableElements,\n    updateScrollState,\n  ]);\n\n  useEffect(() => {\n    const mutationObserver = new MutationObserver(() => {\n      updateScrollListeners();\n    });\n\n    if (containerElement) {\n      mutationObserver.observe(containerElement, {\n        attributes: true,\n        attributeFilter: [\"style\"],\n        childList: true,\n        subtree: true,\n      });\n    }\n\n    updateScrollListeners();\n\n    return () => {\n      cancelAnimationFrame(requestedAnimationFrameIdRef.current);\n      removeScrollEventListeners();\n      mutationObserver.disconnect();\n    };\n  }, [containerElement, removeScrollEventListeners, updateScrollListeners]);\n\n  return {\n    isContentScrolled: isScrolled,\n  };\n};\n"],"mappings":";;;;;;AAYA,IAAAA,MAAA,GAAAC,OAAA;AAZA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIO,MAAMC,wBAAwB,GAAGA,CAAC;EACvCC,gBAAgB;EAChBC;AAIF,CAAC,KAAK;EACJ,MAAMC,sBAAsB,GAC1BF,gBAAgB,CAACG,qBAAqB,CAAC,CAAC,CAACC,MAAM;EACjD,MAAMC,aAAa,GAAGJ,OAAO,CAACE,qBAAqB,CAAC,CAAC,CAACC,MAAM;EAE5D,OAAOC,aAAa,GAAGH,sBAAsB,IAAI,CAAC;AACpD,CAAC;AAACI,OAAA,CAAAP,wBAAA,GAAAA,wBAAA;AAEK,MAAMQ,yBAAyB,GAAIN,OAAoB,IAAK;EACjE,MAAMO,SAAS,GAAGC,MAAM,CAACC,gBAAgB,CAACT,OAAO,CAAC,CAACO,SAAS;EAE5D,OAAOA,SAAS,KAAK,MAAM,IAAIA,SAAS,KAAK,QAAQ;AACvD,CAAC;AAACF,OAAA,CAAAC,yBAAA,GAAAA,yBAAA;AAEK,MAAMI,mBAAmB,GAAIV,OAAoB,IACtDA,OAAO,CAACW,YAAY,GAAGX,OAAO,CAACY,YAAY,GACvCN,yBAAyB,CAACN,OAAO,CAAC,GAClC,KAAK;AAACK,OAAA,CAAAK,mBAAA,GAAAA,mB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