@@ -13,7 +13,7 @@ class Tooltip {
1313
1414 #boundEnterHandler = null
1515 #boundLeaveHandler = null
16- #boundKeyDownHandler = null
16+ #boundWindowChangeHandler = null
1717
1818 #target = null
1919 #content = null
@@ -72,7 +72,7 @@ class Tooltip {
7272 this . #target. setAttribute ( 'style' , 'position: relative' )
7373 this . #target. setAttribute ( 'aria-describedby' , 'tooltip' )
7474
75- disabled ? this . #disableTarget ( ) : this . #enableTarget ( )
75+ disabled ? this . #disable ( ) : this . #enable ( )
7676
7777 Tooltip . #instances. push ( this )
7878 }
@@ -122,14 +122,14 @@ class Tooltip {
122122 }
123123
124124 if ( hasToDisableTarget ) {
125- this . #disableTarget ( )
125+ this . #disable ( )
126126 } else if ( hasToEnableTarget ) {
127- this . #enableTarget ( )
127+ this . #enable ( )
128128 }
129129 }
130130
131- destroy ( ) {
132- this . #removeTooltipFromTarget( )
131+ async destroy ( ) {
132+ await this . #removeTooltipFromTarget( )
133133
134134 this . #disableTarget( )
135135
@@ -139,28 +139,50 @@ class Tooltip {
139139 this . #observer = null
140140 }
141141
142+ #enable( ) {
143+ this . #enableTarget( )
144+ this . #enableWindow( )
145+ }
146+
142147 #enableTarget( ) {
143148 this . #boundEnterHandler = this . #onTargetEnter. bind ( this )
144149 this . #boundLeaveHandler = this . #onTargetLeave. bind ( this )
145- this . #boundKeyDownHandler = this . #onTargetKeyDown. bind ( this )
146150
147151 this . #target. addEventListener ( 'mouseenter' , this . #boundEnterHandler)
148152 this . #target. addEventListener ( 'mouseleave' , this . #boundLeaveHandler)
149153 this . #target. addEventListener ( 'focusin' , this . #boundEnterHandler)
150154 this . #target. addEventListener ( 'focusout' , this . #boundLeaveHandler)
151- window . addEventListener ( 'keydown' , this . #boundKeyDownHandler)
155+ }
156+
157+ #enableWindow( ) {
158+ this . #boundWindowChangeHandler = this . #onWindowChange. bind ( this )
159+
160+ window . addEventListener ( 'keydown' , this . #boundWindowChangeHandler)
161+ window . addEventListener ( 'resize' , this . #boundWindowChangeHandler)
162+ window . addEventListener ( 'scroll' , this . #boundWindowChangeHandler)
163+ }
164+
165+ #disable( ) {
166+ this . #disableTarget( )
167+ this . #disableWindow( )
152168 }
153169
154170 #disableTarget( ) {
155171 this . #target. removeEventListener ( 'mouseenter' , this . #boundEnterHandler)
156172 this . #target. removeEventListener ( 'mouseleave' , this . #boundLeaveHandler)
157173 this . #target. removeEventListener ( 'focusin' , this . #boundEnterHandler)
158174 this . #target. removeEventListener ( 'focusout' , this . #boundLeaveHandler)
159- window . removeEventListener ( 'keydown' , this . #boundKeyDownHandler)
160175
161176 this . #boundEnterHandler = null
162177 this . #boundLeaveHandler = null
163- this . #boundKeyDownHandler = null
178+ }
179+
180+ #disableWindow( ) {
181+ window . removeEventListener ( 'keydown' , this . #boundWindowChangeHandler)
182+ window . removeEventListener ( 'resize' , this . #boundWindowChangeHandler)
183+ window . removeEventListener ( 'scroll' , this . #boundWindowChangeHandler)
184+
185+ this . #boundWindowChangeHandler = null
164186 }
165187
166188 #createTooltip( ) {
@@ -345,9 +367,16 @@ class Tooltip {
345367 await this . #removeTooltipFromTarget( )
346368 }
347369
348- async #onTargetKeyDown( e ) {
349- if ( e . key === 'Escape' || e . key === 'Esc' || e . keyCode === 27 ) {
350- await this . #onTargetLeave( )
370+ async #onWindowChange( e ) {
371+ if (
372+ this . #tooltip &&
373+ this . #tooltip. parentNode &&
374+ ( e . type !== 'keydown' ||
375+ ( e . type === 'keydown' && e . key === 'Escape' ) ||
376+ e . key === 'Esc' ||
377+ e . keyCode === 27 )
378+ ) {
379+ await this . #removeTooltipFromTarget( )
351380 }
352381 }
353382}
0 commit comments