Skip to content

Commit

Permalink
Implements handling focus persistence in the Navigation Selector acco…
Browse files Browse the repository at this point in the history
…rding to #42956

Switching between menus keeps the menu open and focused on the current selection.
Importing classic menus or creating new ones focuses the current navigation block.

Co-authored-by: Daniel Richards <[email protected]>
  • Loading branch information
draganescu and talldan committed Aug 29, 2022
1 parent 4be32ea commit 0ede795
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 66 deletions.
67 changes: 18 additions & 49 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ function Navigation( {
isError: createNavigationMenuIsError,
} = useCreateNavigationMenu( clientId );

const createUntitledEmptyNavigationMenu = () => {
createNavigationMenu( '' );
};

useEffect( () => {
hideNavigationMenuStatusNotice();

Expand All @@ -141,8 +145,7 @@ function Navigation( {
}

if ( createNavigationMenuIsSuccess ) {
setRef( createNavigationMenuPost.id );
selectBlock( clientId );
handleUpdateMenu( createNavigationMenuPost.id, true );

showNavigationMenuStatusNotice(
__( `Navigation Menu successfully created.` )
Expand All @@ -155,7 +158,6 @@ function Navigation( {
);
}
}, [
createNavigationMenu,
createNavigationMenuStatus,
createNavigationMenuError,
createNavigationMenuPost,
Expand Down Expand Up @@ -191,7 +193,6 @@ function Navigation( {
isNavigationMenuResolved,
isNavigationMenuMissing,
navigationMenus,
navigationMenu,
canUserUpdateNavigationMenu,
hasResolvedCanUserUpdateNavigationMenu,
canUserDeleteNavigationMenu,
Expand Down Expand Up @@ -240,8 +241,6 @@ function Navigation( {

const navRef = useRef();

const isDraftNavigationMenu = navigationMenu?.status === 'draft';

const {
convert: convertClassicMenu,
status: classicMenuConversionStatus,
Expand Down Expand Up @@ -331,9 +330,11 @@ function Navigation( {
] = useState();
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();

const handleUpdateMenu = ( menuId ) => {
const handleUpdateMenu = ( menuId, focusNavigationBlock = false ) => {
setRef( menuId );
selectBlock( clientId );
if ( focusNavigationBlock ) {
selectBlock( clientId );
}
};

useEffect( () => {
Expand Down Expand Up @@ -428,27 +429,6 @@ function Navigation( {

const hasManagePermissions =
canUserCreateNavigationMenu || canUserUpdateNavigationMenu;
const navigationSelectorRef = useRef();
const [ shouldFocusNavigationSelector, setShouldFocusNavigationSelector ] =
useState( false );

// Focus support after menu selection.
useEffect( () => {
if (
isDraftNavigationMenu ||
! isEntityAvailable ||
! shouldFocusNavigationSelector
) {
return;
}
navigationSelectorRef?.current?.focus();
setShouldFocusNavigationSelector( false );
}, [
isDraftNavigationMenu,
isEntityAvailable,
shouldFocusNavigationSelector,
] );

const isResponsive = 'never' !== overlayMenu;

const overlayMenuPreviewClasses = classnames(
Expand Down Expand Up @@ -610,24 +590,21 @@ function Navigation( {
<InspectorControls>
<PanelBody title={ __( 'Menu' ) }>
<NavigationMenuSelector
ref={ navigationSelectorRef }
currentMenuId={ ref }
clientId={ clientId }
onSelectNavigationMenu={ ( menuId ) => {
handleUpdateMenu( menuId );
setShouldFocusNavigationSelector( true );
} }
onSelectClassicMenu={ async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id );
setShouldFocusNavigationSelector( true );
handleUpdateMenu( navMenu.id, true );
}
} }
onCreateNew={ () => createNavigationMenu( '', [] ) }
onCreateNew={ createUntitledEmptyNavigationMenu }
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
Expand Down Expand Up @@ -685,24 +662,21 @@ function Navigation( {
<InspectorControls>
<PanelBody title={ __( 'Menu' ) }>
<NavigationMenuSelector
ref={ navigationSelectorRef }
currentMenuId={ null }
clientId={ clientId }
onSelectNavigationMenu={ ( menuId ) => {
handleUpdateMenu( menuId );
setShouldFocusNavigationSelector( true );
} }
onSelectClassicMenu={ async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id );
setShouldFocusNavigationSelector( true );
handleUpdateMenu( navMenu.id, true );
}
} }
onCreateNew={ () => createNavigationMenu( '', [] ) }
onCreateNew={ createUntitledEmptyNavigationMenu }
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
Expand All @@ -723,7 +697,7 @@ function Navigation( {
'Navigation menu has been deleted or is unavailable. '
) }
<Button
onClick={ () => createNavigationMenu( '', [] ) }
onClick={ createUntitledEmptyNavigationMenu }
variant="link"
>
{ __( 'Create a new menu?' ) }
Expand Down Expand Up @@ -770,19 +744,17 @@ function Navigation( {
}
onSelectNavigationMenu={ ( menuId ) => {
handleUpdateMenu( menuId );
setShouldFocusNavigationSelector( true );
} }
onSelectClassicMenu={ async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id );
setShouldFocusNavigationSelector( true );
handleUpdateMenu( navMenu.id, true );
}
} }
onCreateEmpty={ () => createNavigationMenu( '', [] ) }
onCreateEmpty={ createUntitledEmptyNavigationMenu }
/>
</TagName>
);
Expand All @@ -794,24 +766,21 @@ function Navigation( {
<InspectorControls>
<PanelBody title={ __( 'Menu' ) }>
<NavigationMenuSelector
ref={ navigationSelectorRef }
currentMenuId={ ref }
clientId={ clientId }
onSelectNavigationMenu={ ( menuId ) => {
handleUpdateMenu( menuId );
setShouldFocusNavigationSelector( true );
} }
onSelectClassicMenu={ async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id );
setShouldFocusNavigationSelector( true );
handleUpdateMenu( navMenu.id, true );
}
} }
onCreateNew={ () => createNavigationMenu( '', [] ) }
onCreateNew={ createUntitledEmptyNavigationMenu }
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import {
import { Icon, chevronUp, chevronDown } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { forwardRef, useEffect, useMemo, useState } from '@wordpress/element';
import { useEffect, useMemo, useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import useNavigationMenu from '../use-navigation-menu';
import useNavigationEntities from '../use-navigation-entities';

function NavigationMenuSelector(
{
currentMenuId,
onSelectNavigationMenu,
onSelectClassicMenu,
onCreateNew,
actionLabel,
toggleProps = {},
},
forwardedRef
) {
function NavigationMenuSelector( {
currentMenuId,
onSelectNavigationMenu,
onSelectClassicMenu,
onCreateNew,
actionLabel,
toggleProps = {},
} ) {
/* translators: %s: The name of a menu. */
const createActionLabel = __( "Create from '%s'" );

Expand Down Expand Up @@ -106,20 +103,18 @@ function NavigationMenuSelector(
return (
<DropdownMenu
className="wp-block-navigation__navigation-selector"
ref={ forwardedRef }
label={ selectorLabel }
text={ selectorLabel }
icon={ null }
toggleProps={ toggleProps }
>
{ ( { onClose } ) => (
{ () => (
<>
{ showNavigationMenus && hasNavigationMenus && (
<MenuGroup label={ __( 'Menus' ) }>
<MenuItemsChoice
value={ currentMenuId }
onSelect={ ( menuId ) => {
onClose();
onSelectNavigationMenu( menuId );
} }
choices={ menuChoices }
Expand All @@ -133,7 +128,6 @@ function NavigationMenuSelector(
return (
<MenuItem
onClick={ () => {
onClose();
onSelectClassicMenu( menu );
} }
key={ menu.id }
Expand Down Expand Up @@ -164,4 +158,4 @@ function NavigationMenuSelector(
);
}

export default forwardRef( NavigationMenuSelector );
export default NavigationMenuSelector;

0 comments on commit 0ede795

Please sign in to comment.