-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
There was a problem hiding this 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
?
There was a problem hiding this 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
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.
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? |
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. |
There was a problem hiding this 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.
lib/block-supports/elements.php
Outdated
* @param array $block Block object. | ||
* @return string The unique class name. | ||
*/ | ||
function gutenberg_get_elements_class_name ( $block ) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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?
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). |
6d6b142
to
5d55dc2
Compare
I see this one is on the 5.9 must-haves board. |
Let's aim for 5.9.1. |
711d8f5
to
06788a7
Compare
3028707
to
f8399f4
Compare
Can we add a unit test for this behavior to make sure there are no regressions? |
👋 @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? |
58d8251
to
f67438c
Compare
@@ -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; |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 :)
There was a problem hiding this 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.
I've also edited the issue description so #37582 is not closed. Note that the issue is twofold:
|
} | ||
|
||
// 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 ); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. 🙇
There was a problem hiding this comment.
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.
@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! |
@ndiego Yeah, it's a bugfix that should land in 6.0. |
Do we have a PR opened for changes in WordPress core? |
I've looked and haven't found one. I can help with this. |
This is going to be handled in WordPress/wordpress-develop#2624 for WordPress 6.0 Beta 3. |
Backport PR at WordPress/wordpress-develop#2624 and follow-up for Gutenberg at #40594 |
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
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
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
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