'use client' import { usePortalManager } from "./chunk-HK66PB7M.mjs"; // src/portal.tsx import { useSafeLayoutEffect } from "@chakra-ui/react-use-safe-layout-effect"; import { createContext } from "@chakra-ui/react-context"; import { createPortal } from "react-dom"; import { useEffect, useMemo, useRef, useState } from "react"; import { jsx } from "react/jsx-runtime"; var [PortalContextProvider, usePortalContext] = createContext({ strict: false, name: "PortalContext" }); var PORTAL_CLASSNAME = "chakra-portal"; var PORTAL_SELECTOR = `.chakra-portal`; var Container = (props) => /* @__PURE__ */ jsx( "div", { className: "chakra-portal-zIndex", style: { position: "absolute", zIndex: props.zIndex, top: 0, left: 0, right: 0 // NB: Don't add `bottom: 0`, it makes the entire app unusable // @see https://github.com/chakra-ui/chakra-ui/issues/3201 }, children: props.children } ); var DefaultPortal = (props) => { const { appendToParentPortal, children } = props; const [tempNode, setTempNode] = useState(null); const portal = useRef(null); const [, forceUpdate] = useState({}); useEffect(() => forceUpdate({}), []); const parentPortal = usePortalContext(); const manager = usePortalManager(); useSafeLayoutEffect(() => { if (!tempNode) return; const doc = tempNode.ownerDocument; const host = appendToParentPortal ? parentPortal != null ? parentPortal : doc.body : doc.body; if (!host) return; portal.current = doc.createElement("div"); portal.current.className = PORTAL_CLASSNAME; host.appendChild(portal.current); forceUpdate({}); const portalNode = portal.current; return () => { if (host.contains(portalNode)) { host.removeChild(portalNode); } }; }, [tempNode]); const _children = (manager == null ? void 0 : manager.zIndex) ? /* @__PURE__ */ jsx(Container, { zIndex: manager == null ? void 0 : manager.zIndex, children }) : children; return portal.current ? createPortal( /* @__PURE__ */ jsx(PortalContextProvider, { value: portal.current, children: _children }), portal.current ) : /* @__PURE__ */ jsx( "span", { ref: (el) => { if (el) setTempNode(el); } } ); }; var ContainerPortal = (props) => { const { children, containerRef, appendToParentPortal } = props; const containerEl = containerRef.current; const host = containerEl != null ? containerEl : typeof window !== "undefined" ? document.body : void 0; const portal = useMemo(() => { const node = containerEl == null ? void 0 : containerEl.ownerDocument.createElement("div"); if (node) node.className = PORTAL_CLASSNAME; return node; }, [containerEl]); const [, forceUpdate] = useState({}); useSafeLayoutEffect(() => forceUpdate({}), []); useSafeLayoutEffect(() => { if (!portal || !host) return; host.appendChild(portal); return () => { host.removeChild(portal); }; }, [portal, host]); if (host && portal) { return createPortal( /* @__PURE__ */ jsx(PortalContextProvider, { value: appendToParentPortal ? portal : null, children }), portal ); } return null; }; function Portal(props) { const portalProps = { appendToParentPortal: true, ...props }; const { containerRef, ...rest } = portalProps; return containerRef ? /* @__PURE__ */ jsx(ContainerPortal, { containerRef, ...rest }) : /* @__PURE__ */ jsx(DefaultPortal, { ...rest }); } Portal.className = PORTAL_CLASSNAME; Portal.selector = PORTAL_SELECTOR; Portal.displayName = "Portal"; export { Portal }; //# sourceMappingURL=chunk-34PD6CUK.mjs.map