Skip to content

Commit

Permalink
Separator block: Allow divs to be used as separators
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 3, 2024
1 parent 1c3cea4 commit bf467ef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 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
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 } ) } />;
}

0 comments on commit bf467ef

Please sign in to comment.