Skip to content
Prev Previous commit
Next Next commit
Rename hook to match feature
  • Loading branch information
getdave committed Nov 21, 2023
commit 79cca563a23b7d1630fee3700b577255cc7add00
56 changes: 0 additions & 56 deletions packages/block-editor/src/hooks/block-rename.js

This file was deleted.

47 changes: 47 additions & 0 deletions packages/block-editor/src/hooks/block-renaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';

/**
* Internal dependencies
*/
import { InspectorControls } from '../components';
import { useBlockRename } from '../components/block-rename';

/**
* Filters registered block settings, adding an `__experimentalLabel` callback if one does not already exist.
Expand Down Expand Up @@ -38,6 +47,44 @@ export function addLabelCallback( settings ) {
return settings;
}

export const withBlockRenameControl = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { name, attributes, setAttributes, isSelected } = props;

const { canRename } = useBlockRename( name );

return (
<>
{ isSelected && canRename && (
<InspectorControls group="advanced">
<TextControl
__nextHasNoMarginBottom
label={ __( 'Block name' ) }
value={ attributes?.metadata?.name || '' }
onChange={ ( newName ) => {
setAttributes( {
metadata: {
...attributes?.metadata,
name: newName,
},
} );
} }
/>
</InspectorControls>
) }
<BlockEdit key="edit" { ...props } />
</>
);
},
'withToolbarControls'
);

addFilter(
'editor.BlockEdit',
'core/block-rename-ui/with-block-rename-control',
withBlockRenameControl
);

addFilter(
'blocks.registerBlockType',
'core/metadata/addLabelCallback',
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import './content-lock-ui';
import './metadata';
import './custom-fields';
import './block-hooks';
import './block-rename';
import './block-renaming';

export { useCustomSides } from './dimensions';
export { useLayoutClasses, useLayoutStyles } from './layout';
Expand Down