Skip to content

Commit 59564c7

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 59564c7

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

src/lib/tabs/ink-bar.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive, Renderer, ElementRef} from '@angular/core';
1+
import {Directive, Renderer, ElementRef, NgZone} from '@angular/core';
22

33

44
/**
@@ -12,7 +12,10 @@ import {Directive, Renderer, ElementRef} from '@angular/core';
1212
},
1313
})
1414
export class MdInkBar {
15-
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
15+
constructor(
16+
private _renderer: Renderer,
17+
private _elementRef: ElementRef,
18+
private _ngZone: NgZone) {}
1619

1720
/**
1821
* Calculates the styles from the provided element in order to align the ink-bar to that element.
@@ -21,10 +24,15 @@ export class MdInkBar {
2124
*/
2225
alignToElement(element: HTMLElement) {
2326
this.show();
24-
this._renderer.setElementStyle(this._elementRef.nativeElement, 'left',
25-
this._getLeftPosition(element));
26-
this._renderer.setElementStyle(this._elementRef.nativeElement, 'width',
27-
this._getElementWidth(element));
27+
28+
this._ngZone.runOutsideAngular(() => {
29+
requestAnimationFrame(() => {
30+
this._renderer.setElementStyle(this._elementRef.nativeElement, 'left',
31+
this._getLeftPosition(element));
32+
this._renderer.setElementStyle(this._elementRef.nativeElement, 'width',
33+
this._getElementWidth(element));
34+
});
35+
});
2836
}
2937

3038
/** Shows the ink bar. */

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: 28 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) {
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,22 @@ 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._inkBar.alignToElement(this._activeLinkElement.nativeElement);
70+
}
4771
}
4872

4973
/**

0 commit comments

Comments
 (0)