@@ -76,7 +76,9 @@ export class ConnectedPositionStrategy implements PositionStrategy {
7676
7777 // We use the viewport rect to determine whether a position would go off-screen.
7878 const viewportRect = this . _viewportRuler . getViewportRect ( ) ;
79- let attemptedPositions : OverlayPoint [ ] = [ ] ;
79+
80+ // Fallback point if none of the fallbacks fit into the viewport.
81+ let fallbackPoint : OverlayPoint = null ;
8082
8183 // We want to place the overlay in the first of the preferred positions such that the
8284 // overlay fits on-screen.
@@ -91,17 +93,14 @@ export class ConnectedPositionStrategy implements PositionStrategy {
9193 this . _setElementPosition ( element , overlayPoint ) ;
9294 this . _onPositionChange . next ( new ConnectedOverlayPositionChange ( pos ) ) ;
9395 return Promise . resolve ( null ) ;
94- } else {
95- attemptedPositions . push ( overlayPoint ) ;
96+ } else if ( ! fallbackPoint || fallbackPoint . visibleArea < overlayPoint . visibleArea ) {
97+ fallbackPoint = overlayPoint ;
9698 }
9799 }
98100
99- // If none of the preferred positions were in the viewport, rank them based on the
100- // visible area that the element would have at that position and take the one with
101- // largest visible area.
102- this . _setElementPosition ( element , attemptedPositions . sort ( ( a , b ) => {
103- return a . visibleArea - b . visibleArea ;
104- } ) . pop ( ) ) ;
101+ // If none of the preferred positions were in the viewport, take the one
102+ // with the largest visible area.
103+ this . _setElementPosition ( element , fallbackPoint ) ;
105104
106105 return Promise . resolve ( null ) ;
107106 }
0 commit comments