@@ -76,7 +76,9 @@ export class ConnectedPositionStrategy implements PositionStrategy {
76
76
77
77
// We use the viewport rect to determine whether a position would go off-screen.
78
78
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 ;
80
82
81
83
// We want to place the overlay in the first of the preferred positions such that the
82
84
// overlay fits on-screen.
@@ -91,17 +93,14 @@ export class ConnectedPositionStrategy implements PositionStrategy {
91
93
this . _setElementPosition ( element , overlayPoint ) ;
92
94
this . _onPositionChange . next ( new ConnectedOverlayPositionChange ( pos ) ) ;
93
95
return Promise . resolve ( null ) ;
94
- } else {
95
- attemptedPositions . push ( overlayPoint ) ;
96
+ } else if ( ! fallbackPoint || fallbackPoint . visibleArea < overlayPoint . visibleArea ) {
97
+ fallbackPoint = overlayPoint ;
96
98
}
97
99
}
98
100
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 ) ;
105
104
106
105
return Promise . resolve ( null ) ;
107
106
}
0 commit comments