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

Separator block: Allow divs to be used as separators #67530

Merged
merged 3 commits into from
Dec 4, 2024
Merged
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 @@ -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',
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
};
},
};
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;
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
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;
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved

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;
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
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
Loading