Skip to content

Commit 2c34a7e

Browse files
crisbetotinayuangao
authored andcommitted
fix(tabs): pagination not enabled on init on some browsers (#8104)
Fixes an issue where the tabs pagination may not be enabled on some slower browsers, because elements are being measured before they're done rendering. Fixes #7983.
1 parent f0789eb commit 2c34a7e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/lib/tabs/tab-header.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ describe('MatTabHeader', () => {
243243
});
244244
});
245245

246-
it('should re-align the ink bar when the direction changes', () => {
246+
it('should re-align the ink bar when the direction changes', fakeAsync(() => {
247247
fixture = TestBed.createComponent(SimpleTabHeaderApp);
248248

249249
const inkBar = fixture.componentInstance.tabHeader._inkBar;
@@ -253,9 +253,10 @@ describe('MatTabHeader', () => {
253253

254254
change.next();
255255
fixture.detectChanges();
256+
tick(20); // Angular turns rAF calls into 16.6ms timeouts in tests.
256257

257258
expect(inkBar.alignToElement).toHaveBeenCalled();
258-
});
259+
}));
259260

260261
it('should re-align the ink bar when the window is resized', fakeAsync(() => {
261262
fixture = TestBed.createComponent(SimpleTabHeaderApp);

src/lib/tabs/tab-header.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {Direction, Directionality} from '@angular/cdk/bidi';
1010
import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
11-
import {startWith} from 'rxjs/operators/startWith';
1211
import {
1312
AfterContentChecked,
1413
AfterContentInit,
@@ -188,11 +187,15 @@ export class MatTabHeader extends _MatTabHeaderMixinBase
188187
ngAfterContentInit() {
189188
const dirChange = this._dir ? this._dir.change : observableOf(null);
190189
const resize = this._viewportRuler.change(150);
191-
192-
this._realignInkBar = merge(dirChange, resize).pipe(startWith(null)).subscribe(() => {
190+
const realign = () => {
193191
this._updatePagination();
194192
this._alignInkBarToSelectedTab();
195-
});
193+
};
194+
195+
// Defer the first call in order to allow for slower browsers to lay out the elements.
196+
// This helps in cases where the user lands directly on a page with paginated tabs.
197+
typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame(realign) : realign();
198+
this._realignInkBar = merge(dirChange, resize).subscribe(realign);
196199
}
197200

198201
ngOnDestroy() {

0 commit comments

Comments
 (0)