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 13 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"
}
}
}
40 changes: 28 additions & 12 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
InspectorControls,
InnerBlocks,
BlockControls,
__experimentalBlockPatternPicker,
BlockVerticalAlignmentToolbar,
__experimentalBlockPatternPicker,
__experimentalUseColors,
} from '@wordpress/block-editor';
import {
withDispatch,
Expand Down Expand Up @@ -62,12 +63,30 @@ function ColumnsEditContainer( {
};
}, [ clientId ] );

const {
BackgroundColor,
InspectorControlsColorPanel,
} = __experimentalUseColors(
[
{ name: 'backgroundColor', className: 'has-background' },
],
{
contrastCheckers: [ { backgroundColor: false } ],
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what the purpose of including this is?

);

const classes = classnames( className, {
[ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );

return (
<>
<BlockControls>
<BlockVerticalAlignmentToolbar
onChange={ updateAlignment }
value={ verticalAlignment }
/>
</BlockControls>
<InspectorControls>
<PanelBody>
<RangeControl
Expand All @@ -79,17 +98,14 @@ function ColumnsEditContainer( {
/>
</PanelBody>
</InspectorControls>
<BlockControls>
<BlockVerticalAlignmentToolbar
onChange={ updateAlignment }
value={ verticalAlignment }
/>
</BlockControls>
<div className={ classes }>
<InnerBlocks
templateLock="all"
allowedBlocks={ ALLOWED_BLOCKS } />
</div>
{ InspectorControlsColorPanel }
<BackgroundColor>
<div className={ classes }>
<InnerBlocks
templateLock="all"
allowedBlocks={ ALLOWED_BLOCKS } />
</div>
</BackgroundColor>
</>
);
}
Expand Down
20 changes: 16 additions & 4 deletions packages/block-library/src/columns/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@ 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 wrapperClasses = classnames( {
const backgroundClass = getColorClassName( 'background-color', backgroundColor );

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

const style = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
};

return (
<div className={ wrapperClasses }>
<div className={ className ? className : undefined } style={ style }>
<InnerBlocks.Content />
</div>
);
Expand Down
4 changes: 4 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,10 @@
@include break-medium() {
flex-wrap: nowrap;
}

&.has-background {
padding: $block-bg-padding--v $block-bg-padding--h;
}
}

.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