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

Add support "HTML Element" to Site Tagline #59654

Merged
merged 6 commits into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Expand Up @@ -847,7 +847,7 @@ Describe in a few words what the site is about. The tagline can be used in searc
- **Name:** core/site-tagline
- **Category:** theme
- **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** textAlign
- **Attributes:** tagName, textAlign

## Site Title

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/site-tagline/block.json
Expand Up @@ -10,6 +10,10 @@
"attributes": {
"textAlign": {
"type": "string"
},
"tagName": {
"type": "string",
"default": "p"
}
},
"example": {},
Expand Down
28 changes: 25 additions & 3 deletions packages/block-library/src/site-tagline/edit.js
Expand Up @@ -12,17 +12,19 @@ import {
AlignmentControl,
useBlockProps,
BlockControls,
InspectorControls,
RichText,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { SelectControl } from '@wordpress/components';

export default function SiteTaglineEdit( {
attributes,
setAttributes,
insertBlocksAfter,
} ) {
const { textAlign } = attributes;
const { textAlign, tagName: TagName } = attributes;
const { canUserEdit, tagline } = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
select( coreStore );
Expand Down Expand Up @@ -58,7 +60,7 @@ export default function SiteTaglineEdit( {
onChange={ setTagline }
aria-label={ __( 'Site tagline text' ) }
placeholder={ __( 'Write site tagline…' ) }
tagName="p"
tagName={ TagName }
value={ tagline }
disableLineBreaks
__unstableOnSplitAtEnd={ () =>
Expand All @@ -67,10 +69,30 @@ export default function SiteTaglineEdit( {
{ ...blockProps }
/>
) : (
<p { ...blockProps }>{ tagline || __( 'Site Tagline placeholder' ) }</p>
<TagName { ...blockProps }>
{ tagline || __( 'Site Tagline placeholder' ) }
</TagName>
);
return (
<>
<InspectorControls group="advanced">
<SelectControl
label={ __( 'HTML element' ) }
options={ [
{ label: __( 'Default (<p>)' ), value: 'p' },
{ label: '<h1>', value: 'h1' },
{ label: '<h2>', value: 'h2' },
{ label: '<h3>', value: 'h3' },
{ label: '<h4>', value: 'h4' },
{ label: '<h5>', value: 'h5' },
{ label: '<h6>', value: 'h6' },
] }
value={ TagName }
onChange={ ( newTagName ) =>
setAttributes( { tagName: newTagName } )
}
/>
</InspectorControls>
<BlockControls group="block">
<AlignmentControl
onChange={ ( newAlign ) =>
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/site-tagline/index.php
Expand Up @@ -17,11 +17,13 @@ function render_block_core_site_tagline( $attributes ) {
if ( ! $site_tagline ) {
return;
}
$tag_name = empty( $attributes['tagName'] ) ? 'p' : $attributes['tagName'];
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );

return sprintf(
'<p %1$s>%2$s</p>',
'<%1$s %2$s>%3$s</%1$s>',
$tag_name,
$wrapper_attributes,
$site_tagline
);
Expand All @@ -38,4 +40,5 @@ function register_block_core_site_tagline() {
)
);
}

add_action( 'init', 'register_block_core_site_tagline' );
4 changes: 3 additions & 1 deletion test/integration/fixtures/blocks/core__site-tagline.json
Expand Up @@ -2,7 +2,9 @@
{
"name": "core/site-tagline",
"isValid": true,
"attributes": {},
"attributes": {
"tagName": "p"
},
"innerBlocks": []
}
]