@@ -8,7 +8,7 @@ import { Directive, directive } from 'lit/directive.js';
88import { ifDefined } from 'lit/directives/if-defined.js' ;
99import { DisabledMixin } from '@vaadin/a11y-base/src/disabled-mixin.js' ;
1010import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js' ;
11- import { isElementFocused , isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js' ;
11+ import { isElementFocused , isElementHidden , isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js' ;
1212import { KeyboardDirectionMixin } from '@vaadin/a11y-base/src/keyboard-direction-mixin.js' ;
1313import { microTask } from '@vaadin/component-base/src/async.js' ;
1414import { Debouncer } from '@vaadin/component-base/src/debounce.js' ;
@@ -500,12 +500,6 @@ export const MenuBarMixin = (superClass) =>
500500
501501 const items = buttons . filter ( ( b ) => ! remaining . includes ( b ) ) . map ( ( b ) => b . item ) ;
502502 this . __updateOverflow ( items ) ;
503-
504- // Ensure there is at least one button with tabindex set to 0
505- // so that menu-bar is not skipped when navigating with Tab
506- if ( remaining . length && ! remaining . some ( ( btn ) => btn . getAttribute ( 'tabindex' ) === '0' ) ) {
507- this . _setTabindex ( remaining [ remaining . length - 1 ] , true ) ;
508- }
509503 }
510504 }
511505
@@ -536,13 +530,23 @@ export const MenuBarMixin = (superClass) =>
536530 const isSingleButton = newOverflowCount === buttons . length || ( newOverflowCount === 0 && buttons . length === 1 ) ;
537531 this . toggleAttribute ( 'has-single-button' , isSingleButton ) ;
538532
533+ // Collect visible buttons to detect if tabindex should be updated
534+ const visibleButtons = buttons . filter ( ( btn ) => btn . style . visibility !== 'hidden' ) ;
535+
536+ if ( ! visibleButtons . length ) {
537+ // If all buttons except overflow are hidden, set tabindex on it
538+ this . _overflow . setAttribute ( 'tabindex' , '0' ) ;
539+ } else if ( ! visibleButtons . some ( ( btn ) => btn . getAttribute ( 'tabindex' ) === '0' ) ) {
540+ // Ensure there is at least one button with tabindex set to 0
541+ // so that menu-bar is not skipped when navigating with Tab
542+ this . _setTabindex ( visibleButtons [ visibleButtons . length - 1 ] , true ) ;
543+ }
544+
539545 // Apply first/last visible attributes to the visible buttons
540- buttons
541- . filter ( ( btn ) => btn . style . visibility !== 'hidden' )
542- . forEach ( ( btn , index , visibleButtons ) => {
543- btn . toggleAttribute ( 'first-visible' , index === 0 ) ;
544- btn . toggleAttribute ( 'last-visible' , ! this . _hasOverflow && index === visibleButtons . length - 1 ) ;
545- } ) ;
546+ visibleButtons . forEach ( ( btn , index , visibleButtons ) => {
547+ btn . toggleAttribute ( 'first-visible' , index === 0 ) ;
548+ btn . toggleAttribute ( 'last-visible' , ! this . _hasOverflow && index === visibleButtons . length - 1 ) ;
549+ } ) ;
546550 }
547551
548552 /** @private */
@@ -761,8 +765,7 @@ export const MenuBarMixin = (superClass) =>
761765 */
762766 _setFocused ( focused ) {
763767 if ( focused ) {
764- const selector = this . tabNavigation ? '[focused]' : '[tabindex="0"]' ;
765- const target = this . querySelector ( `vaadin-menu-bar-button${ selector } ` ) ;
768+ const target = this . __getFocusTarget ( ) ;
766769 if ( target ) {
767770 this . _buttons . forEach ( ( btn ) => {
768771 this . _setTabindex ( btn , btn === target ) ;
@@ -776,6 +779,24 @@ export const MenuBarMixin = (superClass) =>
776779 }
777780 }
778781
782+ /** @private */
783+ __getFocusTarget ( ) {
784+ // First, check if focus is moving to a visible button
785+ let target = this . _buttons . find ( ( btn ) => isElementFocused ( btn ) ) ;
786+
787+ if ( ! target ) {
788+ const selector = this . tabNavigation ? '[focused]' : '[tabindex="0"]' ;
789+ // Next, check if there is a button that could be focused but is hidden
790+ target = this . querySelector ( `vaadin-menu-bar-button${ selector } ` ) ;
791+
792+ if ( isElementHidden ( target ) ) {
793+ target = this . _buttons [ this . _getFocusableIndex ( ) ] ;
794+ }
795+ }
796+
797+ return target ;
798+ }
799+
779800 /**
780801 * @param {!KeyboardEvent } event
781802 * @private
0 commit comments