{"version":3,"file":"useScrollIndication.cjs","names":["_react","require","_OdysseyDesignTokensContext","useScrollIndication","tableOuterContainer","tableInnerContainer","setIsTableContainerScrolledToStart","setIsTableContainerScrolledToEnd","setTableInnerContainerWidth","animationFrameIdRef","useRef","resizeObserverRef","odysseyDesignTokens","useOdysseyDesignTokens","checkScrollIndicators","useCallback","containerWidth","clientWidth","contentWidth","scrollWidth","containerStartScrollPosition","scrollLeft","containerEndScrollPosition","useEffect","ResizeObserver","current","debounceTimer","clearTimeout","setTimeout","requestAnimationFrame","Number","BorderWidthMain","parentElement","observe","disconnect","cancelAnimationFrame","addEventListener","removeEventListener","exports"],"sources":["../../../src/DataTable/useScrollIndication.tsx"],"sourcesContent":["/*!\n * Copyright (c) 2023-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 {\n  Dispatch,\n  SetStateAction,\n  useCallback,\n  useEffect,\n  useRef,\n} from \"react\";\n\nimport { useOdysseyDesignTokens } from \"../OdysseyDesignTokensContext.js\";\n\ntype UseScrollIndicationProps = {\n  setIsTableContainerScrolledToEnd: Dispatch<SetStateAction<boolean>>;\n  setIsTableContainerScrolledToStart: Dispatch<SetStateAction<boolean>>;\n  setTableInnerContainerWidth: Dispatch<SetStateAction<string>>;\n  tableInnerContainer: HTMLDivElement | null;\n  tableOuterContainer: HTMLDivElement | null;\n};\nexport const useScrollIndication = ({\n  tableOuterContainer,\n  tableInnerContainer,\n  setIsTableContainerScrolledToStart,\n  setIsTableContainerScrolledToEnd,\n  setTableInnerContainerWidth,\n}: UseScrollIndicationProps) => {\n  const animationFrameIdRef = useRef<number | null>(null);\n\n  const resizeObserverRef = useRef<ResizeObserver | null>(null);\n\n  const odysseyDesignTokens = useOdysseyDesignTokens();\n\n  const checkScrollIndicators = useCallback(() => {\n    if (!tableOuterContainer || !tableInnerContainer) return;\n\n    const containerWidth = tableOuterContainer.clientWidth;\n    const contentWidth = tableInnerContainer.scrollWidth;\n    const containerStartScrollPosition = tableInnerContainer.scrollLeft;\n    const containerEndScrollPosition =\n      containerStartScrollPosition + containerWidth;\n\n    setIsTableContainerScrolledToStart(containerStartScrollPosition <= 16);\n    setIsTableContainerScrolledToEnd(\n      containerEndScrollPosition >= contentWidth - 16,\n    );\n  }, [\n    tableInnerContainer,\n    tableOuterContainer,\n    setIsTableContainerScrolledToEnd,\n    setIsTableContainerScrolledToStart,\n  ]);\n\n  useEffect(() => {\n    // Avoid creating multiple observers or if ResizeObserver is unsupported\n    if (typeof ResizeObserver === \"undefined\" || resizeObserverRef.current) {\n      return;\n    }\n\n    let debounceTimer: ReturnType<typeof setTimeout>;\n\n    resizeObserverRef.current = new ResizeObserver(() => {\n      clearTimeout(debounceTimer);\n      debounceTimer = setTimeout(() => {\n        if (!animationFrameIdRef.current) {\n          animationFrameIdRef.current = requestAnimationFrame(() => {\n            checkScrollIndicators();\n            animationFrameIdRef.current = null;\n          });\n        }\n\n        setTableInnerContainerWidth(\n          tableInnerContainer?.clientWidth\n            ? `${tableInnerContainer.clientWidth - Number(odysseyDesignTokens.BorderWidthMain) * 2}px` // Account for the border on the outer container\n            : \"100%\",\n        );\n      }, 100); // debounce delay\n    });\n\n    if (tableOuterContainer && tableOuterContainer.parentElement) {\n      resizeObserverRef.current.observe(tableOuterContainer.parentElement);\n    }\n\n    return () => {\n      if (resizeObserverRef.current) {\n        resizeObserverRef.current.disconnect();\n        resizeObserverRef.current = null;\n      }\n      clearTimeout(debounceTimer); // Ensure timer is cleared on component unmount\n      if (animationFrameIdRef.current) {\n        cancelAnimationFrame(animationFrameIdRef.current);\n        animationFrameIdRef.current = null;\n      }\n    };\n  }, [\n    checkScrollIndicators,\n    setTableInnerContainerWidth,\n    tableOuterContainer,\n    tableInnerContainer,\n    setIsTableContainerScrolledToStart,\n    setIsTableContainerScrolledToEnd,\n    odysseyDesignTokens.BorderWidthMain,\n  ]);\n\n  useEffect(() => {\n    tableInnerContainer?.addEventListener(\"scroll\", checkScrollIndicators);\n    return () =>\n      tableInnerContainer?.removeEventListener(\"scroll\", checkScrollIndicators);\n  }, [tableInnerContainer, checkScrollIndicators]); // Re-run when innerContainerRef changes\n\n  // Initial check to set state correctly on mount\n  useEffect(() => {\n    checkScrollIndicators();\n\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, []);\n};\n"],"mappings":";;;;;;AAYA,IAAAA,MAAA,GAAAC,OAAA;AAQA,IAAAC,2BAAA,GAAAD,OAAA;AApBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmBO,MAAME,mBAAmB,GAAGA,CAAC;EAClCC,mBAAmB;EACnBC,mBAAmB;EACnBC,kCAAkC;EAClCC,gCAAgC;EAChCC;AACwB,CAAC,KAAK;EAC9B,MAAMC,mBAAmB,GAAG,IAAAC,aAAM,EAAgB,IAAI,CAAC;EAEvD,MAAMC,iBAAiB,GAAG,IAAAD,aAAM,EAAwB,IAAI,CAAC;EAE7D,MAAME,mBAAmB,GAAG,IAAAC,kDAAsB,EAAC,CAAC;EAEpD,MAAMC,qBAAqB,GAAG,IAAAC,kBAAW,EAAC,MAAM;IAC9C,IAAI,CAACX,mBAAmB,IAAI,CAACC,mBAAmB,EAAE;IAElD,MAAMW,cAAc,GAAGZ,mBAAmB,CAACa,WAAW;IACtD,MAAMC,YAAY,GAAGb,mBAAmB,CAACc,WAAW;IACpD,MAAMC,4BAA4B,GAAGf,mBAAmB,CAACgB,UAAU;IACnE,MAAMC,0BAA0B,GAC9BF,4BAA4B,GAAGJ,cAAc;IAE/CV,kCAAkC,CAACc,4BAA4B,IAAI,EAAE,CAAC;IACtEb,gCAAgC,CA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