@@ -74,6 +74,12 @@ export class MdTooltip {
74
74
}
75
75
}
76
76
77
+ /** The default delay in ms before showing the tooltip after show is called */
78
+ @Input ( 'tooltipShowDelay' ) showDelay = 0 ;
79
+
80
+ /** The default delay in ms before hiding the tooltip after hide is called */
81
+ @Input ( 'tooltipHideDelay' ) hideDelay = 0 ;
82
+
77
83
/** The message to be displayed in the tooltip */
78
84
private _message : string ;
79
85
@Input ( 'md-tooltip' ) get message ( ) {
@@ -97,22 +103,20 @@ export class MdTooltip {
97
103
}
98
104
}
99
105
100
- /** Shows the tooltip */
101
- show ( ) : void {
102
- if ( ! this . _message || ! this . _message . trim ( ) ) {
103
- return ;
104
- }
106
+ /** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */
107
+ show ( delay : number = this . showDelay ) : void {
108
+ if ( ! this . _message || ! this . _message . trim ( ) ) { return ; }
105
109
106
110
if ( ! this . _tooltipInstance ) {
107
111
this . _createTooltip ( ) ;
108
112
}
109
113
110
114
this . _setTooltipMessage ( this . _message ) ;
111
- this . _tooltipInstance . show ( this . _position ) ;
115
+ this . _tooltipInstance . show ( this . _position , delay ) ;
112
116
}
113
117
114
- /** Hides the tooltip after the provided delay in ms, defaulting to 0ms. */
115
- hide ( delay = 0 ) : void {
118
+ /** Hides the tooltip after the delay in ms, defaults to tooltip-delay-hide or 0ms if no input */
119
+ hide ( delay : number = this . hideDelay ) : void {
116
120
if ( this . _tooltipInstance ) {
117
121
this . _tooltipInstance . hide ( delay ) ;
118
122
}
@@ -222,7 +226,7 @@ export class MdTooltip {
222
226
}
223
227
}
224
228
225
- export type TooltipVisibility = 'visible' | 'hidden' ;
229
+ export type TooltipVisibility = 'initial' | ' visible' | 'hidden' ;
226
230
227
231
@Component ( {
228
232
moduleId : module . id ,
@@ -232,6 +236,7 @@ export type TooltipVisibility = 'visible' | 'hidden';
232
236
animations : [
233
237
trigger ( 'state' , [
234
238
state ( 'void' , style ( { transform : 'scale(0)' } ) ) ,
239
+ state ( 'initial' , style ( { transform : 'scale(0)' } ) ) ,
235
240
state ( 'visible' , style ( { transform : 'scale(1)' } ) ) ,
236
241
state ( 'hidden' , style ( { transform : 'scale(0)' } ) ) ,
237
242
transition ( '* => visible' , animate ( '150ms cubic-bezier(0.0, 0.0, 0.2, 1)' ) ) ,
@@ -246,11 +251,14 @@ export class TooltipComponent {
246
251
/** Message to display in the tooltip */
247
252
message : string ;
248
253
254
+ /** The timeout ID of any current timer set to show the tooltip */
255
+ _showTimeoutId : number ;
256
+
249
257
/** The timeout ID of any current timer set to hide the tooltip */
250
258
_hideTimeoutId : number ;
251
259
252
260
/** Property watched by the animation framework to show or hide the tooltip */
253
- _visibility : TooltipVisibility ;
261
+ _visibility : TooltipVisibility = 'initial' ;
254
262
255
263
/** Whether interactions on the page should close the tooltip */
256
264
_closeOnInteraction : boolean = false ;
@@ -264,23 +272,33 @@ export class TooltipComponent {
264
272
constructor ( @Optional ( ) private _dir : Dir ) { }
265
273
266
274
/** Shows the tooltip with an animation originating from the provided origin */
267
- show ( position : TooltipPosition ) : void {
268
- this . _closeOnInteraction = false ;
269
- this . _visibility = 'visible' ;
270
- this . _setTransformOrigin ( position ) ;
271
-
275
+ show ( position : TooltipPosition , delay : number ) : void {
272
276
// Cancel the delayed hide if it is scheduled
273
277
if ( this . _hideTimeoutId ) {
274
278
clearTimeout ( this . _hideTimeoutId ) ;
275
279
}
276
280
277
- // If this was set to true immediately, then the body click would trigger interaction and
278
- // close the tooltip right after it was displayed.
279
- setTimeout ( ( ) => { this . _closeOnInteraction = true ; } , 0 ) ;
281
+ // Body interactions should cancel the tooltip if there is a delay in showing.
282
+ this . _closeOnInteraction = true ;
283
+
284
+ this . _setTransformOrigin ( position ) ;
285
+ this . _showTimeoutId = setTimeout ( ( ) => {
286
+ this . _visibility = 'visible' ;
287
+
288
+ // If this was set to true immediately, then a body click that triggers show() would
289
+ // trigger interaction and close the tooltip right after it was displayed.
290
+ this . _closeOnInteraction = false ;
291
+ setTimeout ( ( ) => { this . _closeOnInteraction = true ; } , 0 ) ;
292
+ } , delay ) ;
280
293
}
281
294
282
295
/** Begins the animation to hide the tooltip after the provided delay in ms */
283
296
hide ( delay : number ) : void {
297
+ // Cancel the delayed show if it is scheduled
298
+ if ( this . _showTimeoutId ) {
299
+ clearTimeout ( this . _showTimeoutId ) ;
300
+ }
301
+
284
302
this . _hideTimeoutId = setTimeout ( ( ) => {
285
303
this . _visibility = 'hidden' ;
286
304
this . _closeOnInteraction = false ;
0 commit comments