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.

Built from https://develop.svn.wordpress.org/trunk@58123


git-svn-id: http://core.svn.wordpress.org/trunk@57588 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
isabel_brison 2024-05-09 04:09:13 +00:00
parent 5208140670
commit 00b440c27d
3 changed files with 29 additions and 21 deletions

View File

@ -809,12 +809,13 @@ class WP_Duotone {
* @internal * @internal
* *
* @since 6.3.0 * @since 6.3.0
* @since 6.6.0 Replaced body selector with `WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR`.
* *
* @param array $sources The duotone presets. * @param array $sources The duotone presets.
* @return string The CSS for global styles. * @return string The CSS for global styles.
*/ */
private static function get_global_styles_presets( $sources ) { private static function get_global_styles_presets( $sources ) {
$css = 'body{'; $css = WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR . '{';
foreach ( $sources as $filter_id => $filter_data ) { foreach ( $sources as $filter_id => $filter_data ) {
$slug = $filter_data['slug']; $slug = $filter_data['slug'];
$colors = $filter_data['colors']; $colors = $filter_data['colors'];

View File

@ -38,6 +38,14 @@ class WP_Theme_JSON {
*/ */
protected static $blocks_metadata = array(); protected static $blocks_metadata = array();
/**
* The CSS selector for the top-level preset settings.
*
* @since 6.6.0
* @var string
*/
const ROOT_CSS_PROPERTIES_SELECTOR = ':root';
/** /**
* The CSS selector for the top-level styles. * The CSS selector for the top-level styles.
* *
@ -1708,6 +1716,7 @@ class WP_Theme_JSON {
* *
* @since 5.8.0 * @since 5.8.0
* @since 5.9.0 Added the `$origins` parameter. * @since 5.9.0 Added the `$origins` parameter.
* @since 6.6.0 Added check for root CSS properties selector.
* *
* @param array $settings Settings to process. * @param array $settings Settings to process.
* @param string $selector Selector wrapping the classes. * @param string $selector Selector wrapping the classes.
@ -1715,7 +1724,7 @@ class WP_Theme_JSON {
* @return string The result of processing the presets. * @return string The result of processing the presets.
*/ */
protected static function compute_preset_classes( $settings, $selector, $origins ) { protected static function compute_preset_classes( $settings, $selector, $origins ) {
if ( static::ROOT_BLOCK_SELECTOR === $selector ) { if ( static::ROOT_BLOCK_SELECTOR === $selector || static::ROOT_CSS_PROPERTIES_SELECTOR === $selector ) {
/* /*
* Classes at the global level do not need any CSS prefixed, * Classes at the global level do not need any CSS prefixed,
* and we don't want to increase its specificity. * and we don't want to increase its specificity.
@ -2233,7 +2242,7 @@ class WP_Theme_JSON {
// Top-level. // Top-level.
$nodes[] = array( $nodes[] = array(
'path' => array( 'settings' ), 'path' => array( 'settings' ),
'selector' => static::ROOT_BLOCK_SELECTOR, 'selector' => static::ROOT_CSS_PROPERTIES_SELECTOR,
); );
// Calculate paths for blocks. // Calculate paths for blocks.
@ -2614,6 +2623,7 @@ class WP_Theme_JSON {
* Outputs the CSS for layout rules on the root. * Outputs the CSS for layout rules on the root.
* *
* @since 6.1.0 * @since 6.1.0
* @since 6.6.0 Use `ROOT_CSS_PROPERTIES_SELECTOR` for CSS custom properties.
* *
* @param string $selector The root node selector. * @param string $selector The root node selector.
* @param array $block_metadata The metadata for the root block. * @param array $block_metadata The metadata for the root block.
@ -2624,16 +2634,6 @@ class WP_Theme_JSON {
$settings = isset( $this->theme_json['settings'] ) ? $this->theme_json['settings'] : array(); $settings = isset( $this->theme_json['settings'] ) ? $this->theme_json['settings'] : array();
$use_root_padding = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments']; $use_root_padding = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
/*
* Reset default browser margin on the root body element.
* This is set on the root selector **before** generating the ruleset
* from the `theme.json`. This is to ensure that if the `theme.json` declares
* `margin` in its `spacing` declaration for the `body` element then these
* user-generated values take precedence in the CSS cascade.
* @link https://github.com/WordPress/gutenberg/issues/36147.
*/
$css .= 'body { margin: 0;';
/* /*
* If there are content and wide widths in theme.json, output them * If there are content and wide widths in theme.json, output them
* as custom properties on the body element so all blocks can use them. * as custom properties on the body element so all blocks can use them.
@ -2643,11 +2643,19 @@ class WP_Theme_JSON {
$content_size = static::is_safe_css_declaration( 'max-width', $content_size ) ? $content_size : 'initial'; $content_size = static::is_safe_css_declaration( 'max-width', $content_size ) ? $content_size : 'initial';
$wide_size = isset( $settings['layout']['wideSize'] ) ? $settings['layout']['wideSize'] : $settings['layout']['contentSize']; $wide_size = isset( $settings['layout']['wideSize'] ) ? $settings['layout']['wideSize'] : $settings['layout']['contentSize'];
$wide_size = static::is_safe_css_declaration( 'max-width', $wide_size ) ? $wide_size : 'initial'; $wide_size = static::is_safe_css_declaration( 'max-width', $wide_size ) ? $wide_size : 'initial';
$css .= '--wp--style--global--content-size: ' . $content_size . ';'; $css .= static::ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $content_size . ';';
$css .= '--wp--style--global--wide-size: ' . $wide_size . ';'; $css .= '--wp--style--global--wide-size: ' . $wide_size . '; }';
} }
$css .= ' }'; /*
* Reset default browser margin on the body element.
* This is set on the body selector **before** generating the ruleset
* from the `theme.json`. This is to ensure that if the `theme.json` declares
* `margin` in its `spacing` declaration for the `body` element then these
* user-generated values take precedence in the CSS cascade.
* @link https://github.com/WordPress/gutenberg/issues/36147.
*/
$css .= 'body { margin: 0; }';
if ( $use_root_padding ) { if ( $use_root_padding ) {
// Top and bottom padding are applied to the outer block container. // Top and bottom padding are applied to the outer block container.
@ -2670,16 +2678,15 @@ class WP_Theme_JSON {
$css .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }'; $css .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';
$css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }'; $css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';
$block_gap_value = isset( $this->theme_json['styles']['spacing']['blockGap'] ) ? $this->theme_json['styles']['spacing']['blockGap'] : '0.5em'; // Block gap styles will be output unless explicitly set to `null`. See static::PROTECTED_PROPERTIES.
$has_block_gap_support = isset( $this->theme_json['settings']['spacing']['blockGap'] ); if ( isset( $this->theme_json['settings']['spacing']['blockGap'] ) ) {
if ( $has_block_gap_support ) {
$block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) ); $block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
$css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }"; $css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";
$css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }'; $css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }';
$css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }'; $css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }';
// For backwards compatibility, ensure the legacy block gap CSS variable is still available. // For backwards compatibility, ensure the legacy block gap CSS variable is still available.
$css .= "$selector { --wp--style--block-gap: $block_gap_value; }"; $css .= static::ROOT_CSS_PROPERTIES_SELECTOR . " { --wp--style--block-gap: $block_gap_value; }";
} }
$css .= $this->get_layout_styles( $block_metadata ); $css .= $this->get_layout_styles( $block_metadata );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.6-alpha-58122'; $wp_version = '6.6-alpha-58123';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.