Skip to content

Commit

Permalink
Navigation Link: Avoid unnecessary re-renders (#40696)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored and adamziel committed Apr 29, 2022
1 parent 29f53b9 commit d093cc1
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,35 @@ function navStripHTML( html ) {
* Add transforms to Link Control
*/

function LinkControlTransforms( { block, transforms, replace } ) {
function LinkControlTransforms( { clientId, replace } ) {
const { getBlock, blockTransforms } = useSelect(
( select ) => {
const {
getBlock: _getBlock,
getBlockRootClientId,
getBlockTransformItems,
} = select( blockEditorStore );

return {
getBlock: _getBlock,
blockTransforms: getBlockTransformItems(
[ _getBlock( clientId ) ],
getBlockRootClientId( clientId )
),
};
},
[ clientId ]
);

const featuredBlocks = [
'core/site-logo',
'core/social-links',
'core/search',
];
const transforms = blockTransforms.filter( ( item ) => {
return featuredBlocks.includes( item.name );
} );

if ( ! transforms?.length ) {
return null;
}
Expand All @@ -359,8 +387,11 @@ function LinkControlTransforms( { block, transforms, replace } ) {
key={ `transform-${ index }` }
onClick={ () =>
replace(
block.clientId,
switchToBlockType( block, item.name )
clientId,
switchToBlockType(
getBlock( clientId ),
item.name
)
)
}
className="link-control-transform__item"
Expand Down Expand Up @@ -424,19 +455,15 @@ export default function NavigationLinkEdit( {
hasChildren,
userCanCreatePages,
userCanCreatePosts,
thisBlock,
blockTransforms,
} = useSelect(
( select ) => {
const {
getBlock,
getBlocks,
getBlockCount,
getBlockName,
getBlockRootClientId,
hasSelectedInnerBlock,
getBlockParentsByBlockName,
getBlockTransformItems,
} = select( blockEditorStore );

return {
Expand All @@ -462,11 +489,6 @@ export default function NavigationLinkEdit( {
'create',
'posts'
),
thisBlock: getBlock( clientId ),
blockTransforms: getBlockTransformItems(
[ getBlock( clientId ) ],
getBlockRootClientId( clientId )
),
};
},
[ clientId ]
Expand All @@ -493,15 +515,6 @@ export default function NavigationLinkEdit( {
replaceBlock( clientId, newSubmenu );
}

const featuredBlocks = [
'core/site-logo',
'core/social-links',
'core/search',
];
const featuredTransforms = blockTransforms.filter( ( item ) => {
return featuredBlocks.includes( item.name );
} );

useEffect( () => {
// Show the LinkControl on mount if the URL is empty
// ( When adding a new menu item)
Expand Down Expand Up @@ -847,10 +860,7 @@ export default function NavigationLinkEdit( {
! url
? () => (
<LinkControlTransforms
block={ thisBlock }
transforms={
featuredTransforms
}
clientId={ clientId }
replace={ replaceBlock }
/>
)
Expand Down

0 comments on commit d093cc1

Please sign in to comment.