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

Add background color support to Columns block #17813

Merged
merged 16 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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/columns/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"attributes": {
"verticalAlignment": {
"type": "string"
},
"backgroundColor": {
"type": "string"
},
"customBackgroundColor": {
"type": "string"
}
}
}
28 changes: 26 additions & 2 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { dropRight, get, map, times } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import {
PanelBody,
RangeControl,
Expand All @@ -18,6 +19,8 @@ import {
BlockControls,
__experimentalBlockPatternPicker,
BlockVerticalAlignmentToolbar,
PanelColorSettings,
withColors,
} from '@wordpress/block-editor';
import {
withDispatch,
Expand Down Expand Up @@ -52,6 +55,8 @@ function ColumnsEditContainer( {
className,
updateAlignment,
updateColumns,
setBackgroundColor,
backgroundColor,
clientId,
} ) {
const { verticalAlignment } = attributes;
Expand All @@ -63,9 +68,21 @@ function ColumnsEditContainer( {
} );

const classes = classnames( className, {
'has-background': ( backgroundColor.class || backgroundColor.color ),
[ backgroundColor.class ]: backgroundColor.class,
[ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );

const styles = {
backgroundColor: backgroundColor.color,
};

const colorSettings = [ {
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
} ];

return (
<>
<InspectorControls>
Expand All @@ -78,14 +95,19 @@ function ColumnsEditContainer( {
max={ 6 }
/>
</PanelBody>
<PanelColorSettings
title={ __( 'Color Settings' ) }
initialOpen={ false }
colorSettings={ colorSettings }
/>
</InspectorControls>
<BlockControls>
<BlockVerticalAlignmentToolbar
onChange={ updateAlignment }
value={ verticalAlignment }
/>
</BlockControls>
<div className={ classes }>
<div className={ classes } style={ styles }>
<InnerBlocks
templateLock="all"
allowedBlocks={ ALLOWED_BLOCKS } />
Expand Down Expand Up @@ -230,4 +252,6 @@ const ColumnsEdit = ( props ) => {
/> );
};

export default ColumnsEdit;
export default compose( [
withColors( 'backgroundColor' ),
] )( ColumnsEdit );
12 changes: 9 additions & 3 deletions packages/block-library/src/columns/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { InnerBlocks, getColorClassName } from '@wordpress/block-editor';

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

const backgroundClass = getColorClassName( 'background-color', backgroundColor );
const wrapperClasses = classnames( {
'has-background': ( backgroundClass || customBackgroundColor ),
[ backgroundClass ]: backgroundClass,
[ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );
const style = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
};

return (
<div className={ wrapperClasses }>
<div className={ wrapperClasses } style={ style }>
<InnerBlocks.Content />
</div>
);
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/columns/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
@include break-medium() {
flex-wrap: nowrap;
}

&.has-background {
// Matches paragraph Block padding
// Todo: normalise with variables
padding: 20px 30px;
}
}

.wp-block-column {
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-tests/fixtures/blocks/core__columns.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:columns -->
<div class="wp-block-columns">
<!-- wp:columns {"backgroundColor":"secondary"} -->
<div class="wp-block-columns has-background has-secondary-background-color">
<!-- wp:column -->
<div class="wp-block-column">
<!-- wp:paragraph -->
Expand Down
6 changes: 4 additions & 2 deletions packages/e2e-tests/fixtures/blocks/core__columns.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"clientId": "_clientId_0",
"name": "core/columns",
"isValid": true,
"attributes": {},
"attributes": {
"backgroundColor": "secondary"
},
"innerBlocks": [
{
"clientId": "_clientId_0",
Expand Down Expand Up @@ -68,6 +70,6 @@
"originalContent": "<div class=\"wp-block-column\">\n\t\t\n\t\t\n\t</div>"
}
],
"originalContent": "<div class=\"wp-block-columns\">\n\t\n\t\n</div>"
"originalContent": "<div class=\"wp-block-columns has-background has-secondary-background-color\">\n\t\n\t\n</div>"
}
]
8 changes: 5 additions & 3 deletions packages/e2e-tests/fixtures/blocks/core__columns.parsed.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[
{
"blockName": "core/columns",
"attrs": {},
"attrs": {
"backgroundColor": "secondary"
},
"innerBlocks": [
{
"blockName": "core/column",
Expand Down Expand Up @@ -68,9 +70,9 @@
]
}
],
"innerHTML": "\n<div class=\"wp-block-columns\">\n\t\n\t\n</div>\n",
"innerHTML": "\n<div class=\"wp-block-columns has-background has-secondary-background-color\">\n\t\n\t\n</div>\n",
"innerContent": [
"\n<div class=\"wp-block-columns\">\n\t",
"\n<div class=\"wp-block-columns has-background has-secondary-background-color\">\n\t",
null,
"\n\t",
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<!-- wp:columns {"backgroundColor":"secondary"} -->
<div class="wp-block-columns has-background has-secondary-background-color"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Column One, Paragraph One</p>
<!-- /wp:paragraph -->
Expand Down