You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow React Components as label in RadioControl Options for Enhanced Customization #66603
Should the label accept a React Component Instead of a String?
Allowing label to accept a React component could enable richer, more flexible user interfaces. This would be particularly useful for custom icons, rating displays, or other visual indicators.
Accessibility Concerns
Labels in RadioControl are simple strings, which are generally straightforward to manage for screen readers. Allowing components might introduce accessibility issues. How can we deal with the a11y?
So, I'd like your opinion on addressing this issue before I start digging into a tentative implementation.
I think I'm ok with allowing ReactNode as an option label, given that:
We stop using the <label> element, and associate the input with aria-labelledby instead. This is because <label> elements only allow phrasing content, and I can easily imagine a consumer passing a div and ending up with invalid HTML.
Widening the type to accept any ReactNode definitely makes the component more flexible, but it also makes consumers of the component responsible for providing an accessible label.
I'm ok exploring the changes that you suggested (changing the type to ReactNode, switching from <label> to aria-labelledby, improving docs). At the same, I continue to think that we should also improve linting and testing to make sure that components are properly labelled
Thanks for raising this! As mirka noted on #66615, rich option labels are better handled by the new Radio / RadioGroup primitives in @wordpress/ui (tracked in #74178). Those have now landed in #82214 and #83038. Because they're composable, you render the label yourself and it can be any content. @wordpress/components is in maintenance mode, so RadioControl will keep its string-only label. Closing this in favour of the new primitives, but feel free to reopen if they don't cover your use case.
Activity
mirka commentedon Oct 30, 2024
I think I'm ok with allowing
ReactNodeas an option label, given that:We stop using the
<label>element, and associate the input witharia-labelledbyinstead. This is because<label>elements only allow phrasing content, and I can easily imagine a consumer passing adivand ending up with invalid HTML.gutenberg/packages/components/src/radio-control/index.tsx
Lines 126 to 131 in e62588a
We have clear documentation on the accessibility requirements when passing arbitrary elements as a label.
See also
BaseControlas a construct that allowsReactNodeas a label.Let's wait for some other opinions as well.
mirka commentedon Nov 13, 2024
@ciampo ☝️ Can I get a second opinion on this?
ciampo commentedon Nov 26, 2024
@mirka your comment above makes sense.
Widening the type to accept any
ReactNodedefinitely makes the component more flexible, but it also makes consumers of the component responsible for providing an accessible label.I'm ok exploring the changes that you suggested (changing the type to
ReactNode, switching from<label>toaria-labelledby, improving docs). At the same, I continue to think that we should also improve linting and testing to make sure that components are properly labelledyouknowriad commentedon Oct 6, 2026
Thanks for raising this! As mirka noted on #66615, rich option labels are better handled by the new
Radio/RadioGroupprimitives in@wordpress/ui(tracked in #74178). Those have now landed in #82214 and #83038. Because they're composable, you render the label yourself and it can be any content.@wordpress/componentsis in maintenance mode, soRadioControlwill keep its string-onlylabel. Closing this in favour of the new primitives, but feel free to reopen if they don't cover your use case.