@@ -88,8 +88,17 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
8888 /** Cached container dimensions */
8989 private _containerRect : Dimensions ;
9090
91- /** Amount of space that must be maintained between the overlay and the edge of the viewport. */
92- private _viewportMargin = 0 ;
91+ /** Amount of space that must be maintained between the overlay and the top edge of the viewport. */
92+ private _viewportMarginTop = 0 ;
93+
94+ /** Amount of space that must be maintained between the overlay and the bottom edge of the viewport. */
95+ private _viewportMarginBottom = 0 ;
96+
97+ /** Amount of space that must be maintained between the overlay and the left edge of the viewport. */
98+ private _viewportMarginLeft = 0 ;
99+
100+ /** Amount of space that must be maintained between the overlay and the right edge of the viewport. */
101+ private _viewportMarginRight = 0 ;
93102
94103 /** The Scrollable containers used to check scrollable view properties on position change. */
95104 private _scrollables : CdkScrollable [ ] = [ ] ;
@@ -411,11 +420,38 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
411420 }
412421
413422 /**
414- * Sets a minimum distance the overlay may be positioned to the edge of the viewport.
423+ * Sets a minimum distance the overlay may be positioned from the left edge of the viewport.
424+ * @param margin Required margin between the overlay and the viewport edge in pixels.
425+ */
426+ withViewportMarginLeft ( margin : number ) : this {
427+ this . _viewportMarginLeft = margin ;
428+ return this ;
429+ }
430+
431+ /**
432+ * Sets a minimum distance the overlay may be positioned from the right edge of the viewport.
415433 * @param margin Required margin between the overlay and the viewport edge in pixels.
416434 */
417- withViewportMargin ( margin : number ) : this {
418- this . _viewportMargin = margin ;
435+ withViewportMarginRight ( margin : number ) : this {
436+ this . _viewportMarginRight = margin ;
437+ return this ;
438+ }
439+
440+ /**
441+ * Sets a minimum distance the overlay may be positioned from the top edge of the viewport.
442+ * @param margin Required vertical margin between the overlay and the viewport edge in pixels.
443+ */
444+ withViewportMarginTop ( margin : number ) : this {
445+ this . _viewportMarginTop = margin ;
446+ return this ;
447+ }
448+
449+ /**
450+ * Sets a minimum distance the overlay may be positioned from the bottom edge of the viewport.
451+ * @param margin Required vertical margin between the overlay and the viewport edge in pixels.
452+ */
453+ withViewportMarginBottom ( margin : number ) : this {
454+ this . _viewportMarginBottom = margin ;
419455 return this ;
420456 }
421457
@@ -682,13 +718,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
682718 if ( overlay . width <= viewport . width ) {
683719 pushX = overflowLeft || - overflowRight ;
684720 } else {
685- pushX = start . x < this . _viewportMargin ? viewport . left - scrollPosition . left - start . x : 0 ;
721+ pushX = start . x < this . _viewportMarginLeft ? viewport . left - scrollPosition . left - start . x : 0 ;
686722 }
687723
688724 if ( overlay . height <= viewport . height ) {
689725 pushY = overflowTop || - overflowBottom ;
690726 } else {
691- pushY = start . y < this . _viewportMargin ? viewport . top - scrollPosition . top - start . y : 0 ;
727+ pushY = start . y < this . _viewportMarginTop ? viewport . top - scrollPosition . top - start . y : 0 ;
692728 }
693729
694730 this . _previousPushAmount = { x : pushX , y : pushY } ;
@@ -777,13 +813,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
777813 if ( position . overlayY === 'top' ) {
778814 // Overlay is opening "downward" and thus is bound by the bottom viewport edge.
779815 top = origin . y ;
780- height = viewport . height - top + this . _viewportMargin ;
816+ height = viewport . height - top + this . _viewportMarginBottom ;
781817 } else if ( position . overlayY === 'bottom' ) {
782818 // Overlay is opening "upward" and thus is bound by the top viewport edge. We need to add
783819 // the viewport margin back in, because the viewport rect is narrowed down to remove the
784820 // margin, whereas the `origin` position is calculated based on its `DOMRect`.
785- bottom = viewport . height - origin . y + this . _viewportMargin * 2 ;
786- height = viewport . height - bottom + this . _viewportMargin ;
821+ bottom = viewport . height - origin . y + this . _viewportMarginTop + this . _viewportMarginBottom ;
822+ height = viewport . height - bottom + this . _viewportMarginTop ;
787823 } else {
788824 // If neither top nor bottom, it means that the overlay is vertically centered on the
789825 // origin point. Note that we want the position relative to the viewport, rather than
@@ -815,11 +851,11 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
815851 let width : number , left : number , right : number ;
816852
817853 if ( isBoundedByLeftViewportEdge ) {
818- right = viewport . width - origin . x + this . _viewportMargin * 2 ;
819- width = origin . x - this . _viewportMargin ;
854+ right = viewport . width - origin . x + this . _viewportMarginLeft + this . _viewportMarginRight ;
855+ width = origin . x - this . _viewportMarginLeft ;
820856 } else if ( isBoundedByRightViewportEdge ) {
821857 left = origin . x ;
822- width = viewport . right - origin . x ;
858+ width = viewport . right - origin . x - this . _viewportMarginRight ;
823859 } else {
824860 // If neither start nor end, it means that the overlay is horizontally centered on the
825861 // origin point. Note that we want the position relative to the viewport, rather than
@@ -1098,12 +1134,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
10981134 const scrollPosition = this . _viewportRuler . getViewportScrollPosition ( ) ;
10991135
11001136 return {
1101- top : scrollPosition . top + this . _viewportMargin ,
1102- left : scrollPosition . left + this . _viewportMargin ,
1103- right : scrollPosition . left + width - this . _viewportMargin ,
1104- bottom : scrollPosition . top + height - this . _viewportMargin ,
1105- width : width - 2 * this . _viewportMargin ,
1106- height : height - 2 * this . _viewportMargin ,
1137+ top : scrollPosition . top + this . _viewportMarginTop ,
1138+ left : scrollPosition . left + this . _viewportMarginLeft ,
1139+ right : scrollPosition . left + width - this . _viewportMarginRight ,
1140+ bottom : scrollPosition . top + height - this . _viewportMarginBottom ,
1141+ width : width - this . _viewportMarginLeft - this . _viewportMarginRight ,
1142+ height : height - this . _viewportMarginTop - this . _viewportMarginBottom ,
11071143 } ;
11081144 }
11091145
0 commit comments