Skip to content

Allow React Components as label in RadioControl Options for Enhanced Customization #66603

Closed

Description

@retrofox

In some cases, we'd like to pass not only a string to the label property of the options item.
Consider the following one:

<RadioControl
  options={[
    {
      label: <Rating value={ 4 } />
      value: '4'
    },
    //...
  ]}
  // ...
/>

...to render something like...

Image

Questions:

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

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

cc @WordPress/gutenberg-components

Activity

  1. added
    [Focus] Accessibility (a11y)
    on Oct 30, 2024
  2. mirka commented on Oct 30, 2024

    @mirka
    Member

    I think I'm ok with allowing ReactNode as an option label, given that:

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

      <label
      className="components-radio-control__label"
      htmlFor={ generateOptionId( id, index ) }
      >
      { option.label }
      </label>

    2. We have clear documentation on the accessibility requirements when passing arbitrary elements as a label.

    See also BaseControl as a construct that allows ReactNode as a label.

    Let's wait for some other opinions as well.

  3. mirka commented on Nov 13, 2024

    @mirka
    Member

    @ciampo ☝️ Can I get a second opinion on this?

  4. ciampo commented on Nov 26, 2024

    @ciampo
    Contributor

    @mirka your comment above makes sense.

    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

  5. youknowriad commented on Oct 6, 2026

    @youknowriad
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Focus] Accessibility (a11y)[Package] Components

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions