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

Allow custom CSS for theme.json elements #55614

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,22 @@ public function get_block_custom_css_nodes() {
}
}

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

return $block_nodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,9 +1242,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 Down
Loading