Skip to content

Commit

Permalink
Block Editor: Improve data selector for BlockQuickNavigationItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jun 12, 2023
1 parent 625b2de commit a970f80
Showing 1 changed file with 21 additions and 13 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,16 @@ 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 a970f80

Please sign in to comment.