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

Load block support styles in the <head> for block themes. #38750

Merged
merged 8 commits into from
Feb 16, 2022

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Feb 11, 2022

Part of #38434
Related #38745
Follow-up to #32083

The dynamic block styles for layout and elements should be loaded in the <head> for block themes. While that should also be the case for classic themes, the current mechanism we use (render_block) does not allow us to do that, hence, this PR doesn't change anything for them and will be loaded the <body>.

How to test

Block theme in WordPress 5.9:

  • Use TwentyTwentyTwo and load the front end.
  • Verify the embedded styles for elements (.wp-elements-UUID ...) and layout (.wp-container-UUUID ...) are loaded in the <head>.

Classic theme in WordPress 5.9:

  • Use TwentyTwenty and load the front end.
  • Verify the embedded styles for elements (.wp-elements-UUID ...) and layout (.wp-container-UUUID ...) are loaded in the <body>.

@oandregal oandregal self-assigned this Feb 11, 2022
@oandregal oandregal added [Type] Bug An existing feature does not function as intended CSS Styling Related to editor and front end styles, CSS-specific issues. labels Feb 11, 2022
@oandregal oandregal requested review from youknowriad, ellatrix, ajlende and a team February 11, 2022 16:40
@oandregal oandregal added the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Feb 11, 2022
@gziolo gziolo requested a review from aristath February 11, 2022 16:49
@oandregal
Copy link
Member Author

This is working for block themes but it isn't for classic ones. Need to investigate further.

@oandregal
Copy link
Member Author

This is now working. I've updated the issue description to better reflect what this does: the block themes always have the block supports styles in the head and the classic ones in the body.

*
* @param string $style HTML content to load in the <head> or <body>.
*/
function gutenberg_enqueue_block_support( $style ) {
Copy link
Member

Choose a reason for hiding this comment

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

The function wp_add_inline_style, which is similar receives a styles string and outputs it and it receives the styles without "style" tag so I guess this function should also not include the style tag.

On another question would it make sense to use wp_add_inline_style in this function instead of wp_enqueue_scripts? So for example a plugin can remove our styles etc...

Copy link
Member Author

Choose a reason for hiding this comment

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

The function wp_add_inline_style, which is similar receives a styles string and outputs it and it receives the styles without "style" tag so I guess this function should also not include the style tag.

Can do that.

On another question would it make sense to use wp_add_inline_style in this function instead of wp_enqueue_scripts? So for example a plugin can remove our styles etc...

I'm on the fence here. My first instinct is to do not change how this works because I don't know why we made it this way in the first place. This PR just fixes the issue with block themes enqueuing the block supports in the body when they should use the head.

Copy link
Member Author

Choose a reason for hiding this comment

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

3ed7cd0 consumers no longer need to provide the <style> tag.

Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to have a reserved block supports style handle. That doesn't output the link element but can be used for inline styles.

Maybe it's something we can explore for 6.0.

@@ -43,7 +43,7 @@ function gutenberg_render_elements_support( $block_content, $block ) {
}
$link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );

$style = "<style>.$class_name a{" . $link_color_declaration . ";}</style>\n";
$style = ".$class_name a{" . $link_color_declaration . ';}';
Copy link
Member

Choose a reason for hiding this comment

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

Nit pick: we can do $style = ".$class_name a{$link_color_declaration;}";.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah, that's a syntax error (having a ; next to a variable $link_color_declaration). Note that the existing string concatenation also has it in separate strings for the same reason.

@oandregal
Copy link
Member Author

This is ready so I've gone ahead and prepared WordPress/wordpress-develop#2323 to backport it to WordPress develop.

@oandregal oandregal changed the title Fix block styles enqueuing logic Load block support styles in the head for block themes Feb 16, 2022
Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@oandregal oandregal merged commit 1832b77 into trunk Feb 16, 2022
@oandregal oandregal deleted the fix/block-styles-head branch February 16, 2022 15:34
@github-actions github-actions bot added this to the Gutenberg 12.7 milestone Feb 16, 2022
@Mamaduka Mamaduka removed the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Feb 17, 2022
@cbravobernal cbravobernal changed the title Load block support styles in the head for block themes Load block support styles in the <head> for block themes. Feb 26, 2022
@ndiego
Copy link
Member

ndiego commented Mar 2, 2022

@oandregal and @jorgefilipecosta, I believe I have found an issue with this implementation. In testing 12.7, the styles are now added near the top of <head>, which is before the block inline styles and theme.json styles are added. This means that theme.json/inline styles can override the styles set by the user in the Editor. I believe this needs to be updated so the styles are loaded right before </head>.

image

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. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants