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

Try/group block custom text color #19181

Merged
merged 6 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
},
"customBackgroundColor": {
"type": "string"
},
"textColor": {
"type": "string"
},
"customTextColor": {
"type": "string"
}
}
}
68 changes: 29 additions & 39 deletions packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import {
InspectorControls,
InnerBlocks,
PanelColorSettings,
withColors,
__experimentalUseColors,
} from '@wordpress/block-editor';
import { useRef } from '@wordpress/element';

function GroupEdit( {
className,
setBackgroundColor,
backgroundColor,
hasInnerBlocks,
} ) {
const styles = {
backgroundColor: backgroundColor.color,
};

const classes = classnames( className, backgroundColor.class, {
'has-background': !! backgroundColor.color,
} );
const ref = useRef();
const {
TextColor,
BackgroundColor,
InspectorControlsColorPanel,
} = __experimentalUseColors(
[
{ name: 'textColor', property: 'color' },
{ name: 'backgroundColor', className: 'has-background' },
],
{
contrastCheckers: { backgroundColor: true, textColor: true },
colorDetector: { targetRef: ref },
}
);

return (
<>
<InspectorControls>
<PanelColorSettings
title={ __( 'Color Settings' ) }
colorSettings={ [
{
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
},
] }
/>
</InspectorControls>
<div className={ classes } style={ styles }>
<div className="wp-block-group__inner-container">
<InnerBlocks
renderAppender={ ! hasInnerBlocks && InnerBlocks.ButtonBlockAppender }
/>
</div>
</div>
{ InspectorControlsColorPanel }
<BackgroundColor>
<TextColor>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still seems that a single ApplyColors would be better as it's used that way in 90% of the use-cases. cc @epiqueras @jorgefilipecosta

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be an additional component that's returned from the hook.

<div className="wp-block-group" ref={ ref } >
<div className="wp-block-group__inner-container" >
<InnerBlocks
renderAppender={ ! hasInnerBlocks && InnerBlocks.ButtonBlockAppender }
/>
</div>
</div>
</TextColor>
</BackgroundColor>
</>
);
}

export default compose( [
withColors( 'backgroundColor' ),
withSelect( ( select, { clientId } ) => {
const {
getBlock,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const settings = {
example: {
attributes: {
customBackgroundColor: '#ffffff',
customTextColor: '#000000',
},
innerBlocks: [
{
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/group/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import classnames from 'classnames';
import { InnerBlocks, getColorClassName } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { backgroundColor, customBackgroundColor } = attributes;
const { backgroundColor, customBackgroundColor, textColor, customTextColor } = attributes;

const backgroundClass = getColorClassName( 'background-color', backgroundColor );
const textClass = getColorClassName( 'color', textColor );
const className = classnames( backgroundClass, {
'has-text-color': textColor || customTextColor,
'has-background': backgroundColor || customBackgroundColor,
} );

const styles = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
};

return (
Expand Down