From 6d234c0df60fea06b24a9131209b883b6d559a5a Mon Sep 17 00:00:00 2001 From: crisbeto Date: Fri, 9 Mar 2018 07:02:06 -0500 Subject: [PATCH] refactor(tabs): provide method for re-aligning the ink bar Adds the `realignInkBar` method to the `MatTabGroup` which allows consumers to re-align the ink bar programmatically. This is useful for the cases where Material can't figure out that it needs to be re-aligned or doing so would be very inefficient. Fixes #10340. --- src/lib/tabs/tab-group.ts | 9 +++++++++ src/lib/tabs/tab-header.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/tabs/tab-group.ts b/src/lib/tabs/tab-group.ts index 92f199cdcbe7..e8fee8a737b0 100644 --- a/src/lib/tabs/tab-group.ts +++ b/src/lib/tabs/tab-group.ts @@ -84,6 +84,8 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn @ViewChild('tabBodyWrapper') _tabBodyWrapper: ElementRef; + @ViewChild('tabHeader') _tabHeader: MatTabHeader; + /** The tab index that should be selected after the content has been checked. */ private _indexToSelect: number | null = 0; @@ -209,6 +211,13 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn this._tabLabelSubscription.unsubscribe(); } + /** Re-aligns the ink bar to the selected tab element. */ + realignInkBar() { + if (this._tabHeader) { + this._tabHeader._alignInkBarToSelectedTab(); + } + } + _focusChanged(index: number) { this.focusChange.emit(this._createChangeEvent(index)); } diff --git a/src/lib/tabs/tab-header.ts b/src/lib/tabs/tab-header.ts index c361607abd6b..82323d85c45b 100644 --- a/src/lib/tabs/tab-header.ts +++ b/src/lib/tabs/tab-header.ts @@ -450,7 +450,7 @@ export class MatTabHeader extends _MatTabHeaderMixinBase } /** Tells the ink-bar to align itself to the current label wrapper */ - private _alignInkBarToSelectedTab(): void { + _alignInkBarToSelectedTab(): void { const selectedLabelWrapper = this._labelWrappers && this._labelWrappers.length ? this._labelWrappers.toArray()[this.selectedIndex].elementRef.nativeElement : null;