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

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

Open
retrofox opened this issue Oct 30, 2024 · 3 comments · May be fixed by #66615
Open

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

retrofox opened this issue Oct 30, 2024 · 3 comments · May be fixed by #66615
Labels
[Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components

Comments

@retrofox
Copy link
Contributor

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

@retrofox retrofox added the [Package] Components /packages/components label Oct 30, 2024
@mirka mirka added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label Oct 30, 2024
@mirka
Copy link
Member

mirka commented Oct 30, 2024

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.

@mirka
Copy link
Member

mirka commented Nov 13, 2024

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

@ciampo
Copy link
Contributor

ciampo commented Nov 26, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants