From b3fda94d33dabcdb45f59229ab05ed183695607d Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 1 May 2023 10:59:00 +1000 Subject: [PATCH] Selectively display controls only when needed --- packages/block-editor/src/hooks/layout.js | 395 +++++++++------------- 1 file changed, 169 insertions(+), 226 deletions(-) diff --git a/packages/block-editor/src/hooks/layout.js b/packages/block-editor/src/hooks/layout.js index c797375e487c04..922adb72b836f0 100644 --- a/packages/block-editor/src/hooks/layout.js +++ b/packages/block-editor/src/hooks/layout.js @@ -37,6 +37,7 @@ import { justifyCenter, justifyRight, justifySpaceBetween, + justifyStretch, } from '@wordpress/icons'; /** @@ -61,52 +62,6 @@ import { const layoutBlockSupportKey = '__experimentalLayout'; -const horizontalAlignmentOptions = [ - { - value: 'left', - icon: justifyLeft, - label: __( 'Left' ), - }, - { - value: 'center', - icon: justifyCenter, - label: __( 'Middle' ), - }, - { - value: 'right', - icon: justifyRight, - label: __( 'Right' ), - }, - { - value: 'space-between', - icon: justifySpaceBetween, - label: __( 'Space Between' ), - }, -]; - -const verticalAlignmentOptions = [ - { - value: 'top', - icon: alignTop, - label: __( 'Top' ), - }, - { - value: 'center', - icon: alignCenter, - label: __( 'Middle' ), - }, - { - value: 'bottom', - icon: alignBottom, - label: __( 'Bottom' ), - }, - { - value: 'space-between', - icon: spaceBetween, - label: __( 'Space Between' ), - }, -]; - const innerWidthOptions = [ { value: 'fill', @@ -355,18 +310,6 @@ function LayoutPanel( { return null; } - // Only show the inherit toggle if it's supported, - // a default theme layout is set (e.g. one that provides `contentSize` and/or `wideSize` values), - // and either the default / flow or the constrained layout type is in use, as the toggle switches from one to the other. - // const showInheritToggle = !! ( - // allowInheriting && - // !! defaultThemeLayout && - // ( ! layout?.type || - // layout?.type === 'default' || - // layout?.type === 'constrained' || - // layout?.inherit ) - // ); - const usedLayout = layout || defaultBlockLayout || {}; const { inherit = false, @@ -390,7 +333,64 @@ function LayoutPanel( { const constrainedType = getLayoutType( 'constrained' ); const displayControlsForLegacyLayouts = ! usedLayout.type && ( contentSize || inherit ); - // const hasContentSizeOrLegacySettings = !! inherit || !! contentSize; + + const horizontalAlignmentOptions = [ + { + value: 'left', + icon: justifyLeft, + label: __( 'Left' ), + }, + { + value: 'center', + icon: justifyCenter, + label: __( 'Middle' ), + }, + { + value: 'right', + icon: justifyRight, + label: __( 'Right' ), + }, + ]; + + if ( type === 'flex' ) { + horizontalAlignmentOptions.push( { + value: 'space-between', + icon: justifySpaceBetween, + label: __( 'Space Between' ), + } ); + } + + const verticalAlignmentOptions = [ + { + value: 'top', + icon: alignTop, + label: __( 'Top' ), + }, + { + value: 'center', + icon: alignCenter, + label: __( 'Middle' ), + }, + { + value: 'bottom', + icon: alignBottom, + label: __( 'Bottom' ), + }, + ]; + + if ( orientation === 'horizontal' ) { + verticalAlignmentOptions.push( { + value: 'stretch', + icon: justifyStretch, + label: __( 'Stretch' ), + } ); + } else { + verticalAlignmentOptions.push( { + value: 'space-between', + icon: spaceBetween, + label: __( 'Space Between' ), + } ); + } const onChangeType = ( newType ) => { if ( newType === 'stack' ) { @@ -442,7 +442,7 @@ function LayoutPanel( { } } else { setAttributes( { - layout: { ...usedLayout, innerWidth: key }, + layout: { ...usedLayout, type: 'default', innerWidth: key }, } ); } }; @@ -471,53 +471,12 @@ function LayoutPanel( { } ); }; + const defaultHorizontalAlign = type === 'constrained' ? 'center' : 'left'; + return ( <> - { /* { showInheritToggle && ( - <> - - setAttributes( { - layout: { - type: - layoutType?.name === - 'constrained' || - hasContentSizeOrLegacySettings - ? 'default' - : 'constrained', - }, - } ) - } - help={ - layoutType?.name === 'constrained' || - hasContentSizeOrLegacySettings - ? __( - 'Nested blocks use content width with options for full and wide widths.' - ) - : __( - 'Nested blocks will fill the width of this container. Toggle to constrain.' - ) - } - /> - - ) } */ } - - { /* { ! inherit && allowSwitching && ( - - ) } */ } - { layoutType && layoutType.name === 'grid' && ( + { type === 'grid' && ( ) } - - { - setAttributes( { - layout: { + { type === 'flex' && ( + + { + onChangeLayout( { ...usedLayout, verticalAlignment: selectedItem, - }, - } ); - } } - isBlock={ true } - className="components-toggle-group-control__full-width" - > - { verticalAlignmentOptions.map( - ( option ) => ( - - ) - ) } - - + } ); + } } + isBlock={ true } + className="components-toggle-group-control__full-width" + > + { verticalAlignmentOptions.map( + ( option ) => ( + + ) + ) } + + + ) } - { - setAttributes( { - layout: { - ...usedLayout, - justifyContent: selectedItem, - }, - } ); - } } - className="components-toggle-group-control__full-width" - > - { horizontalAlignmentOptions.map( - ( { value, icon, label } ) => ( - - ) - ) } - - - - - { innerWidthOptions.map( ( option ) => ( - - ) ) } - - { layoutType && - layoutType.name === 'grid' && - orientation === 'horizontal' && ( -
+ { ( type === 'flex' || + type === 'constrained' ) && ( { + onChangeLayout( { + ...usedLayout, + justifyContent: selectedItem, + } ); + } } + className="components-toggle-group-control__full-width" > - - + { horizontalAlignmentOptions.map( + ( { value, icon, label } ) => ( + + ) + ) } -
- ) } - { /* { layoutType && - layoutType.name !== 'default' && - layoutType.name !== 'constrained' && ( - - ) } */ } + ) } + + + { ( ( type === 'flex' && orientation === 'vertical' ) || + type === 'default' || + type === 'constrained' ) && ( + + { innerWidthOptions.map( ( option ) => ( + + ) ) } + + ) } + { type === 'flex' && orientation === 'horizontal' && ( +
+ + + + +
+ ) } { constrainedType && displayControlsForLegacyLayouts && ( ); } -// function LayoutTypeSwitcher( { type, onChange } ) { -// return ( -// -// { getLayoutTypes().map( ( { name, label } ) => { -// return ( -// -// ); -// } ) } -// -// ); -// } /** * Filters registered block settings, extending attributes to include `layout`.