Skip to content

Commit 05d726d

Browse files
josephperrottmmalerba
authored andcommitted
fix(tabs): hide tab body content after leaving the tab's view area (#8827)
1 parent 8b3862c commit 05d726d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/lib/tabs/tab-body.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ 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+
/** A subscription to events for when the tab body finishes leaving from center position. */
70+
private _leavingSub: Subscription;
6971

7072
constructor(
7173
_componentFactoryResolver: ComponentFactoryResolver,
@@ -84,17 +86,23 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr
8486
if (!this.hasAttached()) {
8587
this.attach(this._host._content);
8688
}
87-
} else {
88-
this.detach();
8989
}
9090
});
91+
92+
this._leavingSub = this._host._afterLeavingCenter.subscribe(() => {
93+
this.detach();
94+
});
9195
}
9296

9397
/** Clean up centering subscription. */
9498
ngOnDestroy(): void {
9599
if (this._centeringSub && !this._centeringSub.closed) {
96100
this._centeringSub.unsubscribe();
97101
}
102+
103+
if (this._leavingSub && !this._leavingSub.closed) {
104+
this._leavingSub.unsubscribe();
105+
}
98106
}
99107
}
100108

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

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

@@ -198,6 +209,10 @@ export class MatTabBody implements OnInit {
198209
if (this._isCenterPosition(e.toState) && this._isCenterPosition(this._position)) {
199210
this._onCentered.emit();
200211
}
212+
213+
if (this._isCenterPosition(e.fromState) && !this._isCenterPosition(this._position)) {
214+
this._afterLeavingCenter.emit();
215+
}
201216
}
202217

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

0 commit comments

Comments
 (0)