Make WordPress Core


Ignore:
Timestamp:
05/09/2024 04:07:53 AM (2 years ago)
Author:
isabel_brison
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r58030 r58123  
    3838         */
    3939        protected static $blocks_metadata = array();
     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';
    4048
    4149        /**
     
    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.
     
    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,
     
    22342243                $nodes[] = array(
    22352244                        'path'     => array( 'settings' ),
    2236                         'selector' => static::ROOT_BLOCK_SELECTOR,
     2245                        'selector' => static::ROOT_CSS_PROPERTIES_SELECTOR,
    22372246                );
    22382247
     
    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.
     
    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'];
    2626 
    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;';
    26362636
    26372637                /*
     
    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 . ';';
    2648                 }
    2649 
    2650                 $css .= ' }';
     2646                        $css .= static::ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $content_size . ';';
     2647                        $css .= '--wp--style--global--wide-size: ' . $wide_size . '; }';
     2648                }
     2649
     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 ) {
     
    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; }";
     
    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 );
Note: See TracChangeset for help on using the changeset viewer.