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

Consolidate select menus dropdown in Nav block code #38179

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* WordPress dependencies
*/
import { MenuGroup, MenuItem } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';

const ExistingMenusOptions = ( {
showNavigationMenus,
showClassicMenus = false,
navigationMenus,
classicMenus,
onSelectNavigationMenu,
onSelectClassicMenu,
actionLabel,
} ) => {
const hasNavigationMenus = !! navigationMenus?.length;
const hasClassicMenus = !! classicMenus?.length;

/* translators: %s: The name of a menu. */
const createActionLabel = __( "Create from '%s'" );

actionLabel = actionLabel || createActionLabel;

return (
<>
{ showNavigationMenus && hasNavigationMenus && (
<MenuGroup label={ __( 'Menus' ) }>
{ navigationMenus.map( ( menu ) => {
const label = decodeEntities( menu.title.rendered );
return (
<MenuItem
onClick={ () => {
onSelectNavigationMenu( menu );
} }
key={ menu.id }
aria-label={ sprintf( actionLabel, label ) }
>
{ label }
</MenuItem>
);
} ) }
</MenuGroup>
) }
{ showClassicMenus && hasClassicMenus && (
<MenuGroup label={ __( 'Classic Menus' ) }>
{ classicMenus.map( ( menu ) => {
const label = decodeEntities( menu.name );
return (
<MenuItem
onClick={ () => {
onSelectClassicMenu( menu );
} }
key={ menu.id }
aria-label={ sprintf(
createActionLabel,
label
) }
>
{ label }
</MenuItem>
);
} ) }
</MenuGroup>
) }
</>
);
};

export default ExistingMenusOptions;
3 changes: 3 additions & 0 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ function Navigation( {
canUserCreateNavigation={
canUserCreateNavigation
}
canUserSwitchNavigation={
canSwitchNavigationMenu
}
/>
) }
</ToolbarDropdownMenu>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* WordPress dependencies
*/
import { MenuGroup, MenuItem, MenuItemsChoice } from '@wordpress/components';
import { useEntityId } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

import { addQueryArgs } from '@wordpress/url';

/**
Expand All @@ -14,19 +13,20 @@ import useNavigationMenu from '../use-navigation-menu';
import useNavigationEntities from '../use-navigation-entities';
import useConvertClassicMenu from '../use-convert-classic-menu';
import useCreateNavigationMenu from './use-create-navigation-menu';
import ExistingMenusOptions from './existing-menus-options';

export default function NavigationMenuSelector( {
clientId,
onSelect,
onCreateNew,
canUserCreateNavigation = false,
canUserSwitchNavigation = false,
} ) {
const {
menus: classicMenus,
hasMenus: hasClassicMenus,
} = useNavigationEntities();
const { navigationMenus } = useNavigationMenu();
const ref = useEntityId( 'postType', 'wp_navigation' );

const createNavigationMenu = useCreateNavigationMenu( clientId );

Expand All @@ -49,66 +49,42 @@ export default function NavigationMenuSelector( {
onFinishMenuCreation
);

const showSelectMenus =
( canUserSwitchNavigation || canUserCreateNavigation ) &&
( navigationMenus?.length || hasClassicMenus );

if ( ! showSelectMenus ) {
return null;
}

return (
<>
<MenuGroup label={ __( 'Menus' ) }>
<MenuItemsChoice
value={ ref }
onSelect={ ( selectedId ) =>
onSelect(
navigationMenus.find(
( post ) => post.id === selectedId
)
)
}
choices={ navigationMenus.map( ( { id, title } ) => {
const label = decodeEntities( title.rendered );
return {
value: id,
label,
'aria-label': sprintf(
/* translators: %s: The name of a menu. */
__( "Switch to '%s'" ),
label
),
};
} ) }
/>
</MenuGroup>
<ExistingMenusOptions
showNavigationMenus={ canUserSwitchNavigation }
getdave marked this conversation as resolved.
Show resolved Hide resolved
showClassicMenus={ canUserCreateNavigation }
navigationMenus={ navigationMenus }
classicMenus={ classicMenus }
onSelectNavigationMenu={ onSelect }
onSelectClassicMenu={ ( { id, name } ) =>
convertClassicMenuToBlocks( id, name )
}
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>

