Skip to content

Commit d35ce47

Browse files
committed
fix(overlay): incorrect bounding box styles if position is exactly zero
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 c28549d commit d35ce47

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,102 @@ describe('FlexibleConnectedPositionStrategy', () => {
11191119
expect(Math.floor(overlayRect.height)).toBe(OVERLAY_HEIGHT);
11201120
});
11211121

1122+
it('should set the proper styles when the `bottom` value is exactly zero', () => {
1123+
originElement.style.position = 'fixed';
1124+
originElement.style.bottom = '0';
1125+
originElement.style.left = '200px';
1126+
1127+
positionStrategy
1128+
.withFlexibleWidth()
1129+
.withFlexibleHeight()
1130+
.withPositions([{
1131+
overlayY: 'bottom',
1132+
overlayX: 'start',
1133+
originY: 'bottom',
1134+
originX: 'start'
1135+
}]);
1136+
1137+
attachOverlay({positionStrategy});
1138+
1139+
const boundingBox = overlayContainer
1140+
.getContainerElement()
1141+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1142+
1143+
expect(boundingBox.style.bottom).toBe('0px');
1144+
});
1145+
1146+
it('should set the proper styles when the `top` value is exactly zero', () => {
1147+
originElement.style.position = 'fixed';
1148+
originElement.style.top = '0';
1149+
originElement.style.left = '200px';
1150+
1151+
positionStrategy
1152+
.withFlexibleWidth()
1153+
.withFlexibleHeight()
1154+
.withPositions([{
1155+
overlayY: 'top',
1156+
overlayX: 'start',
1157+
originY: 'top',
1158+
originX: 'start'
1159+
}]);
1160+
1161+
attachOverlay({positionStrategy});
1162+
1163+
const boundingBox = overlayContainer
1164+
.getContainerElement()
1165+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1166+
1167+
expect(boundingBox.style.top).toBe('0px');
1168+
});
1169+
1170+
it('should set the proper styles when the `left` value is exactly zero', () => {
1171+
originElement.style.position = 'fixed';
1172+
originElement.style.left = '0';
1173+
originElement.style.top = '200px';
1174+
1175+
positionStrategy
1176+
.withFlexibleWidth()
1177+
.withFlexibleHeight()
1178+
.withPositions([{
1179+
overlayY: 'top',
1180+
overlayX: 'start',
1181+
originY: 'top',
1182+
originX: 'start'
1183+
}]);
1184+
1185+
attachOverlay({positionStrategy});
1186+
1187+
const boundingBox = overlayContainer
1188+
.getContainerElement()
1189+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1190+
1191+
expect(boundingBox.style.left).toBe('0px');
1192+
});
1193+
1194+
it('should set the proper styles when the `right` value is exactly zero', () => {
1195+
originElement.style.position = 'fixed';
1196+
originElement.style.right = '0';
1197+
originElement.style.top = '200px';
1198+
1199+
positionStrategy
1200+
.withFlexibleWidth()
1201+
.withFlexibleHeight()
1202+
.withPositions([{
1203+
overlayY: 'top',
1204+
overlayX: 'end',
1205+
originY: 'top',
1206+
originX: 'end'
1207+
}]);
1208+
1209+
attachOverlay({positionStrategy});
1210+
1211+
const boundingBox = overlayContainer
1212+
.getContainerElement()
1213+
.querySelector('.cdk-overlay-connected-position-bounding-box') as HTMLElement;
1214+
1215+
expect(boundingBox.style.right).toBe('0px');
1216+
});
1217+
11221218
});
11231219

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
635635
styles.height = '100%';
636636
} else {
637637
styles.height = `${boundingBoxRect.height}px`;
638-
styles.top = boundingBoxRect.top ? `${boundingBoxRect.top}px` : '';
639-
styles.bottom = boundingBoxRect.bottom ? `${boundingBoxRect.bottom}px` : '';
638+
styles.top = boundingBoxRect.top != null ? `${boundingBoxRect.top}px` : '';
639+
styles.bottom = boundingBoxRect.bottom != null ? `${boundingBoxRect.bottom}px` : '';
640640
}
641641

642642
if (!this._hasFlexibleWidth || this._isPushed) {
@@ -645,8 +645,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
645645
styles.width = '100%';
646646
} else {
647647
styles.width = `${boundingBoxRect.width}px`;
648-
styles.left = boundingBoxRect.left ? `${boundingBoxRect.left}px` : '';
649-
styles.right = boundingBoxRect.right ? `${boundingBoxRect.right}px` : '';
648+
styles.left = boundingBoxRect.left != null ? `${boundingBoxRect.left}px` : '';
649+
styles.right = boundingBoxRect.right != null ? `${boundingBoxRect.right}px` : '';
650650
}
651651

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

0 commit comments

Comments
 (0)