Skip to content

Commit eefa9c4

Browse files
crisbetoandrewseguin
authored andcommitted
fix(overlay): incorrect bounding box styles if position is exactly zero (#10470)
Handles the edge case where the bounding box styles won't be set correctly, causing the overlay to jump to a wrong position, if either of its values works out to exactly zero.
1 parent e0321db commit eefa9c4

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,109 @@ describe('FlexibleConnectedPositionStrategy', () => {
11871187
window.scroll(0, 0);
11881188
document.body.removeChild(veryLargeElement);
11891189
});
1190+
it('should set the proper styles when the `bottom` value is exactly zero', () => {
1191+
originElement.style.position = 'fixed';
1192+
originElement.style.bottom = '0';
1193+
originElement.style.left = '200px';
1194+
1195+
positionStrategy
1196+
.withFlexibleWidth()
1197+
.withFlexibleHeight()
1198+
.withPositions([{
1199+
overlayY: 'bottom',
1200+
overlayX: 'start',
1201+
originY: 'bottom',
1202+
originX: 'start'
1203+
}]);
1204+
1205+
attachOverlay({positionStrategy});
1206+
1207+
const boundingBox = overlayContainer
1208+
.getContainerElement()
1209+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1210+
1211+
// Ensure that `0px` is set explicitly, rather than the
1212+
// property being left blank due to zero being falsy.
1213+
expect(boundingBox.style.bottom).toBe('0px');
1214+
});
1215+
1216+
it('should set the proper styles when the `top` value is exactly zero', () => {
1217+
originElement.style.position = 'fixed';
1218+
originElement.style.top = '0';
1219+
originElement.style.left = '200px';
1220+
1221+
positionStrategy
1222+
.withFlexibleWidth()
1223+
.withFlexibleHeight()
1224+
.withPositions([{
1225+
overlayY: 'top',
1226+
overlayX: 'start',
1227+
originY: 'top',
1228+
originX: 'start'
1229+
}]);
1230+
1231+
attachOverlay({positionStrategy});
1232+
1233+
const boundingBox = overlayContainer
1234+
.getContainerElement()
1235+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1236+
1237+
// Ensure that `0px` is set explicitly, rather than the
1238+
// property being left blank due to zero being falsy.
1239+
expect(boundingBox.style.top).toBe('0px');
1240+
});
1241+
1242+
it('should set the proper styles when the `left` value is exactly zero', () => {
1243+
originElement.style.position = 'fixed';
1244+
originElement.style.left = '0';
1245+
originElement.style.top = '200px';
1246+
1247+
positionStrategy
1248+
.withFlexibleWidth()
1249+
.withFlexibleHeight()
1250+
.withPositions([{
1251+
overlayY: 'top',
1252+
overlayX: 'start',
1253+
originY: 'top',
1254+
originX: 'start'
1255+
}]);
1256+
1257+
attachOverlay({positionStrategy});
1258+
1259+
const boundingBox = overlayContainer
1260+
.getContainerElement()
1261+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1262+
1263+
// Ensure that `0px` is set explicitly, rather than the
1264+
// property being left blank due to zero being falsy.
1265+
expect(boundingBox.style.left).toBe('0px');
1266+
});
1267+
1268+
it('should set the proper styles when the `right` value is exactly zero', () => {
1269+
originElement.style.position = 'fixed';
1270+
originElement.style.right = '0';
1271+
originElement.style.top = '200px';
1272+
1273+
positionStrategy
1274+
.withFlexibleWidth()
1275+
.withFlexibleHeight()
1276+
.withPositions([{
1277+
overlayY: 'top',
1278+
overlayX: 'end',
1279+
originY: 'top',
1280+
originX: 'end'
1281+
}]);
1282+
1283+
attachOverlay({positionStrategy});
1284+
1285+
const boundingBox = overlayContainer
1286+
.getContainerElement()
1287+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1288+
1289+
// Ensure that `0px` is set explicitly, rather than the
1290+
// property being left blank due to zero being falsy.
1291+
expect(boundingBox.style.right).toBe('0px');
1292+
});
11901293

11911294
});
11921295

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
642642
styles.height = '100%';
643643
} else {
644644
styles.height = `${boundingBoxRect.height}px`;
645-
styles.top = boundingBoxRect.top ? `${boundingBoxRect.top}px` : '';
646-
styles.bottom = boundingBoxRect.bottom ? `${boundingBoxRect.bottom}px` : '';
645+
styles.top = boundingBoxRect.top != null ? `${boundingBoxRect.top}px` : '';
646+
styles.bottom = boundingBoxRect.bottom != null ? `${boundingBoxRect.bottom}px` : '';
647647
}
648648

649649
if (!this._hasFlexibleWidth || this._isPushed) {
@@ -652,8 +652,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
652652
styles.width = '100%';
653653
} else {
654654
styles.width = `${boundingBoxRect.width}px`;
655-
styles.left = boundingBoxRect.left ? `${boundingBoxRect.left}px` : '';
656-
styles.right = boundingBoxRect.right ? `${boundingBoxRect.right}px` : '';
655+
styles.left = boundingBoxRect.left != null ? `${boundingBoxRect.left}px` : '';
656+
styles.right = boundingBoxRect.right != null ? `${boundingBoxRect.right}px` : '';
657657
}
658658

659659
const maxHeight = this._overlayRef.getConfig().maxHeight;

0 commit comments

Comments
 (0)