Skip to content

Commit ba4f2af

Browse files
crisbetoandrewseguin
authored andcommitted
fix(overlay): unable to reset overlay size properties to initial value (#11592)
Fixes the consumer not being able to remove the various overlay sizing properties by setting them to null/an empty string, after they're added, without recreating the overlay. Note that these changes would technically let something like `false` through, but these cases will be caught by TS.
1 parent 75d116d commit ba4f2af

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,14 @@ export class OverlayRef implements PortalOutlet {
264264

265265
/** Updates the size of the overlay element based on the overlay config. */
266266
private _updateElementSize() {
267-
if (this._config.width || this._config.width === 0) {
268-
this._pane.style.width = coerceCssPixelValue(this._config.width);
269-
}
270-
271-
if (this._config.height || this._config.height === 0) {
272-
this._pane.style.height = coerceCssPixelValue(this._config.height);
273-
}
274-
275-
if (this._config.minWidth || this._config.minWidth === 0) {
276-
this._pane.style.minWidth = coerceCssPixelValue(this._config.minWidth);
277-
}
278-
279-
if (this._config.minHeight || this._config.minHeight === 0) {
280-
this._pane.style.minHeight = coerceCssPixelValue(this._config.minHeight);
281-
}
282-
283-
if (this._config.maxWidth || this._config.maxWidth === 0) {
284-
this._pane.style.maxWidth = coerceCssPixelValue(this._config.maxWidth);
285-
}
286-
287-
if (this._config.maxHeight || this._config.maxHeight === 0) {
288-
this._pane.style.maxHeight = coerceCssPixelValue(this._config.maxHeight);
289-
}
267+
const style = this._pane.style;
268+
269+
style.width = coerceCssPixelValue(this._config.width);
270+
style.height = coerceCssPixelValue(this._config.height);
271+
style.minWidth = coerceCssPixelValue(this._config.minWidth);
272+
style.minHeight = coerceCssPixelValue(this._config.minHeight);
273+
style.maxWidth = coerceCssPixelValue(this._config.maxWidth);
274+
style.maxHeight = coerceCssPixelValue(this._config.maxHeight);
290275
}
291276

292277
/** Toggles the pointer events for the overlay pane element. */

src/cdk/overlay/overlay.spec.ts

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('Overlay', () => {
165165

166166
overlayRef.attach(componentPortal);
167167

168-
expect(overlayRef.hostElement.getAttribute('dir')).toEqual('rtl');
168+
expect(overlayRef.hostElement.getAttribute('dir')).toBe('rtl');
169169
});
170170

171171
it('should emit when an overlay is attached', () => {
@@ -375,7 +375,7 @@ describe('Overlay', () => {
375375
const overlayRef = overlay.create(config);
376376

377377
overlayRef.attach(componentPortal);
378-
expect(overlayRef.overlayElement.style.width).toEqual('500px');
378+
expect(overlayRef.overlayElement.style.width).toBe('500px');
379379
});
380380

381381
it('should support using other units if a string width is provided', () => {
@@ -384,7 +384,7 @@ describe('Overlay', () => {
384384
const overlayRef = overlay.create(config);
385385

386386
overlayRef.attach(componentPortal);
387-
expect(overlayRef.overlayElement.style.width).toEqual('200%');
387+
expect(overlayRef.overlayElement.style.width).toBe('200%');
388388
});
389389

390390
it('should apply the height set in the config', () => {
@@ -393,7 +393,7 @@ describe('Overlay', () => {
393393
const overlayRef = overlay.create(config);
394394

395395
overlayRef.attach(componentPortal);
396-
expect(overlayRef.overlayElement.style.height).toEqual('500px');
396+
expect(overlayRef.overlayElement.style.height).toBe('500px');
397397
});
398398

399399
it('should support using other units if a string height is provided', () => {
@@ -402,7 +402,7 @@ describe('Overlay', () => {
402402
const overlayRef = overlay.create(config);
403403

404404
overlayRef.attach(componentPortal);
405-
expect(overlayRef.overlayElement.style.height).toEqual('100vh');
405+
expect(overlayRef.overlayElement.style.height).toBe('100vh');
406406
});
407407

408408
it('should apply the min width set in the config', () => {
@@ -411,17 +411,16 @@ describe('Overlay', () => {
411411
const overlayRef = overlay.create(config);
412412

413413
overlayRef.attach(componentPortal);
414-
expect(overlayRef.overlayElement.style.minWidth).toEqual('200px');
414+
expect(overlayRef.overlayElement.style.minWidth).toBe('200px');
415415
});
416416

417-
418417
it('should apply the min height set in the config', () => {
419418
config.minHeight = 500;
420419

421420
const overlayRef = overlay.create(config);
422421

423422
overlayRef.attach(componentPortal);
424-
expect(overlayRef.overlayElement.style.minHeight).toEqual('500px');
423+
expect(overlayRef.overlayElement.style.minHeight).toBe('500px');
425424
});
426425

427426
it('should apply the max width set in the config', () => {
@@ -430,7 +429,7 @@ describe('Overlay', () => {
430429
const overlayRef = overlay.create(config);
431430

432431
overlayRef.attach(componentPortal);
433-
expect(overlayRef.overlayElement.style.maxWidth).toEqual('200px');
432+
expect(overlayRef.overlayElement.style.maxWidth).toBe('200px');
434433
});
435434

436435

@@ -440,7 +439,7 @@ describe('Overlay', () => {
440439
const overlayRef = overlay.create(config);
441440

442441
overlayRef.attach(componentPortal);
443-
expect(overlayRef.overlayElement.style.maxHeight).toEqual('500px');
442+
expect(overlayRef.overlayElement.style.maxHeight).toBe('500px');
444443
});
445444

446445
it('should support zero widths and heights', () => {
@@ -450,9 +449,45 @@ describe('Overlay', () => {
450449
const overlayRef = overlay.create(config);
451450

452451
overlayRef.attach(componentPortal);
453-
expect(overlayRef.overlayElement.style.width).toEqual('0px');
454-
expect(overlayRef.overlayElement.style.height).toEqual('0px');
452+
expect(overlayRef.overlayElement.style.width).toBe('0px');
453+
expect(overlayRef.overlayElement.style.height).toBe('0px');
455454
});
455+
456+
it('should be able to reset the various size properties', () => {
457+
config.minWidth = config.minHeight = 100;
458+
config.width = config.height = 200;
459+
config.maxWidth = config.maxHeight = 300;
460+
461+
const overlayRef = overlay.create(config);
462+
overlayRef.attach(componentPortal);
463+
const style = overlayRef.overlayElement.style;
464+
465+
expect(style.minWidth).toBe('100px');
466+
expect(style.minHeight).toBe('100px');
467+
expect(style.width).toBe('200px');
468+
expect(style.height).toBe('200px');
469+
expect(style.maxWidth).toBe('300px');
470+
expect(style.maxHeight).toBe('300px');
471+
472+
overlayRef.updateSize({
473+
minWidth: '',
474+
minHeight: '',
475+
width: '',
476+
height: '',
477+
maxWidth: '',
478+
maxHeight: ''
479+
});
480+
481+
overlayRef.updatePosition();
482+
483+
expect(style.minWidth).toBeFalsy();
484+
expect(style.minHeight).toBeFalsy();
485+
expect(style.width).toBeFalsy();
486+
expect(style.height).toBeFalsy();
487+
expect(style.maxWidth).toBeFalsy();
488+
expect(style.maxHeight).toBeFalsy();
489+
});
490+
456491
});
457492

458493
describe('backdrop', () => {

0 commit comments

Comments
 (0)