@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 4.75 kB
Source Map (JSON)
{"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,EAAU;CAEnE,MAAM,WAAW,OAMd;EACD;EACA,UAAU;EACV,MAAM;EACN,UAAU;EACV,UAAU;EACX,CAAC;CAEF,MAAM,kBAAkB,OAAO,eAAe;AAG9C,UAAS,QAAQ,OAAO;AACxB,UAAS,QAAQ,WAAW;AAM5B,KACE,gBAAgB,YAAY,KAAA,KAC5B,mBAAmB,KAAA,KACnB,SAAS,QAAQ,MACjB;EACA,MAAM,OAAO,SAAS,QAAQ;AAE9B,MADe,KAAK,aAAa,YACvB,EAAE;AACV,aAAU,mBAAmB,KAAK,CAAC;AACnC,YAAS,QAAQ,WAAW;;AAG9B,MAAI,CAAC,SAAS,QAAQ,UAAU;GAC9B,MAAM,eAAe;IACnB,MAAM,OAAO,KAAK,aAAa,YAAY;AAC3C,QAAI,QAAQ,CAAC,SAAS,QAAQ,SAC5B,WAAU,mBAAmB,SAAS,QAAQ,KAAK,CAAC;AAEtD,aAAS,QAAQ,WAAW;;GAE9B,MAAM,WAAW,IAAI,iBAAiB,OAAO;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