{"version":3,"file":"DialogAnchor.mjs","names":[],"sources":["../../../../../src/components/Dialog/service/DialogAnchor.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ComponentProps, PropsWithChildren } from 'react';\nimport React, { useEffect, useRef, useState } from 'react';\nimport { FocusScope } from '@react-aria/focus';\nimport { DialogPortalEntry } from './DialogPortal';\nimport { useDialog, useDialogIsOpen } from '../hooks';\nimport { type OffsetOpt, usePopoverPosition } from '../hooks/usePopoverPosition';\nimport type { PopperLikePlacement } from '../hooks';\nimport type { Placement } from '@floating-ui/react';\nimport { AriaLiveOutlet } from '../../Accessibility';\n\nexport interface DialogAnchorOptions {\n  open: boolean;\n  placement: PopperLikePlacement;\n  referenceElement: HTMLElement | null;\n  allowFlip?: boolean;\n  updateKey?: unknown;\n  updatePositionOnContentResize?: boolean;\n  offset?: OffsetOpt;\n}\n\nexport function useDialogAnchor<T extends HTMLElement>({\n  allowFlip,\n  offset,\n  open,\n  placement,\n  referenceElement,\n  updateKey,\n  updatePositionOnContentResize = false,\n}: DialogAnchorOptions) {\n  const [popperElement, setPopperElement] = useState<T | null>(null);\n  // The first placement chosen once `popperElement` is set, so the popover does not jump to a\n  // better-fitting placement on update. Reset when `popperElement` is unset (!open).\n  const [stabilisedChosenPlacement, setStabilisedChosenPlacement] =\n    useState<Placement | null>(null);\n\n  const {\n    placement: chosenPlacement,\n    refs,\n    strategy,\n    update,\n    x,\n    y,\n  } = usePopoverPosition({\n    allowFlip,\n    freeze: true,\n    offset,\n    placement: stabilisedChosenPlacement ?? placement,\n  });\n\n  if (!stabilisedChosenPlacement && popperElement && placement !== chosenPlacement) {\n    setStabilisedChosenPlacement(chosenPlacement);\n  } else if (stabilisedChosenPlacement && !popperElement) {\n    setStabilisedChosenPlacement(null);\n  }\n\n  // The reference is frozen while open so nested popovers (e.g. submenus) stay aligned to the\n  // original anchor.\n  const frozenReferenceRef = useRef<HTMLElement | null>(null);\n  if (open && referenceElement && !frozenReferenceRef.current) {\n    frozenReferenceRef.current = referenceElement;\n  }\n  if (!open) {\n    frozenReferenceRef.current = null;\n  }\n  const effectiveReference = open ? frozenReferenceRef.current : referenceElement;\n\n  useEffect(() => {\n    refs.setReference(effectiveReference);\n  }, [effectiveReference, refs]);\n\n  useEffect(() => {\n    refs.setFloating(popperElement);\n  }, [popperElement, refs]);\n\n  useEffect(() => {\n    if (open && popperElement && effectiveReference) {\n      // Re-runs when the reference becomes available or `updateKey` changes (e.g. a submenu opens).\n      // The reference is often not visible, so the position is forced to update before showing.\n      update?.();\n    }\n  }, [open, placement, popperElement, update, updateKey, effectiveReference]);\n\n  useEffect(() => {\n    if (!popperElement || !updatePositionOnContentResize) return;\n\n    const resizeObserver = new ResizeObserver(update);\n\n    resizeObserver.observe(popperElement);\n\n    return () => {\n      resizeObserver.disconnect();\n    };\n  }, [popperElement, update, updatePositionOnContentResize]);\n\n  if (popperElement && !open) {\n    setPopperElement(null);\n  }\n\n  return {\n    placement: stabilisedChosenPlacement ?? chosenPlacement,\n    setPopperElement,\n    styles: {\n      left: x ?? 0,\n      position: strategy,\n      top: y ?? 0,\n    } as React.CSSProperties,\n  };\n}\n\nexport type DialogAnchorProps = PropsWithChildren<Partial<DialogAnchorOptions>> & {\n  /**\n   * Per-dialog override for outside-click dismissal. Defaults to the manager's\n   * `closeOnClickOutside`.\n   */\n  closeOnClickOutside?: boolean;\n  /**\n   * Whether pressing Escape closes the dialog. Set to `false` for a dialog that only its own close\n   * control should dismiss.\n   * @default true\n   */\n  closeOnEscape?: boolean;\n  id: string;\n  /** Delay (ms) before unmounting a closed dialog, for CSS exit animations. Defaults to 0. */\n  closeTransitionMs?: number;\n  dialogManagerId?: string;\n  focus?: boolean;\n  trapFocus?: boolean;\n} & ComponentProps<'div'>;\n\nexport const DialogAnchor = ({\n  allowFlip = true,\n  children,\n  className,\n  closeOnClickOutside,\n  closeOnEscape = true,\n  closeTransitionMs = 0,\n  dialogManagerId,\n  focus = true,\n  id,\n  offset,\n  placement = 'auto',\n  referenceElement = null,\n  tabIndex,\n  trapFocus,\n  updateKey,\n  updatePositionOnContentResize,\n  ...restDivProps\n}: DialogAnchorProps) => {\n  // During the `closeTransitionMs` delay the contents stay mounted with\n  // `data-str-chat-dialog-state=\"closing\"` (otherwise `\"open\"`). CSS combines it with\n  // `data-str-chat-placement` to pick the closing keyframe; JS only owns the timing.\n  const dialog = useDialog({ closeOnClickOutside, dialogManagerId, id });\n  const open = useDialogIsOpen(id, dialogManagerId);\n  const [shouldRender, setShouldRender] = useState(open);\n  const closeTimeoutRef = useRef<NodeJS.Timeout | null>(null);\n  const isClosing = !open && shouldRender;\n\n  useEffect(() => {\n    if (open) {\n      setShouldRender(true);\n      if (closeTimeoutRef.current) {\n        clearTimeout(closeTimeoutRef.current);\n        closeTimeoutRef.current = null;\n      }\n      return;\n    }\n\n    if (!shouldRender) return;\n    if (!closeTransitionMs) {\n      setShouldRender(false);\n      return;\n    }\n\n    closeTimeoutRef.current = setTimeout(() => {\n      setShouldRender(false);\n      closeTimeoutRef.current = null;\n    }, closeTransitionMs);\n  }, [closeTransitionMs, open, shouldRender]);\n\n  useEffect(\n    () => () => {\n      if (closeTimeoutRef.current) clearTimeout(closeTimeoutRef.current);\n    },\n    [],\n  );\n\n  const {\n    placement: chosenPlacement,\n    setPopperElement,\n    styles,\n  } = useDialogAnchor<HTMLDivElement>({\n    allowFlip,\n    offset,\n    open: shouldRender,\n    placement,\n    referenceElement,\n    updateKey,\n    updatePositionOnContentResize,\n  });\n\n  useEffect(() => {\n    if (!open || !closeOnEscape) return;\n    const hideOnEscape = (event: KeyboardEvent) => {\n      if (event.key !== 'Escape' || event.defaultPrevented) return;\n      dialog?.close();\n    };\n\n    document.addEventListener('keyup', hideOnEscape);\n\n    return () => {\n      document.removeEventListener('keyup', hideOnEscape);\n    };\n  }, [closeOnEscape, dialog, open]);\n\n  if (!shouldRender) {\n    return null;\n  }\n\n  const {\n    ['aria-describedby']: ariaDescribedBy,\n    ['aria-label']: ariaLabel,\n    ['aria-labelledby']: ariaLabelledBy,\n    ['aria-modal']: ariaModal,\n    role,\n    ...anchorDivProps\n  } = restDivProps;\n  const resolvedRole = trapFocus ? (role ?? 'dialog') : role;\n  const resolvedAriaModal = trapFocus ? true : ariaModal;\n  const resolvedAriaLabel = ariaLabelledBy ? undefined : ariaLabel;\n\n  return (\n    <DialogPortalEntry dialogId={id} dialogManagerId={dialogManagerId}>\n      {/* A closing dialog releases the trap, so an action that moves focus elsewhere as it closes the\n      dialog (e.g. \"Edit Message\" focusing the composer) keeps it there. */}\n      <FocusScope autoFocus={focus} contain={trapFocus && !isClosing} restoreFocus>\n        <div\n          {...anchorDivProps}\n          aria-describedby={ariaDescribedBy}\n          aria-label={resolvedAriaLabel}\n          aria-labelledby={ariaLabelledBy}\n          aria-modal={resolvedAriaModal}\n          className={clsx('str-chat__dialog-contents', className)}\n          data-str-chat-dialog-state={isClosing ? 'closing' : 'open'}\n          data-str-chat-placement={chosenPlacement}\n          data-testid='str-chat__dialog-contents'\n          ref={setPopperElement}\n          role={resolvedRole}\n          style={styles}\n          tabIndex={typeof tabIndex !== 'undefined' ? tabIndex : 0}\n        >\n          {children}\n          {/* Only an `aria-modal` dialog gets its own live-region outlet, because assistive\n              technologies can suppress live regions outside a modal. */}\n          {resolvedAriaModal && <AriaLiveOutlet layer={1} />}\n        </div>\n      </FocusScope>\n    </DialogPortalEntry>\n  );\n};\n"],"mappings":";;;;;;;;;AAqBA,SAAgB,gBAAuC,EACrD,WACA,QACA,MACA,WACA,kBACA,WACA,gCAAgC,SACV;CACtB,MAAM,CAAC,eAAe,oBAAoB,SAAmB,IAAI;CAGjE,MAAM,CAAC,2BAA2B,gCAChC,SAA2B,IAAI;CAEjC,MAAM,EACJ,WAAW,iBACX,MACA,UACA,QACA,GACA,MACE,mBAAmB;EACrB;EACA,QAAQ;EACR;EACA,WAAW,6BAA6B;CAC1C,CAAC;CAED,IAAI,CAAC,6BAA6B,iBAAiB,cAAc,iBAC/D,6BAA6B,eAAe;MACvC,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