Skip to content

Commit 56152c6

Browse files
committed
Layout: Try adding a default fallback gap value in block.json
1 parent 78375e9 commit 56152c6

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
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', '__experimentalDefaultValues', 'blockGap' ), '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-library/src/columns/block.json

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
"padding": true,
3434
"__experimentalDefaultControls": {
3535
"padding": true
36+
},
37+
"__experimentalDefaultValues": {
38+
"blockGap": "2em"
3639
}
3740
},
3841
"__experimentalLayout": {

0 commit comments

Comments
 (0)