{"version":3,"file":"Portal.cjs","names":["usePortals","useRef"],"sources":["../../../src/portals/Portal/Portal.tsx"],"sourcesContent":["import { cloneElement, useEffect, useState, type ReactElement } from 'react';\nimport { isPortalHandler, select, type PortalHandler } from '@pastweb/tools';\nimport { useRef } from '../../useRef';\n// import { useMounted } from '../../useMounted';\nimport { usePortals } from '../usePortals';\nimport type { PortalProps, Portals } from './types';\n\n/**\n * Wires a {@link usePortal} handler to a portal path from the installed portal\n * descriptor.\n *\n * The component itself renders `null`; the child element is mounted by the\n * entry returned from the portal `getEntry` function when `use.open()` is\n * called.\n *\n * @param props - Portal target path, handler, and child element.\n * @returns `null`.\n *\n * @throws When `use` is not a valid tools portal handler.\n *\n * @example\n * ```tsx\n * const modal = usePortal();\n *\n * <Portal path=\"modal\" use={modal}>\n *   <Dialog />\n * </Portal>\n * ```\n */\nexport function Portal(props: PortalProps) {\n  const { children, path, use } = props;\n\n  if (!isPortalHandler(use)) {\n    throw Error(\n      'Portal error - the \"use\" property must be a PortalHandler Object.'\n    );\n  }\n\n  const portals = usePortals() as Portals;\n  const handler = useRef<PortalHandler | null>(null);\n  const [entryId, setEntryId] = useState<string | false>(false);\n  const component = useRef<ReactElement>(cloneElement(children));\n\n  useEffect(() => {\n    component.value = cloneElement(children);\n  }, [children]);\n\n  useEffect(() => {\n    if (!use) return;\n\n    use.open = () => {\n      if (entryId) return entryId;\n\n      const createHandler = select(portals, path);\n      handler.value = createHandler(component.value);\n      const h = handler.value as PortalHandler;\n\n      h.onRemove(() => {\n        use.id = false;\n        setEntryId(false);\n      });\n\n      use.update = h.update;\n      use.close = () => h.close();\n      use.remove = () => h.remove();\n\n      const id = h.open();\n      use.id = id;\n      setEntryId(id);\n\n      return id;\n    };\n\n    use.isReady();\n  }, [use]);\n\n  return null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,OAAO,OAAoB;CACzC,MAAM,EAAE,UAAU,MAAM,QAAQ;CAEhC,IAAI,EAAA,GAAA,eAAA,gBAAA,CAAiB,GAAG,GACtB,MAAM,MACJ,qEACF;CAGF,MAAM,UAAUA,sCAAAA,WAAW;CAC3B,MAAM,UAAUC,sBAAAA,OAA6B,IAAI;CACjD,MAAM,CAAC,SAAS,eAAA,GAAA,MAAA,SAAA,CAAuC,KAAK;CAC5D,MAAM,YAAYA,sBAAAA,QAAAA,GAAAA,MAAAA,aAAAA,CAAkC,QAAQ,CAAC;CAE7D,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,UAAU,SAAA,GAAA,MAAA,aAAA,CAAqB,QAAQ;CACzC,GAAG,CAAC,QAAQ,CAAC;CAEb,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,IAAI,CAAC,KAAK;EAEV,IAAI,aAAa;GACf,IAAI,SAAS,OAAO;GAEpB,MAAM,iBAAA,GAAA,eAAA,OAAA,CAAuB,SAAS,IAAI;GAC1C,QAAQ,QAAQ,cAAc,UAAU,KAAK;GAC7C,MAAM,IAAI,QAAQ;GAElB,EAAE,eAAe;IACf,IAAI,KAAK;IACT,WAAW,KAAK;GAClB,CAAC;GAED,IAAI,SAAS,EAAE;GACf,IAAI,cAAc,EAAE,MAAM;GAC1B,IAAI,eAAe,EAAE,OAAO;GAE5B,MAAM,KAAK,EAAE,KAAK;GAClB,IAAI,KAAK;GACT,WAAW,EAAE;GAEb,OAAO;EACT;EAEA,IAAI,QAAQ;CACd,GAAG,CAAC,GAAG,CAAC;CAER,OAAO;AACT"}