-
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
[RNMobile] Buttons block: Fix content justification attribute #37887
Conversation
The only hook currently supported by the native version is "core/layout/addAttribute", which extends block attributes to include "layout".
Size Change: 0 B Total Size: 1.13 MB ℹ️ View Unchanged
|
Additionally, the test case has been nested in a describe block.
const layoutBlockSupport = getBlockSupport( name, '__experimentalLayout' ); | ||
const defaultBlockLayout = layoutBlockSupport?.default; | ||
const usedLayout = layout || defaultBlockLayout || {}; |
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 is based on how the layout panel fetches the layout object to be used in the inspector controls (reference).
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.
Thank you for providing all of these explanations! 😃
// This filter is removed because layout styles shouldn't be added | ||
// until layout types are supported in the native version. | ||
removeFilter( | ||
'editor.BlockListBlock', | ||
'core/editor/layout/with-layout-styles' | ||
); |
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.
Here you can see the logic related to this hook:
gutenberg/packages/block-editor/src/hooks/layout.js
Lines 183 to 226 in 29ec9e0
/** | |
* Override the default block element to add the layout styles. | |
* | |
* @param {Function} BlockListBlock Original component. | |
* | |
* @return {Function} Wrapped component. | |
*/ | |
export const withLayoutStyles = createHigherOrderComponent( | |
( BlockListBlock ) => ( props ) => { | |
const { name, attributes } = props; | |
const shouldRenderLayoutStyles = hasBlockSupport( | |
name, | |
layoutBlockSupportKey | |
); | |
const id = useInstanceId( BlockListBlock ); | |
const defaultThemeLayout = useSetting( 'layout' ) || {}; | |
const element = useContext( BlockList.__unstableElementContext ); | |
const { layout } = attributes; | |
const { default: defaultBlockLayout } = | |
getBlockSupport( name, layoutBlockSupportKey ) || {}; | |
const usedLayout = layout?.inherit | |
? defaultThemeLayout | |
: layout || defaultBlockLayout || {}; | |
const className = classnames( props?.className, { | |
[ `wp-container-${ id }` ]: shouldRenderLayoutStyles, | |
} ); | |
return ( | |
<> | |
{ shouldRenderLayoutStyles && | |
element && | |
createPortal( | |
<LayoutStyle | |
selector={ `.wp-container-${ id }` } | |
layout={ usedLayout } | |
style={ attributes?.style } | |
/>, | |
element | |
) } | |
<BlockListBlock { ...props } className={ className } /> | |
</> | |
); | |
} | |
); |
// This filter is removed because the layout controls shouldn't be | ||
// enabled until layout types are supported in the native version. | ||
removeFilter( | ||
'editor.BlockEdit', | ||
'core/editor/layout/with-inspector-controls' | ||
); |
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.
Here you can see the logic related to this hook:
gutenberg/packages/block-editor/src/hooks/layout.js
Lines 160 to 181 in 29ec9e0
/** | |
* Override the default edit UI to include layout controls | |
* | |
* @param {Function} BlockEdit Original component. | |
* | |
* @return {Function} Wrapped component. | |
*/ | |
export const withInspectorControls = createHigherOrderComponent( | |
( BlockEdit ) => ( props ) => { | |
const { name: blockName } = props; | |
const supportLayout = hasBlockSupport( | |
blockName, | |
layoutBlockSupportKey | |
); | |
return [ | |
supportLayout && <LayoutPanel key="layout" { ...props } />, | |
<BlockEdit key="edit" { ...props } />, | |
]; | |
}, | |
'withInspectorControls' | |
); |
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.
In the Buttons block, regarding the justification content controls, instead of using the one provided by the layout logic:
gutenberg/packages/block-editor/src/layouts/flex.js
Lines 69 to 86 in 29ec9e0
toolBarControls: function FlexLayoutToolbarControls( { | |
layout = {}, | |
onChange, | |
layoutBlockSupport, | |
} ) { | |
if ( layoutBlockSupport?.allowSwitching ) { | |
return null; | |
} | |
return ( | |
<BlockControls group="block" __experimentalShareWithChildBlocks> | |
<FlexLayoutJustifyContentControl | |
layout={ layout } | |
onChange={ onChange } | |
isToolbar | |
/> | |
</BlockControls> | |
); | |
}, |
We define it within the block render:
gutenberg/packages/block-library/src/buttons/edit.native.js
Lines 126 to 138 in 29ec9e0
<BlockControls group="block"> | |
<JustifyContentControl | |
allowedControls={ justifyControls } | |
value={ contentJustification } | |
onChange={ ( value ) => | |
setAttributes( { contentJustification: value } ) | |
} | |
popoverProps={ { | |
position: 'bottom right', | |
isAlternate: true, | |
} } | |
/> | |
</BlockControls> |
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.
Not suggesting a change here, but I'm wondering whether there is an opportunity to extract some of what is in the web implementation. Certainly not a blocker for this PR, just thinking about a possibility where FlexLayoutJustifyContentControl
was extracted, if that would highlight where the platforms deviate. It may be that it isn't worth trying to merge components at that level, if it is mostly UI, but I'm thinking more for the sake of the logical parts of it, like the allowedControls
, etc. 🤔
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 though the FlexLayoutJustifyContentControl
component looks very coupled to the flex layout, which we don't have implemented support in the native version. In any case, I see that the only difference between the implementation of this control between Buttons block and flex layout is the support of space-between
option. Maybe if we add support for that option in the native version, we might consider enabling the layout inspector tools hook:
gutenberg/packages/block-editor/src/hooks/layout.js
Lines 238 to 242 in 43f9ad7
addFilter( | |
'editor.BlockEdit', | |
'core/editor/layout/with-inspector-controls', | |
withInspectorControls | |
); |
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 code looks good, and this resolves the issue as described. I tested this on a Pixel 3a (physical device). Nice work Carlos, I especially appreciate the snapshot tests to hopefully catch future cases!
Gutenberg Mobile PR: wordpress-mobile/gutenberg-mobile#4451
Description
In #35819 the attributes
contentJustification
andorientation
were removed from the Buttons block schema and replaced by the__experimentalLayout
attribute. However, the native version of the block wasn't updated and led to the content justification alignment option stops working.gutenberg/packages/block-library/src/buttons/edit.native.js
Line 33 in 43f9ad7
As outlined in the deprecation code for the Buttons block:
gutenberg/packages/block-library/src/buttons/deprecated.js
Lines 18 to 34 in 43f9ad7
These attributes are now provided within the
layout
attribute, however, for having access to this attribute first, we need to enable the layout hooks:gutenberg/packages/block-editor/src/hooks/layout.js
Lines 228 to 242 in 43f9ad7
Specifically the hook
core/layout/addAttribute
which is in charge of adding thelayout
attribute to the block:gutenberg/packages/block-editor/src/hooks/layout.js
Lines 137 to 158 in 43f9ad7
This PR imports the layout hooks for the native version, but it only enables the required hook by removing the rest, as the native version doesn't support yet the different layout types hence, we can't include either the layout styles or layout controls.
How has this been tested?
Manual test
Integration test
Additionally, test cases have been added to verify that the content justification attribute produces the expected HTML output using snapshot testing.
If the introduced test would have been run before the #35819 changes, we would have produced the following snapshot with the old attributes:
Click here to reveal the snapshot
So if the same test is run with current changes in
trunk
, it would have failed because the HTML output is different:Click here to reveal the snapshot
As a side note, the snapshot generated after applying this fix can be checked here.
Screenshots
buttons-block-justification-options.mp4
Types of changes
Bug fix
Checklist:
*.native.js
files for terms that need renaming or removal).