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

Column: Add border radius support and content clipping #40925

Closed
Closed
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ A single column within a columns block. ([Source](https://github.com/WordPress/g
- **Name:** core/column
- **Category:** text
- **Supports:** anchor, color (background, gradients, link, text), spacing (blockGap, padding), ~~html~~, ~~reusable~~
- **Attributes:** allowedBlocks, templateLock, verticalAlignment, width
- **Attributes:** allowedBlocks, clipContent, templateLock, verticalAlignment, width

## Columns

Expand Down
17 changes: 11 additions & 6 deletions packages/block-library/src/column/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
"description": "A single column within a columns block.",
"textdomain": "default",
"attributes": {
"verticalAlignment": {
"type": "string"
},
"width": {
"type": "string"
},
"allowedBlocks": {
"type": "array"
},
"clipContent": {
"type": "boolean"
},
"templateLock": {
"type": [ "string", "boolean" ],
"enum": [ "all", "insert", false ]
},
"verticalAlignment": {
"type": "string"
},
"width": {
"type": "string"
}
},
"supports": {
Expand All @@ -43,10 +46,12 @@
},
"__experimentalBorder": {
"color": true,
"radius": true,
"style": true,
"width": true,
"__experimentalDefaultControls": {
"color": true,
"radius": true,
"style": true,
"width": true
}
Expand Down
40 changes: 36 additions & 4 deletions packages/block-library/src/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
__experimentalUseCustomUnits as useCustomUnits,
PanelBody,
ToggleControl,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { sprintf, __ } from '@wordpress/i18n';

function ColumnEdit( {
attributes: {
allowedBlocks,
clipContent,
templateLock = false,
verticalAlignment,
width,
templateLock = false,
allowedBlocks,
style,
},
setAttributes,
clientId,
Expand Down Expand Up @@ -79,7 +83,10 @@ function ColumnEdit( {
const widthWithUnit = Number.isFinite( width ) ? width + '%' : width;
const blockProps = useBlockProps( {
className: classes,
style: widthWithUnit ? { flexBasis: widthWithUnit } : undefined,
style: {
flexBasis: widthWithUnit,
overflow: clipContent ? 'hidden' : undefined,
},
} );

const columnsCount = columnsIds.length;
Expand Down Expand Up @@ -128,6 +135,31 @@ function ColumnEdit( {
/>
</PanelBody>
</InspectorControls>
{ !! style?.border?.radius && (
<InspectorControls __experimentalGroup="border">
<ToolsPanelItem
hasValue={ () => clipContent !== undefined }
label={ __( 'Clip content' ) }
onDeselect={ () =>
setAttributes( { clipContent: undefined } )
}
resetAllFilter={ () => ( { clipContent: undefined } ) }
isShownByDefault={ true }
panelId={ clientId }
>
<ToggleControl
label={ __( 'Clip content' ) }
checked={ !! clipContent }
onChange={ () =>
setAttributes( { clipContent: ! clipContent } )
}
help={ __(
"Turning this on prevents content overflowing beyond the column's borders"
) }
/>
</ToolsPanelItem>
</InspectorControls>
) }
<div { ...innerBlocksProps } />
</>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/column/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import classnames from 'classnames';
import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';

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

const wrapperClasses = classnames( {
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
Expand All @@ -32,6 +32,10 @@ export default function save( { attributes } ) {
style = { flexBasis };
}

if ( clipContent ) {
style = { ...style, overflow: 'hidden' };
}

const blockProps = useBlockProps.save( {
className: wrapperClasses,
style,
Expand Down