diff --git a/packages/block-editor/src/components/block-card/index.js b/packages/block-editor/src/components/block-card/index.js index 38fcd33f1c885f..6374082c6fc9d1 100644 --- a/packages/block-editor/src/components/block-card/index.js +++ b/packages/block-editor/src/components/block-card/index.js @@ -11,17 +11,19 @@ import { Icon, __experimentalText as Text, __experimentalVStack as VStack, + __experimentalHStack as HStack, privateApis as componentsPrivateApis, } from '@wordpress/components'; import { useDispatch, useSelect } from '@wordpress/data'; import deprecated from '@wordpress/deprecated'; -import { __, isRTL } from '@wordpress/i18n'; +import { __, sprintf, isRTL } from '@wordpress/i18n'; import { chevronLeft, chevronRight, arrowRight, arrowLeft, } from '@wordpress/icons'; +import { getBlockType, hasBlockSupport } from '@wordpress/blocks'; /** * Internal dependencies @@ -103,18 +105,32 @@ function BlockCard( { ( { title, icon, description } = blockType ); } - const parentNavBlockClientId = useSelect( + const { parentBlockClientId, parentBlockName } = useSelect( ( select ) => { if ( parentClientId || isChild || ! allowParentNavigation ) { - return; + return {}; } - const { getBlockParentsByBlockName } = select( blockEditorStore ); + const { getBlockParents, getBlockName } = + select( blockEditorStore ); - return getBlockParentsByBlockName( - clientId, - 'core/navigation', - true - )[ 0 ]; + // Find the closest parent block that is either: + // 1. A navigation block (special case for ad-hoc list view support) + // 2. Any block with listView support + const parents = getBlockParents( clientId, true ); + const foundParentId = parents.find( ( parentId ) => { + const parentName = getBlockName( parentId ); + return ( + parentName === 'core/navigation' || + hasBlockSupport( parentName, 'listView' ) + ); + } ); + + return { + parentBlockClientId: foundParentId, + parentBlockName: foundParentId + ? getBlockName( foundParentId ) + : null, + }; }, [ clientId, allowParentNavigation, isChild, parentClientId ] ); @@ -134,56 +150,66 @@ function BlockCard( { className ) } > - { parentNavBlockClientId && ( // This is only used by the Navigation block for now. It's not ideal having Navigation block specific code here. -