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

Compound components: throw when subcomponents are not children of their top-level component #66530

Open
2 of 9 tasks
ciampo opened this issue Oct 28, 2024 · 2 comments
Open
2 of 9 tasks
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality

Comments

@ciampo
Copy link
Contributor

ciampo commented Oct 28, 2024

Compound components enable greater flexibility and modularity, at the cost of a slightly added complexity for the consumer.

One of the most frequent mistakes that we witness is when consumers render a subcomponent outside of its compound component's "context" — for example, rendering a Composite.Item without nesting it under a Composite item.

While we want to help consumers in several ways, including proving better documentation and examples, one (complementary) approach is to have compound components throw when they are not rendered inside the relative top-level component.

We should, therefore, implement this standard behaviour across all compound components in the @wordpress/components library:

  • BaseControl
  • Composite
  • CustomSelectControl (internal implementation)
  • ItemGroup
  • Menu
  • SlotFill
  • Tabs
  • ToggleGroupControl
  • Toolbar
@ciampo ciampo added [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality labels Oct 28, 2024
@ciampo
Copy link
Contributor Author

ciampo commented Oct 28, 2024

@WordPress/gutenberg-components let's collaborate on writing down a complete list of all components that should be affected by this change.

@tyxla
Copy link
Member

tyxla commented Oct 29, 2024

Perhaps SlotFill qualifies as well:

registerSlot: () => {
warning(
'Components must be wrapped within `SlotFillProvider`. ' +
'See https://developer.wordpress.org/block-editor/components/slot-fill/'
);
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

No branches or pull requests

2 participants