{"version":3,"file":"usePopoverPosition.mjs","names":[],"sources":["../../../../../src/components/Dialog/hooks/usePopoverPosition.ts"],"sourcesContent":["import {\n  autoPlacement,\n  type AutoPlacementOptions,\n  autoUpdate,\n  flip as flipMw,\n  offset as offsetMw,\n  type Placement,\n  shift as shiftMw,\n  size as sizeMw,\n  useFloating,\n} from '@floating-ui/react';\n\nconst hasResizeObserver = typeof window !== 'undefined' && 'ResizeObserver' in window;\n\nexport type PopperLikePlacement = Placement | 'auto' | 'auto-start' | 'auto-end';\n\nfunction autoMiddlewareFor(p: PopperLikePlacement) {\n  if (!String(p).startsWith('auto')) return null;\n  const alignment: AutoPlacementOptions['alignment'] =\n    p === 'auto-start' ? 'start' : p === 'auto-end' ? 'end' : undefined;\n  return autoPlacement({ alignment });\n}\n\nexport type OffsetOpt =\n  | number\n  | { mainAxis?: number; crossAxis?: number; alignmentAxis?: number }\n  | [crossAxis: number, mainAxis: number];\n\nfunction toOffsetMw(opt?: OffsetOpt) {\n  if (opt == null) return null;\n  if (Array.isArray(opt)) {\n    const [crossAxis, mainAxis] = opt;\n    return offsetMw({ crossAxis, mainAxis });\n  }\n  if (typeof opt === 'number') return offsetMw(opt);\n  return offsetMw(opt);\n}\n\nexport type UsePopoverParams = {\n  placement?: PopperLikePlacement;\n  /** Flips on overflow, except for 'auto*', 'left*' and 'right*' placements. Defaults to true. */\n  allowFlip?: boolean;\n  /** Shifts the popover to stay inside the viewport. Defaults to true. */\n  allowShift?: boolean;\n  /** Options for the Floating UI `shift()` middleware, merged over the default `padding: 8`. */\n  shiftOptions?: Parameters<typeof shiftMw>[0];\n  /** Constrains the max size to the available space instead of overflowing. Defaults to false. */\n  fitAvailableSpace?: boolean;\n  /** Offset as a number, an object, or a `[crossAxis, mainAxis]` tuple. */\n  offset?: OffsetOpt;\n  /** Disables `autoUpdate`, so the position updates only through `update()`. Defaults to false. */\n  freeze?: boolean;\n  /**\n   * Options of Floating UI's `autoUpdate`, merged over defaults that follow ancestor scroll and\n   * resize (and element resize when `ResizeObserver` exists). Ignored when `freeze` is true.\n   */\n  autoUpdateOptions?: Partial<Parameters<typeof autoUpdate>[3]>;\n};\n\nexport function usePopoverPosition({\n  allowFlip = true,\n  allowShift = true,\n  autoUpdateOptions,\n  fitAvailableSpace = false,\n  freeze = false,\n  offset,\n  placement = 'bottom-start',\n  shiftOptions,\n}: UsePopoverParams) {\n  const autoMw = autoMiddlewareFor(placement);\n  const offsetMiddleware = toOffsetMw(offset);\n  const isSidePlacement = placement.startsWith('left') || placement.startsWith('right');\n  const mergedShiftOptions = shiftOptions\n    ? { padding: 8, ...shiftOptions }\n    : { padding: 8 };\n\n  const middleware = [\n    // Offset runs before the other middleware.\n    ...(offsetMiddleware ? [offsetMiddleware] : []),\n\n    // Either autoPlacement ('auto*') or flip(); flip() is skipped for 'left*' and 'right*'.\n    ...(autoMw ? [autoMw] : allowFlip && !isSidePlacement ? [flipMw()] : []),\n\n    ...(allowShift ? [shiftMw(mergedShiftOptions)] : []),\n\n    // Caps the floating element to the space left in the viewport (after flip/shift) so a tall\n    // popover scrolls inside it instead of overflowing.\n    ...(fitAvailableSpace\n      ? [\n          sizeMw({\n            apply({ availableHeight, availableWidth, elements }) {\n              Object.assign(elements.floating.style, {\n                maxHeight: `${Math.max(0, availableHeight)}px`,\n                maxWidth: `${Math.max(0, availableWidth)}px`,\n              });\n            },\n            padding: 8,\n          }),\n        ]\n      : []),\n  ];\n\n  // 'auto*' seeds with 'bottom'; autoPlacement picks the final placement.\n  const seedPlacement: Placement = String(placement).startsWith('auto')\n    ? 'bottom'\n    : (placement as Placement);\n\n  return useFloating({\n    middleware,\n    placement: seedPlacement,\n    strategy: 'fixed',\n    whileElementsMounted: freeze\n      ? undefined\n      : (reference, floating, update) =>\n          autoUpdate(reference, floating, update, {\n            ancestorResize: true,\n            ancestorScroll: true,\n            animationFrame: false,\n            elementResize: hasResizeObserver,\n            ...autoUpdateOptions,\n          }),\n  });\n}\n"],"mappings":";;AAYA,IAAM,oBAA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