Skip to content

Commit

Permalink
fix(tabs): hide tab body content after leaving the tab's view area (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott authored and mmalerba committed Dec 6, 2017
1 parent 8b3862c commit 05d726d
Showing 1 changed file with 17 additions and 2 deletions.
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

0 comments on commit 05d726d

Please # to comment.