@@ -88,8 +88,11 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
88
88
/** Cached container dimensions */
89
89
private _containerRect : Dimensions ;
90
90
91
- /** Amount of space that must be maintained between the overlay and the edge of the viewport. */
92
- private _viewportMargin = 0 ;
91
+ /** Amount of vertical space that must be maintained between the overlay and the edge of the viewport. */
92
+ private _viewportMarginX = 0 ;
93
+
94
+ /** Amount of horizontal space that must be maintained between the overlay and the edge of the viewport. */
95
+ private _viewportMarginY = 0 ;
93
96
94
97
/** The Scrollable containers used to check scrollable view properties on position change. */
95
98
private _scrollables : CdkScrollable [ ] = [ ] ;
@@ -411,11 +414,30 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
411
414
}
412
415
413
416
/**
414
- * Sets a minimum distance the overlay may be positioned to the edge of the viewport.
417
+ * Sets a minimum horizontal distance the overlay may be positioned to the edge of the viewport.
415
418
* @param margin Required margin between the overlay and the viewport edge in pixels.
416
419
*/
420
+ withViewportMarginX ( margin : number ) : this {
421
+ this . _viewportMarginX = margin ;
422
+ return this ;
423
+ }
424
+
425
+ /**
426
+ * Sets a minimum vertical distance the overlay may be positioned to the edge of the viewport.
427
+ * @param margin Required vertical margin between the overlay and the viewport edge in pixels.
428
+ */
429
+ withViewportMarginY ( margin : number ) : this {
430
+ this . _viewportMarginY = margin ;
431
+ return this ;
432
+ }
433
+
434
+ /**
435
+ * Sets a minimum distance the overlay may be positioned to the vertical and horizontal edge of the viewport.
436
+ * @param margin Required vertical and horizontal margin between the overlay and the viewport edge in pixels.
437
+ */
417
438
withViewportMargin ( margin : number ) : this {
418
- this . _viewportMargin = margin ;
439
+ this . _viewportMarginY = margin ;
440
+ this . _viewportMarginX = margin ;
419
441
return this ;
420
442
}
421
443
@@ -682,13 +704,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
682
704
if ( overlay . width <= viewport . width ) {
683
705
pushX = overflowLeft || - overflowRight ;
684
706
} else {
685
- pushX = start . x < this . _viewportMargin ? viewport . left - scrollPosition . left - start . x : 0 ;
707
+ pushX = start . x < this . _viewportMarginX ? viewport . left - scrollPosition . left - start . x : 0 ;
686
708
}
687
709
688
710
if ( overlay . height <= viewport . height ) {
689
711
pushY = overflowTop || - overflowBottom ;
690
712
} else {
691
- pushY = start . y < this . _viewportMargin ? viewport . top - scrollPosition . top - start . y : 0 ;
713
+ pushY = start . y < this . _viewportMarginY ? viewport . top - scrollPosition . top - start . y : 0 ;
692
714
}
693
715
694
716
this . _previousPushAmount = { x : pushX , y : pushY } ;
@@ -777,13 +799,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
777
799
if ( position . overlayY === 'top' ) {
778
800
// Overlay is opening "downward" and thus is bound by the bottom viewport edge.
779
801
top = origin . y ;
780
- height = viewport . height - top + this . _viewportMargin ;
802
+ height = viewport . height - top + this . _viewportMarginY ;
781
803
} else if ( position . overlayY === 'bottom' ) {
782
804
// Overlay is opening "upward" and thus is bound by the top viewport edge. We need to add
783
805
// the viewport margin back in, because the viewport rect is narrowed down to remove the
784
806
// 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 ;
807
+ bottom = viewport . height - origin . y + this . _viewportMarginY * 2 ;
808
+ height = viewport . height - bottom + this . _viewportMarginY ;
787
809
} else {
788
810
// If neither top nor bottom, it means that the overlay is vertically centered on the
789
811
// origin point. Note that we want the position relative to the viewport, rather than
@@ -815,8 +837,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
815
837
let width : number , left : number , right : number ;
816
838
817
839
if ( isBoundedByLeftViewportEdge ) {
818
- right = viewport . width - origin . x + this . _viewportMargin * 2 ;
819
- width = origin . x - this . _viewportMargin ;
840
+ right = viewport . width - origin . x + this . _viewportMarginX * 2 ;
841
+ width = origin . x - this . _viewportMarginX ;
820
842
} else if ( isBoundedByRightViewportEdge ) {
821
843
left = origin . x ;
822
844
width = viewport . right - origin . x ;
@@ -1098,12 +1120,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
1098
1120
const scrollPosition = this . _viewportRuler . getViewportScrollPosition ( ) ;
1099
1121
1100
1122
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 ,
1123
+ top : scrollPosition . top + this . _viewportMarginY ,
1124
+ left : scrollPosition . left + this . _viewportMarginX ,
1125
+ right : scrollPosition . left + width - this . _viewportMarginX ,
1126
+ bottom : scrollPosition . top + height - this . _viewportMarginY ,
1127
+ width : width - 2 * this . _viewportMarginX ,
1128
+ height : height - 2 * this . _viewportMarginY ,
1107
1129
} ;
1108
1130
}
1109
1131
0 commit comments