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

BlockTitle: avoid re-render on attributes change #32118

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 25 additions & 24 deletions packages/block-editor/src/components/block-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,44 @@ import { store as blockEditorStore } from '../../store';
* @return {?string} Block title.
*/
export default function BlockTitle( { clientId } ) {
const { attributes, name, reusableBlockTitle } = useSelect(
const blockInformation = useBlockDisplayInformation( clientId );
const title = useSelect(
( select ) => {
if ( ! clientId ) {
return {};
return null;
}

const {
getBlockName,
getBlockAttributes,
__experimentalGetReusableBlockTitle,
} = select( blockEditorStore );
const blockName = getBlockName( clientId );

if ( ! blockName ) {
return {};
return null;
}

const blockType = getBlockType( blockName );
const isReusable = isReusableBlock( blockType );
const attributes = getBlockAttributes( clientId );
const reusableBlockTitle =
isReusable &&
__experimentalGetReusableBlockTitle( attributes.ref );
const label =
reusableBlockTitle || getBlockLabel( blockType, attributes );

// Label will fallback to the title if no label is defined for the
// current label context. If the label is defined we prioritize it
// over possible possible block variation title match.
if ( label === blockType.title ) {
return null;
}
const isReusable = isReusableBlock( getBlockType( blockName ) );
return {
attributes: getBlockAttributes( clientId ),
name: blockName,
reusableBlockTitle:
isReusable &&
__experimentalGetReusableBlockTitle(
getBlockAttributes( clientId ).ref
),
};

return truncate( label, { length: 35 } );
},
[ clientId ]
);

const blockInformation = useBlockDisplayInformation( clientId );
if ( ! name || ! blockInformation ) return null;
const blockType = getBlockType( name );
const label = reusableBlockTitle || getBlockLabel( blockType, attributes );
// Label will fallback to the title if no label is defined for the current
// label context. If the label is defined we prioritize it over possible
// possible block variation title match.
if ( label !== blockType.title ) {
return truncate( label, { length: 35 } );
}
return blockInformation.title;
return title || blockInformation.title;
}