Skip to content

Commit

Permalink
Adding dispatch to update block styles
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 20, 2021
1 parent c4ddfd7 commit 3ce7ae7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
61 changes: 40 additions & 21 deletions packages/block-editor/src/hooks/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { hasBlockSupport, store as blocksStore } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { check, moreVertical } from '@wordpress/icons';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as blockEditorStore } from '../store';

/**
* Internal dependencies
Expand Down Expand Up @@ -65,37 +65,53 @@ export const withInspectorControl = createHigherOrderComponent(
true
);

//const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { updateBlockAttributes } = useDispatch( blockEditorStore );

const blockStyles = useSelect(
( select ) => {
const { getBlockStyles } = select( blocksStore );
return getBlockStyles( props.name );
},
[ props.name ]
[ props.name, props.attributes.className ]
);

const hasBlockStyles = blockStyles && blockStyles.length;

const activeStyleName = hasBlockStyles
const activeStyle = hasBlockStyles
? getActiveStyle(
blockStyles,
props.attributes.className || ''
)?.name
)
: null;

const onSelectStyleClassName = ( styleClassName ) => {
// updateBlockAttributes( props.clientId, {
// className: styleClassName,
// } );
const onSelectStyleClassName = ( style ) => {
const styleClassName = replaceActiveStyle(
props.attributes.className,
activeStyle,
style
);
updateBlockAttributes( props.clientId, {
className: styleClassName,
} );
};

const additionalClassNameContainerClasses = classnames(
'additional-class-name-control__container',
{
'has-block-styles': hasBlockStyles,
}
);

if ( hasCustomClassName && props.isSelected ) {
return (
<>
<BlockEdit { ...props } />
<InspectorControls __experimentalGroup="advanced">
<div className="custom-class-name-control">
<div
className={
additionalClassNameContainerClasses
}
>
<TextControl
autoComplete="off"
label={ __( 'Additional CSS class(es)' ) }
Expand All @@ -112,21 +128,25 @@ export const withInspectorControl = createHigherOrderComponent(
'Separate multiple classes with spaces.'
) }
/>
{ hasBlockStyles > 0 && (
{ hasBlockStyles && (
<DropdownMenu
icon={ moreVertical }
label={ __( 'Style CSS class(es)' ) }
label={ __( 'Block style classes' ) }
>
{ ( { onClose } ) => (
<MenuGroup>
<MenuGroup
label={ __(
'Block style classes'
) }
>
{ blockStyles.map(
( { name, label } ) => {
( style ) => {
const isSelected =
activeStyleName ===
name;
activeStyle?.name ===
style.name;
return (
<MenuItem
key={ label }
key={ style.label }
icon={
isSelected &&
check
Expand All @@ -136,17 +156,16 @@ export const withInspectorControl = createHigherOrderComponent(
}
onClick={ () => {
onSelectStyleClassName(
name
style
);
onClose();
} }
role="menuitemcheckbox"
>
{ label }
{ style.label }
</MenuItem>
);
}
) }
} ) }
</MenuGroup>
) }
</DropdownMenu>
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/hooks/custom-class-name.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.additional-class-name-control__container.has-block-styles {
display: flex;
}
1 change: 1 addition & 0 deletions packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
@import "./hooks/anchor.scss";
@import "./hooks/layout.scss";
@import "./hooks/border.scss";
@import "./hooks/custom-class-name.scss";

@import "./components/block-toolbar/style.scss";
@import "./components/inserter/style.scss";
Expand Down

0 comments on commit 3ce7ae7

Please sign in to comment.