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

Elements: Add styles to the footer before the block is rendered #37728

Merged
merged 12 commits into from
Apr 13, 2022

Conversation

scruffian
Copy link
Contributor

@scruffian scruffian commented Jan 5, 2022

Related #37582
An alternative to #37593

What

This uses the pre_render hook of the block to render its styles, so the parent styles are enqueued before the children's.

Why

See #37582 for details. Essentially, this fixes an use case where both the parent and the inner block set the link color, and the expectation is that the child link color takes precedence.

How to test

See #37582 for comprehensive instructions

@scruffian scruffian added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label Jan 5, 2022
@scruffian scruffian self-assigned this Jan 5, 2022
Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

This looks like a great alternative to #37593, just because it seems to be addressing how the styles are rendered directly.

I'm seeing the below error though for $content. I guess this should be returning the block content (with the stylesheet?), similar to gutenberg_render_elements_support?

lib/block-supports/elements.php Outdated Show resolved Hide resolved
Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

I was having trouble with this until I realised that pre_render_block will only run on nested blocks from WP 5.9 😅 There's more info in this trac ticket for anyone else testing.

On 5.9, this works great for me and fixes the original issue. Here's a post with different link colors using:

  • a paragraph on its own
  • a nested paragraph in a nested group, in a group
  • a nested paragraph in a group
  • another nested paragraph in a group

image

This is using emptytheme. It also matches what I'm seeing in the editor.

I'm approving based on the above, but it would be great if someone else could review this as well.

@youknowriad
Copy link
Contributor

Hi there! Thanks for the PR. I'm not familiar with this particular issue but is this something specific to the "elements" styles. What about the "layout" block support styles which use a similar technique? Do we need some kind of common helper to use for all the situations where we need to inject styles from blocks?

@youknowriad youknowriad requested a review from gziolo January 7, 2022 13:22
@scruffian
Copy link
Contributor Author

From what I can see the styles in layout aren't expected to inherit in the same way as these ones, so the problem isn't evident there. However I can imagine one day we might expect some of those rules to inherit (maybe block gap for example) so it might make sense to use the same approach in both places.

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for the ping @scruffian, and good find digging into the cause behind the inheritance issue! I don't think we'll have the same issue with block gap now that we've switched away from using CSS variables for it. I've just left a few questions about generating the class name and whether we can make it a common helper function that all the block supports can use, but Riad might have a better idea of where we're headed in the future in regards to managing generated class names.

* @param array $block Block object.
* @return string The unique class name.
*/
function gutenberg_get_elements_class_name ( $block ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the idea of programatically generating the class name based on values within the block object, and I think it helps get us a little closer to some of the ideas @youknowriad raised in the discussion on improving saving/rendering of block styles (here, it's a good example of sharing a class name between two separate functions).

Since this function will be exposed in the global namespace, is this a good opportunity to create a generic gutenberg_get_block_support_class_name function that the other block supports that currently use uniqid could also use? We might then have a first parameter of a string prefix, possibly, so each support could add its own wp-elements- or wp-container- prefix as needed.

With using the entire block object for the MD5, we might only need to use part of the block attributes (e.g. the style attribute) rather than the whole thing. That probably doesn't change the implementation here, but it could mean that we could pass this helper function any arbitrary data for generating the unique class name rather than it necessarily being a block object?

I was also wondering about the md5 string, which at 32 characters is pretty long for a class name. Would it cause any issues to use a sub string and just grab the first 16 - 24 characters?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the idea of having a unique class per block but I think basing it on attributes or content is not necessarily, what we really want is just a unique class for this block any block support can use.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, yes I agree, the objective is for a unique id for the instance of the block (in the context of rendering the supports), rather than verifying data integrity of the attributes/content. I wonder how else we might share an id / class name across function calls 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

@andrewserong could be possible to generate a unique id in the $block object in the server maybe?

@oandregal
Copy link
Member

The approach here seems sensible to me, can't think of a scenario where it wouldn't work.

Just noting that it doesn't fix the issue completely, as it's twofold: it affects the front (fixed by this PR) and the editor where element rendering is not deterministic (needs a different PR).

@aristath
Copy link
Member

aristath commented Jan 18, 2022

I see this one is on the 5.9 must-haves board.
Should it be merged and back ported today? Or moved to 5.9.1?

@noisysocks noisysocks added the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Jan 18, 2022
@noisysocks
Copy link
Member

Let's aim for 5.9.1.

@glendaviesnz
Copy link
Contributor

This tests well for me, and with multiple nesting levels:

link-color

Have rebased and pushed a fix for failing unit tests 🤞.

@oandregal
Copy link
Member

Can we add a unit test for this behavior to make sure there are no regressions?

@adamziel
Copy link
Contributor

adamziel commented Apr 4, 2022

👋 @scruffian I noticed this PR is on the WordPress 5.9.x board. Do you this could still land until Wednesday to be included in WordPress 6.0 release?

@@ -115,7 +115,8 @@ function gutenberg_render_elements_support_footer( $pre_render, $block ) {
$style = ".$class_name a{" . $link_color_declaration . ';}';

gutenberg_enqueue_block_support_styles( $style );
return $style;

return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that for the sake of the test...

Copy link
Member

Choose a reason for hiding this comment

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

I removed the test I suggested we should have. As you said, in this case, the value would lie in a e2e test. It'd be great to have it :)

Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

I've rebased and pushed a couple of fixes: do not output styles if skip serialization is true, do not short-circuit block render.

This works well for me and can't think of a scenario where it'd break, so I'm approving.

@oandregal
Copy link
Member

I've also edited the issue description so #37582 is not closed. Note that the issue is twofold:

  • In the frontend, the paragraph's link color is overwritten by the group's link color always. Fixed by this PR.
  • Applying the link color is not deterministic in the editor. This needs a follow-up PR. Test the following: 1) apply the link color to the paragraph and then to the group and 2) apply the link color to the group and then to the paragraph. While both use cases should render the same results, atm, the last link color set wins.

@oandregal oandregal added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Apr 13, 2022
@scruffian scruffian merged commit 4e31a43 into trunk Apr 13, 2022
@scruffian scruffian deleted the fix/37582-again branch April 13, 2022 22:47
@github-actions github-actions bot added this to the Gutenberg 13.1 milestone Apr 13, 2022
}

