-
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: browse mode list all Navigation Menus. #50840
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ef90205
List Menus or Show Single Menu
getdave 3ca13d1
Ability to direct to Single or List
getdave a55aaa9
Use routing to navigation directly to single nav menu
getdave 57831d5
Fix routing
getdave 252ac8f
Add correct description as per design
getdave 31fdeef
Utilise existing component for loading and missing messages
getdave 78e6317
Add icons to matching design of listing
getdave 84c4e94
Fix path for single Nav menu
getdave c27e592
Replace loading text with spinner
getdave 00a5bf0
Ensure menus query is preloaded
getdave d529914
Centralise menus query to ensure consistency in preloading
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
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
141 changes: 141 additions & 0 deletions
141
packages/edit-site/src/components/sidebar-navigation-screen-navigation-menu/index.js
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityRecord } from '@wordpress/core-data'; | ||
import { __experimentalUseNavigator as useNavigator } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useCallback, useMemo } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
import { BlockEditorProvider } from '@wordpress/block-editor'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../private-apis'; | ||
import { store as editSiteStore } from '../../store'; | ||
import { | ||
isPreviewingTheme, | ||
currentlyPreviewingTheme, | ||
} from '../../utils/is-previewing-theme'; | ||
import { SidebarNavigationScreenWrapper } from '../sidebar-navigation-screen-navigation-menus'; | ||
import NavigationMenuContent from '../sidebar-navigation-screen-navigation-menus/navigation-menu-content'; | ||
|
||
const { useHistory } = unlock( routerPrivateApis ); | ||
const noop = () => {}; | ||
|
||
export default function SidebarNavigationScreenNavigationMenu() { | ||
const postType = `wp_navigation`; | ||
const { | ||
params: { postId }, | ||
} = useNavigator(); | ||
|
||
const { record: navigationMenu, isResolving: isLoading } = useEntityRecord( | ||
'postType', | ||
postType, | ||
postId | ||
); | ||
|
||
const menuTitle = navigationMenu?.title?.rendered || navigationMenu?.slug; | ||
|
||
if ( isLoading ) { | ||
return ( | ||
<SidebarNavigationScreenWrapper | ||
description={ __( 'Loading Navigation Menu.' ) } | ||
/> | ||
); | ||
} | ||
|
||
if ( ! isLoading && ! navigationMenu ) { | ||
return ( | ||
<SidebarNavigationScreenWrapper | ||
description={ __( 'Navigation Menu missing.' ) } | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<SidebarNavigationScreenWrapper | ||
title={ decodeEntities( menuTitle ) } | ||
description={ __( | ||
'Navigation menus are a curated collection of blocks that allow visitors to get around your site.' | ||
) } | ||
> | ||
<NavigationMenuEditor navigationMenu={ navigationMenu } /> | ||
</SidebarNavigationScreenWrapper> | ||
); | ||
} | ||
|
||
function NavigationMenuEditor( { navigationMenu } ) { | ||
const history = useHistory(); | ||
|
||
const onSelect = useCallback( | ||
( selectedBlock ) => { | ||
const { attributes, name } = selectedBlock; | ||
if ( | ||
attributes.kind === 'post-type' && | ||
attributes.id && | ||
attributes.type && | ||
history | ||
) { | ||
history.push( { | ||
postType: attributes.type, | ||
postId: attributes.id, | ||
...( isPreviewingTheme() && { | ||
theme_preview: currentlyPreviewingTheme(), | ||
} ), | ||
} ); | ||
} | ||
if ( name === 'core/page-list-item' && attributes.id && history ) { | ||
history.push( { | ||
postType: 'page', | ||
postId: attributes.id, | ||
...( isPreviewingTheme() && { | ||
theme_preview: currentlyPreviewingTheme(), | ||
} ), | ||
} ); | ||
} | ||
}, | ||
[ history ] | ||
); | ||
|
||
const { storedSettings } = useSelect( ( select ) => { | ||
const { getSettings } = unlock( select( editSiteStore ) ); | ||
|
||
return { | ||
storedSettings: getSettings( false ), | ||
}; | ||
}, [] ); | ||
|
||
const blocks = useMemo( () => { | ||
if ( ! NavigationMenuEditor ) { | ||
return []; | ||
} | ||
|
||
return [ | ||
createBlock( 'core/navigation', { ref: navigationMenu?.id } ), | ||
]; | ||
}, [ navigationMenu ] ); | ||
|
||
if ( ! navigationMenu || ! blocks?.length ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockEditorProvider | ||
settings={ storedSettings } | ||
value={ blocks } | ||
onChange={ noop } | ||
onInput={ noop } | ||
> | ||
<div className="edit-site-sidebar-navigation-screen-navigation-menus__content"> | ||
<NavigationMenuContent | ||
rootClientId={ blocks[ 0 ].clientId } | ||
onSelect={ onSelect } | ||
/> | ||
</div> | ||
</BlockEditorProvider> | ||
); | ||
} |
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
Oops, something went wrong.
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 do you think about using slug instead of ID? Is that possible?
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.
Possibly. In this PR though?
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.
Happy for it to be a followup, but remember once people start using these permalinks we have to support them so it would be good to look into it before the next Gutenberg release.
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.
I'm going to look at the routing again and see what we have available.
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.
Thinking about this more, since pages already use IDs, I think it's fine for navigations to do the same