From 768a2bc836bca4f0b8cdd220ef3c8eb49fe53901 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 21 Oct 2021 10:34:42 +0300 Subject: [PATCH] [Block Editor]: Fix displaying only `none` alignment option --- .../use-available-alignments.js | 21 ++++++++++++++++--- packages/block-editor/src/hooks/align.js | 18 +++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/block-editor/src/components/block-alignment-control/use-available-alignments.js b/packages/block-editor/src/components/block-alignment-control/use-available-alignments.js index ad05b889a578c0..d1d32f82d6d277 100644 --- a/packages/block-editor/src/components/block-alignment-control/use-available-alignments.js +++ b/packages/block-editor/src/components/block-alignment-control/use-available-alignments.js @@ -16,7 +16,7 @@ const WIDE_CONTROLS = [ 'wide', 'full' ]; export default function useAvailableAlignments( controls = DEFAULT_CONTROLS ) { // Always add the `none` option if not exists. if ( ! controls.includes( 'none' ) ) { - controls.unshift( 'none' ); + controls = [ 'none', ...controls ]; } const { wideControlsEnabled = false, themeSupportsLayout } = useSelect( ( select ) => { @@ -34,9 +34,15 @@ export default function useAvailableAlignments( controls = DEFAULT_CONTROLS ) { const layoutAlignments = layoutType.getAlignments( layout ); if ( themeSupportsLayout ) { - return layoutAlignments.filter( ( { name: alignmentName } ) => - controls.includes( alignmentName ) + const alignments = layoutAlignments.filter( + ( { name: alignmentName } ) => controls.includes( alignmentName ) ); + // While we treat `none` as an alignment, we shouldn't return it if no + // other alignments exist. + if ( alignments.length === 1 && alignments[ 0 ].name === 'none' ) { + return []; + } + return alignments; } // Starting here, it's the fallback for themes not supporting the layout config. @@ -54,5 +60,14 @@ export default function useAvailableAlignments( controls = DEFAULT_CONTROLS ) { ) .map( ( enabledControl ) => ( { name: enabledControl } ) ); + // While we treat `none` as an alignment, we shouldn't return it if no + // other alignments exist. + if ( + enabledControls.length === 1 && + enabledControls[ 0 ].name === 'none' + ) { + return []; + } + return enabledControls; } diff --git a/packages/block-editor/src/hooks/align.js b/packages/block-editor/src/hooks/align.js index cdb953fa92171d..a4809d9af08b9c 100644 --- a/packages/block-editor/src/hooks/align.js +++ b/packages/block-editor/src/hooks/align.js @@ -63,7 +63,7 @@ export function getValidAlignments( ); } else if ( blockAlign === true ) { // `true` includes all alignments... - validAlignments = ALL_ALIGNMENTS; + validAlignments = [ ...ALL_ALIGNMENTS ]; } else { validAlignments = []; } @@ -117,15 +117,19 @@ export function addAttribute( settings ) { export const withToolbarControls = createHigherOrderComponent( ( BlockEdit ) => ( props ) => { const { name: blockName } = props; - // Compute the block allowed alignments without taking into account, - // if the theme supports wide alignments or not - // and without checking the layout for availble alignments. - // BlockAlignmentToolbar takes both of these into account. + // 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 updateAlignment = ( nextAlign ) => { if ( ! nextAlign ) { const blockType = getBlockType( props.name ); @@ -139,7 +143,7 @@ export const withToolbarControls = createHigherOrderComponent( return ( <> - { blockAllowedAlignments.length > 0 && ( + { !! validAlignments.length && ( ) }