6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Directive , ElementRef , NgZone } from '@angular/core' ;
9
+ import { Directive , ElementRef , Inject , InjectionToken , NgZone } from '@angular/core' ;
10
10
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
+ } ;
11
36
12
37
/**
13
38
* 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';
22
47
export class MatInkBar {
23
48
constructor (
24
49
private _elementRef : ElementRef ,
25
- private _ngZone : NgZone ) { }
50
+ private _ngZone : NgZone ,
51
+ @Inject ( _MAT_INK_BAR_POSITIONER ) private _inkBarPositioner : _MatInkBarPositioner ) { }
26
52
27
53
/**
28
54
* Calculates the styles from the provided element in order to align the ink-bar to that element.
@@ -56,9 +82,10 @@ export class MatInkBar {
56
82
* @param element
57
83
*/
58
84
private _setStyles ( element : HTMLElement ) {
85
+ const positions = this . _inkBarPositioner ( element ) ;
59
86
const inkBar : HTMLElement = this . _elementRef . nativeElement ;
60
87
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 ;
63
90
}
64
91
}
0 commit comments