-
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
Add flex layout to Buttons block and new layout type. #35819
Changes from 7 commits
edb0d3d
191176d
f4acac2
f0127c7
9bf62af
ee74ef8
0105f2f
ce2ffed
45cbc76
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,9 +1,79 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* @param {Object} attributes Block's attributes. | ||
*/ | ||
const migrateWithLayout = ( attributes ) => { | ||
if ( !! attributes.layout ) { | ||
return attributes; | ||
} | ||
|
||
const { contentJustification, orientation } = attributes; | ||
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. We should remove these attributes here and keep only the rest. You can also see them appear in the fixtures. 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'm not sure I understand. These are used here to migrate any legacy settings to the new layout, e.g. if there's a deprecated block with "orientation" set we use that value in layout when we update the block. Or is there another way of doing it? In the fixtures these attributes only appear in the deprecated versions of Buttons. That should be expected, right? 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. Oh wait, you mean I should be removing them from the updated attributes? 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.
Yes. We extract them here and use them for the |
||
|
||
const updatedAttributes = { | ||
...attributes, | ||
}; | ||
|
||
if ( contentJustification || orientation ) { | ||
Object.assign( updatedAttributes, { | ||
layout: { | ||
type: 'flex', | ||
justifyContent: contentJustification || 'left', | ||
orientation: orientation || 'horizontal', | ||
}, | ||
} ); | ||
} | ||
|
||
return updatedAttributes; | ||
}; | ||
|
||
const deprecated = [ | ||
{ | ||
attributes: { | ||
contentJustification: { | ||
type: 'string', | ||
}, | ||
orientation: { | ||
type: 'string', | ||
default: 'horizontal', | ||
}, | ||
}, | ||
supports: { | ||
anchor: true, | ||
align: [ 'wide', 'full' ], | ||
__experimentalExposeControlsToChildren: true, | ||
spacing: { | ||
blockGap: true, | ||
margin: [ 'top', 'bottom' ], | ||
__experimentalDefaultControls: { | ||
blockGap: true, | ||
}, | ||
}, | ||
}, | ||
isEligible: ( { layout } ) => ! layout, | ||
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 don't think we should have You can check this by adding a different
Add 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. Oooh, interesting. How does it work in the Social Links deprecation? I can't remove 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. All feedback has now been addressed in #36192. Thanks for the review! 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.
Because:
So I didn't remove any attributes there. |
||
migrate: migrateWithLayout, | ||
save( { attributes: { contentJustification, orientation } } ) { | ||
return ( | ||
<div | ||
{ ...useBlockProps.save( { | ||
className: classnames( { | ||
[ `is-content-justification-${ contentJustification }` ]: contentJustification, | ||
'is-vertical': orientation === 'vertical', | ||
} ), | ||
} ) } | ||
> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}, | ||
}, | ||
{ | ||
supports: { | ||
align: [ 'center', 'left', 'right' ], | ||
|
@@ -20,7 +90,7 @@ const deprecated = [ | |
return align && [ 'center', 'left', 'right' ].includes( align ); | ||
}, | ||
migrate( attributes ) { | ||
return { | ||
return migrateWithLayout( { | ||
...attributes, | ||
align: undefined, | ||
// Floating Buttons blocks shouldn't have been supported in the | ||
|
@@ -30,7 +100,7 @@ const deprecated = [ | |
// As for center-aligned Buttons blocks, the content justification | ||
// equivalent will create an identical end result in most cases. | ||
contentJustification: 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.
This check is interesting. It basically means that if the current layout doesn't have any orientation, do not display the control to switch "orientation". That said one can argue that the "no orientation" is just equivalent to "horizontal orientation" meaning we could just always show the toggle.
The problem with always showing the toggle is that it will show up in other blocks using the "flex" layout like the "Group" block (or its Row variation and potentially others). While one could argue that this is wrong since is a "row" right, conceptually it's not wrong. For instance, what about blocks like "Social Icons" and "Query Pagination" also using the "flex" layout, should we allow them to switch orientation? It might make sense.
So the thing here is that I'm hesitant between:
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.
An opt-out flag would be nice, especially if there's only one block where we really don't want to enable vertical orientation. Perhaps something like
"allowOrientation": false
?It also occurred to me that if orientation were enabled for all blocks, we wouldn't need Row as a variation of Group at all. But Group is using flow layout; would there be any issues in migrating it to flex with vertical orientation?
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.
Well, a flexbox layout doesn't let children use
float
, so yes, there would definitely be issues if "flow" did not remain as an option.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.
Oooh good point. I guess we'd better leave it as it is then.