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

Site Title Block: Add alignment and tag level support #22843

Merged
merged 4 commits into from
Jun 5, 2020
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
3 changes: 3 additions & 0 deletions packages/block-library/src/site-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"level": {
"type": "number",
"default": 1
},
"align": {
"type": "string"
}
},
"supports": {
Expand Down
44 changes: 34 additions & 10 deletions packages/block-library/src/site-title/edit.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import {
PlainText,
RichText,
AlignmentToolbar,
BlockControls,
__experimentalBlock as Block,
} from '@wordpress/block-editor';

export default function SiteTitleEdit() {
export default function SiteTitleEdit( { attributes, setAttributes } ) {
const { level, align } = attributes;
const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' );
const tagName = 0 === level ? 'p' : 'h' + level;

return (
<PlainText
__experimentalVersion={ 2 }
tagName={ Block.h1 }
placeholder={ __( 'Site Title' ) }
value={ title }
onChange={ setTitle }
disableLineBreaks
/>
<>
<BlockControls>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
</BlockControls>

<RichText
tagName={ Block[ tagName ] }
placeholder={ __( 'Site Title' ) }
value={ title }
onChange={ setTitle }
className={ classnames( {
[ `has-text-align-${ align }` ]: align,
} ) }
allowedFormats={ [] }
disableLineBreaks
/>
</>
);
}
12 changes: 10 additions & 2 deletions packages/block-library/src/site-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
* @return string The render.
*/
function render_block_core_site_title( $attributes ) {
$tag_name = 'h1';
$tag_name = 'h1';
$align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "has-text-align-{$attributes['align']}";

if ( isset( $attributes['level'] ) ) {
$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level'];
}
return sprintf( '<%1$s>%2$s</%1$s>', $tag_name, get_bloginfo( 'name' ) );

return sprintf(
'<%1$s class="%2$s">%3$s</%1$s>',
$tag_name,
'wp-block-site-title' . esc_attr( $align_class_name ),
get_bloginfo( 'name' )
);
}

/**
Expand Down