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

Block Editor: Optimize 'customClassName' controls #55345

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Changes from 1 commit
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
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 } />
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
</>
);
};
},
'withInspectorControl'
Expand Down
Loading