-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Navigation: Select dropdown encapsulation and further consolidation. #38627
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3f9ee9
Encapsulate permissions checks
getdave acdb553
Utilise primary component inside placeholder
getdave 11f6d58
Inline existing menus options
getdave 9cf6f34
Account for create vs switch in aria label depending on context
getdave c5c3377
Remove explicit truthy in prop
getdave eda20d5
Rename vars to include "Menu"
getdave d2975e8
Improve prop naming for clarity
getdave 7e2c158
Improve conditionals around display of management options
getdave a086afe
Wait for the custom link block
getdave 7edc097
Wait for the menuItems response
getdave bdb24ad
Improve conditional
getdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 0 additions & 70 deletions
70
packages/block-library/src/navigation/edit/existing-menus-options.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
* WordPress dependencies | ||
*/ | ||
import { MenuGroup, MenuItem } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
/** | ||
|
@@ -13,28 +13,35 @@ 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, | ||
showManageActions = false, | ||
actionLabel, | ||
} ) { | ||
/* translators: %s: The name of a menu. */ | ||
const createActionLabel = __( "Create from '%s'" ); | ||
|
||
actionLabel = actionLabel || createActionLabel; | ||
|
||
const { menus: classicMenus } = useNavigationEntities(); | ||
|
||
const { | ||
menus: classicMenus, | ||
hasMenus: hasClassicMenus, | ||
} = useNavigationEntities(); | ||
const { navigationMenus } = useNavigationMenu(); | ||
navigationMenus, | ||
canUserCreateNavigation: canUserCreateNavigationMenu, | ||
canUserUpdateNavigationEntity: canUserUpdateNavigationMenu, | ||
canSwitchNavigationMenu, | ||
} = useNavigationMenu(); | ||
|
||
const createNavigationMenu = useCreateNavigationMenu( clientId ); | ||
|
||
const onFinishMenuCreation = async ( | ||
blocks, | ||
navigationMenuTitle = null | ||
) => { | ||
if ( ! canUserCreateNavigation ) { | ||
if ( ! canUserCreateNavigationMenu ) { | ||
return; | ||
} | ||
|
||
|
@@ -49,43 +56,81 @@ export default function NavigationMenuSelector( { | |
onFinishMenuCreation | ||
); | ||
|
||
const hasNavigationMenus = !! navigationMenus?.length; | ||
const hasClassicMenus = !! classicMenus?.length; | ||
const showNavigationMenus = !! canSwitchNavigationMenu; | ||
const showClassicMenus = !! canUserCreateNavigationMenu; | ||
const showSelectMenus = | ||
( canUserSwitchNavigation || canUserCreateNavigation ) && | ||
( navigationMenus?.length || hasClassicMenus ); | ||
( canSwitchNavigationMenu || canUserCreateNavigationMenu ) && | ||
( hasNavigationMenus || hasClassicMenus ); | ||
|
||
if ( ! showSelectMenus ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<ExistingMenusOptions | ||
showNavigationMenus={ canUserSwitchNavigation } | ||
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 && ( | ||
<MenuGroup label={ __( 'Tools' ) }> | ||
<MenuItem onClick={ onCreateNew }> | ||
{ __( 'Create new menu' ) } | ||
</MenuItem> | ||
<MenuItem | ||
href={ addQueryArgs( 'edit.php', { | ||
post_type: 'wp_navigation', | ||
} ) } | ||
> | ||
{ __( 'Manage menus' ) } | ||
</MenuItem> | ||
{ showNavigationMenus && hasNavigationMenus && ( | ||
<MenuGroup label={ __( 'Menus' ) }> | ||
{ navigationMenus.map( ( menu ) => { | ||
const label = decodeEntities( menu.title.rendered ); | ||
return ( | ||
<MenuItem | ||
onClick={ () => { | ||
onSelect( 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={ () => { | ||
convertClassicMenuToBlocks( | ||
menu.id, | ||
menu.name | ||
); | ||
} } | ||
key={ menu.id } | ||
aria-label={ sprintf( | ||
createActionLabel, | ||
label | ||
) } | ||
> | ||
{ label } | ||
</MenuItem> | ||
); | ||
} ) } | ||
</MenuGroup> | ||
) } | ||
|
||
{ showManageActions && | ||
( canUserCreateNavigationMenu || | ||
canUserUpdateNavigationMenu ) && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's quite a condition :D how about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
<MenuGroup label={ __( 'Tools' ) }> | ||
{ canUserCreateNavigationMenu && ( | ||
<MenuItem onClick={ onCreateNew }> | ||
{ __( 'Create new menu' ) } | ||
</MenuItem> | ||
) } | ||
<MenuItem | ||
href={ addQueryArgs( 'edit.php', { | ||
post_type: 'wp_navigation', | ||
} ) } | ||
> | ||
{ __( 'Manage menus' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
) } | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the hook returned the final names already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't...yet but yes this will require a fix when my other PR lands.