Skip to content

Commit 02f5256

Browse files
feat(tabs): Allow tab ink bar positioning to be determined with a provided method. (#9972)
1 parent 0e37d6c commit 02f5256

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/lib/tabs/ink-bar.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,33 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, NgZone} from '@angular/core';
9+
import { Directive, ElementRef, Inject, InjectionToken, NgZone } from '@angular/core';
1010

11+
/**
12+
* Interface for a a MatInkBar positioner method, defining the positioning and width of the ink
13+
* bar in a set of tabs.
14+
*/
15+
// tslint:disable-next-line class-name Using leading underscore to denote internal interface.
16+
export interface _MatInkBarPositioner {
17+
(element: HTMLElement): { left: string, width: string };
18+
}
19+
20+
/** Injection token for the MatInkBar's Positioner. */
21+
export const _MAT_INK_BAR_POSITIONER =
22+
new InjectionToken<_MatInkBarPositioner>('MatInkBarPositioner', {
23+
providedIn: 'root',
24+
factory: () => _matInkBarPositioner
25+
});
26+
27+
/**
28+
* The default positioner function for the MatInkBar.
29+
*/
30+
export const _matInkBarPositioner: _MatInkBarPositioner = (element: HTMLElement) => {
31+
return {
32+
left: element ? (element.offsetLeft || 0) + 'px' : '0',
33+
width: element ? (element.offsetWidth || 0) + 'px' : '0',
34+
};
35+
};
1136

1237
/**
1338
* The ink-bar is used to display and animate the line underneath the current active tab label.
@@ -22,7 +47,8 @@ import {Directive, ElementRef, NgZone} from '@angular/core';
2247
export class MatInkBar {
2348
constructor(
2449
private _elementRef: ElementRef,
25-
private _ngZone: NgZone) {}
50+
private _ngZone: NgZone,
51+
@Inject(_MAT_INK_BAR_POSITIONER) private _inkBarPositioner: _MatInkBarPositioner) { }
2652

2753
/**
2854
* Calculates the styles from the provided element in order to align the ink-bar to that element.
@@ -56,9 +82,10 @@ export class MatInkBar {
5682
* @param element
5783
*/
5884
private _setStyles(element: HTMLElement) {
85+
const positions = this._inkBarPositioner(element);
5986
const inkBar: HTMLElement = this._elementRef.nativeElement;
6087

61-
inkBar.style.left = element ? (element.offsetLeft || 0) + 'px' : '0';
62-
inkBar.style.width = element ? (element.offsetWidth || 0) + 'px' : '0';
88+
inkBar.style.left = positions.left;
89+
inkBar.style.width = positions.width;
6390
}
6491
}

src/lib/tabs/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
export * from './tabs-module';
1010
export * from './tab-group';
11-
export {MatInkBar} from './ink-bar';
11+
export {MatInkBar, _MatInkBarPositioner, _MAT_INK_BAR_POSITIONER} from './ink-bar';
1212
export {
1313
MatTabBody,
1414
MatTabBodyOriginState,

0 commit comments

Comments
 (0)