Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not focus new navigation block menu until loading is finished #59801

Merged
merged 7 commits into from Mar 14, 2024
83 changes: 43 additions & 40 deletions packages/block-library/src/navigation/edit/index.js
Expand Up @@ -390,6 +390,7 @@ function Navigation( {
handleUpdateMenu,
hideNavigationMenuStatusNotice,
showNavigationMenuStatusNotice,
isLoading,
] );

useEffect( () => {
Expand Down Expand Up @@ -866,50 +867,52 @@ function Navigation( {
</InspectorControls>
) }

{ isLoading && (
<TagName { ...blockProps }>
<TagName
{ ...blockProps }
aria-describedby={
! isPlaceholder && ! isLoading
? accessibleDescriptionId
: undefined
}
>
{ isLoading && (
<div className="wp-block-navigation__loading-indicator-container">
<Spinner className="wp-block-navigation__loading-indicator" />
</div>
</TagName>
) }
) }

{ ! isLoading && (
<TagName
{ ...blockProps }
aria-describedby={
! isPlaceholder
? accessibleDescriptionId
: undefined
}
>
<AccessibleMenuDescription
id={ accessibleDescriptionId }
/>
<ResponsiveWrapper
id={ clientId }
onToggle={ setResponsiveMenuVisibility }
hasIcon={ hasIcon }
icon={ icon }
isOpen={ isResponsiveMenuOpen }
isResponsive={ isResponsive }
isHiddenByDefault={ 'always' === overlayMenu }
overlayBackgroundColor={ overlayBackgroundColor }
overlayTextColor={ overlayTextColor }
>
{ isEntityAvailable && (
<NavigationInnerBlocks
clientId={ clientId }
hasCustomPlaceholder={
!! CustomPlaceholder
}
templateLock={ templateLock }
orientation={ orientation }
/>
) }
</ResponsiveWrapper>
</TagName>
) }
{ ! isLoading && (
<>
<AccessibleMenuDescription
id={ accessibleDescriptionId }
/>
<ResponsiveWrapper
id={ clientId }
onToggle={ setResponsiveMenuVisibility }
hasIcon={ hasIcon }
icon={ icon }
isOpen={ isResponsiveMenuOpen }
isResponsive={ isResponsive }
isHiddenByDefault={ 'always' === overlayMenu }
overlayBackgroundColor={
overlayBackgroundColor
}
overlayTextColor={ overlayTextColor }
>
{ isEntityAvailable && (
<NavigationInnerBlocks
clientId={ clientId }
hasCustomPlaceholder={
!! CustomPlaceholder
}
templateLock={ templateLock }
orientation={ orientation }
/>
) }
</ResponsiveWrapper>
</>
) }
</TagName>
</RecursionProvider>
</EntityProvider>
);
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/specs/editor/blocks/navigation-list-view.spec.js
Expand Up @@ -543,6 +543,50 @@ test.describe( 'Navigation block - List view editing', () => {
// we have unmounted the list view and then remounted it).
await expect( linkControl.getSearchInput() ).toBeHidden();
} );

test( `can create a new menu without losing focus`, async ( {
page,
editor,
requestUtils,
linkControl,
} ) => {
await requestUtils.createNavigationMenu( navMenuBlocksFixture );

await editor.insertBlock( { name: 'core/navigation' } );

await editor.openDocumentSettingsSidebar();

await page.getByLabel( 'Test Menu' ).click();

await page.keyboard.press( 'ArrowUp' );

await expect(
page.getByRole( 'menuitem', { name: 'Create new menu' } )
).toBeFocused();

await page.keyboard.press( 'Enter' );

// Check that the menu was created
await expect(
page
.getByTestId( 'snackbar' )
.getByText( 'Navigation Menu successfully created.' )
).toBeVisible();
await expect(
page.getByText( 'This navigation menu is empty.' )
).toBeVisible();

// Move focus to the appender
await page.keyboard.press( 'ArrowDown' );
await expect( editor.canvas.getByLabel( 'Add block' ) ).toBeFocused();

// Make sure it's a nav block appender. If it is, the linkControl search input will be opened on enter.
await page.keyboard.press( 'Enter' );

// Expect to see the Link creation UI be focused.
const linkUIInput = linkControl.getSearchInput();
await expect( linkUIInput ).toBeFocused();
Copy link
Contributor

Choose a reason for hiding this comment

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

As this behaviour might change in future, would it be possible to scope the selector for the Add block to be relative to the Navigaiton block.

So in pseudo code

editor.canvas.getNavBlock().getByLabel('Add block')

} );
} );

class LinkControl {
Expand Down