Skip to content

Commit

Permalink
Update: Hide page list on single page list menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Mar 28, 2023
1 parent 67d0516 commit c6f61bf
Showing 1 changed file with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand All @@ -10,24 +15,59 @@ import {
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { useCallback } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { unlock } from '../../private-apis';
import { NavigationMenuLoader } from './loader';

// Needs to be kept in sync with the query used at packages/block-library/src/page-list/edit.js.
const MAX_PAGE_COUNT = 100;
const PAGES_QUERY = [
'postType',
'page',
{
per_page: MAX_PAGE_COUNT,
_fields: [ 'id', 'link', 'menu_order', 'parent', 'title', 'type' ],
// TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby
// values is resolved, update 'orderby' to [ 'menu_order', 'post_title' ] to provide a consistent
// sort.
orderby: 'menu_order',
order: 'asc',
},
];

export default function NavigationMenuContent( { rootClientId, onSelect } ) {
const { clientIdsTree, isLoading } = useSelect(
const { clientIdsTree, isLoading, isSinglePageList } = useSelect(
( select ) => {
const { __unstableGetClientIdsTree, areInnerBlocksControlled } =
select( blockEditorStore );
return {
clientIdsTree: __unstableGetClientIdsTree( rootClientId ),
const {
__unstableGetClientIdsTree,
areInnerBlocksControlled,
getBlockName,
} = select( blockEditorStore );
const { isResolving } = select( coreStore );

const _clientIdsTree = __unstableGetClientIdsTree( rootClientId );
const hasOnlyPageListBlock =
_clientIdsTree.length === 1 &&
getBlockName( _clientIdsTree[ 0 ].clientId ) ===
'core/page-list';
const isLoadingPages =
hasOnlyPageListBlock &&
isResolving( 'getEntityRecords', PAGES_QUERY );
return {
clientIdsTree: _clientIdsTree,
// This is a small hack to wait for the navigation block
// to actually load its inner blocks.
isLoading: ! areInnerBlocksControlled( rootClientId ),
isLoading:
! areInnerBlocksControlled( rootClientId ) ||
isLoadingPages,
isSinglePageList:
hasOnlyPageListBlock &&
! isLoadingPages &&
_clientIdsTree[ 0 ].innerBlocks.length > 0,
};
},
[ rootClientId ]
Expand Down Expand Up @@ -62,7 +102,11 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
{ isLoading && <NavigationMenuLoader /> }
{ ! isLoading && (
<OffCanvasEditor
blocks={ clientIdsTree }
blocks={
isSinglePageList
? clientIdsTree[ 0 ].innerBlocks
: clientIdsTree
}
onSelect={ offCanvasOnselect }
LeafMoreMenu={ LeafMoreMenu }
showAppender={ false }
Expand Down

0 comments on commit c6f61bf

Please sign in to comment.