{"version":3,"file":"useLayerZIndex.mjs","names":[],"sources":["../../../src/base-ui/zIndex/useLayerZIndex.tsx"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nimport { type LayerTier } from './constants';\nimport { acquireLayerZIndex } from './manager';\n\nexport interface LayerZIndexResult<T extends HTMLElement = HTMLElement> {\n  ref: (node: T | null) => void;\n  zIndex: number | undefined;\n}\n\nexport function useLayerZIndex<T extends HTMLElement = HTMLElement>(\n  tier: LayerTier,\n  explicitZIndex?: number,\n): LayerZIndexResult<T> {\n  const [zIndex, setZIndex] = useState<number | undefined>(undefined);\n\n  const stateRef = useRef<{\n    tier: LayerTier;\n    explicit: number | undefined;\n    node: T | null;\n    observer: MutationObserver | null;\n    prevOpen: boolean;\n  }>({\n    tier,\n    explicit: explicitZIndex,\n    node: null,\n    observer: null,\n    prevOpen: false,\n  });\n\n  const prevExplicitRef = useRef(explicitZIndex);\n\n  // Keep ref in sync with latest props so prop changes after mount are respected.\n  stateRef.current.tier = tier;\n  stateRef.current.explicit = explicitZIndex;\n\n  // When explicitZIndex changes from a value to undefined, and the node is\n  // currently open, we need to acquire a dynamic z-index and start the\n  // observer — neither was set up because the initial mount had an explicit\n  // value that short-circuited the ref callback.\n  if (\n    prevExplicitRef.current !== undefined &&\n    explicitZIndex === undefined &&\n    stateRef.current.node\n  ) {\n    const node = stateRef.current.node;\n    const isOpen = node.hasAttribute('data-open');\n    if (isOpen) {\n      setZIndex(acquireLayerZIndex(tier));\n      stateRef.current.prevOpen = true;\n    }\n    // Ensure observer is running for future open/close changes.\n    if (!stateRef.current.observer) {\n      const handle = () => {\n        const open = node.hasAttribute('data-open');\n        if (open && !stateRef.current.prevOpen) {\n          setZIndex(acquireLayerZIndex(stateRef.current.tier));\n        }\n        stateRef.current.prevOpen = open;\n      };\n      const observer = new MutationObserver(handle);\n      observer.observe(node, {\n        attributes: true,\n        attributeFilter: ['data-open', 'data-closed'],\n      });\n      stateRef.current.observer = observer;\n    }\n  }\n  prevExplicitRef.current = explicitZIndex;\n\n  const ref = useCallback((node: T | null) => {\n    if (node === stateRef.current.node) return;\n    stateRef.current.observer?.disconnect();\n    stateRef.current.observer = null;\n    stateRef.current.node = node;\n    stateRef.current.prevOpen = false;\n    if (!node) return;\n    if (stateRef.current.explicit !== undefined) return;\n    const handle = () => {\n      const isOpen = node.hasAttribute('data-open');\n      if (isOpen && !stateRef.current.prevOpen) {\n        setZIndex(acquireLayerZIndex(stateRef.current.tier));\n      }\n      stateRef.current.prevOpen = isOpen;\n    };\n    handle();\n    const observer = new MutationObserver(handle);\n    observer.observe(node, {\n      attributes: true,\n      attributeFilter: ['data-open', 'data-closed'],\n    });\n    stateRef.current.observer = observer;\n  }, []);\n\n  useEffect(\n    () => () => {\n      stateRef.current.observer?.disconnect();\n    },\n    [],\n  );\n\n  return { zIndex: stateRef.current.explicit ?? zIndex, ref };\n}\n"],"mappings":";;;AAUA,SAAgB,eACd,MACA,gBACsB;CACtB,MAAM,CAAC,QAAQ,aAAa,SAA6B,KAAA,CAAS;CAElE,MAAM,WAAW,OAMd;EACD;EACA,UAAU;EACV,MAAM;EACN,UAAU;EACV,UAAU;CACZ,CAAC;CAED,MAAM,kBAAkB,OAAO,cAAc;CAG7C,SAAS,QAAQ,OAAO;CACxB,SAAS,QAAQ,WAAW;CAM5B,IACE,gBAAgB,YAAY,KAAA,KAC5B,mBAAmB,KAAA,KACnB,SAAS,QAAQ,MACjB;EACA,MAAM,OAAO,SAAS,QAAQ;EAE9B,IADe,KAAK,aAAa,WACxB,GAAG;GACV,UAAU,mBAAmB,IAAI,CAAC;GAClC,SAAS,QAAQ,WAAW;EAC9B;EAEA,IAAI,CAAC,SAAS,QAAQ,UAAU;GAC9B,MAAM,eAAe;IACnB,MAAM,OAAO,KAAK,aAAa,WAAW;IAC1C,IAAI,QAAQ,CAAC,SAAS,QAAQ,UAC5B,UAAU,mBAAmB,SAAS,QAAQ,IAAI,CAAC;IAErD,SAAS,QAAQ,WAAW;GAC9B;GAC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