Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation Link: Avoid unnecessary re-renders #40696

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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