Skip to content

Commit

Permalink
Block Editor: Improve data selector for BlockQuickNavigationItem (#51429
Browse files Browse the repository at this point in the history
)

* Block Editor: Improve data selector for BlockQuickNavigationItem

* Remove unnecessary 'key' prop
  • Loading branch information
Mamaduka authored Jun 14, 2023
1 parent 05a229b commit 664a5af
Showing 1 changed file with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
__experimentalHStack as HStack,
FlexItem,
} from '@wordpress/components';
import { getBlockType, __experimentalGetBlockLabel } from '@wordpress/blocks';
import {
__experimentalGetBlockLabel,
store as blocksStore,
} from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -33,17 +36,28 @@ export default function BlockQuickNavigation( { clientIds } ) {
}

function BlockQuickNavigationItem( { clientId } ) {
const { name, attributes, isSelected } = useSelect(
const { name, icon, isSelected } = useSelect(
( select ) => {
const {
getBlockName,
getBlockAttributes,
isBlockSelected,
hasSelectedInnerBlock,
} = select( blockEditorStore );
const { getBlockType } = select( blocksStore );

const blockType = getBlockType( getBlockName( clientId ) );
const attributes = getBlockAttributes( clientId );

return {
name: getBlockName( clientId ),
attributes: getBlockAttributes( clientId ),
name:
blockType &&
__experimentalGetBlockLabel(
blockType,
attributes,
'list-view'
),
icon: blockType?.icon,
isSelected:
isBlockSelected( clientId ) ||
hasSelectedInnerBlock( clientId, /* deep: */ true ),
Expand All @@ -52,22 +66,15 @@ function BlockQuickNavigationItem( { clientId } ) {
[ clientId ]
);
const { selectBlock } = useDispatch( blockEditorStore );
const blockType = getBlockType( name );

return (
<Button
key={ clientId }
isPressed={ isSelected }
onClick={ () => selectBlock( clientId ) }
>
<HStack justify="flex-start">
<BlockIcon icon={ blockType.icon } />
<FlexItem>
{ __experimentalGetBlockLabel(
blockType,
attributes,
'list-view'
) }
</FlexItem>
<BlockIcon icon={ icon } />
<FlexItem>{ name }</FlexItem>
</HStack>
</Button>
);
Expand Down

0 comments on commit 664a5af

Please sign in to comment.