2024-10-13 17:37:01 +00:00
|
|
|
import type { ComputePositionConfig } from '@floating-ui/dom'
|
|
|
|
import { computePosition } from '@floating-ui/dom'
|
2022-11-13 11:57:39 +00:00
|
|
|
|
|
|
|
export const updateFloatingUi = async (
|
|
|
|
reference: HTMLElement,
|
|
|
|
floating: HTMLElement,
|
|
|
|
options: Partial<ComputePositionConfig>,
|
2024-10-13 17:37:01 +00:00
|
|
|
arrow?: HTMLElement,
|
2022-11-13 11:57:39 +00:00
|
|
|
) => {
|
2022-12-02 16:17:37 +00:00
|
|
|
options.placement = options.placement || 'bottom'
|
2022-11-13 11:57:39 +00:00
|
|
|
const { x, y, middlewareData } = await computePosition(reference, floating, options)
|
|
|
|
|
|
|
|
floating.style.left = `${x}px`
|
|
|
|
floating.style.top = `${y}px`
|
|
|
|
|
|
|
|
if (arrow) {
|
2022-12-02 16:17:37 +00:00
|
|
|
const { x: arrowX, y: arrowY } = middlewareData.arrow!
|
2022-11-13 11:57:39 +00:00
|
|
|
|
|
|
|
const staticSide = {
|
|
|
|
top: 'bottom',
|
|
|
|
right: 'left',
|
|
|
|
bottom: 'top',
|
2024-10-13 17:37:01 +00:00
|
|
|
left: 'right',
|
2022-11-13 11:57:39 +00:00
|
|
|
}[options.placement.split('-')[0]]
|
|
|
|
|
|
|
|
Object.assign(arrow.style, {
|
|
|
|
left: arrowX != null ? `${arrowX}px` : '',
|
|
|
|
top: arrowY != null ? `${arrowY}px` : '',
|
|
|
|
right: '',
|
|
|
|
bottom: '',
|
2024-10-13 17:37:01 +00:00
|
|
|
[staticSide!]: '-4px',
|
2022-11-13 11:57:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|