Skip to content

Commit 0c6bc58

Browse files
committed
fix(tabs): re-align ink bar on direction change
Re-aligns the ink bar if the user's layout direction changed. Fixes #3615.
1 parent 05c865d commit 0c6bc58

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import {RIGHT_ARROW, LEFT_ARROW, ENTER} from '../core/keyboard/keycodes';
1111
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
1212
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1313
import {dispatchKeyboardEvent} from '../core/testing/dispatch-events';
14+
import {Subject} from 'rxjs/Subject';
1415

1516

1617
describe('MdTabHeader', () => {
1718
let dir: LayoutDirection = 'ltr';
19+
let dirChange = new Subject();
1820
let fixture: ComponentFixture<SimpleTabHeaderApp>;
1921
let appComponent: SimpleTabHeaderApp;
2022

@@ -29,7 +31,9 @@ describe('MdTabHeader', () => {
2931
SimpleTabHeaderApp,
3032
],
3133
providers: [
32-
{provide: Dir, useFactory: () => { return {value: dir}; }},
34+
{provide: Dir, useFactory: () => {
35+
return {value: dir, dirChange: dirChange.asObservable()};
36+
}},
3337
{provide: ViewportRuler, useClass: FakeViewportRuler},
3438
]
3539
});
@@ -211,7 +215,7 @@ interface Tab {
211215
*ngFor="let tab of tabs; let i = index"
212216
[disabled]="!!tab.disabled"
213217
(click)="selectedIndex = i">
214-
{{tab.label}}
218+
{{tab.label}}
215219
</div>
216220
</md-tab-header>
217221
</div>

src/lib/tabs/tab-header.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import {
1212
Optional,
1313
AfterContentChecked,
1414
AfterContentInit,
15+
OnDestroy,
1516
} from '@angular/core';
1617
import {RIGHT_ARROW, LEFT_ARROW, ENTER, Dir, LayoutDirection} from '../core';
1718
import {MdTabLabelWrapper} from './tab-label-wrapper';
1819
import {MdInkBar} from './ink-bar';
19-
import 'rxjs/add/operator/map';
20+
import {Subscription} from 'rxjs/Subscription';
2021
import {applyCssTransform} from '../core/style/apply-transform';
22+
import 'rxjs/add/operator/map';
2123

2224
/**
2325
* The directions that scrolling can go in when the header's tabs exceed the header width. 'After'
@@ -51,7 +53,7 @@ const EXAGGERATED_OVERSCROLL = 60;
5153
'[class.mat-tab-header-rtl]': "_getLayoutDirection() == 'rtl'",
5254
}
5355
})
54-
export class MdTabHeader implements AfterContentChecked, AfterContentInit {
56+
export class MdTabHeader implements AfterContentChecked, AfterContentInit, OnDestroy {
5557
@ContentChildren(MdTabLabelWrapper) _labelWrappers: QueryList<MdTabLabelWrapper>;
5658

5759
@ViewChild(MdInkBar) _inkBar: MdInkBar;
@@ -67,6 +69,9 @@ export class MdTabHeader implements AfterContentChecked, AfterContentInit {
6769
/** Whether the header should scroll to the selected index after the view has been checked. */
6870
private _selectedIndexChanged = false;
6971

72+
/** Subscription to changes in the layout direction. */
73+
private _directionChange: Subscription;
74+
7075
/** Whether the controls for pagination should be displayed */
7176
_showPaginationControls = false;
7277

@@ -149,6 +154,17 @@ export class MdTabHeader implements AfterContentChecked, AfterContentInit {
149154
*/
150155
ngAfterContentInit() {
151156
this._alignInkBarToSelectedTab();
157+
158+
if (this._dir) {
159+
this._directionChange = this._dir.dirChange.subscribe(() => this._alignInkBarToSelectedTab());
160+
}
161+
}
162+
163+
ngOnDestroy() {
164+
if (this._directionChange) {
165+
this._directionChange.unsubscribe();
166+
this._directionChange = null;
167+
}
152168
}
153169

154170
/**

src/lib/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import {
44
ViewChild,
55
ElementRef,
66
ViewEncapsulation,
7-
Directive, NgZone, Inject, Optional,
7+
Directive,
8+
NgZone,
9+
Inject,
10+
Optional,
11+
OnDestroy,
812
} from '@angular/core';
913
import {MdInkBar} from '../ink-bar';
1014
import {MdRipple} from '../../core/ripple/index';
1115
import {ViewportRuler} from '../../core/overlay/position/viewport-ruler';
12-
import {MD_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '../../core/ripple/ripple';
16+
import {MD_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions, Dir} from '../../core';
17+
import {Subscription} from 'rxjs/Subscription';
1318

1419
/**
1520
* Navigation component matching the styles of the tab group header.
@@ -25,12 +30,19 @@ import {MD_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '../../core/ripple/r
2530
},
2631
encapsulation: ViewEncapsulation.None,
2732
})
28-
export class MdTabNavBar {
33+
export class MdTabNavBar implements OnDestroy {
34+
private _directionChange: Subscription;
2935
_activeLinkChanged: boolean;
3036
_activeLinkElement: ElementRef;
3137

3238
@ViewChild(MdInkBar) _inkBar: MdInkBar;
3339

40+
constructor(@Optional() private _dir: Dir, private _ngZone: NgZone) {
41+
if (_dir) {
42+
this._directionChange = _dir.dirChange.subscribe(() => this._alignInkBar());
43+
}
44+
}
45+
3446
/** Notifies the component that the active link has been changed. */
3547
updateActiveLink(element: ElementRef) {
3648
this._activeLinkChanged = this._activeLinkElement != element;
@@ -40,10 +52,26 @@ export class MdTabNavBar {
4052
/** Checks if the active link has been changed and, if so, will update the ink bar. */
4153
ngAfterContentChecked(): void {
4254
if (this._activeLinkChanged) {
43-
this._inkBar.alignToElement(this._activeLinkElement.nativeElement);
55+
this._alignInkBar();
4456
this._activeLinkChanged = false;
4557
}
4658
}
59+
60+
ngOnDestroy() {
61+
if (this._directionChange) {
62+
this._directionChange.unsubscribe();
63+
this._directionChange = null;
64+
}
65+
}
66+
67+
/** Aligns the ink bar to the active link. */
68+
private _alignInkBar(): void {
69+
this._ngZone.runOutsideAngular(() => {
70+
requestAnimationFrame(() => {
71+
this._inkBar.alignToElement(this._activeLinkElement.nativeElement);
72+
});
73+
});
74+
}
4775
}
4876

4977
/**

0 commit comments

Comments
 (0)