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 5 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:** level, 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"
},
"level": {
"type": "number",
"default": 0
}
},
"example": {},
Expand Down
19 changes: 16 additions & 3 deletions packages/block-library/src/site-tagline/edit.js
Expand Up @@ -12,17 +12,20 @@ import {
AlignmentControl,
useBlockProps,
BlockControls,
HeadingLevelDropdown,
RichText,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';

const HEADING_LEVELS = [ 0, 1, 2, 3, 4, 5, 6 ];

export default function SiteTaglineEdit( {
attributes,
setAttributes,
insertBlocksAfter,
} ) {
const { textAlign } = attributes;
const { textAlign, level } = attributes;
const { canUserEdit, tagline } = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
select( coreStore );
Expand All @@ -38,6 +41,7 @@ export default function SiteTaglineEdit( {
};
}, [] );

const TagName = level === 0 ? 'p' : `h${ level }`;
const { editEntityRecord } = useDispatch( coreStore );

function setTagline( newTagline ) {
Expand All @@ -58,7 +62,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,11 +71,20 @@ export default function SiteTaglineEdit( {
{ ...blockProps }
/>
) : (
<p { ...blockProps }>{ tagline || __( 'Site Tagline placeholder' ) }</p>
<TagName { ...blockProps }>
{ tagline || __( 'Site Tagline placeholder' ) }
</TagName>
);
return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
options={ HEADING_LEVELS }
value={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
onChange={ ( newAlign ) =>
setAttributes( { textAlign: newAlign } )
Expand Down
10 changes: 9 additions & 1 deletion packages/block-library/src/site-tagline/index.php
Expand Up @@ -17,11 +17,18 @@ function render_block_core_site_tagline( $attributes ) {
if ( ! $site_tagline ) {
return;
}

$tag_name = 'p';
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );

if ( isset( $attributes['level'] ) ) {
$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level'];
}
torounit marked this conversation as resolved.
Show resolved Hide resolved

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 +45,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": {
"level": 0
},
"innerBlocks": []
}
]