{ canUserCreateNavigation && (
<>
{ hasClassicMenus && (
<MenuGroup label={ __( 'Classic Menus' ) }>
{ classicMenus.map( ( menu ) => {
return (
<MenuItem
onClick={ () => {
convertClassicMenuToBlocks(
menu.id,
menu.name
);
} }
key={ menu.id }
>
{ decodeEntities( menu.name ) }
</MenuItem>
);
} ) }
</MenuGroup>
) }
<MenuGroup label={ __( 'Tools' ) }>
<MenuItem onClick={ onCreateNew }>
{ __( 'Create new menu' ) }
</MenuItem>
<MenuItem
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</MenuItem>
</MenuGroup>
</>
<MenuGroup label={ __( 'Tools' ) }>
<MenuItem onClick={ onCreateNew }>
{ __( 'Create new menu' ) }
</MenuItem>
<MenuItem
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</MenuItem>
</MenuGroup>
) }
</>
);
Expand Down
132 changes: 41 additions & 91 deletions packages/block-library/src/navigation/edit/placeholder/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
/**
* WordPress dependencies
*/
import {
Placeholder,
Button,
DropdownMenu,
MenuGroup,
MenuItem,
} from '@wordpress/components';
import { Placeholder, Button, DropdownMenu } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { navigation, Icon } from '@wordpress/icons';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
Expand All @@ -21,77 +14,7 @@ import PlaceholderPreview from './placeholder-preview';
import useNavigationMenu from '../../use-navigation-menu';
import useCreateNavigationMenu from '../use-create-navigation-menu';
import useConvertClassicMenu from '../../use-convert-classic-menu';

const ExistingMenusDropdown = ( {
showNavigationMenus,
navigationMenus,
onFinish,
menus,
onCreateFromMenu,
showClassicMenus = false,
} ) => {
const toggleProps = {
variant: 'tertiary',
iconPosition: 'right',
className: 'wp-block-navigation-placeholder__actions__dropdown',
};

const hasNavigationMenus = !! navigationMenus?.length;
const hasClassicMenus = !! menus?.length;

return (
<DropdownMenu
text={ __( 'Select menu' ) }
icon={ null }
toggleProps={ toggleProps }
popoverProps={ { isAlternate: true } }
>
{ ( { onClose } ) => (
<>
{ showNavigationMenus && hasNavigationMenus && (
<MenuGroup label={ __( 'Menus' ) }>
{ navigationMenus.map( ( menu ) => {
return (
<MenuItem
onClick={ () => {
onFinish( menu );
} }
onClose={ onClose }
key={ menu.id }
>
{ decodeEntities(
menu.title.rendered
) }
</MenuItem>
);
} ) }
</MenuGroup>
) }
{ showClassicMenus && hasClassicMenus && (
<MenuGroup label={ __( 'Classic Menus' ) }>
{ menus.map( ( menu ) => {
return (
<MenuItem
onClick={ () => {
onCreateFromMenu(
menu.id,
menu.name
);
} }
onClose={ onClose }
key={ menu.id }
>
{ decodeEntities( menu.name ) }
</MenuItem>
);
} ) }
</MenuGroup>
) }
</>
) }
</DropdownMenu>
);
};
import ExistingMenusOptions from '../existing-menus-options';

export default function NavigationPlaceholder( {
clientId,
Expand Down Expand Up @@ -159,21 +82,48 @@ export default function NavigationPlaceholder( {

{ showSelectMenus ? (
<>
<ExistingMenusDropdown
showNavigationMenus={
canSwitchNavigationMenu
}
navigationMenus={ navigationMenus }
onFinish={ onFinish }
menus={ menus }
onCreateFromMenu={ convertClassicMenu }
showClassicMenus={
canUserCreateNavigation
}
/>
<DropdownMenu
text={ __( 'Select menu' ) }
icon={ null }
toggleProps={ {
variant: 'tertiary',
iconPosition: 'right',
className:
'wp-block-navigation-placeholder__actions__dropdown',
} }
popoverProps={ { isAlternate: true } }
>
{ () => (
<ExistingMenusOptions
showNavigationMenus={
canSwitchNavigationMenu
}
navigationMenus={
navigationMenus
}
onSelectNavigationMenu={
onFinish
}
classicMenus={ menus }
onSelectClassicMenu={ ( {
id,
name,
} ) =>
convertClassicMenu(
id,
name
)
}
showClassicMenus={
canUserCreateNavigation
}
/>
) }
</DropdownMenu>
<hr />
</>
) : undefined }

{ canUserCreateNavigation && (
<Button
variant="tertiary"
Expand Down