Skip to content

Commit

Permalink
Block Editor: Optimize 'customClassName' controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Oct 13, 2023
1 parent e21581b commit 171f92d
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions packages/block-editor/src/hooks/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ export function addAttribute( settings ) {
return settings;
}

function CustomClassNameControls( { attributes, setAttributes } ) {
const blockEditingMode = useBlockEditingMode();
if ( blockEditingMode !== 'default' ) {
return null;
}

return (
<InspectorControls group="advanced">
<TextControl
__nextHasNoMarginBottom
autoComplete="off"
label={ __( 'Additional CSS class(es)' ) }
value={ attributes.className || '' }
onChange={ ( nextValue ) => {
setAttributes( {
className: nextValue !== '' ? nextValue : undefined,
} );
} }
help={ __( 'Separate multiple classes with spaces.' ) }
/>
</InspectorControls>
);
}

/**
* Override the default edit UI to include a new block inspector control for
* assigning the custom class name, if block supports custom class name.
Expand All @@ -51,42 +75,23 @@ export function addAttribute( settings ) {
export const withInspectorControl = createHigherOrderComponent(
( BlockEdit ) => {
return ( props ) => {
const blockEditingMode = useBlockEditingMode();
const hasCustomClassName = hasBlockSupport(
props.name,
'customClassName',
true
);
if ( hasCustomClassName && props.isSelected ) {
return (
<>
<BlockEdit { ...props } />
{ blockEditingMode === 'default' && (
<InspectorControls group="advanced">
<TextControl
__nextHasNoMarginBottom
autoComplete="off"
label={ __( 'Additional CSS class(es)' ) }
value={ props.attributes.className || '' }
onChange={ ( nextValue ) => {
props.setAttributes( {
className:
nextValue !== ''
? nextValue
: undefined,
} );
} }
help={ __(
'Separate multiple classes with spaces.'
) }
/>
</InspectorControls>
) }
</>
);
}

return <BlockEdit { ...props } />;

return (
<>
{ hasCustomClassName && props.isSelected && (
<CustomClassNameControls
attributes={ props.attributes }
setAttributes={ props.setAttributes }
/>
) }
<BlockEdit { ...props } />
</>
);
};
},
'withInspectorControl'
Expand Down

0 comments on commit 171f92d

Please sign in to comment.