diff --git a/packages/block-editor/src/hooks/align.js b/packages/block-editor/src/hooks/align.js index 8d5c7f850e89b..4ab55521b4f55 100644 --- a/packages/block-editor/src/hooks/align.js +++ b/packages/block-editor/src/hooks/align.js @@ -6,7 +6,7 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { createHigherOrderComponent } from '@wordpress/compose'; +import { createHigherOrderComponent, ifCondition } from '@wordpress/compose'; import { addFilter } from '@wordpress/hooks'; import { getBlockSupport, @@ -109,6 +109,61 @@ export function addAttribute( settings ) { return settings; } +function AlignControls( props ) { + const { name: blockName } = props; + // Compute the block valid alignments by taking into account, + // if the theme supports wide alignments or not and the layout's + // availble alignments. We do that for conditionally rendering + // Slot. + const blockAllowedAlignments = getValidAlignments( + getBlockSupport( blockName, 'align' ), + hasBlockSupport( blockName, 'alignWide', true ) + ); + + const validAlignments = useAvailableAlignments( + blockAllowedAlignments + ).map( ( { name } ) => name ); + const isContentLocked = useSelect( + ( select ) => { + return select( blockEditorStore ).__unstableGetContentLockingParent( + props.clientId + ); + }, + [ props.clientId ] + ); + + if ( ! validAlignments.length || isContentLocked ) { + return null; + } + + const updateAlignment = ( nextAlign ) => { + if ( ! nextAlign ) { + const blockType = getBlockType( props.name ); + const blockDefaultAlign = blockType?.attributes?.align?.default; + if ( blockDefaultAlign ) { + nextAlign = ''; + } + } + props.setAttributes( { align: nextAlign } ); + }; + + return ( + + ); +} + +const ConditionalAlignControls = ifCondition( ( props ) => + hasBlockSupport( props.name, 'align' ) +)( ( props ) => ( + + + +) ); + /** * Override the default edit UI to include new toolbar controls for block * alignment, if block defines support. @@ -118,57 +173,13 @@ export function addAttribute( settings ) { * @return {Function} Wrapped component. */ export const withToolbarControls = createHigherOrderComponent( - ( BlockEdit ) => ( props ) => { - const blockEdit = ; - const { name: blockName } = props; - // Compute the block valid alignments by taking into account, - // if the theme supports wide alignments or not and the layout's - // availble alignments. We do that for conditionally rendering - // Slot. - const blockAllowedAlignments = getValidAlignments( - getBlockSupport( blockName, 'align' ), - hasBlockSupport( blockName, 'alignWide', true ) - ); - - const validAlignments = useAvailableAlignments( - blockAllowedAlignments - ).map( ( { name } ) => name ); - const isContentLocked = useSelect( - ( select ) => { - return select( - blockEditorStore - ).__unstableGetContentLockingParent( props.clientId ); - }, - [ props.clientId ] - ); - if ( ! validAlignments.length || isContentLocked ) { - return blockEdit; - } - - const updateAlignment = ( nextAlign ) => { - if ( ! nextAlign ) { - const blockType = getBlockType( props.name ); - const blockDefaultAlign = blockType?.attributes?.align?.default; - if ( blockDefaultAlign ) { - nextAlign = ''; - } - } - props.setAttributes( { align: nextAlign } ); - }; - - return ( + ( BlockEdit ) => ( props ) => + ( <> - - - - { blockEdit } + + - ); - }, + ), 'withToolbarControls' );