@@ -15,7 +15,7 @@ const buttonSelectors = [
15
15
'md-mention' ,
16
16
'md-ref'
17
17
]
18
- function getButtons ( toolbar : Element ) : Element [ ] {
18
+ function getButtons ( toolbar : Element ) : HTMLElement [ ] {
19
19
const els = [ ]
20
20
for ( const button of toolbar . querySelectorAll ( buttonSelectors . join ( ', ' ) ) ) {
21
21
// Skip buttons that are hidden, either via `hidden` attribute or CSS:
@@ -251,11 +251,8 @@ class MarkdownToolbarElement extends HTMLElement {
251
251
this . field . addEventListener ( 'keydown' , fn )
252
252
shortcutListeners . set ( this , fn )
253
253
}
254
- let tabindex = '0'
255
- for ( const button of getButtons ( this ) ) {
256
- button . setAttribute ( 'tabindex' , tabindex )
257
- if ( tabindex === '0' ) tabindex = '-1'
258
- }
254
+ this . setAttribute ( 'tabindex' , '0' )
255
+ this . addEventListener ( 'focus' , onToolbarFocus )
259
256
}
260
257
261
258
disconnectedCallback ( ) {
@@ -275,6 +272,19 @@ class MarkdownToolbarElement extends HTMLElement {
275
272
}
276
273
}
277
274
275
+ function onToolbarFocus ( { target} : FocusEvent ) {
276
+ if ( ! ( target instanceof Element ) ) return
277
+ let tabindex = '0'
278
+ target . removeAttribute ( 'tabindex' )
279
+ for ( const button of getButtons ( target ) ) {
280
+ button . setAttribute ( 'tabindex' , tabindex )
281
+ if ( tabindex === '0' ) {
282
+ button . focus ( )
283
+ tabindex = '-1'
284
+ }
285
+ }
286
+ }
287
+
278
288
function focusKeydown ( event : KeyboardEvent ) {
279
289
const key = event . key
280
290
if ( key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End' ) return
@@ -296,9 +306,7 @@ function focusKeydown(event: KeyboardEvent) {
296
306
buttons [ i ] . setAttribute ( 'tabindex' , i === n ? '0' : '-1' )
297
307
}
298
308
299
- if ( buttons [ n ] instanceof HTMLElement ) {
300
- buttons [ n ] . focus ( )
301
- }
309
+ buttons [ n ] . focus ( )
302
310
}
303
311
304
312
const shortcutListeners = new WeakMap ( )
0 commit comments