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 Apr 29, 2022
1 parent 012c86c commit 2897371
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,20 +455,16 @@ export default function NavigationLinkEdit( {
hasDescendants,
userCanCreatePages,
userCanCreatePosts,
thisBlock,
blockTransforms,
} = useSelect(
( select ) => {
const {
getBlock,
getBlocks,
getBlockName,
getBlockRootClientId,
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
getBlockParentsByBlockName,
getBlockTransformItems,
} = select( blockEditorStore );

const selectedBlockId = getSelectedBlockClientId();
Expand Down Expand Up @@ -475,11 +502,6 @@ export default function NavigationLinkEdit( {
'create',
'posts'
),
thisBlock: getBlock( clientId ),
blockTransforms: getBlockTransformItems(
[ getBlock( clientId ) ],
getBlockRootClientId( clientId )
),
};
},
[ clientId ]
Expand All @@ -506,15 +528,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 @@ -860,10 +873,7 @@ export default function NavigationLinkEdit( {
! url
? () => (
<LinkControlTransforms
block={ thisBlock }
transforms={
featuredTransforms
}
clientId={ clientId }
replace={ replaceBlock }
/>
)
Expand Down

0 comments on commit 2897371

Please sign in to comment.