Skip to content

refactor(tabs): provide method for re-aligning the ink bar #10343

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
Apr 12, 2018
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions src/lib/tabs/tab-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test in for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, although these tests that have to do with element dimensions/positioning tend to be very flaky.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to make this work. I am subscribing to a change in the menu toggle (when the matsidenav is opening or closing), and then I realign the nav bar each time a toggle happens, but the aligning is still not correct :/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented using a timeout like this:

setTimeout(() => {
  this.tabGroup.realignInkBar();
}, 300);

This will align the inkbar, but it has to wait for the first animation to finish (being pushed because the sidenav is expanding), and then animate back to the correct position. I will try to make a Stackblitz to illustrate what I mean. Maybe I am doing something fundamentally wrong to begin with.

if (this._tabHeader) {
this._tabHeader._alignInkBarToSelectedTab();
}
}

_focusChanged(index: number) {
this.focusChange.emit(this._createChangeEvent(index));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tabs/tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down