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

Navigation Submenu: Refactor to use the block supports function #48936

Merged
merged 10 commits into from
Mar 23, 2023

Conversation

scruffian
Copy link
Contributor

@scruffian scruffian commented Mar 8, 2023

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:

npm run test:unit:php -- --filter Render_Block_Navigation_Submenu_Test
  1. Set text and background preset colors on the overlay for the Navigation block
  2. Check that the colors on the frontend match those set in the editor.
  3. Do the same for custom colors.

Screenshots or screencast

Screenshot 2023-03-08 at 13 49 16

@scruffian scruffian added the [Block] Navigation Affects the Navigation Block label Mar 8, 2023
@scruffian scruffian self-assigned this Mar 8, 2023
}

// This allows us to be able to get a response from gutenberg_apply_colors_support.
$block->block_type->supports['color'] = true;
Copy link
Contributor Author

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?

@draganescu
Copy link
Contributor

I have tested this and the colors do not apply on the front end.

Backend

Screenshot 2023-03-14 at 16 11 25

Frontend

Screenshot 2023-03-14 at 16 12 23

Copy link
Contributor

@getdave getdave left a 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

Copy link
Contributor

@getdave getdave left a 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 ) {
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it ideal?

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 was thinking that then the sub-menu block would be the one to control it, rather than being orchestrated by the parent.

Copy link
Contributor

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.

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 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Follow up then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely.

Copy link
Contributor Author

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;
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Comment on lines 84 to 97
$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'
);
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@getdave
Copy link
Contributor

getdave commented Mar 21, 2023

@scruffian I added some tests cases specifically to test for color supports.

Can be run with

npm run test:unit:php -- --filter Render_Block_Navigation_Submenu_Test

I feel like they could be extended to use a @dataProvider to test multiple variants but it would require abstracting the code of the tests themselves and at that point it's becoming quite complex to reason about the tests which I dislike as an anti-pattern.

Let me know what you think?

/**
* Tests for various cases in Navigation Submenu rendering
*/
class Render_Block_Navigation_Submenu_Test extends WP_UnitTestCase {
Copy link
Contributor

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?

Copy link
Contributor Author

@scruffian scruffian Mar 22, 2023

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.

@scruffian
Copy link
Contributor Author

I updated some of the variables. I'm happy to merge this if someone else can give me a ✅

@github-actions
Copy link

Flaky tests detected in 67f9d9c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4494361988
📝 Reported issues:

$navigation_submenu_block
);

$tags = new WP_HTML_Tag_Processor( $rendered_html );
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

Copy link
Contributor

@getdave getdave left a 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.

@scruffian scruffian merged commit 88e97c4 into trunk Mar 23, 2023
@scruffian scruffian deleted the remove/unused-code-from-navigation-submenu branch March 23, 2023 15:58
@github-actions github-actions bot added this to the Gutenberg 15.5 milestone Mar 23, 2023
ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request Jun 30, 2023
…Press/gutenberg#48936 in favour of wp_apply_colors_support

this commit adds a deprecation to core
ramonjd added a commit that referenced this pull request Jun 30, 2023
ramonjd added a commit that referenced this pull request Jun 30, 2023
@Marc-pi
Copy link

Marc-pi commented Jun 30, 2023

@ramonjd the milestone is wrong ;-)

@ramonjd
Copy link
Member

ramonjd commented Jun 30, 2023

the milestone is wrong ;-)

Wait, where? 😄

@Marc-pi
Copy link

Marc-pi commented Jun 30, 2023

image

@ramonjd
Copy link
Member

ramonjd commented Jun 30, 2023

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).

@Marc-pi
Copy link

Marc-pi commented Jun 30, 2023

ok, my mistake, i saw this in the commit feed, i was suprised

@ramonjd
Copy link
Member

ramonjd commented Jun 30, 2023

All good, thank you anyway! 🙇🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants