@@ -4,6 +4,8 @@ import { Utils } from "./utils";
4
4
import { Bounding } from "./bounding" ;
5
5
import { Component , BaseOptions , InitElements , MElement } from "./component" ;
6
6
7
+ export type TooltipPosition = 'top' | 'right' | 'bottom' | 'left' ;
8
+
7
9
export interface TooltipOptions extends BaseOptions {
8
10
/**
9
11
* Delay time before tooltip disappears.
@@ -15,6 +17,11 @@ export interface TooltipOptions extends BaseOptions {
15
17
* @default 0
16
18
*/
17
19
enterDelay : number ;
20
+ /**
21
+ * Element Id for the tooltip.
22
+ * @default ""
23
+ */
24
+ tooltipId ?: string ;
18
25
/**
19
26
* Text string for the tooltip.
20
27
* @default ""
@@ -45,7 +52,7 @@ export interface TooltipOptions extends BaseOptions {
45
52
* Set the direction of the tooltip.
46
53
* @default 'bottom'
47
54
*/
48
- position : 'top' | 'right' | 'bottom' | 'left' ;
55
+ position : TooltipPosition ;
49
56
/**
50
57
* Amount in px that the tooltip moves during its transition.
51
58
* @default 10
@@ -60,7 +67,7 @@ const _defaults: TooltipOptions = {
60
67
margin : 5 ,
61
68
inDuration : 250 ,
62
69
outDuration : 200 ,
63
- position : 'bottom' ,
70
+ position : 'bottom' as TooltipPosition ,
64
71
transitionMovement : 10 ,
65
72
opacity : 1
66
73
} ;
@@ -90,6 +97,7 @@ export class Tooltip extends Component<TooltipOptions> {
90
97
91
98
this . options = {
92
99
...Tooltip . defaults ,
100
+ ...this . _getAttributeOptions ( ) ,
93
101
...options
94
102
} ;
95
103
@@ -139,15 +147,22 @@ export class Tooltip extends Component<TooltipOptions> {
139
147
this . tooltipEl = document . createElement ( 'div' ) ;
140
148
this . tooltipEl . classList . add ( 'material-tooltip' ) ;
141
149
142
- const tooltipContentEl = document . createElement ( 'div' ) ;
150
+ const tooltipContentEl = this . options . tooltipId
151
+ ? document . getElementById ( this . options . tooltipId )
152
+ : document . createElement ( 'div' ) ;
153
+ this . tooltipEl . append ( tooltipContentEl ) ;
154
+ tooltipContentEl . style . display = "" ;
155
+
143
156
tooltipContentEl . classList . add ( 'tooltip-content' ) ;
144
157
this . _setTooltipContent ( tooltipContentEl ) ;
145
158
this . tooltipEl . appendChild ( tooltipContentEl ) ;
146
159
document . body . appendChild ( this . tooltipEl ) ;
147
160
}
148
161
149
162
_setTooltipContent ( tooltipContentEl : HTMLElement ) {
150
- tooltipContentEl . innerText = this . options . text ;
163
+ if ( this . options . tooltipId )
164
+ return ;
165
+ tooltipContentEl . innerText = this . options . text ;
151
166
}
152
167
153
168
_updateTooltipContent ( ) {
@@ -331,16 +346,21 @@ export class Tooltip extends Component<TooltipOptions> {
331
346
this . close ( ) ;
332
347
}
333
348
334
- _getAttributeOptions ( ) {
335
- const attributeOptions = { } ;
349
+ _getAttributeOptions ( ) : Partial < TooltipOptions > {
350
+ let attributeOptions : Partial < TooltipOptions > = { } ;
336
351
const tooltipTextOption = this . el . getAttribute ( 'data-tooltip' ) ;
352
+ const tooltipId = this . el . getAttribute ( 'data-tooltip-id' ) ;
337
353
const positionOption = this . el . getAttribute ( 'data-position' ) ;
338
354
if ( tooltipTextOption ) {
339
- ( attributeOptions as any ) . text = tooltipTextOption ;
355
+ attributeOptions . text = tooltipTextOption ;
340
356
}
341
357
if ( positionOption ) {
342
- ( attributeOptions as any ) . position = positionOption ;
358
+ attributeOptions . position = positionOption as TooltipPosition ;
359
+ }
360
+ if ( tooltipId ) {
361
+ attributeOptions . tooltipId = tooltipId ;
343
362
}
363
+
344
364
return attributeOptions ;
345
365
}
346
366
}
0 commit comments