export function maxHeightForDirection()

in src/popup/position.ts [179:215]


export function maxHeightForDirection(direction: Directions, anchorNode: Element, containerNode?: Element | null) {
  const container = containerNode || document.documentElement;
  const domRect = anchorNode.getBoundingClientRect();
  const containerRect = container.getBoundingClientRect();
  const containerTop = domRect.top < 0 ? containerRect.top : Math.max(containerRect.top, 0);
  const topMaxHeight = Math.max(domRect.top - containerTop, 0);
  const containerHeight = Math.max(
    containerRect.height,
    // XXX
    // If container is the document element
    // then we check client height too because we may have situation when
    // "height" from "getBoundingClientRect" less then "clientHeight".
    container === document.documentElement ? container.clientHeight : 0,
  );
  const bottomMaxHeight = Math.max(containerHeight - (topMaxHeight + domRect.height), 0);
  switch (direction) {
    case Directions.TOP_LEFT:
    case Directions.TOP_CENTER:
    case Directions.TOP_RIGHT:
      return topMaxHeight;
    case Directions.BOTTOM_LEFT:
    case Directions.BOTTOM_CENTER:
    case Directions.BOTTOM_RIGHT:
      return bottomMaxHeight;
    case Directions.LEFT_BOTTOM:
    case Directions.RIGHT_BOTTOM:
      return domRect.height + bottomMaxHeight;
    case Directions.LEFT_TOP:
    case Directions.RIGHT_TOP:
      return domRect.height + topMaxHeight;
    case Directions.RIGHT_CENTER:
    case Directions.LEFT_CENTER:
      return domRect.height / 2 + Math.min(bottomMaxHeight / 2, topMaxHeight / 2);
    default:
      return null;
  }
}