Skip to content

Commit f4b4e61

Browse files
crisbetommalerba
authored andcommittedFeb 4, 2019
fix(overlay): unnecessarily pushing overlay if it is exactly as wide as the viewport (#14975)
Fixes the flexible position strategy pushing the overlay when it doesn't have to, if it's exactly as wide as the viewport. Fixes #14968.
1 parent c23512a commit f4b4e61

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed
 

‎src/cdk/overlay/position/flexible-connected-position-strategy.spec.ts

+58
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,9 @@ describe('FlexibleConnectedPositionStrategy', () => {
12611261
]);
12621262

12631263
attachOverlay({
1264+
// Set a large max-width to override the one that comes from the
1265+
// overlay structural styles. Otherwise the `width` will stop at the viewport width.
1266+
maxWidth: '200vw',
12641267
width: viewport.getViewportRect().width + 100,
12651268
positionStrategy
12661269
});
@@ -1292,6 +1295,9 @@ describe('FlexibleConnectedPositionStrategy', () => {
12921295
]);
12931296

12941297
attachOverlay({
1298+
// Set a large max-width to override the one that comes from the
1299+
// overlay structural styles. Otherwise the `width` will stop at the viewport width.
1300+
maxWidth: '200vw',
12951301
width: viewport.getViewportRect().width + 100,
12961302
positionStrategy
12971303
});
@@ -1869,6 +1875,58 @@ describe('FlexibleConnectedPositionStrategy', () => {
18691875
document.body.removeChild(veryLargeElement);
18701876
});
18711877

1878+
it('should not push the overlay if it is exactly as wide as the viewport', () => {
1879+
originElement.style.position = 'fixed';
1880+
originElement.style.top = '100px';
1881+
originElement.style.right = '0';
1882+
1883+
positionStrategy
1884+
.withFlexibleDimensions()
1885+
.withPush(true)
1886+
.withPositions([{
1887+
originX: 'center',
1888+
originY: 'bottom',
1889+
overlayX: 'center',
1890+
overlayY: 'top',
1891+
}]);
1892+
1893+
attachOverlay({
1894+
width: viewport.getViewportRect().width,
1895+
positionStrategy
1896+
});
1897+
1898+
const originRect = originElement.getBoundingClientRect();
1899+
const overlayRect = overlayRef.overlayElement.getBoundingClientRect();
1900+
1901+
expect(Math.floor(overlayRect.right)).toBe(Math.floor(originRect.right));
1902+
});
1903+
1904+
it('should not push the overlay if it is exactly as tall as the viewport', () => {
1905+
originElement.style.position = 'fixed';
1906+
originElement.style.left = '100px';
1907+
originElement.style.bottom = '0';
1908+
1909+
positionStrategy
1910+
.withFlexibleDimensions()
1911+
.withPush(true)
1912+
.withPositions([{
1913+
originX: 'start',
1914+
originY: 'bottom',
1915+
overlayX: 'start',
1916+
overlayY: 'bottom',
1917+
}]);
1918+
1919+
attachOverlay({
1920+
width: viewport.getViewportRect().height,
1921+
positionStrategy
1922+
});
1923+
1924+
const originRect = originElement.getBoundingClientRect();
1925+
const overlayRect = overlayRef.overlayElement.getBoundingClientRect();
1926+
1927+
expect(Math.floor(overlayRect.bottom)).toBe(Math.floor(originRect.bottom));
1928+
});
1929+
18721930
});
18731931

18741932
describe('onPositionChange with scrollable view properties', () => {

‎src/cdk/overlay/position/flexible-connected-position-strategy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
630630
// If the overlay fits completely within the bounds of the viewport, push it from whichever
631631
// direction is goes off-screen. Otherwise, push the top-left corner such that its in the
632632
// viewport and allow for the trailing end of the overlay to go out of bounds.
633-
if (overlay.width < viewport.width) {
633+
if (overlay.width <= viewport.width) {
634634
pushX = overflowLeft || -overflowRight;
635635
} else {
636636
pushX = start.x < this._viewportMargin ? (viewport.left - scrollPosition.left) - start.x : 0;
637637
}
638638

639-
if (overlay.height < viewport.height) {
639+
if (overlay.height <= viewport.height) {
640640
pushY = overflowTop || -overflowBottom;
641641
} else {
642642
pushY = start.y < this._viewportMargin ? (viewport.top - scrollPosition.top) - start.y : 0;

0 commit comments

Comments
 (0)
Please sign in to comment.