{
            if (spaceBelow >= spaceAbove) {
              newTop = triggerRect.bottom + scrollY + cssGap;
            } else {
              newTop =
                triggerRect.top + scrollY - popoverHeightForFitting - cssGap;
            }
          }

          newLeft = triggerRect.left + scrollX;

          if (newLeft + popoverRect.width > viewportWidth + scrollX) {
            let rightAlignedLeft =
              triggerRect.right + scrollX - popoverRect.width;

            if (rightAlignedLeft >= 