Skip to content

Commit 6188537

Browse files
Editor: add grid layout type
Add a new `grid` type to the layout block support. Props andrewserong, costdev, ramonopoly. Fixes #58554. git-svn-id: https://develop.svn.wordpress.org/trunk@55975 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 04aeddc commit 6188537

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/wp-includes/block-supports/layout.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function wp_register_layout_support( $block_type ) {
3434
*
3535
* @since 5.9.0
3636
* @since 6.1.0 Added `$block_spacing` param, use style engine to enqueue styles.
37+
* @since 6.3.0 Added grid layout type.
3738
* @access private
3839
*
3940
* @param string $selector CSS selector.
@@ -287,6 +288,44 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
287288
);
288289
}
289290
}
291+
} elseif ( 'grid' === $layout_type ) {
292+
if ( ! empty( $layout['columnCount'] ) ) {
293+
$layout_styles[] = array(
294+
'selector' => $selector,
295+
'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
296+
);
297+
} else {
298+
$minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';
299+
300+
$layout_styles[] = array(
301+
'selector' => $selector,
302+
'declarations' => array( 'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))' ),
303+
);
304+
}
305+
306+
if ( $has_block_gap_support && isset( $gap_value ) ) {
307+
$combined_gap_value = '';
308+
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
309+
310+
foreach ( $gap_sides as $gap_side ) {
311+
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
312+
// Get spacing CSS variable from preset value if provided.
313+
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
314+
$index_to_splice = strrpos( $process_value, '|' ) + 1;
315+
$slug = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
316+
$process_value = "var(--wp--preset--spacing--$slug)";
317+
}
318+
$combined_gap_value .= "$process_value ";
319+
}
320+
$gap_value = trim( $combined_gap_value );
321+
322+
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
323+
$layout_styles[] = array(
324+
'selector' => $selector,
325+
'declarations' => array( 'gap' => $gap_value ),
326+
);
327+
}
328+
}
290329
}
291330

292331
if ( ! empty( $layout_styles ) ) {

src/wp-includes/class-wp-theme-json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ protected function get_layout_styles( $block_metadata ) {
13701370

13711371
if (
13721372
! empty( $class_name ) &&
1373-
! empty( $base_style_rules )
1373+
is_array( $base_style_rules )
13741374
) {
13751375
// Output display mode. This requires special handling as `display` is not exposed in `safe_style_css_filter`.
13761376
if (

src/wp-includes/theme.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,28 @@
332332
}
333333
}
334334
]
335+
},
336+
"grid": {
337+
"name": "grid",
338+
"slug": "grid",
339+
"className": "is-layout-grid",
340+
"displayMode": "grid",
341+
"baseStyles": [
342+
{
343+
"selector": " > *",
344+
"rules": {
345+
"margin": "0"
346+
}
347+
}
348+
],
349+
"spacingStyles": [
350+
{
351+
"selector": "",
352+
"rules": {
353+
"gap": null
354+
}
355+
}
356+
]
335357
}
336358
}
337359
},

0 commit comments

Comments
 (0)