{"version":3,"file":"useDialogPortal.cjs","sources":["../../../src/components/Dialog/useDialogPortal.ts"],"sourcesContent":["'use client'\n\nimport { type ReactNode, type RefObject, useCallback, useLayoutEffect, useRef } from 'react'\nimport { createPortal } from 'react-dom'\n\nexport function useDialogPortal(parent?: HTMLElement | RefObject<HTMLElement>, id?: string) {\n  const portalContainer = useRef<HTMLDivElement | null>(\n    typeof document === 'undefined' ? null : (document.createElement('div') as HTMLDivElement),\n  ).current\n\n  useLayoutEffect(() => {\n    if (!portalContainer) {\n      return\n    }\n\n    if (id) {\n      portalContainer.id = id\n    }\n\n    const parentElement = parent && 'current' in parent ? parent.current : parent\n    // SSR を考慮し、useEffect 内で初期値 document.body を指定\n    const actualParent = parentElement || document.body\n\n    actualParent.appendChild(portalContainer)\n\n    return () => {\n      actualParent.removeChild(portalContainer)\n    }\n  }, [id, parent, portalContainer])\n\n  const wrappedCreatePortal = useCallback(\n    (children: ReactNode) => {\n      if (portalContainer === null) {\n        return null\n      }\n\n      return createPortal(children, portalContainer)\n    },\n    [portalContainer],\n  )\n\n  return {\n    createPortal: wrappedCreatePortal,\n  }\n}\n"],"names":[],"mappings":";;;;;;AAKM;;;;;;;AAWA;;AAGF;;AAEA;AAEA;AAEA;AACE;AACF;;AAGF;AAEI;AACE;;AAGF;AACF;;AAKA;;AAEJ;;"}