export interface CalculateOverlayPositionOptions { /** The closest parent element that is `position: relative` */ relativeElement : HTMLElement ; /** The DOM Node that was hovered */ target : HTMLElement ; /** The DOM Node of the tooltip */ hoverOverlayElement : HTMLElement ; } export interface CSSOffsets { /** Offset from the left in pixel */ left : number ; /** Offset from the top in pixel */ top : number ; } /**
 * Calculates the desired position of the hover overlay depending on the container,
 * the hover target and the size of the hover overlay
 */ export const calculateOverlayPosition = ( { relativeElement , target , hoverOverlayElement , } ) : CSSOffsets => { const relativeElementBounds = relativeElement . getBoundingClientRect ( ) ; const targetBounds = target . getBoundingClientRect ( ) ; const hoverOverlayBounds = hoverOverlayElement . getBoundingClientRect ( ) ; const relativeHoverOverlayLeft = targetBounds . left + relativeElement . scrollLeft - relativeElementBounds . left ; let relativeHoverOverlayTop ; if ( targetBounds . top - hoverOverlayBounds . height < Math . max ( relativeElementBounds . top , 0 ) ) { relativeHoverOverlayTop = targetBounds . bottom - relativeElementBounds . top + relativeElement . scrollTop ; } else { const relativeTargetTop = targetBounds . top - relativeElementBounds . top + relativeElement . scrollTop ; relativeHoverOverlayTop = relativeTargetTop - hoverOverlayBounds . height ; } return { left : relativeHoverOverlayLeft , top : relativeHoverOverlayTop } ; } ;