const STEP_LABEL_MIN_GAP_BEFORE_HIDE_PX = 12 export const hasStepLabelOverlap = ( labels: HTMLElement[], minGapBeforeHidePx = STEP_LABEL_MIN_GAP_BEFORE_HIDE_PX, ): boolean => { const sortedLabelRects = labels .map((label) => ({ rect: label.getBoundingClientRect() })) .sort((a, b) => a.rect.left - b.rect.left) for (let i = 0; i < sortedLabelRects.length - 1; i += 1) { const current = sortedLabelRects[i] const next = sortedLabelRects[i + 1] if (!current || !next) continue const horizontalOverlap = current.rect.right - next.rect.left const verticalOverlap = Math.min(current.rect.bottom, next.rect.bottom) - Math.max(current.rect.top, next.rect.top) if (horizontalOverlap > -minGapBeforeHidePx && verticalOverlap > 0) { return true } } return false }