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 support to elements and block style variations #49396

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
33 changes: 31 additions & 2 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,13 +1141,42 @@ public function get_custom_css() {
// Add the global styles root CSS.
$stylesheet = _wp_array_get( $this->theme_json, array( 'styles', 'css' ), '' );

// Add the global styles element CSS.
if ( isset( $this->theme_json['styles']['elements'] ) ) {
foreach ( $this->theme_json['styles']['elements'] as $element => $node ) {
$custom_element_css = _wp_array_get( $this->theme_json, array( 'styles', 'elements', $element, 'css' ) );
if ( $custom_element_css ) {
$selector = static::ELEMENTS[ $element ];
$stylesheet .= $this->process_blocks_custom_css( $custom_element_css, $selector );
}
}
}

// Add the global styles block CSS.
if ( isset( $this->theme_json['styles']['blocks'] ) ) {
foreach ( $this->theme_json['styles']['blocks'] as $name => $node ) {
$custom_block_css = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $name, 'css' ) );
if ( $custom_block_css ) {
$selector = static::$blocks_metadata[ $name ]['selector'];
$stylesheet .= $this->process_blocks_custom_css( $custom_block_css, $selector );
// Only use the class for the default style variation if the block has style variations.
if ( isset( static::$blocks_metadata[ $name ]['styleVariations'] ) ) {
// Add the block selector and the .is-style-default style variation class.
$selector = static::$blocks_metadata[ $name ]['selector'] . ', ' . static::$blocks_metadata[ $name ]['styleVariations']['default'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default variation isn't always named "default". This won't work for the Button block, for example, and we also don't have any way of enforcing the naming for third party blocks. The only way to make sure a style variation is default is by checking for the "isDefault" property.

$stylesheet .= $this->process_blocks_custom_css( $custom_block_css, $selector );
} else {
// Add the block selector.
$selector = static::$blocks_metadata[ $name ]['selector'];
$stylesheet .= $this->process_blocks_custom_css( $custom_block_css, $selector );
}
}
if ( isset( $this->theme_json['styles']['blocks'][ $name ]['variations'] ) ) {
foreach ( $this->theme_json['styles']['blocks'][ $name ]['variations'] as $variation_name => $node ) {
$custom_variation_css = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $name, 'variations', $variation_name, 'css' ) );
if ( $custom_variation_css && 'default' !== $variation_name ) {
// Add the variation selectors, excluding the default variation.
$variation_selector = static::$blocks_metadata[ $name ]['styleVariations'][ $variation_name ];
$stylesheet .= $this->process_blocks_custom_css( $custom_variation_css, $variation_selector );
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,9 +1174,25 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
},
];

// Loop through the elements to check if there are custom CSS values.
// If there are, push the selector together with
// the CSS value to the 'styles' array.
Object.entries( ELEMENTS ).forEach( ( element ) => {
const [ name, elementsSelector ] = element;
if ( mergedConfig.styles.elements[ name ]?.css ) {
styles.push( {
css: processCSSNesting(
mergedConfig.styles.elements[ name ]?.css,
elementsSelector
),
isGlobalStyles: true,
} );
}
} );

// Loop through the blocks to check if there are custom CSS values.
// If there are, get the block selector and push the selector together with
// the CSS value to the 'stylesheets' array.
// the CSS value to the 'styles' array.
getBlockTypes().forEach( ( blockType ) => {
if ( mergedConfig.styles.blocks[ blockType.name ]?.css ) {
const selector = blockSelectors[ blockType.name ].selector;
Expand All @@ -1188,6 +1204,26 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
isGlobalStyles: true,
} );
}

/* CSS for block style variations */
if ( mergedConfig.styles.blocks[ blockType.name ]?.variations ) {
Object.entries(
mergedConfig.styles.blocks[ blockType.name ]?.variations
).forEach( ( variation ) => {
if ( variation[ 1 ].css && 'default' !== variation[ 0 ] ) {
const variationSelector =
blockSelectors[ blockType.name ]
.styleVariationSelectors[ variation[ 0 ] ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a version of the "not" selector in here, otherwise styles applied to the default variation won't appear in the editor.

styles.push( {
css: processCSSNesting(
variation[ 1 ].css,
variationSelector
),
isGlobalStyles: true,
} );
}
} );
}
} );

return [ styles, mergedConfig.settings ];
Expand Down