Skip to content

Commit 969d5c2

Browse files
crisbetommalerba
authored andcommitted
fix(overlay): maxWidth and maxHeight not applied when using ex… (#17586)
Usually the `maxWidth` and `maxHeight` are applied to the connected overlay's bounding box, because it's the one that determines the dimensions and the overlay pane stretches to fill them. In the case where exact dimensions are used, we make the bounding box the size of the viewport and we let the overlay grow on its own, however we weren't re-applying the `maxWidth` and `maxHeight`. Fixes #17582.
1 parent 903777f commit 969d5c2

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,65 @@ describe('FlexibleConnectedPositionStrategy', () => {
20192019
document.body.removeChild(veryLargeElement);
20202020
});
20212021

2022+
it('should set the maxWidth and maxHeight on the bounding box when exact dimension are ' +
2023+
'not used', () => {
2024+
originElement.style.top = '50px';
2025+
originElement.style.left = '50%';
2026+
originElement.style.position = 'fixed';
2027+
2028+
positionStrategy
2029+
.withFlexibleDimensions()
2030+
.withPositions([{
2031+
overlayX: 'start',
2032+
overlayY: 'top',
2033+
originX: 'start',
2034+
originY: 'bottom'
2035+
}]);
2036+
2037+
attachOverlay({
2038+
positionStrategy,
2039+
maxWidth: 250,
2040+
maxHeight: 300
2041+
});
2042+
2043+
const overlayStyle = overlayRef.overlayElement.style;
2044+
const boundingBoxStyle = overlayRef.hostElement.style;
2045+
2046+
expect(overlayStyle.maxWidth).toBeFalsy();
2047+
expect(overlayStyle.maxHeight).toBeFalsy();
2048+
expect(boundingBoxStyle.maxWidth).toBe('250px');
2049+
expect(boundingBoxStyle.maxHeight).toBe('300px');
2050+
});
2051+
2052+
it('should set the maxWidth and maxHeight on the overlay pane when exact dimensions are used',
2053+
() => {
2054+
originElement.style.bottom = '0';
2055+
originElement.style.left = '50%';
2056+
originElement.style.position = 'fixed';
2057+
2058+
positionStrategy
2059+
.withFlexibleDimensions()
2060+
.withPositions([{
2061+
overlayX: 'start',
2062+
overlayY: 'top',
2063+
originX: 'start',
2064+
originY: 'bottom'
2065+
}]);
2066+
2067+
attachOverlay({
2068+
positionStrategy,
2069+
maxWidth: 250,
2070+
maxHeight: 300
2071+
});
2072+
2073+
const overlayStyle = overlayRef.overlayElement.style;
2074+
const boundingBoxStyle = overlayRef.hostElement.style;
2075+
2076+
expect(overlayStyle.maxWidth).toBe('250px');
2077+
expect(overlayStyle.maxHeight).toBe('300px');
2078+
expect(boundingBoxStyle.maxWidth).toBeFalsy();
2079+
expect(boundingBoxStyle.maxHeight).toBeFalsy();
2080+
});
20222081

20232082
});
20242083

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
791791

792792
if (this._hasExactPosition()) {
793793
styles.top = styles.left = '0';
794-
styles.bottom = styles.right = '';
794+
styles.bottom = styles.right = styles.maxHeight = styles.maxWidth = '';
795795
styles.width = styles.height = '100%';
796796
} else {
797797
const maxHeight = this._overlayRef.getConfig().maxHeight;
@@ -860,8 +860,11 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
860860
/** Sets positioning styles to the overlay element. */
861861
private _setOverlayElementStyles(originPoint: Point, position: ConnectedPosition): void {
862862
const styles = {} as CSSStyleDeclaration;
863+
const hasExactPosition = this._hasExactPosition();
864+
const hasFlexibleDimensions = this._hasFlexibleDimensions;
865+
const config = this._overlayRef.getConfig();
863866

864-
if (this._hasExactPosition()) {
867+
if (hasExactPosition) {
865868
const scrollPosition = this._viewportRuler.getViewportScrollPosition();
866869
extendStyles(styles, this._getExactOverlayY(position, originPoint, scrollPosition));
867870
extendStyles(styles, this._getExactOverlayX(position, originPoint, scrollPosition));
@@ -891,12 +894,22 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
891894
// If a maxWidth or maxHeight is specified on the overlay, we remove them. We do this because
892895
// we need these values to both be set to "100%" for the automatic flexible sizing to work.
893896
// The maxHeight and maxWidth are set on the boundingBox in order to enforce the constraint.
894-
if (this._hasFlexibleDimensions && this._overlayRef.getConfig().maxHeight) {
895-
styles.maxHeight = '';
897+
// Note that this doesn't apply when we have an exact position, in which case we do want to
898+
// apply them because they'll be cleared from the bounding box.
899+
if (config.maxHeight) {
900+
if (hasExactPosition) {
901+
styles.maxHeight = coerceCssPixelValue(config.maxHeight);
902+
} else if (hasFlexibleDimensions) {
903+
styles.maxHeight = '';
904+
}
896905
}
897906

898-
if (this._hasFlexibleDimensions && this._overlayRef.getConfig().maxWidth) {
899-
styles.maxWidth = '';
907+
if (config.maxWidth) {
908+
if (hasExactPosition) {
909+
styles.maxWidth = coerceCssPixelValue(config.maxWidth);
910+
} else if (hasFlexibleDimensions) {
911+
styles.maxWidth = '';
912+
}
900913
}
901914

902915
extendStyles(this._pane.style, styles);

0 commit comments

Comments
 (0)