WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.53k stars 4.21k forks source link

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

Open retrofox opened 3 weeks ago

retrofox commented 3 weeks ago

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.

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

mirka commented 3 weeks ago

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.

    https://github.com/WordPress/gutenberg/blob/e62588a450d38aee9f25c7cd08f1ecb4c795238a/packages/components/src/radio-control/index.tsx#L126-L131

  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.

mirka commented 1 week ago

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