-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Update: Allow blocks with supports align to have a default alignment #9338
Update: Allow blocks with supports align to have a default alignment #9338
Conversation
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.
LGTM 👍
packages/editor/src/hooks/align.js
Outdated
@@ -23,6 +23,9 @@ import { BlockControls, BlockAlignmentToolbar } from '../components'; | |||
* @return {Object} Filtered block settings | |||
*/ | |||
export function addAttribute( settings ) { | |||
if ( has( settings, [ 'attributes', 'align' ] ) ) { |
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.
Should we also check if it has a proper type
=== string
?
032c003
to
ddda54a
Compare
@@ -23,6 +23,10 @@ import { BlockControls, BlockAlignmentToolbar } from '../components'; | |||
* @return {Object} Filtered block settings | |||
*/ | |||
export function addAttribute( settings ) { | |||
// allow blocks to specify their own attribute definition with default values if needed. | |||
if ( get( settings.attributes, [ 'align', 'type' ] ) === 'string' ) { |
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.
Why do we specifically check the type? Would it be okay for us to override the attribute definition if it was a type other than string
? Would has( settings.attributes, [ 'align' ] )
not have been sufficient?
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.
We are checking the type because when using the align hook the hook is the one setting the align value and the hook sets a string, so specifying an attribute with a different type would not work correctly.
When setting supports align the expectation is that the hook will work properly so in this case for the hook to work properly we need to change the align attribute, I think it is acceptable.
Description
Supports align option allow blocks to have an alignment option (toolbar, attributes, and class names in the editor and save) with just one line. That is awesome to avoid duplicate code.
But if we need a block with supports align but also have a default alignment (e.g: right) that was not possible.
This Pr's makes sure blocks can use supports align and have a default alignment. To do that blocks have to provide their own align attribute definition with the default option set.
How has this been tested?
I used the following test block https://gist.github.com/jorgefilipecosta/d3cc0a9937ac1105e0a0537efd0e615b and checked the default align right is applied.