@@ -497,13 +497,19 @@ function wp_register_layout_support( $block_type ) {
497497 * @return string CSS styles on success. Else, empty string.
498498 */
499499function wp_get_layout_style ( $ selector , $ layout , $ has_block_gap_support = false , $ gap_value = null , $ should_skip_gap_serialization = false , $ fallback_gap_value = '0.5em ' , $ block_spacing = null , $ options = array () ) {
500- $ base_layout = is_array ( $ layout ) ? $ layout : array ();
501- $ viewport_overrides = $ options ['viewport_overrides ' ] ?? null ;
502- $ layout_for_styles = null === $ viewport_overrides ? $ base_layout : array_replace ( $ base_layout , $ viewport_overrides );
503- $ layout_type = $ base_layout ['type ' ] ?? 'default ' ;
504- $ rules_group = $ options ['rules_group ' ] ?? null ;
505- $ has_block_gap_override = ! empty ( $ options ['has_block_gap_override ' ] );
506- $ should_output_block_gap = null === $ viewport_overrides || $ has_block_gap_override ;
500+ $ base_layout = is_array ( $ layout ) ? $ layout : array ();
501+ $ viewport_overrides = $ options ['viewport_overrides ' ] ?? null ;
502+ $ layout_for_styles = null === $ viewport_overrides ? $ base_layout : array_replace ( $ base_layout , $ viewport_overrides );
503+ $ layout_type = $ base_layout ['type ' ] ?? 'default ' ;
504+ $ rules_group = $ options ['rules_group ' ] ?? null ;
505+ $ has_block_gap_override = ! empty ( $ options ['has_block_gap_override ' ] );
506+ $ should_output_block_gap = null === $ viewport_overrides || $ has_block_gap_override ;
507+
508+ /*
509+ * Viewport styles only store changed fields. If a field is present with null,
510+ * the user cleared a value inherited from the default viewport, so check
511+ * whether the key exists rather than whether the value is truthy.
512+ */
507513 $ has_viewport_property_override = static function ( $ property ) use ( $ viewport_overrides ) {
508514 return array_key_exists ( $ property , $ viewport_overrides );
509515 };
@@ -546,8 +552,29 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
546552 $ wide_size = $ layout_for_styles ['wideSize ' ] ?? '' ;
547553 $ justify_content = $ layout_for_styles ['justifyContent ' ] ?? 'center ' ;
548554
549- $ all_max_width_value = $ content_size ? $ content_size : $ wide_size ;
550- $ wide_max_width_value = $ wide_size ? $ wide_size : $ content_size ;
555+ // Check if viewport-specific ("override") values exist. Null values are valid and mean the user cleared a value inherited from the default viewport.
556+ $ has_justify_content_override = null !== $ viewport_overrides && $ has_viewport_property_override ( 'justifyContent ' );
557+ $ has_content_size_override = null !== $ viewport_overrides && $ has_viewport_property_override ( 'contentSize ' );
558+ $ has_wide_size_override = null !== $ viewport_overrides && $ has_viewport_property_override ( 'wideSize ' );
559+
560+ /*
561+ * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
562+ * value for contentSize or wideSize. If a viewport clears a custom constrained size, reset to the global layout variable.
563+ */
564+ $ should_output_constrained_sizes = null === $ viewport_overrides || $ has_content_size_override || $ has_wide_size_override ;
565+ $ is_resetting_constrained_sizes = null !== $ viewport_overrides &&
566+ (
567+ ( $ has_content_size_override && ! $ content_size ) ||
568+ ( $ has_wide_size_override && ! $ wide_size )
569+ );
570+
571+ // If a viewport clears a custom constrained size, reset to the global layout variable.
572+ $ all_max_width_value = $ content_size
573+ ? $ content_size
574+ : ( $ wide_size && ! $ has_content_size_override ? $ wide_size : 'var(--wp--style--global--content-size, none) ' );
575+ $ wide_max_width_value = $ wide_size
576+ ? $ wide_size
577+ : ( $ content_size && ! $ has_wide_size_override ? $ content_size : 'var(--wp--style--global--wide-size, none) ' );
551578
552579 // Make sure there is a single CSS rule, and all tags are stripped for security.
553580 $ all_max_width_value = safecss_filter_attr ( explode ( '; ' , $ all_max_width_value )[0 ] );
@@ -556,9 +583,7 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
556583 $ margin_left = 'left ' === $ justify_content ? '0 !important ' : 'auto !important ' ;
557584 $ margin_right = 'right ' === $ justify_content ? '0 !important ' : 'auto !important ' ;
558585
559- $ has_justify_content_override = null !== $ viewport_overrides && $ has_viewport_property_override ( 'justifyContent ' );
560- $ should_output_constrained_sizes = null === $ viewport_overrides || $ has_viewport_property_override ( 'contentSize ' ) || $ has_viewport_property_override ( 'wideSize ' );
561- if ( $ should_output_constrained_sizes && ( $ content_size || $ wide_size ) ) {
586+ if ( $ should_output_constrained_sizes && ( $ content_size || $ wide_size || $ is_resetting_constrained_sizes ) ) {
562587 $ content_size_declarations = array (
563588 'max-width ' => $ all_max_width_value ,
564589 );
@@ -698,6 +723,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
698723 $ vertical_alignment_options += array ( 'space-between ' => 'space-between ' );
699724 }
700725
726+ /*
727+ * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
728+ * value for any of the flex properties.
729+ */
701730 $ should_output_flex_wrap = null === $ viewport_overrides || $ has_viewport_property_override ( 'flexWrap ' );
702731 $ should_output_flex_orientation = null === $ viewport_overrides || $ has_viewport_property_override ( 'orientation ' );
703732 $ should_output_flex_justification = null === $ viewport_overrides || $ has_viewport_property_override ( 'justifyContent ' ) || $ has_viewport_property_override ( 'orientation ' );
@@ -828,6 +857,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
828857 $ responsive_gap_value = '0px ' ;
829858 }
830859
860+ /*
861+ * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
862+ * value for any of the grid properties.
863+ */
831864 $ should_output_grid_columns = null === $ viewport_overrides || $ has_viewport_property_override ( 'minimumColumnWidth ' ) || $ has_viewport_property_override ( 'columnCount ' ) || $ has_viewport_property_override ( 'autoFit ' );
832865 $ uses_gap_in_grid_columns = ! empty ( $ layout_for_styles ['columnCount ' ] ) && ! empty ( $ layout_for_styles ['minimumColumnWidth ' ] );
833866 if ( $ has_block_gap_override && $ uses_gap_in_grid_columns ) {
@@ -837,8 +870,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
837870 $ should_output_grid_rows = ( null === $ viewport_overrides || $ has_viewport_property_override ( 'rowCount ' ) ) && ! empty ( $ layout_for_styles ['columnCount ' ] ) && ! empty ( $ layout_for_styles ['rowCount ' ] );
838871 $ grid_declarations = array ();
839872
840- // When enabled, columns stretch to fill the available space using
841- // `auto-fit`; otherwise empty tracks are preserved with `auto-fill`.
873+ /*
874+ * When enabled, columns stretch to fill the available space using
875+ * `auto-fit`; otherwise empty tracks are preserved with `auto-fill`.
876+ */
842877 $ auto_placement = ! empty ( $ layout_for_styles ['autoFit ' ] ) ? 'auto-fit ' : 'auto-fill ' ;
843878
844879 if ( $ should_output_grid_columns && ! empty ( $ layout_for_styles ['columnCount ' ] ) && ! empty ( $ layout_for_styles ['minimumColumnWidth ' ] ) ) {
0 commit comments