Skip to content

Commit

Permalink
Inserter: Make ChildBlocks have a similar API to InserterPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jun 18, 2020
1 parent ea1c443 commit 436d7c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
17 changes: 9 additions & 8 deletions packages/block-editor/src/components/inserter/block-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ export function InserterBlockList( {
return (
<div>
{ hasChildItems && (
<ChildBlocks
rootClientId={ rootClientId }
// Pass along every block, as getInserterItems() will have
// already filtered out non-child blocks.
items={ filteredItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
<ChildBlocks rootClientId={ rootClientId }>
<BlockTypesList
// Pass along every block, as getInserterItems() will have
// already filtered out non-child blocks.
items={ filteredItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
</ChildBlocks>
) }

{ ! hasChildItems && !! suggestedItems.length && ! filterValue && (
Expand Down
33 changes: 14 additions & 19 deletions packages/block-editor/src/components/inserter/child-blocks.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { ifCondition, compose } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import BlockTypesList from '../block-types-list';
import BlockIcon from '../block-icon';

function ChildBlocks( { rootBlockIcon, rootBlockTitle, items, ...props } ) {
export default function ChildBlocks( { rootClientId, children } ) {
const { rootBlockTitle, rootBlockIcon } = useSelect( ( select ) => {
const { getBlockType } = select( 'core/blocks' );
const { getBlockName } = select( 'core/block-editor' );
const rootBlockName = getBlockName( rootClientId );
const rootBlockType = getBlockType( rootBlockName );
return {
rootBlockTitle: rootBlockType && rootBlockType.title,
rootBlockIcon: rootBlockType && rootBlockType.icon,
};
} );

return (
<div className="block-editor-inserter__child-blocks">
{ ( rootBlockIcon || rootBlockTitle ) && (
Expand All @@ -19,21 +28,7 @@ function ChildBlocks( { rootBlockIcon, rootBlockTitle, items, ...props } ) {
{ rootBlockTitle && <h2>{ rootBlockTitle }</h2> }
</div>
) }
<BlockTypesList items={ items } { ...props } />
{ children }
</div>
);
}

export default compose(
ifCondition( ( { items } ) => items && items.length > 0 ),
withSelect( ( select, { rootClientId } ) => {
const { getBlockType } = select( 'core/blocks' );
const { getBlockName } = select( 'core/block-editor' );
const rootBlockName = getBlockName( rootClientId );
const rootBlockType = getBlockType( rootBlockName );
return {
rootBlockTitle: rootBlockType && rootBlockType.title,
rootBlockIcon: rootBlockType && rootBlockType.icon,
};
} )
)( ChildBlocks );

0 comments on commit 436d7c0

Please sign in to comment.