Skip to content

Commit 08b4dd3

Browse files
Editor: move global CSS custom properties to :root selector.
Changes the rules outputting global styles CSS custom properties to use `:root` instead of `body`, and cleans up some unused variables. Props ramonopoly, isabel_brison. Fixes #61135. git-svn-id: https://develop.svn.wordpress.org/trunk@58123 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3a6cca9 commit 08b4dd3

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

src/wp-includes/class-wp-duotone.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,13 @@ private static function get_svg_definitions( $sources ) {
809809
* @internal
810810
*
811811
* @since 6.3.0
812+
* @since 6.6.0 Replaced body selector with `WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR`.
812813
*
813814
* @param array $sources The duotone presets.
814815
* @return string The CSS for global styles.
815816
*/
816817
private static function get_global_styles_presets( $sources ) {
817-
$css = 'body{';
818+
$css = WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR . '{';
818819
foreach ( $sources as $filter_id => $filter_data ) {
819820
$slug = $filter_data['slug'];
820821
$colors = $filter_data['colors'];

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class WP_Theme_JSON {
3838
*/
3939
protected static $blocks_metadata = array();
4040

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+
4149
/**
4250
* The CSS selector for the top-level styles.
4351
*
@@ -1708,14 +1716,15 @@ static function ( $carry, $element ) {
17081716
*
17091717
* @since 5.8.0
17101718
* @since 5.9.0 Added the `$origins` parameter.
1719+
* @since 6.6.0 Added check for root CSS properties selector.
17111720
*
17121721
* @param array $settings Settings to process.
17131722
* @param string $selector Selector wrapping the classes.
17141723
* @param string[] $origins List of origins to process.
17151724
* @return string The result of processing the presets.
17161725
*/
17171726
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 ) {
17191728
/*
17201729
* Classes at the global level do not need any CSS prefixed,
17211730
* and we don't want to increase its specificity.
@@ -2233,7 +2242,7 @@ protected static function get_setting_nodes( $theme_json, $selectors = array() )
22332242
// Top-level.
22342243
$nodes[] = array(
22352244
'path' => array( 'settings' ),
2236-
'selector' => static::ROOT_BLOCK_SELECTOR,
2245+
'selector' => static::ROOT_CSS_PROPERTIES_SELECTOR,
22372246
);
22382247

22392248
// Calculate paths for blocks.
@@ -2614,6 +2623,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
26142623
* Outputs the CSS for layout rules on the root.
26152624
*
26162625
* @since 6.1.0
2626+
* @since 6.6.0 Use `ROOT_CSS_PROPERTIES_SELECTOR` for CSS custom properties.
26172627
*
26182628
* @param string $selector The root node selector.
26192629
* @param array $block_metadata The metadata for the root block.
@@ -2624,16 +2634,6 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26242634
$settings = isset( $this->theme_json['settings'] ) ? $this->theme_json['settings'] : array();
26252635
$use_root_padding = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
26262636

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-
26372637
/*
26382638
* If there are content and wide widths in theme.json, output them
26392639
* 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 ) {
26432643
$content_size = static::is_safe_css_declaration( 'max-width', $content_size ) ? $content_size : 'initial';
26442644
$wide_size = isset( $settings['layout']['wideSize'] ) ? $settings['layout']['wideSize'] : $settings['layout']['contentSize'];
26452645
$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 . '; }';
26482648
}
26492649

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; }';
26512659

26522660
if ( $use_root_padding ) {
26532661
// Top and bottom padding are applied to the outer block container.
@@ -2670,16 +2678,15 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26702678
$css .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';
26712679
$css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';
26722680

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'] ) ) {
26762683
$block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
26772684
$css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";
26782685
$css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }';
26792686
$css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }';
26802687

26812688
// 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; }";
26832690
}
26842691
$css .= $this->get_layout_styles( $block_metadata );
26852692

0 commit comments

Comments
 (0)