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
98 changes: 48 additions & 50 deletions packages/block-library/src/navigation/edit/index.js
Expand Up @@ -342,16 +342,7 @@ function Navigation( {
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();

const onSelectClassicMenu = async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name,
'draft'
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id, {
focusNavigationBlock: true,
} );
}
return convertClassicMenu( classicMenu.id, classicMenu.name, 'draft' );
};

const onSelectNavigationMenu = ( menuId ) => {
Expand Down Expand Up @@ -402,6 +393,9 @@ function Navigation( {
showClassicMenuConversionNotice(
__( 'Classic menu imported successfully.' )
);
handleUpdateMenu( createNavigationMenuPost?.id, {
focusNavigationBlock: true,
} );
}

if ( classicMenuConversionStatus === CLASSIC_MENU_CONVERSION_ERROR ) {
Expand All @@ -414,6 +408,8 @@ function Navigation( {
classicMenuConversionError,
hideClassicMenuConversionNotice,
showClassicMenuConversionNotice,
createNavigationMenuPost?.id,
handleUpdateMenu,
] );

useEffect( () => {
Expand Down Expand Up @@ -866,50 +862,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
42 changes: 42 additions & 0 deletions test/e2e/specs/editor/blocks/navigation-list-view.spec.js
Expand Up @@ -543,6 +543,48 @@ 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,
} ) => {
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
.getByRole( 'document', {
name: 'Block: Navigation',
} )
.getByLabel( 'Add block' )
).toBeFocused();
} );
} );

class LinkControl {
Expand Down