{"version":3,"file":"useElementAtContainerEdge.cjs","names":["_react","require","_getLogicalBoundaries","defaultEdgeState","isAtContainerInlineStart","isAtContainerInlineEnd","isAtContainerBlockStart","isAtContainerBlockEnd","useElementAtContainerEdge","containerElement","element","monitoringElement","edgeState","setEdgeState","useState","requestedAnimationFrameIdRef","useRef","resolvedMonitoringElement","parentElement","updateEdgeState","useCallback","cancelAnimationFrame","current","requestAnimationFrame","elementBoundaries","getLogicalBoundaries","containerBoundaries","document","body","left","right","top","bottom","useEffect","mutationObserver","MutationObserver","observe","attributes","attributeFilter","childList","subtree","window","addEventListener","disconnect","removeEventListener"],"sources":["../../../src/ui-shell/useElementAtContainerEdge.ts"],"sourcesContent":["/*!\n * Copyright (c) 2025-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\nimport { getLogicalBoundaries } from \"../getLogicalBoundaries.js\";\n\nconst defaultEdgeState = {\n  isAtContainerInlineStart: null,\n  isAtContainerInlineEnd: null,\n  isAtContainerBlockStart: null,\n  isAtContainerBlockEnd: null,\n};\n\ntype EdgeState =\n  | {\n      isAtContainerBlockEnd: null;\n      isAtContainerBlockStart: null;\n      isAtContainerInlineEnd: null;\n      isAtContainerInlineStart: null;\n    }\n  | {\n      isAtContainerBlockEnd: boolean;\n      isAtContainerBlockStart: boolean;\n      isAtContainerInlineEnd: boolean;\n      isAtContainerInlineStart: boolean;\n    };\n\n/**\n * Determines if an element is at the edge of its container.\n *\n * If `element` is `null`, all returned edge state values will be `null`.\n */\nexport function useElementAtContainerEdge({\n  containerElement,\n  element,\n  monitoringElement,\n}: {\n  /**\n   * The container element against which the edges are checked. If not provided, it defaults to the document body.\n   */\n  containerElement?: HTMLElement | null;\n  /**\n   * The element whose edges you want to track.\n   */\n  element?: HTMLElement | null;\n  /**\n   * The element whose mutations you want to track. If not provided, it defaults to the parent element of the `element`.\n   */\n  monitoringElement?: HTMLElement | null;\n}) {\n  const [edgeState, setEdgeState] = useState<EdgeState>(defaultEdgeState);\n\n  const requestedAnimationFrameIdRef = useRef(0);\n\n  const resolvedMonitoringElement = monitoringElement ?? element?.parentElement;\n\n  // will check the position of the element and update state accordingly\n  const updateEdgeState = useCallback(() => {\n    cancelAnimationFrame(requestedAnimationFrameIdRef.current);\n\n    requestedAnimationFrameIdRef.current = requestAnimationFrame(() => {\n      if (element) {\n        const elementBoundaries = getLogicalBoundaries(element);\n        const containerBoundaries = getLogicalBoundaries(\n          containerElement ?? document.body,\n        );\n\n        setEdgeState({\n          isAtContainerInlineStart:\n            elementBoundaries.left === containerBoundaries.left,\n          isAtContainerInlineEnd:\n            elementBoundaries.right === containerBoundaries.right,\n          isAtContainerBlockStart:\n            elementBoundaries.top === containerBoundaries.top,\n          isAtContainerBlockEnd:\n            elementBoundaries.bottom === containerBoundaries.bottom,\n        });\n      } else {\n        setEdgeState(defaultEdgeState);\n      }\n    });\n  }, [containerElement, element]);\n\n  useEffect(() => {\n    // Listen for mutations that might affect position\n    const mutationObserver = new MutationObserver(() => {\n      updateEdgeState();\n    });\n\n    if (resolvedMonitoringElement) {\n      mutationObserver.observe(resolvedMonitoringElement, {\n        attributes: true,\n        attributeFilter: [\"style\"],\n        childList: true,\n        subtree: true,\n      });\n    }\n\n    // Listen for window resize, which can affect position\n    window.addEventListener(\"resize\", updateEdgeState);\n\n    // Initial check\n    updateEdgeState();\n\n    // cleanup function to remove listeners and observer\n    return () => {\n      cancelAnimationFrame(requestedAnimationFrameIdRef.current);\n      mutationObserver.disconnect();\n      window.removeEventListener(\"resize\", updateEdgeState);\n    };\n  }, [resol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