Skip to content

Commit 403ab93

Browse files
crisbetommalerba
authored andcommitted
fix(sidenav): content margins not updated on viewport changes (#14089)
Since the size of the sidenav depends partially on the viewport width, its size can change if the viewport is resized (e.g. the user change their device orientation or they make the window smaller). These changes add some extra logic to update the content margins in such cases.
1 parent 0ec3d7b commit 403ab93

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/lib/sidenav/drawer.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {Directionality} from '@angular/cdk/bidi';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1212
import {ESCAPE} from '@angular/cdk/keycodes';
1313
import {Platform} from '@angular/cdk/platform';
14-
import {CdkScrollable, ScrollDispatcher} from '@angular/cdk/scrolling';
14+
import {CdkScrollable, ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
1515
import {DOCUMENT} from '@angular/common';
1616
import {
1717
AfterContentChecked,
@@ -508,7 +508,12 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
508508
private _ngZone: NgZone,
509509
private _changeDetectorRef: ChangeDetectorRef,
510510
@Inject(MAT_DRAWER_DEFAULT_AUTOSIZE) defaultAutosize = false,
511-
@Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string) {
511+
@Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string,
512+
/**
513+
* @deprecated viewportRuler to become a required parameter.
514+
* @breaking-change 8.0.0
515+
*/
516+
@Optional() viewportRuler?: ViewportRuler) {
512517

513518
// If a `Dir` directive exists up the tree, listen direction changes
514519
// and update the left/right properties to point to the proper start/end.
@@ -519,6 +524,14 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
519524
});
520525
}
521526

527+
// Since the minimum width of the sidenav depends on the viewport width,
528+
// we need to recompute the margins if the viewport changes.
529+
if (viewportRuler) {
530+
viewportRuler.change()
531+
.pipe(takeUntil(this._destroyed))
532+
.subscribe(() => this._updateContentMargins());
533+
}
534+
522535
this._autosize = defaultAutosize;
523536
}
524537

0 commit comments

Comments
 (0)