-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Post Title Block: Add alignment and heading level support #22872
Changes from 2 commits
423d35a
5b11a16
3332552
dfed092
dee15ea
48899c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { | ||
AlignmentToolbar, | ||
BlockControls, | ||
__experimentalBlock as Block, | ||
apeatling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} from '@wordpress/block-editor'; | ||
import { ToolbarGroup } from '@wordpress/components'; | ||
|
||
export default function PostTitleEdit( { context } ) { | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import HeadingLevelDropdown from '../heading/heading-level-dropdown'; | ||
apeatling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default function PostTitleEdit( { | ||
attributes, | ||
setAttributes, | ||
context, | ||
} ) { | ||
const { level, align } = attributes; | ||
const { postType, postId } = context; | ||
const tagName = 0 === level ? 'p' : 'h' + level; | ||
|
||
const post = useSelect( | ||
( select ) => | ||
|
@@ -20,5 +42,32 @@ export default function PostTitleEdit( { context } ) { | |
return null; | ||
} | ||
|
||
return <h2>{ post.title }</h2>; | ||
return ( | ||
<> | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<HeadingLevelDropdown | ||
selectedLevel={ level } | ||
onChange={ ( newLevel ) => | ||
setAttributes( { level: newLevel } ) | ||
} | ||
/> | ||
</ToolbarGroup> | ||
<AlignmentToolbar | ||
value={ align } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { align: nextAlign } ); | ||
} } | ||
/> | ||
</BlockControls> | ||
Comment on lines
+47
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR. We need to document the move from |
||
<Block | ||
tagName={ tagName } | ||
className={ classnames( { | ||
[ `has-text-align-${ align }` ]: align, | ||
} ) } | ||
> | ||
{ post.title } | ||
</Block> | ||
</> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,19 @@ function render_block_core_post_title( $attributes, $content, $block ) { | |
return ''; | ||
} | ||
|
||
return '<h1>' . get_the_title( $block->context['postId'] ) . '</h1>'; | ||
$tag_name = 'h2'; | ||
$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 class="%2$s">%3$s</%1$s>', | ||
$tag_name, | ||
'wp-block-post-title' . esc_attr( $align_class_name ), | ||
get_the_title( $block->context['postId'] ) | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an aside or a potential follow-up: The "align" support flag automatically handles the addition of the className for static blocks (save functions), but it doesn't do so on the server. Now that we have server-side registration for most blocks, we could write some hooks to do this automatically. It involves some Regex work but I think it's doable. cc @gziolo (same for other hooks like the custom className...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduth had the logic for what you described in one of his PRs :) The challenge is that we would make sure that the client and server always stay in sync. It's not only "align" and "className", but also colors, fonts, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I have the logic here too #21420 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to supporting this, I'm looking to add color, font and line height support to the Site Title block and it needs server side support. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youknowriad I've opened the Site Title block PR that would need support on the server side for the above: #23007 What needs to be done to get that support in master? I'm very happy to help with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @apeatling I think right now the server side block registration already knows about the "supports" key (@gziolo confirm?), which means what's remaining is to write a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll give this one a go 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Started working on this here https://github.com/WordPress/gutenberg/pull/23007/files#diff-1334f2cb73e38f2f89d2aaf763f07ed9R307 |
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think instead of handling the alignment manually, we could use the "align" support flag? Other blocks do that like "button".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The support flag adds block level alignment using floats though doesn't it? Other heading blocks use text alignment, so it seems more appropriate to replicate that for the post title block and provide better continuity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we might want to add a support flag for text alignment later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, I misunderstood this, I thought it was adding the text alignment support. Makes me wonder if we should rename the attribute or not? Do we already have blocks that support both text and block alignments? How do we name these things there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's super confusing. It should be
textAlign
.