Skip to content

Commit

Permalink
Separator block: Allow divs to be used as separators (#67530)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: jorgefilipecosta <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: luminuu <[email protected]>
  • Loading branch information
7 people authored Dec 4, 2024
1 parent 8ddab81 commit dc532bb
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ Create a break between ideas or sections with a horizontal separator. ([Source](
- **Name:** core/separator
- **Category:** design
- **Supports:** align (center, full, wide), anchor, color (background, gradients, ~~enableContrastChecker~~, ~~text~~), interactivity (clientNavigation), spacing (margin)
- **Attributes:** opacity
- **Attributes:** opacity, tagName

## Shortcode

Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/separator/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"opacity": {
"type": "string",
"default": "alpha-channel"
},
"tagName": {
"type": "string",
"enum": [ "hr", "div" ],
"default": "hr"
}
},
"supports": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/separator/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const v1 = {
style: customColor
? { color: { background: customColor } }
: undefined,
tagName: 'hr',
};
},
};
Expand Down
31 changes: 28 additions & 3 deletions packages/block-library/src/separator/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { HorizontalRule } from '@wordpress/components';
import { HorizontalRule, SelectControl } from '@wordpress/components';
import {
useBlockProps,
getColorClassName,
__experimentalUseColorProps as useColorProps,
InspectorControls,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import useDeprecatedOpacity from './use-deprecated-opacity';

const htmlElementMessages = {
div: __(
'The <div> element should only be used if the separator is a design element that should not be announced.'
),
};

export default function SeparatorEdit( { attributes, setAttributes } ) {
const { backgroundColor, opacity, style } = attributes;
const { backgroundColor, opacity, style, tagName } = attributes;
const colorProps = useColorProps( attributes );
const currentColor = colorProps?.style?.backgroundColor;
const hasCustomColor = !! style?.color?.background;
Expand All @@ -44,10 +52,27 @@ export default function SeparatorEdit( { attributes, setAttributes } ) {
color: currentColor,
backgroundColor: currentColor,
};
const Wrapper = tagName === 'hr' ? HorizontalRule : tagName;

return (
<>
<HorizontalRule
<InspectorControls group="advanced">
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'HTML element' ) }
options={ [
{ label: __( 'Default (<hr>)' ), value: 'hr' },
{ label: '<div>', value: 'div' },
] }
value={ tagName }
onChange={ ( value ) =>
setAttributes( { tagName: value } )
}
help={ htmlElementMessages[ tagName ] }
/>
</InspectorControls>
<Wrapper
{ ...useBlockProps( {
className,
style: hasCustomColor ? styles : undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/separator/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@wordpress/block-editor';

export default function separatorSave( { attributes } ) {
const { backgroundColor, style, opacity } = attributes;
const { backgroundColor, style, opacity, tagName: Tag } = attributes;
const customColor = style?.color?.background;
const colorProps = getColorClassesAndStyles( attributes );
// The hr support changing color using border-color, since border-color
Expand All @@ -37,5 +37,5 @@ export default function separatorSave( { attributes } ) {
backgroundColor: colorProps?.style?.backgroundColor,
color: colorClass ? undefined : customColor,
};
return <hr { ...useBlockProps.save( { className, style: styles } ) } />;
return <Tag { ...useBlockProps.save( { className, style: styles } ) } />;
}
1 change: 1 addition & 0 deletions packages/block-library/src/separator/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaultAttributes = {
opacity: 'alpha-channel',
style: {},
className: '',
tagName: 'hr',
};
const defaultProps = {
attributes: defaultAttributes,
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__separator-color.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"isValid": true,
"attributes": {
"opacity": "alpha-channel",
"backgroundColor": "accent"
"backgroundColor": "accent",
"tagName": "hr"
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"color": {
"background": "#5da54c"
}
}
},
"tagName": "hr"
},
"innerBlocks": []
}
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__separator.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"name": "core/separator",
"isValid": true,
"attributes": {
"opacity": "alpha-channel"
"opacity": "alpha-channel",
"tagName": "hr"
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"isValid": true,
"attributes": {
"backgroundColor": "accent",
"opacity": "css"
"opacity": "css",
"tagName": "hr"
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"color": {
"background": "#cc1d1d"
}
}
},
"tagName": "hr"
},
"innerBlocks": []
}
Expand Down

0 comments on commit dc532bb

Please sign in to comment.