-
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
Navigation Submenu: Refactor to use the block supports function #48936
Conversation
} | ||
|
||
// This allows us to be able to get a response from gutenberg_apply_colors_support. | ||
$block->block_type->supports['color'] = true; |
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 it ok to modify this?
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.
Test both Preset and Custom colors and this worked as described in both the Site and the Post Editor.
Screen.Capture.on.2023-03-21.at.15-28-32.mp4
Screen.Capture.on.2023-03-21.at.15-29-56.mp4
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.
Left some notes and questions. For me the biggest thing missing here is addition of tests. We should/could write some.
@@ -14,66 +14,6 @@ | |||
* @param bool $is_sub_menu Whether the block is a sub-menu. | |||
* @return array Colors CSS classes and inline styles. | |||
*/ | |||
function block_core_navigation_submenu_build_css_colors( $context, $attributes, $is_sub_menu = false ) { |
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 love it that we can ditch this custom code 🥳
$colors['css_classes'] | ||
); | ||
$css_classes = trim( implode( ' ', $classes ) ); | ||
// Copy some attributes from the parent block to this one. |
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 is unusual code so should we be more specific about what's happening here?
); | ||
$css_classes = trim( implode( ' ', $classes ) ); | ||
// Copy some attributes from the parent block to this one. | ||
// Ideally this would happen in the client when the block is created. |
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.
Why is it ideal?
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 thinking that then the sub-menu block would be the one to control it, rather than being orchestrated by the parent.
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'm not sure. The reason it's orchestrated by the parent is so you can set the colors for all children in a single place rather than have to visit every single child block and configure the colors.
We could make it so changing one child block's colors changes all of them but I think that's very confusing.
I would consider leaving the controls in place on the Nav block but allowing the values set their to determine the defaults for controls we expose on the child blocks.
That means we still have a single place to se the colors.
Let me know if I've misunderstood here.
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 think what I envisage is that the controls remain on the parent block, but when they are set, the block updates the child blocks in the client, so that the block itself handles all of the logic about the display. That way nothing about the submenu blocks is stored in the parent block, its just a UI layer which allows the user to update multiple blocks at once.
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.
Follow up then?
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.
Absolutely.
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.
|
||
$style_attribute = $colors['inline_styles']; | ||
// This allows us to be able to get a response from gutenberg_apply_colors_support. | ||
$block->block_type->supports['color'] = true; |
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.
Why not rely on the actual block supports. If someone decides to disable block supports for this block then we should respect that.
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.
If we do that then users will be able to modify the settings for the submenu block in the UI, independently from the parent block. I think what we need to do is move the color settings from the navigation block to the submenu block, which will require a UI update and a block transform, and a lot of thinking about backwards compatibility. I do think we should do all of that, but if I had done it in this PR you'd be telling me it was too big, so this is just a first step toward that goal.
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.
Ok sure. I agree with small PRs.
I was thinking that as $block->block_type->supports['color'] = true;
appears to indicate the supports
is a Boolean we could just rely on that Boolean being set in the block.json
for Submenu. But now I think I understand that if you did that the UI for colors would appear in the Submenu block - which isn't what we want.
So yes I think this workaround is an ok tradeoff for being able to utilise the standardised color code generation.
What's the long term plan? Expose UI in the Submenu Block to allow changing colors on individual blocks but have the initial settings still coming from the parent Nav 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.
What's the long term plan? Expose UI in the Submenu Block to allow changing colors on individual blocks but have the initial settings still coming from the parent Nav block?
I agree that we still need a way to set it on the Nav block so that you don't need to change it for every child, but I've not given much thought to how that would work in practice yet.
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 had the same gut reaction initially and even tried to code a variant that just relies on block supports, but we do have some blockers there. However:
users will be able to modify the settings for the submenu block in the UI, independently from the parent block
@jasmussen is that a problem really?
$p = new WP_HTML_Tag_Processor( $rendered_html ); | ||
$p->next_tag( | ||
array( | ||
'tag_name' => 'ul', | ||
'class_name' => 'wp-block-navigation__submenu-container', | ||
) | ||
); | ||
$p->get_attribute( 'class' ); | ||
|
||
$this->assertEquals( | ||
'wp-block-navigation__submenu-container has-text-color has-purple-color has-background has-yellow-background-color', | ||
$p->get_attribute( 'class' ), | ||
'Submenu block colors inherited from context not applied correctly' | ||
); |
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 tried using WP_HTML_Tag_Processor
to improve the readability of the tests. It seems better to use this to assert on the HTML rather than assertStringContainsString
which is very simplistic.
Open to opinions on whether this is a good idea.
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.
What's the worry about the more simplistic approach?
Is it important that we have the test care about equaling all of those classnames in that order? Or should the classname include all 5 of the classes but in any order? If it's not important that it only equals that, it could be nice to check that it contains the classnames so it doesn't fail unnecessarily.
I'm not versed in the block nav though, so I'm not sure what the main considerations are.
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.
What's the worry about the more simplistic approach?
If the test fails it's quite difficult to see the difference between the two strings. Where the output for equals seemed clearer.
Also the Tag Processor makes it clearer we are asserting directly on the className
rather than on any other portion of the <ul>
HTML which could change and cause unrelated failures.
@scruffian I added some tests cases specifically to test for color supports. Can be run with
I feel like they could be extended to use a Let me know what you think? |
/** | ||
* Tests for various cases in Navigation Submenu rendering | ||
*/ | ||
class Render_Block_Navigation_Submenu_Test extends WP_UnitTestCase { |
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.
@scruffian What do you think of the test coverage?
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.
Should we use the navigation block itself in the tests rather than the navigation submenu block? I think I prefer using the submenu block as you have done, as it feels more inline with the future direction of the block.
The coverage looks good, but I have two test failures
Edit: This was an environment issue.
I updated some of the variables. I'm happy to merge this if someone else can give me a ✅ |
Flaky tests detected in 67f9d9c. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4494361988
|
$navigation_submenu_block | ||
); | ||
|
||
$tags = new WP_HTML_Tag_Processor( $rendered_html ); |
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.
❤️
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.
LGTM. We have tested this manually but I'd feel a lot more comfortable if we'd had tests to assert nothing changed.
At least we now have some tests for this block going forward.
Thanks for working on this.
If there are next steps (e.g. expose child controls on parent) then let's create Issues now to track them.
…Press/gutenberg#48936 in favour of wp_apply_colors_support this commit adds a deprecation to core
@ramonjd the milestone is wrong ;-) |
Wait, where? 😄 |
I think that's right? This is PR was merged Mar 24, 2023. All I've done is remove the doc block that isn't being used (today). |
ok, my mistake, i saw this in the commit feed, i was suprised |
All good, thank you anyway! 🙇🏻 |
What?
This takes the custom implementation of color supports from the navigation submenu block and changes it to use
gutenberg_apply_colors_support
from the color supports.Why?
This is the first step towards using standard color supports for the navigation block, rather than having a custom implementation. This will help the block to behave in a more predictable way, eliminate bugs and take advantage of tools in Global Styles.
How?
We have to modify the block_type and block_attributes, so that the block supports work as expected. I'm not sure that is an acceptable workaround. Long term we should actually enable this block support, but we need to migrate the colors from the navigation block attribute to the submenu block which is complex. This begins the process of unifying the approach in a simple way.
Testing Instructions
Run the tests:
Screenshots or screencast