Make WordPress Core


Ignore:
Timestamp:
01/23/2026 01:54:48 AM (4 months ago)
Author:
isabel_brison
Message:

Editor: Grid block responsive enhancements.

Adds styles for responsive grid layouts and fixes a block gap bug and a max column width bug.

Props isabel_brison, aaronrobertshaw.
Fixes #64532.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/layout.php

    r61430 r61513  
    234234 * @param string|string[]|null $gap_value                     Optional. The block gap value to apply. Default null.
    235235 * @param bool                 $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
    236  * @param string               $fallback_gap_value            Optional. The block gap value to apply. Default '0.5em'.
     236 * @param string|array         $fallback_gap_value            Optional. The block gap value to apply. If it's an array expected properties are "top" and/or "left". Default '0.5em'.
    237237 * @param array|null           $block_spacing                 Optional. Custom spacing set on the block. Default null.
    238238 * @return string CSS styles on success. Else, empty string.
     
    428428                $process_value = $gap_value;
    429429                if ( is_array( $gap_value ) ) {
    430                     $process_value = $gap_value[ $gap_side ] ?? $fallback_gap_value;
     430                    if ( is_array( $fallback_gap_value ) ) {
     431                        $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
     432                    } else {
     433                        $fallback_value = $fallback_gap_value;
     434                    }
     435                    $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
    431436                }
    432437                // Get spacing CSS variable from preset value if provided.
     
    491496        }
    492497    } elseif ( 'grid' === $layout_type ) {
    493         if ( ! empty( $layout['columnCount'] ) ) {
    494             $layout_styles[] = array(
    495                 'selector'     => $selector,
    496                 'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
    497             );
     498        /*
     499         * If the gap value is an array, we use the "left" value because it represents the vertical gap, which
     500         * is the relevant one for computation of responsive grid columns.
     501         */
     502        if ( is_array( $fallback_gap_value ) ) {
     503            $responsive_gap_value = $fallback_gap_value['left'] ?? reset( $fallback_gap_value );
    498504        } else {
    499             $minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';
    500 
    501             $layout_styles[] = array(
    502                 'selector'     => $selector,
    503                 'declarations' => array(
    504                     'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))',
    505                     'container-type'        => 'inline-size',
    506                 ),
    507             );
     505            $responsive_gap_value = $fallback_gap_value;
    508506        }
    509507
     
    515513                $process_value = $gap_value;
    516514                if ( is_array( $gap_value ) ) {
    517                     $process_value = $gap_value[ $gap_side ] ?? $fallback_gap_value;
     515                    if ( is_array( $fallback_gap_value ) ) {
     516                        $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
     517                    } else {
     518                        $fallback_value = $fallback_gap_value;
     519                    }
     520                    $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
    518521                }
    519522                // Get spacing CSS variable from preset value if provided.
     
    525528                $combined_gap_value .= "$process_value ";
    526529            }
    527             $gap_value = trim( $combined_gap_value );
    528 
    529             if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
     530            $gap_value            = trim( $combined_gap_value );
     531            $responsive_gap_value = $gap_value;
     532        }
     533
     534        // Ensure 0 values have a unit so they work in calc().
     535        if ( '0' === $responsive_gap_value || 0 === $responsive_gap_value ) {
     536            $responsive_gap_value = '0px';
     537        }
     538
     539        if ( ! empty( $layout['columnCount'] ) && ! empty( $layout['minimumColumnWidth'] ) ) {
     540            $max_value       = 'max(min(' . $layout['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout['columnCount'] . ' - 1))) /' . $layout['columnCount'] . ')';
     541            $layout_styles[] = array(
     542                'selector'     => $selector,
     543                'declarations' => array(
     544                    'grid-template-columns' => 'repeat(auto-fill, minmax(' . $max_value . ', 1fr))',
     545                    'container-type'        => 'inline-size',
     546                ),
     547            );
     548            if ( ! empty( $layout['rowCount'] ) ) {
    530549                $layout_styles[] = array(
    531550                    'selector'     => $selector,
    532                     'declarations' => array( 'gap' => $gap_value ),
     551                    'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(1rem, auto))' ),
    533552                );
    534553            }
     554        } elseif ( ! empty( $layout['columnCount'] ) ) {
     555            $layout_styles[] = array(
     556                'selector'     => $selector,
     557                'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
     558            );
     559            if ( ! empty( $layout['rowCount'] ) ) {
     560                $layout_styles[] = array(
     561                    'selector'     => $selector,
     562                    'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(1rem, auto))' ),
     563                );
     564            }
     565        } else {
     566            $minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';
     567
     568            $layout_styles[] = array(
     569                'selector'     => $selector,
     570                'declarations' => array(
     571                    'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))',
     572                    'container-type'        => 'inline-size',
     573                ),
     574            );
     575        }
     576
     577        if ( $has_block_gap_support && null !== $gap_value && ! $should_skip_gap_serialization ) {
     578            $layout_styles[] = array(
     579                'selector'     => $selector,
     580                'declarations' => array( 'gap' => $gap_value ),
     581            );
    535582        }
    536583    }
     
    569616 */
    570617function wp_render_layout_support_flag( $block_content, $block ) {
     618    static $global_styles = null;
     619
    571620    $block_type            = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    572621    $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
     
    805854        $has_block_gap_support = isset( $block_gap );
    806855
     856        // Get default blockGap value from global styles for use in layouts like grid.
     857        // Check block-specific styles first, then fall back to root styles.
     858        $block_name = $block['blockName'] ?? '';
     859        if ( null === $global_styles ) {
     860            $global_styles = wp_get_global_styles();
     861        }
     862        $global_block_gap_value = $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? ( $global_styles['spacing']['blockGap'] ?? null );
     863
     864        if ( null !== $global_block_gap_value ) {
     865            $fallback_gap_value = $global_block_gap_value;
     866        }
     867
    807868        /*
    808869         * Generates a unique ID based on all the data required to obtain the
Note: See TracChangeset for help on using the changeset viewer.