@@ -319,22 +319,43 @@ function applyFromToolbar(event: Event) {
319319 applyStyle ( target , style )
320320}
321321
322+ function setFocusManagement ( toolbar : MarkdownToolbarElement ) {
323+ toolbar . addEventListener ( 'keydown' , focusKeydown )
324+ toolbar . setAttribute ( 'tabindex' , '0' )
325+ toolbar . addEventListener ( 'focus' , onToolbarFocus , { once : true } )
326+ }
327+
328+ function unsetFocusManagement ( toolbar : MarkdownToolbarElement ) {
329+ toolbar . removeEventListener ( 'keydown' , focusKeydown )
330+ toolbar . removeAttribute ( 'tabindex' )
331+ toolbar . removeEventListener ( 'focus' , onToolbarFocus )
332+ }
333+
322334class MarkdownToolbarElement extends HTMLElement {
335+ static observedAttributes = [ 'data-no-focus' ]
336+
323337 connectedCallback ( ) : void {
324338 if ( ! this . hasAttribute ( 'role' ) ) {
325339 this . setAttribute ( 'role' , 'toolbar' )
326340 }
327341 if ( ! this . hasAttribute ( 'data-no-focus' ) ) {
328- this . addEventListener ( 'keydown' , focusKeydown )
329- this . setAttribute ( 'tabindex' , '0' )
330- this . addEventListener ( 'focus' , onToolbarFocus , { once : true } )
342+ setFocusManagement ( this )
331343 }
332344 this . addEventListener ( 'keydown' , keydown ( applyFromToolbar ) )
333345 this . addEventListener ( 'click' , applyFromToolbar )
334346 }
335347
348+ attributeChangedCallback ( name : string , oldValue : string , newValue : string ) : void {
349+ if ( name !== 'data-no-focus' ) return
350+ if ( newValue === null ) {
351+ setFocusManagement ( this )
352+ } else {
353+ unsetFocusManagement ( this )
354+ }
355+ }
356+
336357 disconnectedCallback ( ) : void {
337- this . removeEventListener ( 'keydown' , focusKeydown )
358+ unsetFocusManagement ( this )
338359 }
339360
340361 get field ( ) : HTMLTextAreaElement | null {
0 commit comments