Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Styles: Add CSS vars for root level settings #29714

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,30 @@ private static function compute_preset_vars( $declarations, $settings ) {
return $declarations;
}

/**
* Takes the root level settings from theme JSON and creates variables for them.
*
* @param array $declarations Holds the existing declarations.
* @param array $settings Settings to process.
*
* @return array Returns the modified $declarations.
*/
private static function compute_root_vars( $declarations, $settings ) {
if ( empty( $settings['styles'] ) ) {
return;
}

$root_values = gutenberg_experimental_get( $settings, array( 'root' ) );
$css_vars = self::flatten_tree( $root_values );
foreach ( $css_vars as $key => $value ) {
$declarations[] = array(
'name' => '--wp--style--' . $key,
'value' => $value,
);
}
return $declarations;
}

/**
* Given an array of settings, it extracts the CSS Custom Properties
* for the custom values and adds them to the $declarations
Expand Down Expand Up @@ -797,6 +821,10 @@ private function get_css_variables( $nodes ) {
$stylesheet .= self::to_ruleset( $selector, $declarations );
}

// Create variables for root level settings
$declarations = self::compute_root_vars( array(), $this->theme_json );
$stylesheet .= self::to_ruleset( ':root', $declarations );

return $stylesheet;
}

Expand Down