Skip to content

Commit 08f14e9

Browse files
jeryjdraganescugetdave
authored
Do not focus new navigation block menu until loading is finished (WordPress#59801)
* Do not focus navigation block until loading is finished When a new navigation is created, we place focus on the navigation block. If we place focus while loading is still happening, focus will be lost when focus the loading is finished and the block gets replaced with the new content. * Nest loading state inside of navigation ref Instead of replacing the navigation block after loading, only replace the inside part of it so that focus is not lost * Add coverage for focus loss on menu creation * select the navigatiom after import in an effect similar to the method used when creating a menu, this way the select block call is later and the focus is maintained * remove extraneous dependency causing err on delete newly created menus * Update test to not determine link control behavior * No need to return await inside an async function as it will always return a Promise Co-authored-by: Dave Smith <[email protected]> --------- Co-authored-by: Andrei Draganescu <[email protected]> Co-authored-by: Dave Smith <[email protected]>
1 parent 87d3d4f commit 08f14e9

File tree

2 files changed

+90
-50
lines changed

2 files changed

+90
-50
lines changed

packages/block-library/src/navigation/edit/index.js

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,7 @@ function Navigation( {
342342
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();
343343

344344
const onSelectClassicMenu = async ( classicMenu ) => {
345-
const navMenu = await convertClassicMenu(
346-
classicMenu.id,
347-
classicMenu.name,
348-
'draft'
349-
);
350-
if ( navMenu ) {
351-
handleUpdateMenu( navMenu.id, {
352-
focusNavigationBlock: true,
353-
} );
354-
}
345+
return convertClassicMenu( classicMenu.id, classicMenu.name, 'draft' );
355346
};
356347

357348
const onSelectNavigationMenu = ( menuId ) => {
@@ -402,6 +393,9 @@ function Navigation( {
402393
showClassicMenuConversionNotice(
403394
__( 'Classic menu imported successfully.' )
404395
);
396+
handleUpdateMenu( createNavigationMenuPost?.id, {
397+
focusNavigationBlock: true,
398+
} );
405399
}
406400

407401
if ( classicMenuConversionStatus === CLASSIC_MENU_CONVERSION_ERROR ) {
@@ -414,6 +408,8 @@ function Navigation( {
414408
classicMenuConversionError,
415409
hideClassicMenuConversionNotice,
416410
showClassicMenuConversionNotice,
411+
createNavigationMenuPost?.id,
412+
handleUpdateMenu,
417413
] );
418414

419415
useEffect( () => {
@@ -866,50 +862,52 @@ function Navigation( {
866862
</InspectorControls>
867863
) }
868864

869-
{ isLoading && (
870-
<TagName { ...blockProps }>
865+
<TagName
866+
{ ...blockProps }
867+
aria-describedby={
868+
! isPlaceholder && ! isLoading
869+
? accessibleDescriptionId
870+
: undefined
871+
}
872+
>
873+
{ isLoading && (
871874
<div className="wp-block-navigation__loading-indicator-container">
872875
<Spinner className="wp-block-navigation__loading-indicator" />
873876
</div>
874-
</TagName>
875-
) }
877+
) }
876878

877-
{ ! isLoading && (
878-
<TagName
879-
{ ...blockProps }
880-
aria-describedby={
881-
! isPlaceholder
882-
? accessibleDescriptionId
883-
: undefined
884-
}
885-
>
886-
<AccessibleMenuDescription
887-
id={ accessibleDescriptionId }
888-
/>
889-
<ResponsiveWrapper
890-
id={ clientId }
891-
onToggle={ setResponsiveMenuVisibility }
892-
hasIcon={ hasIcon }
893-
icon={ icon }
894-
isOpen={ isResponsiveMenuOpen }
895-
isResponsive={ isResponsive }
896-
isHiddenByDefault={ 'always' === overlayMenu }
897-
overlayBackgroundColor={ overlayBackgroundColor }
898-
overlayTextColor={ overlayTextColor }
899-
>
900-
{ isEntityAvailable && (
901-
<NavigationInnerBlocks
902-
clientId={ clientId }
903-
hasCustomPlaceholder={
904-
!! CustomPlaceholder
905-
}
906-
templateLock={ templateLock }
907-
orientation={ orientation }
908-
/>
909-
) }
910-
</ResponsiveWrapper>
911-
</TagName>
912-
) }
879+
{ ! isLoading && (
880+
<>
881+
<AccessibleMenuDescription
882+
id={ accessibleDescriptionId }
883+
/>
884+
<ResponsiveWrapper
885+
id={ clientId }
886+
onToggle={ setResponsiveMenuVisibility }
887+
hasIcon={ hasIcon }
888+
icon={ icon }
889+
isOpen={ isResponsiveMenuOpen }
890+
isResponsive={ isResponsive }
891+
isHiddenByDefault={ 'always' === overlayMenu }
892+
overlayBackgroundColor={
893+
overlayBackgroundColor
894+
}
895+
overlayTextColor={ overlayTextColor }
896+
>
897+
{ isEntityAvailable && (
898+
<NavigationInnerBlocks
899+
clientId={ clientId }
900+
hasCustomPlaceholder={
901+
!! CustomPlaceholder
902+
}
903+
templateLock={ templateLock }
904+
orientation={ orientation }
905+
/>
906+
) }
907+
</ResponsiveWrapper>
908+
</>
909+
) }
910+
</TagName>
913911
</RecursionProvider>
914912
</EntityProvider>
915913
);

test/e2e/specs/editor/blocks/navigation-list-view.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,48 @@ test.describe( 'Navigation block - List view editing', () => {
543543
// we have unmounted the list view and then remounted it).
544544
await expect( linkControl.getSearchInput() ).toBeHidden();
545545
} );
546+
547+
test( `can create a new menu without losing focus`, async ( {
548+
page,
549+
editor,
550+
requestUtils,
551+
} ) => {
552+
await requestUtils.createNavigationMenu( navMenuBlocksFixture );
553+
554+
await editor.insertBlock( { name: 'core/navigation' } );
555+
556+
await editor.openDocumentSettingsSidebar();
557+
558+
await page.getByLabel( 'Test Menu' ).click();
559+
560+
await page.keyboard.press( 'ArrowUp' );
561+
562+
await expect(
563+
page.getByRole( 'menuitem', { name: 'Create new menu' } )
564+
).toBeFocused();
565+
566+
await page.keyboard.press( 'Enter' );
567+
568+
// Check that the menu was created
569+
await expect(
570+
page
571+
.getByTestId( 'snackbar' )
572+
.getByText( 'Navigation Menu successfully created.' )
573+
).toBeVisible();
574+
await expect(
575+
page.getByText( 'This navigation menu is empty.' )
576+
).toBeVisible();
577+
578+
// Move focus to the appender
579+
await page.keyboard.press( 'ArrowDown' );
580+
await expect(
581+
editor.canvas
582+
.getByRole( 'document', {
583+
name: 'Block: Navigation',
584+
} )
585+
.getByLabel( 'Add block' )
586+
).toBeFocused();
587+
} );
546588
} );
547589

548590
class LinkControl {

0 commit comments

Comments
 (0)