Skip to content

fix(tabs): hide tab body content after leaving the tab's view area #8827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/lib/tabs/tab-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export type MatTabBodyOriginState = 'left' | 'right';
export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestroy {
/** A subscription to events for when the tab body begins centering. */
private _centeringSub: Subscription;
/** A subscription to events for when the tab body finishes leaving from center position. */
private _leavingSub: Subscription;

constructor(
_componentFactoryResolver: ComponentFactoryResolver,
Expand All @@ -84,17 +86,23 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr
if (!this.hasAttached()) {
this.attach(this._host._content);
}
} else {
this.detach();
}
});

this._leavingSub = this._host._afterLeavingCenter.subscribe(() => {
this.detach();
});
}

/** Clean up centering subscription. */
ngOnDestroy(): void {
if (this._centeringSub && !this._centeringSub.closed) {
this._centeringSub.unsubscribe();
}

if (this._leavingSub && !this._leavingSub.closed) {
this._leavingSub.unsubscribe();
}
}
}

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

/** Event emitted before the centering of the tab begins. */
@Output() _afterLeavingCenter: EventEmitter<boolean> = new EventEmitter<boolean>();

/** Event emitted when the tab completes its animation towards the center. */
@Output() _onCentered: EventEmitter<void> = new EventEmitter<void>(true);

Expand Down Expand Up @@ -198,6 +209,10 @@ export class MatTabBody implements OnInit {
if (this._isCenterPosition(e.toState) && this._isCenterPosition(this._position)) {
this._onCentered.emit();
}

if (this._isCenterPosition(e.fromState) && !this._isCenterPosition(this._position)) {
this._afterLeavingCenter.emit();
}
}

/** The text direction of the containing app. */
Expand Down