Skip to content

Commit 51db4bf

Browse files
Layout: Add default fallback gap value in block.json, and set to 2em for Columns blocks (#41098)
* Layout: Try adding a default fallback gap value in block.json * Implement in the editor, too * Try moving the default value to sit under the blockGap key in spacing, updated useCustomSides to support an object * Update doc comment * Add explanatory comments * Add __experimental prefix to default property. Co-authored-by: Adam Zielinski <adam@adamziel.com> Co-authored-by: Adam Zielinski <adam@adamziel.com>
1 parent 0da4e25 commit 51db4bf

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

lib/block-supports/layout.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ function gutenberg_register_layout_support( $block_type ) {
3333
* @param boolean $has_block_gap_support Whether the theme has support for the block gap.
3434
* @param string $gap_value The block gap value to apply.
3535
* @param boolean $should_skip_gap_serialization Whether to skip applying the user-defined value set in the editor.
36+
* @param string $fallback_gap_value The block gap value to apply.
3637
*
3738
* @return string CSS style.
3839
*/
39-
function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false ) {
40+
function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em' ) {
4041
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
4142

4243
$style = '';
@@ -102,14 +103,14 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
102103
$style .= 'display: flex;';
103104
if ( $has_block_gap_support ) {
104105
if ( is_array( $gap_value ) ) {
105-
$gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : '0.5em';
106-
$gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : '0.5em';
106+
$gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap_value;
107+
$gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap_value;
107108
$gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
108109
}
109-
$gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : 'var( --wp--style--block-gap, 0.5em )';
110+
$gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : "var( --wp--style--block-gap, $fallback_gap_value )";
110111
$style .= "gap: $gap_style;";
111112
} else {
112-
$style .= 'gap: 0.5em;';
113+
$style .= "gap: $fallback_gap_value;";
113114
}
114115

115116
$style .= "flex-wrap: $flex_wrap;";
@@ -184,10 +185,12 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
184185
$gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
185186
}
186187

188+
$fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' );
189+
187190
// If a block's block.json skips serialization for spacing or spacing.blockGap,
188191
// don't apply the user-defined value to the styles.
189192
$should_skip_gap_serialization = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
190-
$style = gutenberg_get_layout_style( ".$class_name", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization );
193+
$style = gutenberg_get_layout_style( ".$class_name", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value );
191194
// This assumes the hook only applies to blocks with a single wrapper.
192195
// I think this is a reasonable limitation for that particular hook.
193196
$content = preg_replace(

packages/block-editor/src/hooks/dimensions.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ const useIsDimensionsDisabled = ( props = {} ) => {
153153
};
154154

155155
/**
156-
* Custom hook to retrieve which padding/margin is supported
156+
* Custom hook to retrieve which padding/margin/blockGap is supported
157157
* e.g. top, right, bottom or left.
158158
*
159159
* Sides are opted into by default. It is only if a specific side is set to
@@ -162,7 +162,7 @@ const useIsDimensionsDisabled = ( props = {} ) => {
162162
* @param {string} blockName Block name.
163163
* @param {string} feature The feature custom sides relate to e.g. padding or margins.
164164
*
165-
* @return {Object} Sides supporting custom margin.
165+
* @return {?string[]} Strings representing the custom sides available.
166166
*/
167167
export function useCustomSides( blockName, feature ) {
168168
const support = getBlockSupport( blockName, SPACING_SUPPORT_KEY );
@@ -172,7 +172,15 @@ export function useCustomSides( blockName, feature ) {
172172
return;
173173
}
174174

175-
return support[ feature ];
175+
// Return if the setting is an array of sides (e.g. `[ 'top', 'bottom' ]`).
176+
if ( Array.isArray( support[ feature ] ) ) {
177+
return support[ feature ];
178+
}
179+
180+
// Finally, attempt to return `.sides` if the setting is an object.
181+
if ( support[ feature ]?.sides ) {
182+
return support[ feature ].sides;
183+
}
176184
}
177185

178186
/**

packages/block-editor/src/layouts/flex.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
arrowDown,
1212
} from '@wordpress/icons';
1313
import { Button, ToggleControl, Flex, FlexItem } from '@wordpress/components';
14+
import { getBlockSupport } from '@wordpress/blocks';
1415

1516
/**
1617
* Internal dependencies
@@ -109,14 +110,21 @@ export default {
109110
save: function FlexLayoutStyle( { selector, layout, style, blockName } ) {
110111
const { orientation = 'horizontal' } = layout;
111112
const blockGapSupport = useSetting( 'spacing.blockGap' );
113+
const fallbackValue =
114+
getBlockSupport( blockName, [
115+
'spacing',
116+
'blockGap',
117+
'__experimentalDefault',
118+
] ) || '0.5em';
119+
112120
const hasBlockGapStylesSupport = blockGapSupport !== null;
113121
// If a block's block.json skips serialization for spacing or spacing.blockGap,
114122
// don't apply the user-defined value to the styles.
115123
const blockGapValue =
116124
style?.spacing?.blockGap &&
117125
! shouldSkipSerialization( blockName, 'spacing', 'blockGap' )
118-
? getGapCSSValue( style?.spacing?.blockGap, '0.5em' )
119-
: 'var( --wp--style--block-gap, 0.5em )';
126+
? getGapCSSValue( style?.spacing?.blockGap, fallbackValue )
127+
: `var( --wp--style--block-gap, ${ fallbackValue } )`;
120128
const justifyContent =
121129
justifyContentMap[ layout.justifyContent ] ||
122130
justifyContentMap.left;
@@ -143,7 +151,7 @@ export default {
143151
${ appendSelectors( selector ) } {
144152
display: flex;
145153
flex-wrap: ${ flexWrap };
146-
gap: ${ hasBlockGapStylesSupport ? blockGapValue : '0.5em' };
154+
gap: ${ hasBlockGapStylesSupport ? blockGapValue : fallbackValue };
147155
${ orientation === 'horizontal' ? rowOrientation : columnOrientation }
148156
}
149157

packages/block-library/src/columns/block.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
}
2929
},
3030
"spacing": {
31-
"blockGap": true,
31+
"blockGap": {
32+
"__experimentalDefault": "2em"
33+
},
3234
"margin": [ "top", "bottom" ],
3335
"padding": true,
3436
"__experimentalDefaultControls": {

0 commit comments

Comments
 (0)