@@ -9,6 +9,9 @@ class Tooltip {
99
1010 #observer = null
1111 #events = [ ]
12+ #enterDelay = 0
13+ #leaveDelay = 0
14+ #delay = null
1215
1316 #tooltip = null
1417
@@ -44,25 +47,30 @@ class Tooltip {
4447 animated ,
4548 animationEnterClassName ,
4649 animationLeaveClassName ,
50+ enterDelay ,
51+ leaveDelay ,
4752 disabled
4853 ) {
4954 this . #target = target
5055 this . #content = content
5156 this . #contentSelector = contentSelector
57+ this . #contentClone = contentClone || false
5258 this . #contentActions = contentActions
53- this . #contentClone = contentClone
5459 this . #containerClassName = containerClassName
55- this . #position = position
56- this . #animated = animated
60+ this . #position = position || 'top'
61+ this . #animated = animated || false
5762 this . #animationEnterClassName = animationEnterClassName || '__tooltip-enter'
5863 this . #animationLeaveClassName = animationLeaveClassName || '__tooltip-leave'
64+ this . #enterDelay = enterDelay || 0
65+ this . #leaveDelay = leaveDelay || 0
5966
6067 this . #observer = new DOMObserver ( )
6168
6269 this . #target. title = ''
6370 this . #target. setAttribute ( 'style' , 'position: relative' )
6471
6572 this . #createTooltip( )
73+ this . #tooltip. classList . add ( this . #containerClassName || '__tooltip' , `__tooltip-${ this . #position} ` )
6674
6775 disabled ? this . #disableTarget( ) : this . #enableTarget( )
6876
@@ -79,31 +87,41 @@ class Tooltip {
7987 animated ,
8088 animationEnterClassName ,
8189 animationLeaveClassName ,
90+ enterDelay ,
91+ leaveDelay ,
8292 disabled
8393 ) {
8494 const hasContentChanged = contentSelector !== this . #contentSelector || content !== this . #content
8595 const hasContainerClassNameChanged = containerClassName !== this . #containerClassName
96+ const oldPosition = this . #position
97+ const hasPositionChanged = position !== this . #position
8698 const hasToDisableTarget = disabled && this . #boundEnterHandler
8799 const hasToEnableTarget = ! disabled && ! this . #boundEnterHandler
88100
89101 this . #content = content
90102 this . #contentSelector = contentSelector
91- this . #contentClone = contentClone
103+ this . #contentClone = contentClone || false
92104 this . #contentActions = contentActions
93105 this . #containerClassName = containerClassName
94- this . #position = position
95- this . #animated = animated
106+ this . #position = position || 'top'
107+ this . #animated = animated || false
96108 this . #animationEnterClassName = animationEnterClassName || '__tooltip-enter'
97109 this . #animationLeaveClassName = animationLeaveClassName || '__tooltip-leave'
110+ this . #enterDelay = enterDelay || 0
111+ this . #leaveDelay = leaveDelay || 0
98112
99113 if ( hasContentChanged ) {
100114 this . #removeTooltipFromTarget( )
101-
102115 this . #createTooltip( )
103116 }
104117
105118 if ( hasContainerClassNameChanged ) {
106- this . #tooltip. className = this . #containerClassName || `__tooltip __tooltip-${ this . #position} `
119+ this . #tooltip. classList . add ( this . #containerClassName || '__tooltip' )
120+ }
121+
122+ if ( hasPositionChanged ) {
123+ this . #tooltip. classList . remove ( `__tooltip-${ oldPosition } ` )
124+ this . #tooltip. classList . add ( `__tooltip-${ this . #position} ` )
107125 }
108126
109127 if ( hasToDisableTarget ) {
@@ -118,6 +136,8 @@ class Tooltip {
118136
119137 this . #disableTarget( )
120138
139+ this . #clearDelay( )
140+
121141 this . #observer?. clear ( )
122142 this . #observer = null
123143 }
@@ -140,7 +160,6 @@ class Tooltip {
140160
141161 #createTooltip( ) {
142162 this . #tooltip = document . createElement ( 'div' )
143- this . #tooltip. className = this . #containerClassName || `__tooltip __tooltip-${ this . #position} `
144163
145164 if ( this . #contentSelector) {
146165 this . #observer
@@ -229,6 +248,22 @@ class Tooltip {
229248 this . #events = [ ]
230249 }
231250
251+ #waitForDelay( delay ) {
252+ this . #clearDelay( )
253+ return new Promise (
254+ ( resolve ) =>
255+ ( this . #delay = setTimeout ( ( ) => {
256+ this . #clearDelay( )
257+ resolve ( )
258+ } , delay ) )
259+ )
260+ }
261+
262+ #clearDelay( ) {
263+ clearTimeout ( this . #delay)
264+ this . #delay = null
265+ }
266+
232267 #transitionTooltip( direction ) {
233268 return new Promise ( ( resolve ) => {
234269 let classToAdd , classToRemove
@@ -260,10 +295,12 @@ class Tooltip {
260295 }
261296
262297 async #onTargetEnter( ) {
298+ await this . #waitForDelay( this . #enterDelay)
263299 await this . #appendTooltipToTarget( )
264300 }
265301
266302 async #onTargetLeave( ) {
303+ await this . #waitForDelay( this . #leaveDelay)
267304 await this . #removeTooltipFromTarget( )
268305 }
269306}
0 commit comments