@@ -38,6 +38,14 @@ class WP_Theme_JSON {
38
38
*/
39
39
protected static $ blocks_metadata = array ();
40
40
41
+ /**
42
+ * The CSS selector for the top-level preset settings.
43
+ *
44
+ * @since 6.6.0
45
+ * @var string
46
+ */
47
+ const ROOT_CSS_PROPERTIES_SELECTOR = ':root ' ;
48
+
41
49
/**
42
50
* The CSS selector for the top-level styles.
43
51
*
@@ -1708,14 +1716,15 @@ static function ( $carry, $element ) {
1708
1716
*
1709
1717
* @since 5.8.0
1710
1718
* @since 5.9.0 Added the `$origins` parameter.
1719
+ * @since 6.6.0 Added check for root CSS properties selector.
1711
1720
*
1712
1721
* @param array $settings Settings to process.
1713
1722
* @param string $selector Selector wrapping the classes.
1714
1723
* @param string[] $origins List of origins to process.
1715
1724
* @return string The result of processing the presets.
1716
1725
*/
1717
1726
protected static function compute_preset_classes ( $ settings , $ selector , $ origins ) {
1718
- if ( static ::ROOT_BLOCK_SELECTOR === $ selector ) {
1727
+ if ( static ::ROOT_BLOCK_SELECTOR === $ selector || static :: ROOT_CSS_PROPERTIES_SELECTOR === $ selector ) {
1719
1728
/*
1720
1729
* Classes at the global level do not need any CSS prefixed,
1721
1730
* and we don't want to increase its specificity.
@@ -2233,7 +2242,7 @@ protected static function get_setting_nodes( $theme_json, $selectors = array() )
2233
2242
// Top-level.
2234
2243
$ nodes [] = array (
2235
2244
'path ' => array ( 'settings ' ),
2236
- 'selector ' => static ::ROOT_BLOCK_SELECTOR ,
2245
+ 'selector ' => static ::ROOT_CSS_PROPERTIES_SELECTOR ,
2237
2246
);
2238
2247
2239
2248
// Calculate paths for blocks.
@@ -2614,6 +2623,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
2614
2623
* Outputs the CSS for layout rules on the root.
2615
2624
*
2616
2625
* @since 6.1.0
2626
+ * @since 6.6.0 Use `ROOT_CSS_PROPERTIES_SELECTOR` for CSS custom properties.
2617
2627
*
2618
2628
* @param string $selector The root node selector.
2619
2629
* @param array $block_metadata The metadata for the root block.
@@ -2624,16 +2634,6 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
2624
2634
$ settings = isset ( $ this ->theme_json ['settings ' ] ) ? $ this ->theme_json ['settings ' ] : array ();
2625
2635
$ use_root_padding = isset ( $ this ->theme_json ['settings ' ]['useRootPaddingAwareAlignments ' ] ) && true === $ this ->theme_json ['settings ' ]['useRootPaddingAwareAlignments ' ];
2626
2636
2627
- /*
2628
- * Reset default browser margin on the root body element.
2629
- * This is set on the root selector **before** generating the ruleset
2630
- * from the `theme.json`. This is to ensure that if the `theme.json` declares
2631
- * `margin` in its `spacing` declaration for the `body` element then these
2632
- * user-generated values take precedence in the CSS cascade.
2633
- * @link https://github.com/WordPress/gutenberg/issues/36147.
2634
- */
2635
- $ css .= 'body { margin: 0; ' ;
2636
-
2637
2637
/*
2638
2638
* If there are content and wide widths in theme.json, output them
2639
2639
* as custom properties on the body element so all blocks can use them.
@@ -2643,11 +2643,19 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
2643
2643
$ content_size = static ::is_safe_css_declaration ( 'max-width ' , $ content_size ) ? $ content_size : 'initial ' ;
2644
2644
$ wide_size = isset ( $ settings ['layout ' ]['wideSize ' ] ) ? $ settings ['layout ' ]['wideSize ' ] : $ settings ['layout ' ]['contentSize ' ];
2645
2645
$ wide_size = static ::is_safe_css_declaration ( 'max-width ' , $ wide_size ) ? $ wide_size : 'initial ' ;
2646
- $ css .= ' --wp--style--global--content-size: ' . $ content_size . '; ' ;
2647
- $ css .= '--wp--style--global--wide-size: ' . $ wide_size . '; ' ;
2646
+ $ css .= static :: ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $ content_size . '; ' ;
2647
+ $ css .= '--wp--style--global--wide-size: ' . $ wide_size . '; } ' ;
2648
2648
}
2649
2649
2650
- $ css .= ' } ' ;
2650
+ /*
2651
+ * Reset default browser margin on the body element.
2652
+ * This is set on the body selector **before** generating the ruleset
2653
+ * from the `theme.json`. This is to ensure that if the `theme.json` declares
2654
+ * `margin` in its `spacing` declaration for the `body` element then these
2655
+ * user-generated values take precedence in the CSS cascade.
2656
+ * @link https://github.com/WordPress/gutenberg/issues/36147.
2657
+ */
2658
+ $ css .= 'body { margin: 0; } ' ;
2651
2659
2652
2660
if ( $ use_root_padding ) {
2653
2661
// Top and bottom padding are applied to the outer block container.
@@ -2670,16 +2678,15 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
2670
2678
$ css .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; } ' ;
2671
2679
$ css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; } ' ;
2672
2680
2673
- $ block_gap_value = isset ( $ this ->theme_json ['styles ' ]['spacing ' ]['blockGap ' ] ) ? $ this ->theme_json ['styles ' ]['spacing ' ]['blockGap ' ] : '0.5em ' ;
2674
- $ has_block_gap_support = isset ( $ this ->theme_json ['settings ' ]['spacing ' ]['blockGap ' ] );
2675
- if ( $ has_block_gap_support ) {
2681
+ // Block gap styles will be output unless explicitly set to `null`. See static::PROTECTED_PROPERTIES.
2682
+ if ( isset ( $ this ->theme_json ['settings ' ]['spacing ' ]['blockGap ' ] ) ) {
2676
2683
$ block_gap_value = static ::get_property_value ( $ this ->theme_json , array ( 'styles ' , 'spacing ' , 'blockGap ' ) );
2677
2684
$ css .= ":where(.wp-site-blocks) > * { margin-block-start: $ block_gap_value; margin-block-end: 0; } " ;
2678
2685
$ css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; } ' ;
2679
2686
$ css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; } ' ;
2680
2687
2681
2688
// For backwards compatibility, ensure the legacy block gap CSS variable is still available.
2682
- $ css .= " $ selector { --wp--style--block-gap: $ block_gap_value; } " ;
2689
+ $ css .= static :: ROOT_CSS_PROPERTIES_SELECTOR . " { --wp--style--block-gap: $ block_gap_value; } " ;
2683
2690
}
2684
2691
$ css .= $ this ->get_layout_styles ( $ block_metadata );
2685
2692
0 commit comments