Skip to content

Commit c936bc9

Browse files
crisbetojelbourn
authored andcommitted
perf(sidenav): avoid hitting zone continuously when using autosize option (#11231)
Avoids hitting the `NgZone` infinitely when using the `autosize` option. Fixes #11215.
1 parent a20dfd3 commit c936bc9

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/lib/sidenav/drawer.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,20 @@ export function MAT_DRAWER_DEFAULT_AUTOSIZE_FACTORY(): boolean {
6767
template: '<ng-content></ng-content>',
6868
host: {
6969
'class': 'mat-drawer-content',
70-
'[style.margin-left.px]': '_margins.left',
71-
'[style.margin-right.px]': '_margins.right',
70+
'[style.margin-left.px]': '_container._contentMargins.left',
71+
'[style.margin-right.px]': '_container._contentMargins.right',
7272
},
7373
changeDetection: ChangeDetectionStrategy.OnPush,
7474
encapsulation: ViewEncapsulation.None,
7575
})
7676
export class MatDrawerContent implements AfterContentInit {
77-
/**
78-
* Margins to be applied to the content. These are used to push / shrink the drawer content when a
79-
* drawer is open. We use margin rather than transform even for push mode because transform breaks
80-
* fixed position elements inside of the transformed element.
81-
*/
82-
_margins: {left: number|null, right: number|null} = {left: null, right: null};
83-
8477
constructor(
8578
private _changeDetectorRef: ChangeDetectorRef,
86-
@Inject(forwardRef(() => MatDrawerContainer)) private _container: MatDrawerContainer) {
79+
@Inject(forwardRef(() => MatDrawerContainer)) public _container: MatDrawerContainer) {
8780
}
8881

8982
ngAfterContentInit() {
90-
this._container._contentMargins.subscribe(margins => {
91-
this._margins = margins;
83+
this._container._contentMarginChanges.subscribe(() => {
9284
this._changeDetectorRef.markForCheck();
9385
});
9486
}
@@ -466,7 +458,14 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
466458
/** Emits on every ngDoCheck. Used for debouncing reflows. */
467459
private readonly _doCheckSubject = new Subject<void>();
468460

469-
readonly _contentMargins = new Subject<{left: number|null, right: number|null}>();
461+
/**
462+
* Margins to be applied to the content. These are used to push / shrink the drawer content when a
463+
* drawer is open. We use margin rather than transform even for push mode because transform breaks
464+
* fixed position elements inside of the transformed element.
465+
*/
466+
_contentMargins: {left: number|null, right: number|null} = {left: null, right: null};
467+
468+
readonly _contentMarginChanges = new Subject<{left: number|null, right: number|null}>();
470469

471470
/** Reference to the CdkScrollable instance that wraps the scrollable content. */
472471
@ViewChild(CdkScrollable) scrollable: CdkScrollable;
@@ -699,7 +698,13 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
699698
}
700699
}
701700

702-
// Pull back into the NgZone since in some cases we could be outside.
703-
this._ngZone.run(() => this._contentMargins.next({left, right}));
701+
if (left !== this._contentMargins.left || right !== this._contentMargins.right) {
702+
this._contentMargins = {left, right};
703+
704+
// Pull back into the NgZone since in some cases we could be outside. We need to be careful
705+
// to do it only when something changed, otherwise we can end up hitting the zone too often.
706+
this._ngZone.run(() => this._contentMarginChanges.next(this._contentMargins));
707+
}
708+
704709
}
705710
}

src/lib/sidenav/sidenav.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion
2929
template: '<ng-content></ng-content>',
3030
host: {
3131
'class': 'mat-drawer-content mat-sidenav-content',
32-
'[style.margin-left.px]': '_margins.left',
33-
'[style.margin-right.px]': '_margins.right',
32+
'[style.margin-left.px]': '_container._contentMargins.left',
33+
'[style.margin-right.px]': '_container._contentMargins.right',
3434
},
3535
changeDetection: ChangeDetectionStrategy.OnPush,
3636
encapsulation: ViewEncapsulation.None,

0 commit comments

Comments
 (0)