-
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
One hook to rule them all: preparation for a block supports API #56862
Changes from all commits
f13bd13
35242d7
575fac5
70d34c4
4ab0f93
e629659
738c07a
aa5e8f5
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ import classnames from 'classnames'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createHigherOrderComponent, pure } from '@wordpress/compose'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { | ||
getBlockSupport, | ||
|
@@ -109,7 +109,7 @@ export function addAttribute( settings ) { | |
} | ||
|
||
function BlockEditAlignmentToolbarControlsPure( { | ||
blockName, | ||
name: blockName, | ||
align, | ||
setAttributes, | ||
} ) { | ||
|
@@ -152,45 +152,14 @@ function BlockEditAlignmentToolbarControlsPure( { | |
); | ||
} | ||
|
||
// We don't want block controls to re-render when typing inside a block. `pure` | ||
// will prevent re-renders unless props change, so only pass the needed props | ||
// and not the whole attributes object. | ||
const BlockEditAlignmentToolbarControls = pure( | ||
BlockEditAlignmentToolbarControlsPure | ||
); | ||
|
||
/** | ||
* Override the default edit UI to include new toolbar controls for block | ||
* alignment, if block defines support. | ||
* | ||
* @param {Function} BlockEdit Original component. | ||
* | ||
* @return {Function} Wrapped component. | ||
*/ | ||
export const withAlignmentControls = createHigherOrderComponent( | ||
( BlockEdit ) => ( props ) => { | ||
const hasAlignmentSupport = hasBlockSupport( | ||
props.name, | ||
'align', | ||
false | ||
); | ||
|
||
return ( | ||
<> | ||
{ hasAlignmentSupport && ( | ||
<BlockEditAlignmentToolbarControls | ||
blockName={ props.name } | ||
// This component is pure, so only pass needed props! | ||
align={ props.attributes.align } | ||
setAttributes={ props.setAttributes } | ||
/> | ||
) } | ||
<BlockEdit key="edit" { ...props } /> | ||
</> | ||
); | ||
export default { | ||
shareWithChildBlocks: true, | ||
edit: BlockEditAlignmentToolbarControlsPure, | ||
attributeKeys: [ 'align' ], | ||
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'm using this to "subscribe" to specific attributes only. But on the other hand, extra 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. And we can decide this later when we actually make the real API. 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, this is totally fine for me and to be honest, I'm not entirely sold on opening this API to the public yet but it's definitely a great improvement for us. 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 sure, no public API for now :D |
||
hasSupport( name ) { | ||
return hasBlockSupport( name, 'align', false ); | ||
}, | ||
'withAlignmentControls' | ||
); | ||
}; | ||
|
||
function BlockListBlockWithDataAlign( { block: BlockListBlock, props } ) { | ||
const { name, attributes } = props; | ||
|
@@ -273,11 +242,6 @@ addFilter( | |
'core/editor/align/with-data-align', | ||
withDataAlign | ||
); | ||
addFilter( | ||
'editor.BlockEdit', | ||
'core/editor/align/with-toolbar-controls', | ||
withAlignmentControls | ||
); | ||
addFilter( | ||
'blocks.getSaveContent.extraProps', | ||
'core/editor/align/addAssignedAlign', | ||
|
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.
I think I'd have preferred to leave this one to its own PR, to have a clear picture of impact.
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.
It's needed, otherwise the controls are not rendered. For example for a button block, the align controls from the buttons blocks should be rendered.