@@ -88,8 +88,17 @@ 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 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 ;
93
102
94
103
/** The Scrollable containers used to check scrollable view properties on position change. */
95
104
private _scrollables : CdkScrollable [ ] = [ ] ;
@@ -411,11 +420,38 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
411
420
}
412
421
413
422
/**
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.
415
433
* @param margin Required margin between the overlay and the viewport edge in pixels.
416
434
*/
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 ;
419
455
return this ;
420
456
}
421
457
@@ -682,13 +718,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
682
718
if ( overlay . width <= viewport . width ) {
683
719
pushX = overflowLeft || - overflowRight ;
684
720
} 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 ;
686
722
}
687
723
688
724
if ( overlay . height <= viewport . height ) {
689
725
pushY = overflowTop || - overflowBottom ;
690
726
} 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 ;
692
728
}
693
729
694
730
this . _previousPushAmount = { x : pushX , y : pushY } ;
@@ -777,13 +813,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
777
813
if ( position . overlayY === 'top' ) {
778
814
// Overlay is opening "downward" and thus is bound by the bottom viewport edge.
779
815
top = origin . y ;
780
- height = viewport . height - top + this . _viewportMargin ;
816
+ height = viewport . height - top + this . _viewportMarginBottom ;
781
817
} else if ( position . overlayY === 'bottom' ) {
782
818
// Overlay is opening "upward" and thus is bound by the top viewport edge. We need to add
783
819
// the viewport margin back in, because the viewport rect is narrowed down to remove the
784
820
// 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 ;
787
823
} else {
788
824
// If neither top nor bottom, it means that the overlay is vertically centered on the
789
825
// origin point. Note that we want the position relative to the viewport, rather than
@@ -815,11 +851,11 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
815
851
let width : number , left : number , right : number ;
816
852
817
853
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 ;
820
856
} else if ( isBoundedByRightViewportEdge ) {
821
857
left = origin . x ;
822
- width = viewport . right - origin . x ;
858
+ width = viewport . right - origin . x - this . _viewportMarginRight ;
823
859
} else {
824
860
// If neither start nor end, it means that the overlay is horizontally centered on the
825
861
// origin point. Note that we want the position relative to the viewport, rather than
@@ -1098,12 +1134,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
1098
1134
const scrollPosition = this . _viewportRuler . getViewportScrollPosition ( ) ;
1099
1135
1100
1136
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 ,
1107
1143
} ;
1108
1144
}
1109
1145
0 commit comments