Skip to content

Commit f94dadb

Browse files
talldanhypest
authored andcommitted
Try setting a block display name for the Block Navigator. (#17519)
* Really simple first attempt at showing a display name in the navigator * Strip any RichText formatting * Add display name for navigation menu item block * Refactor to use displayNameAttribute property * Change name of displayName options
1 parent 56a2759 commit f94dadb

File tree

2 files changed

+26
-1
lines changed
  • packages
    • block-editor/src/components/block-navigation
    • block-library/src/navigation-menu-item

2 files changed

+26
-1
lines changed

packages/block-editor/src/components/block-navigation/list.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,35 @@ import classnames from 'classnames';
1010
import { Button } from '@wordpress/components';
1111
import { getBlockType } from '@wordpress/blocks';
1212
import { __ } from '@wordpress/i18n';
13+
import { create, getTextContent } from '@wordpress/rich-text';
1314

1415
/**
1516
* Internal dependencies
1617
*/
1718
import BlockIcon from '../block-icon';
1819

20+
/**
21+
* Get the block display name, if it has one, or the block title if it doesn't.
22+
*
23+
* @param {Object} blockType The block type.
24+
* @param {Object} attributes The values of the block's attributes
25+
*
26+
* @return {string} The display name value.
27+
*/
28+
function getBlockDisplayName( blockType, attributes ) {
29+
const displayNameAttribute = blockType.__experimentalDisplayName;
30+
31+
if ( ! displayNameAttribute || ! attributes[ displayNameAttribute ] ) {
32+
return blockType.title;
33+
}
34+
35+
// Strip any formatting.
36+
const richTextValue = create( { html: attributes[ displayNameAttribute ] } );
37+
const formatlessDisplayName = getTextContent( richTextValue );
38+
39+
return formatlessDisplayName;
40+
}
41+
1942
export default function BlockNavigationList( {
2043
blocks,
2144
selectedBlockClientId,
@@ -43,7 +66,7 @@ export default function BlockNavigationList( {
4366
onClick={ () => selectBlock( block.clientId ) }
4467
>
4568
<BlockIcon icon={ blockType.icon } showColors />
46-
{ blockType.title }
69+
{ getBlockDisplayName( blockType, block.attributes ) }
4770
{ isSelected && <span className="screen-reader-text">{ __( '(selected block)' ) }</span> }
4871
</Button>
4972
</div>

packages/block-library/src/navigation-menu-item/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export const settings = {
2222

2323
description: __( 'Add a page, link, or other item to your Navigation Menu.' ),
2424

25+
__experimentalDisplayName: 'label',
26+
2527
edit,
2628
save,
2729
};

0 commit comments

Comments
 (0)