Skip to content

Commit 90b2436

Browse files
committed
fix(tabs): unnecessarily adding pagination when changing to new list of tabs with same labels
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 809d991 commit 90b2436

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {ViewportRuler} from '@angular/cdk/scrolling';
2424
import {FocusKeyManager, FocusableOption} from '@angular/cdk/a11y';
2525
import {END, ENTER, HOME, SPACE, hasModifierKey} from '@angular/cdk/keycodes';
2626
import {merge, of as observableOf, Subject, timer, fromEvent} from 'rxjs';
27-
import {takeUntil} from 'rxjs/operators';
27+
import {takeUntil, delay} from 'rxjs/operators';
2828
import {MatInkBar} from './ink-bar';
2929
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
3030

@@ -192,7 +192,8 @@ export abstract class MatPaginatedTabHeader implements AfterContentChecked, Afte
192192
// On dir change or window resize, realign the ink bar and update the orientation of
193193
// the key manager if the direction has changed.
194194
merge(dirChange, resize, this._items.changes).pipe(takeUntil(this._destroyed)).subscribe(() => {
195-
realign();
195+
// We need to defer this to give the browser some time to recalculate the element dimensions.
196+
Promise.resolve().then(realign);
196197
this._keyManager.withHorizontalOrientation(this._getLayoutDirection());
197198
});
198199

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)