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

Menu: throw when subcomponents are not rendered inside top level Menu #67411

Merged
merged 4 commits into from
Nov 29, 2024

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented Nov 29, 2024

What?

Part of #66530
Extracted from #66383

Add a check for all Menu subcomponents, so that they throw if not rendered inside a parent Menu component.

Why?

This check ensures that the Menu compound component is used as intended (see #66530)

How?

We perform a check on the internal component's context (which is created and exposed in the top level Menu component)

Testing Instructions

Try to render any subcomponents in isolation (ie. not inside a Menu component) — the component should throw an error.

@ciampo ciampo added [Type] Enhancement A suggestion for improvement. [Package] Components /packages/components labels Nov 29, 2024
@ciampo ciampo requested a review from a team November 29, 2024 06:41
@ciampo ciampo self-assigned this Nov 29, 2024
@ciampo ciampo requested a review from ajitbohra as a code owner November 29, 2024 06:41
Copy link

github-actions bot commented Nov 29, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ciampo <[email protected]>
Co-authored-by: tyxla <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ciampo ciampo force-pushed the feat/menu-add-runtime-check branch from bb3bf84 to df87182 Compare November 29, 2024 06:45
Copy link

github-actions bot commented Nov 29, 2024

Flaky tests detected in 5fb1df2.
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/12081605771
📝 Reported issues:

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

LGTM 👍 thank you 🚀

Comment on lines +30 to +32
throw new Error(
'Menu.CheckboxItem can only be rendered inside a Menu component'
);
Copy link
Member

Choose a reason for hiding this comment

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

Just for context to anyone else reading this: throwing an error in this scenario makes sense even if we just look at Ariakit - it is consistent with how Ariakit does it:

https://github.com/ariakit/ariakit/blob/22b84f02b85bbc8c66b2c6b58cf947f24a5316ce/packages/ariakit-react-core/src/menu/menu-item.tsx#L77-L81

https://github.com/ariakit/ariakit/blob/22b84f02b85bbc8c66b2c6b58cf947f24a5316ce/packages/ariakit-core/src/utils/misc.ts#L217-L232

As you can see, if we don't have this check, Ariakit will throw this error:

MenuItem must be wrapped in a MenuList, Menu or Menubar component

however, relying on this would be confusing, since our Menu components don't necessarily follow the same naming scheme.

Comment on lines 13 to 15

### Internal

Copy link
Member

Choose a reason for hiding this comment

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

No need to add yet another "Internal" section.

Suggested change
### Internal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I was meant to rename it to "Experimental" and I forgot to do it. Will amend and merge

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense 👍

@ciampo ciampo enabled auto-merge (squash) November 29, 2024 08:44
@ciampo ciampo merged commit 8acc11d into trunk Nov 29, 2024
64 checks passed
@ciampo ciampo deleted the feat/menu-add-runtime-check branch November 29, 2024 09:19
@github-actions github-actions bot added this to the Gutenberg 19.9 milestone Nov 29, 2024
im3dabasia pushed a commit to im3dabasia/gutenberg that referenced this pull request Dec 4, 2024
…WordPress#67411)

* Menu: throw when subcomponents are not rendered inside top level Menu

* CHANGELOG

* Remove unnecessary optional chaining

* Rename changelog section

---

Co-authored-by: ciampo <[email protected]>
Co-authored-by: tyxla <[email protected]>
michalczaplinski pushed a commit that referenced this pull request Dec 5, 2024
…#67411)

* Menu: throw when subcomponents are not rendered inside top level Menu

* CHANGELOG

* Remove unnecessary optional chaining

* Rename changelog section

---

Co-authored-by: ciampo <[email protected]>
Co-authored-by: tyxla <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants