Skip to content

Commit fbf2987

Browse files
josephperrottjelbourn
authored andcommitted
fix(tabs): detach tab portal when tab hides from view (#8486)
1 parent 0f954a0 commit fbf2987

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/lib/tabs/tab-body.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,19 @@ 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 {
9498
if (this._centeringSub && !this._centeringSub.closed) {
9599
this._centeringSub.unsubscribe();
@@ -136,7 +140,7 @@ export class MatTabBody implements OnInit {
136140
@Output() _onCentering: EventEmitter<number> = new EventEmitter<number>();
137141

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

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

187191
_onTranslateTabStarted(e: AnimationEvent): void {
188-
if (this._isCenterPosition(e.toState)) {
189-
this._beforeCentering.emit();
192+
const isCentering = this._isCenterPosition(e.toState);
193+
this._beforeCentering.emit(isCentering);
194+
if (isCentering) {
190195
this._onCentering.emit(this._elementRef.nativeElement.clientHeight);
191196
}
192197
}

src/lib/tabs/tab-group.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,19 @@ describe('MatTabGroup', () => {
350350
expect(fixture.componentInstance.legumes).toBeTruthy();
351351
});
352352

353+
it('should only have the active tab in the DOM', async(() => {
354+
expect(fixture.nativeElement.textContent).toContain('Pizza, fries');
355+
expect(fixture.nativeElement.textContent).not.toContain('Peanuts');
356+
357+
tabGroup.selectedIndex = 3;
358+
fixture.detectChanges();
359+
// Use whenStable to wait for async observables and change detection to run in content.
360+
fixture.whenStable().then(() => {
361+
expect(fixture.nativeElement.textContent).not.toContain('Pizza, fries');
362+
expect(fixture.nativeElement.textContent).toContain('Peanuts');
363+
});
364+
}));
365+
353366
it('should support setting the header position', () => {
354367
let tabGroupNode = fixture.debugElement.query(By.css('mat-tab-group')).nativeElement;
355368

0 commit comments

Comments
 (0)