Skip to content

Commit 8196536

Browse files
committed
Hide tab body content after leaving the tab's view area.
1 parent 5690c87 commit 8196536

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/lib/tabs/tab-body.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export type MatTabBodyOriginState = 'left' | 'right';
6666
export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestroy {
6767
/** A subscription to events for when the tab body begins centering. */
6868
private _centeringSub: Subscription;
69+
private _leavingSub: Subscription;
6970

7071
constructor(
7172
_componentFactoryResolver: ComponentFactoryResolver,
@@ -84,17 +85,23 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr
8485
if (!this.hasAttached()) {
8586
this.attach(this._host._content);
8687
}
87-
} else {
88-
this.detach();
8988
}
9089
});
90+
91+
this._leavingSub = this._host._afterLeavingCenter.subscribe(() => {
92+
this.detach();
93+
});
9194
}
9295

9396
/** Clean up centering subscription. */
9497
ngOnDestroy(): void {
9598
if (this._centeringSub && !this._centeringSub.closed) {
9699
this._centeringSub.unsubscribe();
97100
}
101+
102+
if (this._leavingSub && !this._leavingSub.closed) {
103+
this._leavingSub.unsubscribe();
104+
}
98105
}
99106
}
100107

@@ -139,6 +146,9 @@ export class MatTabBody implements OnInit {
139146
/** Event emitted before the centering of the tab begins. */
140147
@Output() _beforeCentering: EventEmitter<boolean> = new EventEmitter<boolean>();
141148

149+
/** Event emitted before the centering of the tab begins. */
150+
@Output() _afterLeavingCenter: EventEmitter<boolean> = new EventEmitter<boolean>();
151+
142152
/** Event emitted when the tab completes its animation towards the center. */
143153
@Output() _onCentered: EventEmitter<void> = new EventEmitter<void>(true);
144154

@@ -198,6 +208,10 @@ export class MatTabBody implements OnInit {
198208
if (this._isCenterPosition(e.toState) && this._isCenterPosition(this._position)) {
199209
this._onCentered.emit();
200210
}
211+
212+
if (this._isCenterPosition(e.fromState) && !this._isCenterPosition(this._position)) {
213+
this._afterLeavingCenter.emit();
214+
}
201215
}
202216

203217
/** The text direction of the containing app. */

0 commit comments

Comments
 (0)