{"version":3,"file":"Portal.mjs","names":[],"sources":["../../../src/components/Portal/Portal.tsx"],"sourcesContent":["import { useState } from 'react';\nimport { createPortal } from 'react-dom';\nimport { assignRef, useIsomorphicEffect } from '@mantine/hooks';\nimport { Factory, factory, useProps } from '../../core';\n\nfunction createPortalNode(props: React.ComponentProps<'div'>) {\n  const node = document.createElement('div');\n  node.setAttribute('data-portal', 'true');\n  typeof props.className === 'string' &&\n    node.classList.add(...props.className.split(' ').filter(Boolean));\n  typeof props.style === 'object' && Object.assign(node.style, props.style);\n  typeof props.id === 'string' && node.setAttribute('id', props.id);\n  return node;\n}\n\nexport interface BasePortalProps extends React.ComponentProps<'div'> {\n  /**\n   * Target element where portal should be rendered. Accepts:\n   * - HTMLElement: Renders portal inside this element\n   * - string: CSS selector - renders inside first matching element\n   * - undefined: Uses shared portal node or creates new one based on `reuseTargetNode`\n   *\n   * Note: If selector doesn't match any element, portal will not render\n   */\n  target?: HTMLElement | string;\n\n  /**\n   * When true and target is not specified, all Portal instances share a single\n   * container node appended to document.body. When false, each Portal creates\n   * its own container node.\n   *\n   * Has no effect when target is specified.\n   *\n   * @default true\n   */\n  reuseTargetNode?: boolean;\n}\n\nexport interface PortalProps extends BasePortalProps {\n  /** Content to render inside the portal */\n  children: React.ReactNode;\n}\n\nlet cachedSharedPortalNode: HTMLElement | null = null;\n\nfunction getTargetNode({ target, reuseTargetNode, ...others }: BasePortalProps): HTMLElement {\n  if (target) {\n    if (typeof target === 'string') {\n      return document.querySelector<HTMLElement>(target) || createPortalNode(others);\n    }\n\n    return target;\n  }\n\n  if (reuseTargetNode) {\n    if (cachedSharedPortalNode) {\n      if (document.body.contains(cachedSharedPortalNode)) {\n        return cachedSharedPortalNode;\n      }\n\n      cachedSharedPortalNode = null;\n    }\n\n    const existingNode = document.querySelector<HTMLElement>('[data-mantine-shared-portal-node]');\n\n    if (existingNode) {\n      cachedSharedPortalNode = existingNode;\n      return existingNode;\n    }\n\n    const node = createPortalNode(others);\n    node.setAttribute('data-mantine-shared-portal-node', 'true');\n    document.body.appendChild(node);\n    cachedSharedPortalNode = node;\n    return node;\n  }\n\n  return createPortalNode(others);\n}\n\nexport type PortalFactory = Factory<{\n  props: PortalProps;\n  ref: HTMLDivElement;\n}>;\n\nconst defaultProps = {\n  reuseTargetNode: true,\n} satisfies Partial<PortalProps>;\n\nexport const Portal = factory<PortalFactory>((props) => {\n  const { children, target, reuseTargetNode, ref, ...others } = useProps(\n    'Portal',\n    defaultProps,\n    props\n  );\n\n  const [node, setNode] = useState<HTMLElement | null>(null);\n\n  useIsomorphicEffect(() => {\n    let portalNode: HTMLElement | null = null;\n    let observer: MutationObserver | null = null;\n\n    const initialize = () => {\n      if (!target && !document.body) {\n        return false;\n      }\n\n      portalNode = getTargetNode({ target, reuseTargetNode, ...others });\n\n      if (!target && !reuseTargetNode) {\n        document.body.appendChild(portalNode);\n      }\n\n      observer?.disconnect();\n      assignRef(ref, portalNode);\n      setNode(portalNode);\n      return true;\n    };\n\n    if (!initialize()) {\n      setNode(null);\n      observer = new MutationObserver(initialize);\n      observer.observe(document.documentElement, { childList: true });\n    }\n\n    return () => {\n      observer?.disconnect();\n\n      if (!target && !reuseTargetNode) {\n        portalNode?.remove();\n      }\n    };\n  }, [target]);\n\n  if (!node) {\n    return null;\n  }\n\n  return createPortal(<>{children}</>, node);\n});\n\nPortal.displayName = '@mantine/core/Portal';\n\nexport namespace Portal {\n  export type Props = PortalProps;\n}\n"],"mappings":";;;;;;;;AAKA,SAAS,iBAAiB,OAAoC;CAC5D,MAAM,OAAO,SAAS,cAAc,KAAK;CACzC,KAAK,aAAa,eAAe,MAAM;CACvC,OAAO,MAAM,cAAc,YACzB,KAAK,UAAU,IAAI,GAAG,MAAM,UAAU,MAAM,GAAG,CAAC,CAAC,OAAO,OAAO,CAAC;CAClE,OAAO,MAAM,UAAU,YAAY,OAAO,OAAO,KAAK,OAAO,MAAM,KAAK;CACxE,OAAO,MAAM,OAAO,YAAY,KAAK,aAAa,MAAM,M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