Repository: adobe/react-spectrum
File: packages/@react-aria/overlays/src/calculatePosition.ts (TypeScript)

Code before:
    // height, as `bottom` will be relative to this height.  But if the container is static,
    // then it can only be the `document.body`, and `bottom` will be relative to _its_
    // container.
    const containerHeight = (isContainerPositioned ? containerOffsetWithBoundary[size] : containerDimensions[TOTAL_SIZE[size]]);
    position[FLIPPED_DIRECTION[axis]] = Math.floor(containerHeight - childOffset[axis] + offset);
  } else {
    position[axis] = Math.floor(childOffset[axis] + childOffset[size] + offset);
  }
  return position;
}

function getMaxHeight(
  position: Position,
  boundaryDimensions: Dimensions,
  containerOffsetWithBoundary: Offset,
  isContainerPositioned: boolean,
  margins: Position,
  padding: number,
  overlayHeight: number,
  heightGrowthDirection: HeightGrowthDirection,
  containerDimensions: Dimensions
) {
  const containerHeight = (isContainerPositioned ? containerOffsetWithBoundary.height : containerDimensions[TOTAL_SIZE.height]);
  // For cases where position is set via "bottom" instead of "top", we need to calculate the true overlay top
  // with respect to the container.
  let overlayTop = position.top != null ? position.top : (containerHeight - (position.bottom ?? 0) - overlayHeight);
  let maxHeight = heightGrowthDirection !== 'top' ?
    // We want the distance between the top of the overlay to the bottom of the boundary
    Math.max(0,
      (boundaryDimensions.height + boundaryDimensions.top + (boundaryDimensions.scroll.top ?? 0)) // this is the bottom of the boundary
      - overlayTop // this is the top of the overlay
      - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
    )
    // We want the distance between the bottom of the overlay to the top of the boundary
    : Math.max(0,
      (overlayTop + overlayHeight) // this is the bottom of the overlay
      - (boundaryDimensions.top + (boundaryDimensions.scroll.top ?? 0)) // this is the top of the boundary
      - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
    );
  return Math.min(boundaryDimensions.height - (padding * 2), maxHeight);
}

function getAvailableSpace(
  boundaryDimensions: Dimensions, // boundary
  containerOffsetWithBoundary: Offset,
  childOffset: Offset, // trigger
  margins: Position, // overlay
  padding: number, // overlay <-> boundary
  placementInfo: ParsedPlacement
) {
  let {placement, axis, size} = placementInfo;

Review comment (suggestion):
ugh just found this case: 
<img width="471" height="351" alt="image" src="https://github.com/user-attachments/assets/676d6644-2a87-4333-8495-8459e304d08d" />

it seems pretty tied to this scroll position where there the trigger is basically flush with the boundary give or take a little bit. https://reactspectrum.blob.core.windows.net/reactspectrum/4097f3c57914b81db847e123fdc996f066df81b3/storybook/index.html?path=/story/react-aria-components-popover--scrolling-boundary-container&providerSwitcher-express=false

Code after:
    // height, as `bottom` will be relative to this height.  But if the container is static,
    // then it can only be the `document.body`, and `bottom` will be relative to _its_
    // container.
    const containerHeight = (isContainerPositioned ? containerOffsetWithBoundary[size] : containerDimensions[TOTAL_SIZE[size]]);
    position[FLIPPED_DIRECTION[axis]] = Math.floor(containerHeight - childOffset[axis] + offset);
  } else {
    position[axis] = Math.floor(childOffset[axis] + childOffset[size] + offset);
  }
  return position;
}

function getMaxHeight(
  position: Position,
  boundaryDimensions: Dimensions,
  containerOffsetWithBoundary: Offset,
  isContainerPositioned: boolean,
  margins: Position,
  padding: number,
  overlayHeight: number,
  heightGrowthDirection: HeightGrowthDirection,
  containerDimensions: Dimensions
) {
  const containerHeight = (isContainerPositioned ? containerOffsetWithBoundary.height : containerDimensions[TOTAL_SIZE.height]);
  // For cases where position is set via "bottom" instead of "top", we need to calculate the true overlay top
  // with respect to the container.
  let overlayTop = position.top != null ? position.top : (containerHeight - (position.bottom ?? 0) - overlayHeight);
  let maxHeight = heightGrowthDirection !== 'top' ?
    // We want the distance between the top of the overlay to the bottom of the boundary
    Math.max(0,
      (boundaryDimensions.height + boundaryDimensions.top) // this is the bottom of the boundary
      - overlayTop // this is the top of the overlay
      - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
    )
    // We want the distance between the bottom of the overlay to the top of the boundary
    : Math.max(0,
      (overlayTop + overlayHeight) // this is the bottom of the overlay
      - boundaryDimensions.top // this is the top of the boundary
      - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding
    );
  return Math.min(boundaryDimensions.height - (padding * 2), maxHeight);
}

function getAvailableSpace(
  boundaryDimensions: Dimensions, // boundary
  containerOffsetWithBoundary: Offset,
  childOffset: Offset, // trigger
  margins: Position, // overlay
  padding: number, // overlay <-> boundary
  placementInfo: ParsedPlacement
) {
  let {placement, axis, size} = placementInfo;