Skip to content

Commit

Permalink
Exclude disabled buttons when setting initialIndex of NavigableToolbar (
Browse files Browse the repository at this point in the history
#57280)

* Exclude disabled buttons when setting initial focus on toolbar

When the first item was disabled, it was causing the last item of the toolbar to be focused because the the toolbar was trying to focus a disabled button. This updates the check to only include focusable buttons so we do not try to send focus to an unfocusable button.

* Change getAllToolbarItemsIn to getAllFocusableToolbarItemsIn for clarity
  • Loading branch information
jeryj authored Dec 21, 2023
1 parent c3e60ff commit 05b7175
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/block-editor/src/components/navigable-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ function hasOnlyToolbarItem( elements ) {
return ! elements.some( ( element ) => ! ( dataProp in element.dataset ) );
}

function getAllToolbarItemsIn( container ) {
return Array.from( container.querySelectorAll( '[data-toolbar-item]' ) );
function getAllFocusableToolbarItemsIn( container ) {
return Array.from(
container.querySelectorAll(
'[data-toolbar-item]:not([disabled]):not([aria-disabled="true"])'
)
);
}

function hasFocusWithin( container ) {
Expand Down Expand Up @@ -141,7 +145,8 @@ function useToolbarFocus( {
let raf = 0;
if ( ! initialFocusOnMount ) {
raf = window.requestAnimationFrame( () => {
const items = getAllToolbarItemsIn( navigableToolbarRef );
const items =
getAllFocusableToolbarItemsIn( navigableToolbarRef );
const index = initialIndex || 0;
if ( items[ index ] && hasFocusWithin( navigableToolbarRef ) ) {
items[ index ].focus( {
Expand All @@ -158,7 +163,7 @@ function useToolbarFocus( {
if ( ! onIndexChange || ! navigableToolbarRef ) return;
// When the toolbar element is unmounted and onIndexChange is passed, we
// pass the focused toolbar item index so it can be hydrated later.
const items = getAllToolbarItemsIn( navigableToolbarRef );
const items = getAllFocusableToolbarItemsIn( navigableToolbarRef );
const index = items.findIndex( ( item ) => item.tabIndex === 0 );
onIndexChange( index );
};
Expand Down

1 comment on commit 05b7175

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 05b7175.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7288051617
📝 Reported issues:

Please sign in to comment.