{"version":3,"file":"useContainerQuery.cjs","names":["_react","require","resolveTargetElement","target","current","isResizeObserverSizeList","borderBoxSize","Array","isArray","resolveBorderBoxSize","doesSizeMatchQuery","blockSize","inlineSize","maxHeight","maxWidth","minHeight","minWidth","undefined","useContainerQuery","resolvedTargetElement","hasMatches","setHasMatches","useState","height","width","getBoundingClientRect","animationFrameIdRef","useRef","useEffect","targetElement","ResizeObserver","resizeObserver","resizeObserverEntry","cancelAnimationFrame","requestAnimationFrame","contentRect","observe","disconnect","exports"],"sources":["../../../src/theme/useContainerQuery.ts"],"sourcesContent":["/*!\n * Copyright (c) 2026-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 { type RefObject, useEffect, useRef, useState } from \"react\";\n\nexport type UseContainerQueryProps = {\n  /** Inclusive upper bound on the target's block-size (px). */\n  maxHeight?: number;\n  /** Inclusive upper bound on the target's inline-size (px). */\n  maxWidth?: number;\n  /** Inclusive lower bound on the target's block-size (px). */\n  minHeight?: number;\n  /** Inclusive lower bound on the target's inline-size (px). */\n  minWidth?: number;\n  /**\n   * Element to observe, passed as a ref or a raw element. The hook returns `false` until an element is present.\n   */\n  target: RefObject<HTMLElement | null> | HTMLElement | null;\n};\n\nconst resolveTargetElement = (target: UseContainerQueryProps[\"target\"]) =>\n  target && \"current\" in target ? target.current : target;\n\n/**\n * `borderBoxSize` is typed as an array but is absent in older browsers and a single object in some, so it is narrowed through a predicate that keeps the element type intact (`Array.isArray` alone widens a readonly array to `any[]`).\n */\nconst isResizeObserverSizeList = (\n  borderBoxSize: ResizeObserverSize | readonly ResizeObserverSize[] | undefined,\n): borderBoxSize is readonly ResizeObserverSize[] =>\n  Array.isArray(borderBoxSize);\n\nconst resolveBorderBoxSize = (\n  borderBoxSize: ResizeObserverSize | readonly ResizeObserverSize[] | undefined,\n): ResizeObserverSize | undefined =>\n  isResizeObserverSizeList(borderBoxSize) ? borderBoxSize[0] : borderBoxSize;\n\nconst doesSizeMatchQuery = ({\n  blockSize,\n  inlineSize,\n  maxHeight,\n  maxWidth,\n  minHeight,\n  minWidth,\n}: {\n  blockSize: number;\n  inlineSize: number;\n} & Omit<UseContainerQueryProps, \"target\">) =>\n  (minWidth === undefined || inlineSize >= minWidth) &&\n  (maxWidth === undefined || inlineSize <= maxWidth) &&\n  (minHeight === undefined || blockSize >= minHeight) &&\n  (maxHeight === undefined || blockSize <= maxHeight);\n\n/**\n * Reports whether an element's size satisfies a container query, mirroring `useMediaQuery` but observing an element with `ResizeObserver` instead of the viewport. Bounds are matched against the target's `inline-size` (width) and `block-size` (height); an omitted bound is always satisfied. Returns `false` until the target element exists (and when `ResizeObserver` is unavailable, e.g. during server rendering).\n */\nexport const useContainerQuery = ({\n  target,\n  minWidth,\n  maxWidth,\n  minHeight,\n  maxHeight,\n}: UseContainerQueryProps) => {\n  const resolvedTargetElement = resolveTargetElement(target);\n\n  const [hasMatches, setHasMatches] = useState(() => {\n    if (!resolvedTargetElement) {\n      return false;\n    }\n\n    const { height, width } = resolvedTargetElement.getBoundingClientRect();\n\n    return doesSizeMatchQuery({\n      blockSize: height,\n      inlineSize: width,\n      maxHeight,\n      maxWidth,\n      minHeight,\n      minWidth,\n    });\n  });\n\n  const animationFrameIdRef = useRef<number | null>(null);\n\n  useEffect(() => {\n    // Resolve the element inside the effect: a `RefObject`'s `current` is `null` during render and only populated after commit, so resolving here (rather than during render) is what lets a ref target attach the observer.\n    const targetElement = resolveTargetElement(target);\n\n    if (typeof ResizeObserver === \"undefined\" || !targetElement) {\n      return;\n    }\n\n    const resizeObserver = new ResizeObserver(([resizeObserverEntry]) => {\n      if (animationFrameIdRef.current !== null) {\n        cancelAnimationFrame(animationFrameIdRef.current);\n      }\n\n      animationFrameIdRef.current = requestAnimationFrame(() => {\n        animationFrameIdRef.current = null;\n\n        if (!resizeObserverEntry) {\n          return;\n        }\n\n        const borderBoxSize = resolveBorderBoxSize(\n          resizeObserverEntry.borderBoxSize,\n        );\n\n        setHasMatches(\n          doesSizeMatchQuery({\n            blockSize:\n              borderBoxSize?.blockSize ??\n              resizeObserverEntry.contentRect.height,\n            inlineSize:\n              borderBoxSize?.inlineSize ??\n              resizeObserverEntry.contentRect.width,\n            maxHeight,\n            maxWidth,\n            minHeight,\n            minWidth,\n          }),\n        );\n      });\n    });\n\n    resizeObserver.observe(targetElement);\n\n    return () => {\n      resizeObserver.disconnect();\n\n      if (animationFrameIdRef.current !== null) {\n        cancelAnimationFrame(animationFrameIdRef.current);\n        animationFrameIdRef.current = null;\n      }\n    };\n  }, [maxHeight, maxWidth, minHeight, minWidth, target]);\n\n  return hasMatches;\n};\n"],"mappings":";;;;;;AAYA,IAAAA,MAAA,GAAAC,OAAA;AAZA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmBA,MAAMC,oBAAoB,GAAIC,MAAwC,IACpEA,MAAM,IAAI,SAAS,IAAIA,MAAM,GAAGA,MAAM,CAACC,OAAO,GAAGD,MAAM;AAKzD,MAAME,wBAAwB,GAC5BC,aAA6E,IAE7EC,KAAK,CAACC,OAAO,CAACF,aAAa,CAAC;AAE9B,MAAMG,oBAAoB,GACxBH,aAA6E,IAE7ED,wBAAwB,CAACC,aAAa,CAAC,GAAGA,aAAa,CAAC,CAAC,CAAC,GAAGA,aAAa;AAE5E,MAAMI,kBAAkB,GAAGA,CAAC;EAC1BC,SAAS;EACTC,UAAU;EACVC,SAAS;EACTC,QAAQ;EACRC,SAAS;EACTC;AAIuC,CAAC,KACxC,CAACA,QAAQ,KAAKC,SAAS,IAAIL,UAAU,IAAII,QAAQ,MAChDF,QAAQ,KAAKG,SAA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