@@ -124,10 +124,38 @@ class JSPanel {
124
124
}
125
125
}
126
126
} ) ;
127
- this . button . addEventListener ( "click" , ( e ) => this . _togglePanel ( e ) ) ;
128
- this . button . addEventListener ( "keydown" , ( e ) => this . _toggleOnKeyboardEvent ( e ) ) ;
127
+
128
+ this . button . onclick = ( e ) => { this . _togglePanel ( e ) } ;
129
+ this . button . onkeydown = ( e ) => { this . _toggleOnKeyboardEvent ( e ) } ;
129
130
130
131
this . _insertAfterButton ( this . panel ) ;
132
+
133
+ this . panel . onkeydown = ( e : KeyboardEvent ) => {
134
+ if ( e . key === "Tab" || e . keyCode === 9 ) {
135
+ if ( this . _isOpen ( ) ) this . _focusInPanel ( e ) ;
136
+ }
137
+ } ;
138
+
139
+ // I absolutly want the focus to stay inside (and only INSIDE) the panel :)
140
+ // For that, I need to add a keydown event to the button too because
141
+ // I want to include it during the keyboard navigation (with Tab)
142
+ // So that it's easier for the user to close the panel with his/her keyboard.
143
+ this . button . onkeydown = ( e : KeyboardEvent ) => {
144
+ if ( e . key === "Tab" || e . keyCode === 9 ) {
145
+ if ( this . _isOpen ( ) ) {
146
+ e . preventDefault ( ) ;
147
+
148
+ const active_elements = this . _getAllActiveItems ( ) ;
149
+ if ( active_elements && active_elements [ 0 ] ) {
150
+ if ( e . shiftKey === true ) {
151
+ active_elements [ active_elements . length - 2 ] . focus ( ) ; // -2 because we don't want to take into account the button once again
152
+ } else {
153
+ active_elements [ 0 ] . focus ( ) ;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ } ;
131
159
}
132
160
133
161
/**
@@ -192,9 +220,9 @@ class JSPanel {
192
220
193
221
/**
194
222
* Gets all the active items from the panel if it's open.
195
- * @returns {Array<Element >|null } All the items that have an onclick property.
223
+ * @returns {Array<HTMLElement >|null } All the items that have an onclick property.
196
224
*/
197
- private _getAllActiveItems ( ) : Element [ ] | null {
225
+ private _getAllActiveItems ( ) : HTMLElement [ ] | null {
198
226
if ( this . _isOpen ( ) ) {
199
227
const active_elements : HTMLElement [ ] = Array . from ( ( this . panel as HTMLElement ) . querySelectorAll ( "button" ) ) ;
200
228
active_elements . push ( this . button as HTMLElement ) ;
@@ -300,12 +328,6 @@ class JSPanel {
300
328
this . _closePanel ( ) ;
301
329
} ) ;
302
330
303
- window . addEventListener ( "keydown" , ( e : KeyboardEvent ) => {
304
- if ( e . key === "Tab" || e . keyCode === 9 ) {
305
- if ( this . _isOpen ( ) ) this . _focusInPanel ( e ) ;
306
- }
307
- } ) ;
308
-
309
331
return button ;
310
332
}
311
333
}
@@ -330,7 +352,7 @@ class JSPanel {
330
352
if ( index < 0 ) {
331
353
index = all_items . length - 1 ;
332
354
}
333
- ( all_items [ index ] as HTMLElement ) . focus ( ) ;
355
+ all_items [ index ] . focus ( ) ;
334
356
}
335
357
}
336
358
@@ -376,4 +398,4 @@ class JSPanel {
376
398
max = Math . floor ( max ) ;
377
399
return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
378
400
}
379
- }
401
+ }
0 commit comments