{"version":3,"file":"usePopoverPosition.mjs","sources":["../../../packages/popover/usePopoverPosition.ts"],"sourcesContent":["import {\n  useRef,\n  useLayoutEffect,\n  useState,\n  CSSProperties,\n  RefObject,\n} from 'react'\nimport { useWindowSize } from '../hooks/index.js'\nimport { PopoverRootProps } from './types.js'\nimport { INITIAL_STYLE, DEFAULT_PLACEMENT } from './constants.js'\nimport { calculatePosition } from './calculatePosition.js'\n\nexport const usePopoverPosition = <\n  P extends HTMLDivElement,\n  W extends HTMLDivElement,\n>({\n  placement = DEFAULT_PLACEMENT,\n  anchorRef,\n  style: propsStyle,\n}: Pick<PopoverRootProps, 'placement' | 'anchorRef' | 'style'>): {\n  popoverRef: RefObject<P>\n  wrapperRef: RefObject<W>\n  style: CSSProperties\n} => {\n  const popoverRef = useRef<P>(null)\n  const wrapperRef = useRef<W>(null)\n\n  const [popoverStyle, setPopoverStyle] = useState<CSSProperties>(INITIAL_STYLE)\n  const [windowWidth, windowHeight] = useWindowSize()\n\n  useLayoutEffect(() => {\n    if (!anchorRef.current || !popoverRef.current || !wrapperRef.current) return\n\n    const anchorRect = anchorRef.current.getBoundingClientRect()\n    const popoverRect = popoverRef.current.getBoundingClientRect()\n    const wrapperRect = wrapperRef.current.getBoundingClientRect()\n\n    const position = calculatePosition(\n      anchorRect,\n      popoverRect,\n      wrapperRect,\n      placement,\n    )\n    setPopoverStyle(position)\n  }, [anchorRef, placement, windowWidth, windowHeight])\n\n  const style = {\n    ...propsStyle,\n    ...popoverStyle,\n  }\n\n  return {\n    popoverRef,\n    wrapperRef,\n    style,\n  }\n}\n"],"names":["usePopoverPosition","_ref","placement","DEFAULT_PLACEMENT","anchorRef","style","propsStyle","popoverRef","useRef","wrapperRef","popoverStyle","setPopoverStyle","useState","INITIAL_STYLE","windowWidth","windowHeight","useWindowSize","useLayoutEffect","current","anchorRect","getBoundingClientRect","popoverRect","wrapperRect","position","calculatePosition"],"mappings":";;;;;;;;;;;;;;;;AAYaA,MAAAA,kBAAkB,GAA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