{"version":3,"file":"DialogPortal.mjs","names":[],"sources":["../../../../../src/components/Dialog/service/DialogPortal.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { PropsWithChildren } from 'react';\nimport React, { useCallback, useEffect, useState } from 'react';\nimport { useDialogIsOpen, useOpenedDialogCount } from '../hooks';\nimport { Portal } from '../../Portal/Portal';\nimport {\n  modalDialogManagerId,\n  useDialogManager,\n  useNearestDialogManagerContext,\n} from '../../../context';\n\nconst shouldCloseOnOutsideClick = ({\n  dialog,\n  managerCloseOnClickOutside,\n}: {\n  dialog: { closeOnClickOutside?: boolean };\n  managerCloseOnClickOutside: boolean;\n}) => dialog.closeOnClickOutside ?? managerCloseOnClickOutside;\n\nexport type DialogPortalDestinationProps = {\n  captureOutsideClicks?: boolean;\n  className?: string;\n};\n\nexport const DialogPortalDestination = ({\n  captureOutsideClicks,\n  className,\n}: DialogPortalDestinationProps) => {\n  const { dialogManager } = useNearestDialogManagerContext() ?? {};\n  const openedDialogCount = useOpenedDialogCount({ dialogManagerId: dialogManager?.id });\n  const [destinationRoot, setDestinationRoot] = useState<HTMLDivElement | null>(null);\n\n  // Clicks on the overlay itself or outside it close open dialogs; clicks on dialog contents or\n  // inside another portal are ignored.\n  useEffect(() => {\n    if (!destinationRoot || !dialogManager) return;\n\n    const handleDocumentClick = (event: MouseEvent) => {\n      const target = event.target as Node;\n      const clickedOverlay = (target as HTMLElement).closest?.(\n        '[data-str-chat__portal-id]',\n      );\n      if (clickedOverlay && clickedOverlay !== destinationRoot) return;\n      if (target !== destinationRoot && destinationRoot.contains(target)) return;\n      // Deferred so the click handlers of the target (e.g. a toggle button) run first.\n      setTimeout(() => {\n        Object.values(dialogManager.state.getLatestValue().dialogsById).forEach(\n          (dialog) => {\n            if (!dialog.isOpen) return;\n            if (\n              !shouldCloseOnOutsideClick({\n                dialog,\n                managerCloseOnClickOutside: dialogManager.closeOnClickOutside,\n              })\n            )\n              return;\n            dialogManager.close(dialog.id);\n          },\n        );\n      }, 0);\n    };\n\n    document.addEventListener('click', handleDocumentClick, { capture: true });\n\n    return () => {\n      document.removeEventListener('click', handleDocumentClick, { capture: true });\n    };\n  }, [destinationRoot, dialogManager]);\n\n  if (!openedDialogCount) return null;\n\n  const isModalDialogManager = dialogManager?.id === modalDialogManagerId;\n\n  return (\n    <div\n      className={clsx(\n        'str-chat__dialog-overlay',\n        {\n          'str-chat__dialog-overlay--modal': isModalDialogManager,\n          'str-chat__dialog-overlay--with-outside-click-capture': captureOutsideClicks,\n        },\n        className,\n      )}\n      data-str-chat__portal-id={dialogManager?.id}\n      data-testid='str-chat__dialog-overlay'\n      ref={setDestinationRoot}\n      style={\n        {\n          '--str-chat__dialog-overlay-height': openedDialogCount > 0 ? '100%' : '0',\n        } as React.CSSProperties\n      }\n    />\n  );\n};\n\ntype DialogPortalEntryProps = {\n  dialogId: string;\n  dialogManagerId?: string;\n};\n\nexport const DialogPortalEntry = ({\n  children,\n  dialogId,\n  dialogManagerId,\n}: PropsWithChildren<DialogPortalEntryProps>) => {\n  const { dialogManager } = useDialogManager({ dialogId, dialogManagerId });\n  const dialogIsOpen = useDialogIsOpen(dialogId, dialogManagerId);\n\n  const getPortalDestination = useCallback(\n    () => document.querySelector(`div[data-str-chat__portal-id=\"${dialogManager.id}\"]`),\n    [dialogManager.id],\n  );\n\n  return (\n    <Portal getPortalDestination={getPortalDestination} isOpen={dialogIsOpen}>\n      {children}\n    </Portal>\n  );\n};\n"],"mappings":";;;;;;;AAWA,IAAM,6BAA6B,EACjC,QACA,iCAII,OAAO,uBAAuB;AAOpC,IAAa,2BAA2B,EACtC,sBACA,gBACkC;CAClC,MAAM,EAAE,kBAAkB,+BAA+B,KAAK,CAAC;CAC/D,MAAM,oBAAoB,qBAAqB,EAAE,iBAAiB,eAAe,GAAG,CAAC;CACrF,MAAM,CAAC,iBAAiB,sBAAsB,SAAgC,IAAI;CAIlF,gBAAgB;EACd,IAAI,CAAC,mBAAmB,CAAC,eAAe;EAExC,MAAM,uBAAuB,UA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