Skip to content

Commit 78f55ea

Browse files
crisbetojelbourn
authored andcommitted
fix(select): update panel width if the viewport size changes (#14932)
Updates the width of the select panel if the width of the viewport has changed. This handles cases like the user switch their device's orientation. Fixes #7811.
1 parent 0185dd1 commit 78f55ea

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/lib/select/select.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,27 @@ describe('MatSelect', () => {
10061006
expect(pane.style.minWidth).toBe('200px');
10071007
}));
10081008

1009+
it('should update the width of the panel on resize', fakeAsync(() => {
1010+
trigger.style.width = '300px';
1011+
1012+
trigger.click();
1013+
fixture.detectChanges();
1014+
flush();
1015+
1016+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
1017+
const initialWidth = parseInt(pane.style.minWidth || '0');
1018+
1019+
expect(initialWidth).toBeGreaterThan(0);
1020+
1021+
trigger.style.width = '400px';
1022+
dispatchFakeEvent(window, 'resize');
1023+
fixture.detectChanges();
1024+
tick(1000);
1025+
fixture.detectChanges();
1026+
1027+
expect(parseInt(pane.style.minWidth || '0')).toBeGreaterThan(initialWidth);
1028+
}));
1029+
10091030
it('should not attempt to open a select that does not have any options', fakeAsync(() => {
10101031
fixture.componentInstance.foods = [];
10111032
fixture.detectChanges();

src/lib/select/select.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
527527
this._changeDetectorRef.markForCheck();
528528
}
529529
});
530+
531+
this._viewportRuler.change()
532+
.pipe(takeUntil(this._destroy))
533+
.subscribe(() => {
534+
if (this._panelOpen) {
535+
this._triggerRect = this.trigger.nativeElement.getBoundingClientRect();
536+
this._changeDetectorRef.markForCheck();
537+
}
538+
});
530539
}
531540

532541
ngAfterContentInit() {

0 commit comments

Comments
 (0)