Skip to content

Commit a6e5332

Browse files
committed
Detach tabs hidden tabs from view.
1 parent 262c23b commit a6e5332

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/lib/tabs/tab-body.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,21 @@ export class MatTabBodyPortal extends _MatTabBodyPortalBaseClass implements OnIn
8181
ngOnInit(): void {
8282
if (this._host._isCenterPosition(this._host._position)) {
8383
this.attach(this._host._content);
84-
} else {
85-
this._centeringSub = this._host._beforeCentering.subscribe(() => {
86-
this.attach(this._host._content);
87-
this._centeringSub.unsubscribe();
88-
});
8984
}
85+
this._centeringSub = this._host._beforeCentering.subscribe((isCentering: boolean) => {
86+
if (isCentering) {
87+
if (!this.hasAttached()) {
88+
this.attach(this._host._content);
89+
}
90+
} else {
91+
this.detach();
92+
}
93+
});
9094
}
9195

92-
/** Clean up subscription if necessary. */
96+
/** Clean up centering subscription. */
9397
ngOnDestroy(): void {
94-
if (this._centeringSub && !this._centeringSub.closed) {
95-
this._centeringSub.unsubscribe();
96-
}
98+
this._centeringSub.unsubscribe();
9799
}
98100
}
99101

@@ -136,7 +138,7 @@ export class MatTabBody implements OnInit {
136138
@Output() _onCentering: EventEmitter<number> = new EventEmitter<number>();
137139

138140
/** Event emitted before the centering of the tab begins. */
139-
@Output() _beforeCentering: EventEmitter<number> = new EventEmitter<number>();
141+
@Output() _beforeCentering: EventEmitter<boolean> = new EventEmitter<boolean>();
140142

141143
/** Event emitted when the tab completes its animation towards the center. */
142144
@Output() _onCentered: EventEmitter<void> = new EventEmitter<void>(true);
@@ -185,8 +187,9 @@ export class MatTabBody implements OnInit {
185187
}
186188

187189
_onTranslateTabStarted(e: AnimationEvent): void {
188-
if (this._isCenterPosition(e.toState)) {
189-
this._beforeCentering.emit();
190+
const isCentering = this._isCenterPosition(e.toState);
191+
this._beforeCentering.emit(isCentering);
192+
if (isCentering) {
190193
this._onCentering.emit(this._elementRef.nativeElement.clientHeight);
191194
}
192195
}

0 commit comments

Comments
 (0)