UNPKG

@yamada-ui/portal

Version:

Yamada UI portal component

43 lines (41 loc) 1.22 kB
"use client" import { PortalProvider } from "./chunk-KRSUD7HN.mjs"; // src/container-portal.tsx import { useSafeLayoutEffect } from "@yamada-ui/utils"; import { useMemo, useState } from "react"; import { createPortal } from "react-dom"; import { jsx } from "react/jsx-runtime"; var ContainerPortal = ({ appendToParentPortal, children, containerRef }) => { const parent = containerRef.current; const host = parent != null ? parent : typeof window !== "undefined" ? document.body : void 0; const portal = useMemo(() => { if (!parent) return; const { ownerDocument } = parent; const node = ownerDocument.createElement("div"); node.className = "ui-portal"; return node; }, [parent]); const [, forceUpdate] = useState({}); useSafeLayoutEffect(() => forceUpdate({}), []); useSafeLayoutEffect(() => { if (!portal || !host) return; host.appendChild(portal); return () => { host.removeChild(portal); }; }, [portal, host]); return host && portal ? createPortal( /* @__PURE__ */ jsx(PortalProvider, { value: appendToParentPortal ? portal : null, children }), portal ) : null; }; export { ContainerPortal }; //# sourceMappingURL=chunk-DMWOTLFW.mjs.map