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

Columns Block: Add custom gutter widths #27661

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions packages/block-library/src/columns/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"name": "core/columns",
"category": "design",
"attributes": {
"gutterSize": {
"type": "number"
},
"verticalAlignment": {
"type": "string"
}
Expand Down
27 changes: 24 additions & 3 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ import {
*/
const ALLOWED_BLOCKS = [ 'core/column' ];

// Values to constrain the customizable gutter size.
const MIN_GUTTER_VALUE = 0;
const MAX_GUTTER_VALUE = 100;
const INITIAL_GUTTER_POSITION = 32;

function ColumnsEditContainer( {
attributes,
setAttributes,
updateAlignment,
updateColumns,
clientId,
} ) {
const { verticalAlignment } = attributes;
const { gutterSize, verticalAlignment } = attributes;

const { count } = useSelect(
( select ) => {
Expand All @@ -69,6 +75,10 @@ function ColumnsEditContainer( {

const blockProps = useBlockProps( {
className: classes,
style: {
'--gutter-size':
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
gutterSize !== undefined ? `${ gutterSize }px` : undefined,
},
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
Expand All @@ -85,7 +95,7 @@ function ColumnsEditContainer( {
/>
</BlockControls>
<InspectorControls>
<PanelBody>
<PanelBody title={ __( 'Display settings' ) }>
<RangeControl
label={ __( 'Columns' ) }
value={ count }
Expand All @@ -100,6 +110,17 @@ function ColumnsEditContainer( {
) }
</Notice>
) }
<RangeControl
label={ __( 'Gutter size' ) }
value={ gutterSize }
onChange={ ( value ) => {
setAttributes( { gutterSize: value } );
} }
min={ MIN_GUTTER_VALUE }
max={ MAX_GUTTER_VALUE }
initialPosition={ INITIAL_GUTTER_POSITION }
allowReset
/>
</PanelBody>
</InspectorControls>
<div { ...innerBlocksProps } />
Expand All @@ -112,7 +133,7 @@ const ColumnsEditContainerWrapper = withDispatch(
/**
* Update all child Column blocks with a new vertical alignment setting
* based on whatever alignment is passed in. This allows change to parent
* to overide anything set on a individual column basis.
* to override anything set on a individual column basis.
*
* @param {string} verticalAlignment the vertical alignment setting
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/columns/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
@include break-small() {
.editor-styles-wrapper
.block-editor-block-list__block.wp-block-column:nth-child(even) {
margin-left: $grid-unit-20 * 2;
margin-left: var(--gutter-size, $grid-unit-20 * 2);
}
}

@include break-medium() {
.editor-styles-wrapper
.block-editor-block-list__block.wp-block-column:not(:first-child) {
margin-left: $grid-unit-20 * 2;
margin-left: var(--gutter-size, $grid-unit-20 * 2);
}
}

Expand Down
17 changes: 11 additions & 6 deletions packages/block-library/src/columns/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import classnames from 'classnames';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

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

const className = classnames( {
[ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );
const { gutterSize, verticalAlignment } = attributes;
const blockProps = {
className: classnames( {
[ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} ),
style: {
'--gutter-size':
gutterSize !== undefined ? `${ gutterSize }px` : undefined,
},
};

return (
<div { ...useBlockProps.save( { className } ) }>
<div { ...useBlockProps.save( blockProps ) }>
<InnerBlocks.Content />
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/columns/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
// Add space between the multiple columns. Themes can customize this if they wish to work differently.
// Only apply this beyond the mobile breakpoint, as there's only a single column on mobile.
&:nth-child(even) {
margin-left: 2em;
margin-left: var(--gutter-size, 2em);
}
}

Expand All @@ -83,7 +83,7 @@

// When columns are in a single row, add space before all except the first.
&:not(:first-child) {
margin-left: 2em;
margin-left: var(--gutter-size, 2em);
}
}

Expand Down