Skip to content

Commit

Permalink
fix(tabs): pagination not enabled on init on some browsers
Browse files Browse the repository at this point in the history
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 angular#7983.
  • Loading branch information
crisbeto committed Nov 5, 2017
1 parent 24f0471 commit 5b6b794
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/lib/tabs/tab-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('MatTabHeader', () => {
});
});

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

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

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

expect(inkBar.alignToElement).toHaveBeenCalled();
});
}));

it('should re-align the ink bar when the window is resized', fakeAsync(() => {
fixture = TestBed.createComponent(SimpleTabHeaderApp);
Expand Down
11 changes: 7 additions & 4 deletions src/lib/tabs/tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import {Direction, Directionality} from '@angular/cdk/bidi';
import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
import {startWith} from 'rxjs/operators/startWith';
import {
AfterContentChecked,
AfterContentInit,
Expand Down Expand Up @@ -188,11 +187,15 @@ export class MatTabHeader extends _MatTabHeaderMixinBase
ngAfterContentInit() {
const dirChange = this._dir ? this._dir.change : observableOf(null);
const resize = this._viewportRuler.change(150);

this._realignInkBar = merge(dirChange, resize).pipe(startWith(null)).subscribe(() => {
const realign = () => {
this._updatePagination();
this._alignInkBarToSelectedTab();
});
};

// Defer the first call in order to allow for slower browsers to lay out the elements.
// This helps in cases where the user lands directly on a page with paginated tabs.
typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame(realign) : realign();
this._realignInkBar = merge(dirChange, resize).subscribe(realign);
}

ngOnDestroy() {
Expand Down

0 comments on commit 5b6b794

Please sign in to comment.