{"version":3,"sources":["../src/portal.tsx"],"sourcesContent":["import { useSafeLayoutEffect } from \"@chakra-ui/react-use-safe-layout-effect\"\nimport { createContext } from \"@chakra-ui/react-context\"\nimport { createPortal } from \"react-dom\"\nimport { usePortalManager } from \"./portal-manager\"\nimport { useEffect, useMemo, useRef, useState } from \"react\"\n\ntype PortalContext = HTMLDivElement | null\n\nconst [PortalContextProvider, usePortalContext] = createContext({\n strict: false,\n name: \"PortalContext\",\n})\n\nconst PORTAL_CLASSNAME = \"chakra-portal\"\nconst PORTAL_SELECTOR = `.chakra-portal`\n\nconst Container = (props: React.PropsWithChildren<{ zIndex: number }>) => (\n \n {props.children}\n \n)\n\n/**\n * Portal that uses `document.body` as container\n */\nconst DefaultPortal = (\n props: React.PropsWithChildren<{ appendToParentPortal?: boolean }>,\n) => {\n const { appendToParentPortal, children } = props\n\n const [tempNode, setTempNode] = useState(null)\n const portal = useRef(null)\n\n const [, forceUpdate] = useState({})\n useEffect(() => forceUpdate({}), [])\n\n const parentPortal = usePortalContext()\n const manager = usePortalManager()\n\n useSafeLayoutEffect(() => {\n if (!tempNode) return\n\n const doc = tempNode.ownerDocument\n const host = appendToParentPortal ? parentPortal ?? doc.body : doc.body\n\n if (!host) return\n\n portal.current = doc.createElement(\"div\")\n portal.current.className = PORTAL_CLASSNAME\n\n host.appendChild(portal.current)\n forceUpdate({})\n\n const portalNode = portal.current\n return () => {\n if (host.contains(portalNode)) {\n host.removeChild(portalNode)\n }\n }\n }, [tempNode])\n\n const _children = manager?.zIndex ? (\n {children}\n ) : (\n children\n )\n\n return portal.current ? (\n createPortal(\n \n {_children}\n ,\n portal.current,\n )\n ) : (\n {\n if (el) setTempNode(el)\n }}\n />\n )\n}\n\ninterface ContainerPortalProps extends React.PropsWithChildren<{}> {\n containerRef: React.RefObject\n /**\n * @default false\n */\n appendToParentPortal?: boolean\n}\n\n/**\n * Portal that uses a custom container\n */\nconst ContainerPortal = (props: ContainerPortalProps) => {\n const { children, containerRef, appendToParentPortal } = props\n const containerEl = containerRef.current\n const host =\n containerEl ?? (typeof window !== \"undefined\" ? document.body : undefined)\n\n const portal = useMemo(() => {\n const node = containerEl?.ownerDocument.createElement(\"div\")\n if (node) node.className = PORTAL_CLASSNAME\n return node\n }, [containerEl])\n\n const [, forceUpdate] = useState({})\n useSafeLayoutEffect(() => forceUpdate({}), [])\n\n useSafeLayoutEffect(() => {\n if (!portal || !host) return\n host.appendChild(portal)\n return () => {\n host.removeChild(portal)\n }\n }, [portal, host])\n\n if (host && portal) {\n return createPortal(\n \n {children}\n ,\n portal,\n )\n }\n\n return null\n}\n\nexport interface PortalProps {\n /**\n * The `ref` to the component where the portal will be attached to.\n */\n containerRef?: React.RefObject\n /**\n * The content or node you'll like to portal\n */\n children: React.ReactNode\n /**\n * If `true`, the portal will check if it is within a parent portal\n * and append itself to the parent's portal node.\n * This provides nesting for portals.\n *\n * If `false`, the portal will always append to `document.body`\n * regardless of nesting. It is used to opt out of portal nesting.\n *\n * @default true\n */\n appendToParentPortal?: boolean\n}\n\n/**\n * Portal\n *\n * Declarative component used to render children into a DOM node\n * that exists outside the DOM hierarchy of the parent component.\n *\n * @see Docs https://chakra-ui.com/portal\n */\n\nexport function Portal(props: PortalProps) {\n const portalProps: PortalProps = {\n appendToParentPortal: true,\n ...props,\n }\n\n const { containerRef, ...rest } = portalProps\n return containerRef ? (\n \n ) : (\n \n )\n}\n\nPortal.className = PORTAL_CLASSNAME\nPortal.selector = PORTAL_SELECTOR\n\nPortal.displayName = \"Portal\"\n"],"mappings":";;;;;;AAAA,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAE7B,SAAS,WAAW,SAAS,QAAQ,gBAAgB;AAanD;AATF,IAAM,CAAC,uBAAuB,gBAAgB,IAAI,cAA6B;AAAA,EAC7E,QAAQ;AAAA,EACR,MAAM;AACR,CAAC;AAED,IAAM,mBAAmB;AACzB,IAAM,kBAAkB;AAExB,IAAM,YAAY,CAAC,UACjB;AA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