diff --git a/src/index.ts b/src/index.ts index e0773444..120f619e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,10 +34,10 @@ export class KeyboardNavigation { private widgetDropDownDivFocusOutListener: (e: Event) => void; /** Event handler run when the toolbox gains focus. */ - private toolboxFocusListener: () => void; + private toolboxFocusInListener: (e: Event) => void; /** Event handler run when the toolbox loses focus. */ - private toolboxBlurListener: (e: Event) => void; + private toolboxFocusOutListener: (e: Event) => void; /** Event handler run when the flyout gains focus. */ private flyoutFocusListener: () => void; @@ -112,10 +112,6 @@ export class KeyboardNavigation { flyoutElement, workspace.getParentSvg(), ); - // Allow tab to the flyout only when there's no toolbox. - if (workspace.getToolbox() && flyoutElement) { - flyoutElement.tabIndex = -1; - } // Temporary workaround for #136. this.oldMarkFocused = workspace.markFocused; @@ -179,10 +175,26 @@ export class KeyboardNavigation { ); const toolboxElement = getToolboxElement(workspace); - this.toolboxFocusListener = () => { + this.toolboxFocusInListener = (e: Event) => { + if ( + (e.currentTarget as Element).contains( + (e as FocusEvent).relatedTarget as Node, + ) + ) { + return; + } + this.navigationController.handleFocusToolbox(workspace); }; - this.toolboxBlurListener = (e: Event) => { + this.toolboxFocusOutListener = (e: Event) => { + if ( + (e.currentTarget as Element).contains( + (e as FocusEvent).relatedTarget as Node, + ) + ) { + return; + } + this.navigationController.handleBlurToolbox( workspace, this.shouldCloseFlyoutOnBlur( @@ -191,8 +203,8 @@ export class KeyboardNavigation { ), ); }; - toolboxElement?.addEventListener('focus', this.toolboxFocusListener); - toolboxElement?.addEventListener('blur', this.toolboxBlurListener); + toolboxElement?.addEventListener('focusin', this.toolboxFocusInListener); + toolboxElement?.addEventListener('focusout', this.toolboxFocusOutListener); this.flyoutFocusListener = () => { this.navigationController.handleFocusFlyout(workspace); @@ -238,8 +250,11 @@ export class KeyboardNavigation { ); const toolboxElement = getToolboxElement(this.workspace); - toolboxElement?.removeEventListener('focus', this.toolboxFocusListener); - toolboxElement?.removeEventListener('blur', this.toolboxBlurListener); + toolboxElement?.removeEventListener('focusin', this.toolboxFocusInListener); + toolboxElement?.removeEventListener( + 'focusout', + this.toolboxFocusOutListener, + ); const flyoutElement = getFlyoutElement(this.workspace); flyoutElement?.removeEventListener('focus', this.flyoutFocusListener); diff --git a/src/navigation.ts b/src/navigation.ts index 29cf84cb..6092d4b5 100644 --- a/src/navigation.ts +++ b/src/navigation.ts @@ -540,12 +540,6 @@ export class Navigation { if (!Blockly.Gesture.inProgress()) { this.defaultFlyoutCursorIfNeeded(workspace); } - - // Prevent shift-tab to the toolbox while the flyout has focus. - const toolboxElement = getToolboxElement(workspace); - if (toolboxElement) { - toolboxElement.tabIndex = -1; - } } /** @@ -561,12 +555,6 @@ export class Navigation { workspace.hideChaff(); } this.getFlyoutCursor(workspace)?.hide(); - - // Reinstate tab to toolbox. - const toolboxElement = getToolboxElement(workspace); - if (toolboxElement) { - toolboxElement.tabIndex = 0; - } } /**