// Remove WordPress core filter to avoid rendering duplicate elements stylesheet.
remove_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
add_filter( 'render_block', 'gutenberg_render_elements_support', 10, 2 );
add_filter( 'pre_render_block', 'gutenberg_render_elements_support_styles', 10, 2 );
Copy link
Contributor

Choose a reason for hiding this comment

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

For me this PR raises an interesting question about the styles order. Is there any place where we want the block styles to be generated "child" first then "parent" later? In other words, this PR does invert the order for the "elements" styles but why not invert it for all kind of styles.

It seems it's something worth thinking about for the future in regards of the style engine work. cc @andrewserong @ramonjd

Copy link
Member

Choose a reason for hiding this comment

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

Is there any place where we want the block styles to be generated "child" first then "parent" later?

Thanks for the ping.

I suppose depending on how we output styles we might want to look at a mix of specificity and style rendering order.

Anyway, I've added a note to look at a complete inheritance modal over at the style engine tracking issue. 🙇

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it should be like this for all styles, to follow the pattern of CSS inheritance.

@ndiego
Copy link
Member

ndiego commented Apr 25, 2022

@youknowriad and @oandregal is this PR still something we want to get into 6.0? If so great, I'll add this to the list of needed backports, but noticed the recent discussions. Let me know!

@oandregal
Copy link
Member

@ndiego Yeah, it's a bugfix that should land in 6.0.

@gziolo
Copy link
Member

gziolo commented Apr 25, 2022

Do we have a PR opened for changes in WordPress core?

@oandregal
Copy link
Member

Do we have a PR opened for changes in WordPress core?

I've looked and haven't found one. I can help with this.

@gziolo gziolo removed the Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release label Apr 25, 2022
@gziolo gziolo removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Apr 25, 2022
@gziolo
Copy link
Member

gziolo commented Apr 25, 2022

This is going to be handled in WordPress/wordpress-develop#2624 for WordPress 6.0 Beta 3.

@oandregal
Copy link
Member

Backport PR at WordPress/wordpress-develop#2624 and follow-up for Gutenberg at #40594

pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Apr 25, 2022
This fixes an issue by which link color behaves differently in the editor and front end.

Related Gutenberg PR: WordPress/gutenberg#37728.

Props oandregal.
See #55567.



git-svn-id: https://develop.svn.wordpress.org/trunk@53260 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Apr 25, 2022
This fixes an issue by which link color behaves differently in the editor and front end.

Related Gutenberg PR: WordPress/gutenberg#37728.

Props oandregal.
See #55567.


Built from https://develop.svn.wordpress.org/trunk@53260


git-svn-id: http://core.svn.wordpress.org/trunk@52849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Apr 25, 2022
This fixes an issue by which link color behaves differently in the editor and front end.

Related Gutenberg PR: WordPress/gutenberg#37728.

Props oandregal.
See #55567.


Built from https://develop.svn.wordpress.org/trunk@53260


git-svn-id: https://core.svn.wordpress.org/trunk@52849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.