({
elementReference = null,
tooltipReference = null,
tooltipArrowReference = null,
place = 'top',
offset: offsetValue = 10,
strategy = 'absolute',
middlewares = [offset(Number(offsetValue)), defaultFlip, defaultShift],
border,
arrowSize = 8,
}: IComputePositionArgs)
| 6 | const defaultShift = shift({ padding: 5 }) |
| 7 | |
| 8 | const computeTooltipPosition = async ({ |
| 9 | elementReference = null, |
| 10 | tooltipReference = null, |
| 11 | tooltipArrowReference = null, |
| 12 | place = 'top', |
| 13 | offset: offsetValue = 10, |
| 14 | strategy = 'absolute', |
| 15 | middlewares = [offset(Number(offsetValue)), defaultFlip, defaultShift], |
| 16 | border, |
| 17 | arrowSize = 8, |
| 18 | }: IComputePositionArgs) => { |
| 19 | if (!elementReference) { |
| 20 | // elementReference can be null or undefined and we will not compute the position |
| 21 | |
| 22 | // console.error('The reference element for tooltip was not defined: ', elementReference) |
| 23 | return { tooltipStyles: {}, tooltipArrowStyles: {}, place } |
| 24 | } |
| 25 | |
| 26 | if (tooltipReference === null) { |
| 27 | return { tooltipStyles: {}, tooltipArrowStyles: {}, place } |
| 28 | } |
| 29 | |
| 30 | const middleware = [...middlewares] |
| 31 | |
| 32 | if (tooltipArrowReference) { |
| 33 | middleware.push(arrow({ element: tooltipArrowReference as HTMLElement, padding: 5 })) |
| 34 | |
| 35 | return computePosition(elementReference as HTMLElement, tooltipReference as HTMLElement, { |
| 36 | placement: place, |
| 37 | strategy, |
| 38 | middleware, |
| 39 | }).then(({ x, y, placement, middlewareData }) => { |
| 40 | const styles = { left: `${x}px`, top: `${y}px`, border } |
| 41 | |
| 42 | /* c8 ignore start */ |
| 43 | const { x: arrowX, y: arrowY } = middlewareData.arrow ?? { x: 0, y: 0 } |
| 44 | |
| 45 | const staticSide = |
| 46 | { |
| 47 | top: 'bottom', |
| 48 | right: 'left', |
| 49 | bottom: 'top', |
| 50 | left: 'right', |
| 51 | }[placement.split('-')[0]] ?? 'bottom' |
| 52 | /* c8 ignore end */ |
| 53 | |
| 54 | const borderSide = border && { |
| 55 | borderBottom: border, |
| 56 | borderRight: border, |
| 57 | } |
| 58 | |
| 59 | let borderWidth = 0 |
| 60 | if (border) { |
| 61 | const match = `${border}`.match(/(\d+)px/) |
| 62 | if (match?.[1]) { |
| 63 | borderWidth = Number(match[1]) |
| 64 | } else { |
| 65 | /** |
no outgoing calls
no test coverage detected
searching dependent graphs…