@@ -23,13 +23,20 @@ import {
2323 ViewChild ,
2424 ViewEncapsulation ,
2525} from '@angular/core' ;
26- import { animationFrameScheduler , asapScheduler , Observable , Subject , Observer } from 'rxjs' ;
26+ import {
27+ animationFrameScheduler ,
28+ asapScheduler ,
29+ Observable ,
30+ Subject ,
31+ Observer ,
32+ Subscription ,
33+ } from 'rxjs' ;
2734import { auditTime , startWith , takeUntil } from 'rxjs/operators' ;
2835import { ScrollDispatcher } from './scroll-dispatcher' ;
2936import { CdkScrollable , ExtendedScrollToOptions } from './scrollable' ;
3037import { CdkVirtualForOf } from './virtual-for-of' ;
3138import { VIRTUAL_SCROLL_STRATEGY , VirtualScrollStrategy } from './virtual-scroll-strategy' ;
32-
39+ import { ViewportRuler } from './viewport-ruler' ;
3340
3441/** Checks if the given ranges are equal. */
3542function rangesEqual ( r1 : ListRange , r2 : ListRange ) : boolean {
@@ -142,18 +149,33 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
142149 /** A list of functions to run after the next change detection cycle. */
143150 private _runAfterChangeDetection : Function [ ] = [ ] ;
144151
152+ /** Subscription to changes in the viewport size. */
153+ private _viewportChanges = Subscription . EMPTY ;
154+
145155 constructor ( public elementRef : ElementRef < HTMLElement > ,
146156 private _changeDetectorRef : ChangeDetectorRef ,
147157 ngZone : NgZone ,
148158 @Optional ( ) @Inject ( VIRTUAL_SCROLL_STRATEGY )
149159 private _scrollStrategy : VirtualScrollStrategy ,
150160 @Optional ( ) dir : Directionality ,
151- scrollDispatcher : ScrollDispatcher ) {
161+ scrollDispatcher : ScrollDispatcher ,
162+ /**
163+ * @deprecated `viewportRuler` parameter to become required.
164+ * @breaking -change 11.0.0
165+ */
166+ @Optional ( ) viewportRuler ?: ViewportRuler ) {
152167 super ( elementRef , scrollDispatcher , ngZone , dir ) ;
153168
154169 if ( ! _scrollStrategy ) {
155170 throw Error ( 'Error: cdk-virtual-scroll-viewport requires the "itemSize" property to be set.' ) ;
156171 }
172+
173+ // @breaking -change 11.0.0 Remove null check for `viewportRuler`.
174+ if ( viewportRuler ) {
175+ this . _viewportChanges = viewportRuler . change ( ) . subscribe ( ( ) => {
176+ this . checkViewportSize ( ) ;
177+ } ) ;
178+ }
157179 }
158180
159181 ngOnInit ( ) {
@@ -188,6 +210,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
188210 // Complete all subjects
189211 this . _renderedRangeSubject . complete ( ) ;
190212 this . _detachedSubject . complete ( ) ;
213+ this . _viewportChanges . unsubscribe ( ) ;
191214
192215 super . ngOnDestroy ( ) ;
193216 }
0 commit comments