@@ -15,7 +15,7 @@ const buttonSelectors = [
1515 'md-mention' ,
1616 'md-ref'
1717]
18- function getButtons ( toolbar : Element ) : Element [ ] {
18+ function getButtons ( toolbar : Element ) : HTMLElement [ ] {
1919 const els = [ ]
2020 for ( const button of toolbar . querySelectorAll ( buttonSelectors . join ( ', ' ) ) ) {
2121 // Skip buttons that are hidden, either via `hidden` attribute or CSS:
@@ -251,11 +251,8 @@ class MarkdownToolbarElement extends HTMLElement {
251251 this . field . addEventListener ( 'keydown' , fn )
252252 shortcutListeners . set ( this , fn )
253253 }
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 )
259256 }
260257
261258 disconnectedCallback ( ) {
@@ -275,6 +272,19 @@ class MarkdownToolbarElement extends HTMLElement {
275272 }
276273}
277274
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+
278288function focusKeydown ( event : KeyboardEvent ) {
279289 const key = event . key
280290 if ( key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End' ) return
@@ -296,9 +306,7 @@ function focusKeydown(event: KeyboardEvent) {
296306 buttons [ i ] . setAttribute ( 'tabindex' , i === n ? '0' : '-1' )
297307 }
298308
299- if ( buttons [ n ] instanceof HTMLElement ) {
300- buttons [ n ] . focus ( )
301- }
309+ buttons [ n ] . focus ( )
302310}
303311
304312const shortcutListeners = new WeakMap ( )
0 commit comments