@@ -14,6 +14,7 @@ import {
14
14
OverlayRef ,
15
15
PositionStrategy ,
16
16
ScrollStrategy ,
17
+ ViewportRuler ,
17
18
} from '@angular/cdk/overlay' ;
18
19
import { TemplatePortal } from '@angular/cdk/portal' ;
19
20
import { DOCUMENT } from '@angular/common' ;
@@ -122,6 +123,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
122
123
/** The subscription for closing actions (some are bound to document). */
123
124
private _closingActionsSubscription : Subscription ;
124
125
126
+ /** Subscription to viewport size changes. */
127
+ private _viewportSubscription = Subscription . EMPTY ;
128
+
125
129
/** Stream of keyboard events that can close the panel. */
126
130
private readonly _closeKeyEventStream = new Subject < void > ( ) ;
127
131
@@ -141,9 +145,12 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
141
145
@Inject ( MAT_AUTOCOMPLETE_SCROLL_STRATEGY ) private _scrollStrategy ,
142
146
@Optional ( ) private _dir : Directionality ,
143
147
@Optional ( ) @Host ( ) private _formField : MatFormField ,
144
- @Optional ( ) @Inject ( DOCUMENT ) private _document : any ) { }
148
+ @Optional ( ) @Inject ( DOCUMENT ) private _document : any ,
149
+ // @deletion -target 7.0.0 Make `_viewportRuler` required.
150
+ private _viewportRuler ?: ViewportRuler ) { }
145
151
146
152
ngOnDestroy ( ) {
153
+ this . _viewportSubscription . unsubscribe ( ) ;
147
154
this . _componentDestroyed = true ;
148
155
this . _destroyPanel ( ) ;
149
156
this . _closeKeyEventStream . complete ( ) ;
@@ -482,6 +489,14 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
482
489
if ( ! this . _overlayRef ) {
483
490
this . _portal = new TemplatePortal ( this . autocomplete . template , this . _viewContainerRef ) ;
484
491
this . _overlayRef = this . _overlay . create ( this . _getOverlayConfig ( ) ) ;
492
+
493
+ if ( this . _viewportRuler ) {
494
+ this . _viewportSubscription = this . _viewportRuler . change ( ) . subscribe ( ( ) => {
495
+ if ( this . panelOpen && this . _overlayRef ) {
496
+ this . _overlayRef . updateSize ( { width : this . _getHostWidth ( ) } ) ;
497
+ }
498
+ } ) ;
499
+ }
485
500
} else {
486
501
/** Update the panel width, in case the host width has changed */
487
502
this . _overlayRef . updateSize ( { width : this . _getHostWidth ( ) } ) ;
0 commit comments