Skip to content

Commit

Permalink
Site Title Block: Add alignment and tag level support (#22843)
Browse files Browse the repository at this point in the history
* Add alignment control to the site title block edit function.

* Add support for setting site title tag, and applying text alignment in the editor and front end.

* Fix up classname output, and add a default site title classname.

* Fix PHP linting issues.
  • Loading branch information
apeatling authored Jun 5, 2020
1 parent 0ab323a commit 5262790
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
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

0 comments on commit 5262790

Please sign in to comment.