Skip to content

Commit d26ba73

Browse files
crisbetojelbourn
authored andcommitted
fix(tabs): unnecessarily adding pagination when changing to new list of tabs with same labels (#16869)
These changes fix an issue where swapping out the list of tabs to one with the same labels causes the header to add a pagination when it doesn't have to. It looks like issue comes down to the fact that we check the element dimensions before they've had the chance to update. This works for the case where the tabs have different text, because we also have a `MutationObserver` that looks at them and it fires a bit later. Fixes #16789.
1 parent d7d3be1 commit d26ba73

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/material/tabs/paginated-tab-header.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ export abstract class MatPaginatedTabHeader implements AfterContentChecked, Afte
204204
// On dir change or window resize, realign the ink bar and update the orientation of
205205
// the key manager if the direction has changed.
206206
merge(dirChange, resize, this._items.changes).pipe(takeUntil(this._destroyed)).subscribe(() => {
207-
realign();
207+
// We need to defer this to give the browser some time to recalculate the element dimensions.
208+
Promise.resolve().then(realign);
208209
this._keyManager.withHorizontalOrientation(this._getLayoutDirection());
209210
});
210211

src/material/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,29 @@ describe('MatTabNavBar', () => {
142142
expect(tabLinkElement.classList).toContain('mat-tab-disabled');
143143
});
144144

145-
it('should re-align the ink bar when the direction changes', () => {
145+
it('should re-align the ink bar when the direction changes', fakeAsync(() => {
146146
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
147147

148148
spyOn(inkBar, 'alignToElement');
149149

150150
dirChange.next();
151+
tick();
151152
fixture.detectChanges();
152153

153154
expect(inkBar.alignToElement).toHaveBeenCalled();
154-
});
155+
}));
155156

156-
it('should re-align the ink bar when the tabs list change', () => {
157+
it('should re-align the ink bar when the tabs list change', fakeAsync(() => {
157158
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
158159

159160
spyOn(inkBar, 'alignToElement');
160161

161162
fixture.componentInstance.tabs = [1, 2, 3, 4];
162163
fixture.detectChanges();
164+
tick();
163165

164166
expect(inkBar.alignToElement).toHaveBeenCalled();
165-
});
167+
}));
166168

167169
it('should re-align the ink bar when the tab labels change the width', done => {
168170
const inkBar = fixture.componentInstance.tabNavBar._inkBar;

0 commit comments

Comments
 (0)