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

Element block styles from theme.json not applied in frontend #41443

Closed
glendaviesnz opened this issue May 31, 2022 · 7 comments · Fixed by #41446
Closed

Element block styles from theme.json not applied in frontend #41443

glendaviesnz opened this issue May 31, 2022 · 7 comments · Fixed by #41446
Assignees
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Bug An existing feature does not function as intended

Comments

@glendaviesnz
Copy link
Contributor

Description

Since #41160 was merged, custom block element styles in a theme.json file are not being applied in the frontend, but are still applied in the editor.

Step-by-step reproduction instructions

  • In a block based theme add the following to theme.json styles.blocks section
			"core/columns": {
				"elements": {
					"h2": {
						"color": {
							"text": "red",
							"background": "black"
						},
						"typography": {
							"fontSize": "var(--wp--preset--font-size--small)"
						}
					}
				}
			}
  • Add a columns block and within it nest an h2 header, and see if it displays with black background and red text in editor and frontend.

Screenshots, screen recording, code snippet

Before #41160 :
Screen Shot 2022-05-31 at 12 30 02 PM

After:
Screen Shot 2022-05-31 at 12 32 24 PM

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@glendaviesnz glendaviesnz added [Type] Bug An existing feature does not function as intended [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. CSS Styling Related to editor and front end styles, CSS-specific issues. labels May 31, 2022
@ramonjd ramonjd assigned scruffian and unassigned scruffian May 31, 2022
@glendaviesnz glendaviesnz added this to the Gutenberg 13.4 milestone May 31, 2022
@glendaviesnz
Copy link
Contributor Author

@scruffian I will try and get some time this afternoon to take a look at this, but if not you may have some thoughts overnight as probably good to get a fix into 13.4 if possible

@ramonjd
Copy link
Member

ramonjd commented May 31, 2022

I'm seeing the following error:

Notice: Undefined index: name in /var/www/html/wp-content/plugins/gutenberg/lib/compat/wordpress-6.1/get-global-styles-and-settings.php on line 19

Looks like the following line –

$block_name = str_replace( 'core/', '', $metadata['name'] );

– is trying to grab a name from this object:

array(2) { ["path"]=> array(5) { [0]=> string(6) "styles" [1]=> string(6) "blocks" [2]=> string(12) "core/columns" [3]=> string(8) "elements" [4]=> string(2) "h2" } ["selector"]=> string(20) ".wp-block-columns h2" }

Not sure if the fix is in that method yet!

Edit:

I am very sure this is not the right fix, but it demonstrates that we need to handle block styles separately

Example
function wp_add_global_styles_for_blocks() {
	$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
	// TODO some nodes dont have a name...
	$block_nodes = $tree->get_styles_block_nodes();
	foreach ( $block_nodes as $metadata ) {
		$block_css = $tree->get_styles_for_block( $metadata );

		if ( isset( $metadata['name'] ) ) {
			$block_name = str_replace( 'core/', '', $metadata['name'] );
			// These block styles are added on block_render.
			// This hooks inline CSS to them so that they are loaded conditionally
			// based on whether or not the block is used on the page.
			wp_add_inline_style( 'wp-block-' . $block_name, $block_css );
		}

		if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
			$result = array_values(
				array_filter(
					$metadata['path'],
					function ( $item ) {
						if ( strpos( $item, 'core/' ) !== false ) {
							return true;
						}
						return false;
					}
				)
			);
			if ( isset( $result[0] ) ) {
				$block_name = str_replace( 'core/', '', $result[0] );
				wp_add_inline_style( 'wp-block-' . $block_name, $block_css );
			}
		}
	}
}

@glendaviesnz
Copy link
Contributor Author

I am very sure this is not the right fix, but it demonstrates that we need to handle block styles separately

This fix works for me @ramonjd . The one issue with it I can see is that it outputs the styles even if the particular block/element combination is not in the content ... but it is difficult to see an easy workaround for that as with the example given parsing all the column blocks on the page to see if they contain an h2 would be expensive.

@priethor
Copy link
Contributor

priethor commented Jun 6, 2022

Hi @glendaviesnz ! I'm preparing cherry-picking for 13.4. Is this PR still intended to land before the 13.4 stable release?

@glendaviesnz
Copy link
Contributor Author

Hi @glendaviesnz ! I'm preparing cherry-picking for 13.4. Is this PR still intended to land before the 13.4 stable release?

It would be nice, but it would also be good to get some input from @scruffian on this first as we may be overlooking something that he had intended by not allowing for this in the original PR. I think he is back from AFK now so will see if he has time to verify it.

@scruffian
Copy link
Contributor

This has been merged. @priethor can we get it into 13.4?

@priethor
Copy link
Contributor

priethor commented Jun 7, 2022

Sure, I'll cherry-pick the commit 👍

@priethor priethor removed this from the Gutenberg 13.4 milestone Jun 7, 2022
@priethor priethor removed the [Status] In Progress Tracking issues with work in progress label May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants