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

Add custom CSS for block style variations. #62526

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions backport-changelog/6.6/6797.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/6797

* https://github.com/WordPress/gutenberg/pull/62526
9 changes: 8 additions & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,7 @@ public function get_styles_for_block( $block_metadata ) {

// If there are style variations, generate the declarations for them, including any feature selectors the block may have.
$style_variation_declarations = array();
$style_variation_custom_css = array();
if ( ! empty( $block_metadata['variations'] ) ) {
foreach ( $block_metadata['variations'] as $style_variation ) {
$style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() );
Expand All @@ -2729,9 +2730,12 @@ static function ( $split_selector ) use ( $clean_style_variation_selector ) {
// Add the new declarations to the overall results under the modified selector.
$style_variation_declarations[ $combined_selectors ] = $new_declarations;
}

// Compute declarations for remaining styles not covered by feature level selectors.
$style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json );
// Store custom CSS for the style variation.
if ( isset( $style_variation_node['css'] ) ) {
$style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $style_variation['selector'] );
}
}
}

Expand Down Expand Up @@ -2862,6 +2866,9 @@ static function ( $pseudo_selector ) use ( $selector ) {
// 6. Generate and append the style variation rulesets.
foreach ( $style_variation_declarations as $style_variation_selector => $individual_style_variation_declarations ) {
$block_rules .= static::to_ruleset( ":root :where($style_variation_selector)", $individual_style_variation_declarations );
if ( isset( $style_variation_custom_css[ $style_variation_selector ] ) ) {
$block_rules .= $style_variation_custom_css[ $style_variation_selector ];
}
}

// 7. Generate and append any custom CSS rules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,9 @@ export const getNodesWithStyles = ( tree, blockSelectors ) => {
( [ variationName, variation ] ) => {
variations[ variationName ] =
pickStyleKeys( variation );

if ( variation?.css ) {
variations[ variationName ].css = variation.css;
}
const variationSelector =
blockSelectors[ blockName ]
.styleVariationSelectors?.[ variationName ];
Expand Down Expand Up @@ -1041,6 +1043,12 @@ export const toStyles = (
';'
) };}`;
}
if ( styleVariations?.css ) {
ruleset += processCSSNesting(
styleVariations.css,
`:root :where(${ styleVariationSelector })`
);
}
}
}
);
Expand Down
Loading