From 475324a3e47029719f73a1a661e74c5c0177acda Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 17 Nov 2022 15:44:57 +1100 Subject: [PATCH] LayoutPanel: Update to use ToolsPanel and ToolsPanelItems --- .../src/components/block-inspector/index.js | 8 + .../components/inspector-controls/groups.js | 2 + .../block-editor/src/hooks/content-layout.js | 70 ++++++++ packages/block-editor/src/hooks/layout.js | 160 +++++++++-------- packages/block-editor/src/hooks/layout.scss | 6 + .../block-editor/src/layouts/constrained.js | 167 +++++++++++------- packages/block-editor/src/layouts/flex.js | 89 ++++++++-- 7 files changed, 356 insertions(+), 146 deletions(-) create mode 100644 packages/block-editor/src/hooks/content-layout.js diff --git a/packages/block-editor/src/components/block-inspector/index.js b/packages/block-editor/src/components/block-inspector/index.js index 379d8806f2a68c..22f37a01337159 100644 --- a/packages/block-editor/src/components/block-inspector/index.js +++ b/packages/block-editor/src/components/block-inspector/index.js @@ -171,6 +171,10 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
+ {
) } + { const { getSettings } = select( blockEditorStore ); @@ -138,7 +141,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) { const layoutBlockSupport = getBlockSupport( blockName, - layoutBlockSupportKey, + LAYOUT_SUPPORT_KEY, {} ); const { @@ -192,67 +195,87 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) { const onChangeLayout = ( newLayout ) => setAttributes( { layout: newLayout } ); + const defaultControls = getBlockSupport( blockName, [ + LAYOUT_SUPPORT_KEY, + '__experimentalDefaultControls', + ] ); + 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 !== 'default' && ( - - ) } - { constrainedType && displayControlsForLegacyLayouts && ( - + { showInheritToggle && ( + hasContentLayoutValue( props ) } + onDeselect={ () => resetContentLayout( props ) } + isShownByDefault={ true } + resetAllFilter={ ( newAttributes ) => ( { + ...newAttributes, + layout: { + ...newAttributes.layout, + type: undefined, + }, + } ) } + panelId={ clientId } + > + + 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 !== 'default' && ( + + ) } + { constrainedType && displayControlsForLegacyLayouts && ( + + ) } { ! inherit && layoutType && ( ( props ) => { const { name: blockName } = props; - const supportLayout = hasBlockSupport( - blockName, - layoutBlockSupportKey - ); + const supportLayout = hasBlockSupport( blockName, LAYOUT_SUPPORT_KEY ); return [ supportLayout && , @@ -341,7 +361,7 @@ export const withLayoutStyles = createHigherOrderComponent( const { name, attributes, block } = props; const hasLayoutBlockSupport = hasBlockSupport( name, - layoutBlockSupportKey + LAYOUT_SUPPORT_KEY ); const disableLayoutStyles = useSelect( ( select ) => { const { getSettings } = select( blockEditorStore ); @@ -354,7 +374,7 @@ export const withLayoutStyles = createHigherOrderComponent( const element = useContext( BlockList.__unstableElementContext ); const { layout } = attributes; const { default: defaultBlockLayout } = - getBlockSupport( name, layoutBlockSupportKey ) || {}; + getBlockSupport( name, LAYOUT_SUPPORT_KEY ) || {}; const usedLayout = layout?.inherit || layout?.contentSize || layout?.wideSize ? { ...layout, type: 'constrained' } diff --git a/packages/block-editor/src/hooks/layout.scss b/packages/block-editor/src/hooks/layout.scss index 8ac39b1dec5f41..9491e6dbe86ddf 100644 --- a/packages/block-editor/src/hooks/layout.scss +++ b/packages/block-editor/src/hooks/layout.scss @@ -1,3 +1,9 @@ +.layout-block-support-panel { + .single-column { + grid-column: span 1; + } +} + .block-editor-hooks__layout-controls { display: flex; margin-bottom: $grid-unit-10; diff --git a/packages/block-editor/src/layouts/constrained.js b/packages/block-editor/src/layouts/constrained.js index 5e3b7eab405d89..6d3bf186b3c25a 100644 --- a/packages/block-editor/src/layouts/constrained.js +++ b/packages/block-editor/src/layouts/constrained.js @@ -6,6 +6,7 @@ import { __experimentalUnitControl as UnitControl, __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, + __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { @@ -30,6 +31,8 @@ export default { name: 'constrained', label: __( 'Constrained' ), inspectorControls: function DefaultLayoutInspectorControls( { + clientId, + defaultControls = {}, layout, onChange, } ) { @@ -68,69 +71,113 @@ export default { } ); return ( <> -
-
- { - nextWidth = - 0 > parseFloat( nextWidth ) - ? '0' - : nextWidth; - onChange( { - ...layout, - contentSize: nextWidth, - } ); - } } - units={ units } - /> - -
-
- { - nextWidth = - 0 > parseFloat( nextWidth ) - ? '0' - : nextWidth; - onChange( { - ...layout, - wideSize: nextWidth, - } ); - } } - units={ units } - /> - -
-
-

- { __( - 'Customize the width for all elements that are assigned to the center or wide columns.' - ) } -

- + wideSize !== undefined || contentSize !== undefined + } + label={ __( 'Content size' ) } + onDeselect={ () => + onChange( { + ...layout, + contentSize: undefined, + wideSize: undefined, + } ) + } + isShownByDefault={ + defaultControls?.contentSize || + defaultControls?.wideSize + } + resetAllFilter={ ( newAttributes ) => ( { + ...newAttributes, + layout: { + ...newAttributes?.layout, + contentSize: undefined, + wideSize: undefined, + }, + } ) } + panelId={ clientId } > - { justificationOptions.map( ( { value, icon, label } ) => { - return ( - +
+ { + nextWidth = + 0 > parseFloat( nextWidth ) + ? '0' + : nextWidth; + onChange( { + ...layout, + contentSize: nextWidth, + } ); + } } + units={ units } + /> + +
+
+ { + nextWidth = + 0 > parseFloat( nextWidth ) + ? '0' + : nextWidth; + onChange( { + ...layout, + wideSize: nextWidth, + } ); + } } + units={ units } /> - ); + +
+ +

+ { __( + 'Customize the width for all elements that are assigned to the center or wide columns.' + ) } +

+ + justifyContent !== undefined } + label={ __( 'Justification' ) } + onDeselect={ () => onJustificationChange( undefined ) } + isShownByDefault={ defaultControls?.justifyContent } + resetAllFilter={ ( newAttributes ) => ( { + ...newAttributes, + layout: { + ...newAttributes?.layout, + justifyContent: undefined, + }, } ) } -
+ panelId={ clientId } + > + + { justificationOptions.map( + ( { value, icon, label } ) => { + return ( + + ); + } + ) } + + ); }, diff --git a/packages/block-editor/src/layouts/flex.js b/packages/block-editor/src/layouts/flex.js index 948eceb6624d60..61aaab97d4b756 100644 --- a/packages/block-editor/src/layouts/flex.js +++ b/packages/block-editor/src/layouts/flex.js @@ -13,10 +13,9 @@ import { import { Button, ToggleControl, - Flex, - FlexItem, __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, + __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; /** @@ -58,6 +57,8 @@ export default { name: 'flex', label: __( 'Flex' ), inspectorControls: function FlexLayoutInspectorControls( { + clientId, + defaultControls, layout = {}, onChange, layoutBlockSupport = {}, @@ -65,23 +66,79 @@ export default { const { allowOrientation = true } = layoutBlockSupport; return ( <> - - - layout?.justifyContent } + label={ __( 'Justification' ) } + onDeselect={ () => + onChange( { + ...layout, + justifyContent: undefined, + } ) + } + isShownByDefault={ defaultControls?.justifyContent } + resetAllFilter={ ( newAttributes ) => ( { + ...newAttributes, + layout: { + ...newAttributes?.layout, + justifyContent: undefined, + }, + } ) } + panelId={ clientId } + > + + + { allowOrientation && ( + layout?.orientation } + label={ __( 'Orientation' ) } + onDeselect={ () => + onChange( { + ...layout, + orientation: undefined, + } ) + } + isShownByDefault={ defaultControls?.orientation } + resetAllFilter={ ( newAttributes ) => ( { + ...newAttributes, + layout: { + ...newAttributes?.layout, + orientation: undefined, + }, + } ) } + panelId={ clientId } + > + - - - { allowOrientation && ( - - ) } - - - + + ) } + layout?.flexWrap } + label={ __( 'Wrap' ) } + onDeselect={ () => + onChange( { + ...layout, + flexWrap: undefined, + } ) + } + isShownByDefault={ defaultControls?.flexWrap } + resetAllFilter={ ( newAttributes ) => ( { + ...newAttributes, + layout: { + ...newAttributes?.layout, + flexWrap: undefined, + }, + } ) } + panelId={ clientId } + > + + ); },