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 define RadioControl label with ReactNode component #66615

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from

Conversation

retrofox
Copy link
Contributor

@retrofox retrofox commented Oct 30, 2024

What?

This PR allows using a ReactNode element for label in the RadioControl component

Why?

Because sometimes it's desired to render not only text for the radio control label

How?

  • Allow to define ReactNode for radio label

Testing Instructions

  1. Run storybook
npm run storybook:dev
  1. Go to the new With Component Labels story
  2. Confirm the story works as expected
image

Testing Instructions for Keyboard

Screenshots or screencast

@retrofox retrofox added the [Package] Components /packages/components label Oct 30, 2024
@retrofox retrofox self-assigned this Oct 30, 2024
@retrofox retrofox requested a review from ajitbohra as a code owner October 30, 2024 18:17
Copy link

github-actions bot commented Oct 30, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: retrofox <[email protected]>
Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: ciampo <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@retrofox retrofox requested a review from mirka October 30, 2024 18:17
@retrofox retrofox added the [Type] Enhancement A suggestion for improvement. label Oct 30, 2024
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay!

Like I noted in the original issue, I think a requirement for this change is that we stop using a <label> internally and associate the input with a aria-labelledby instead. The usage example in this PR already renders invalid HTML (<div> inside a <label>), so we can assume other consumers would make the same mistake 😄

packages/components/src/radio-control/README.md Outdated Show resolved Hide resolved
packages/components/src/radio-control/types.ts Outdated Show resolved Hide resolved
@mirka mirka requested a review from a team November 26, 2024 20:22
*/
label: string;
label: string | React.ReactNode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactNode is a bit broad. Do we really want label to possibly be boolean or null or even number? I wonder if we meant ReactElement instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, I don't think so. Maybe a number, but I believe string covers that.
Replacing...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want label to possibly be boolean or null

This is a reasonable point, but I think we should also consider how many of our components that accept non-string labels right now already accept ReactNode. Introducing a subtle inconsistency like this could cause complications when composing components/types. Would it be ok to consider a package-wide switch from ReactNode to ReactElement as a separate issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to consider this a separate issue 👍 Let's document it in an issue though.

@retrofox
Copy link
Contributor Author

retrofox commented Nov 27, 2024

Sorry for the delay!

Like I noted in the original issue, I think a requirement for this change is that we stop using a <label> internally and associate the input with a aria-labelledby instead. The usage example in this PR already renders invalid HTML (<div> inside a <label>), so we can assume other consumers would make the same mistake 😄

Never stop learning. Thanks a bunch for your :-) feedback

Copy link

github-actions bot commented Nov 27, 2024

Flaky tests detected in c6b9c9e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12055287484
📝 Reported issues:

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like all feedback so far has been addressed — @mirka and @tyxla, do you have any more feedback to share?

packages/components/src/radio-control/README.md Outdated Show resolved Hide resolved
@tyxla
Copy link
Member

tyxla commented Dec 4, 2024

Nothing from my end 👍

@mirka
Copy link
Member

mirka commented Dec 4, 2024

Looks like all feedback so far has been addressed

The internals need to be altered to stop using <label>.

@retrofox
Copy link
Contributor Author

Sorry for the delay!

Like I noted in the original issue, I think a requirement for this change is that we stop using a <label> internally and associate the input with a aria-labelledby instead.

Does it mean we need to handle the onClick event when the user clicks on the not-label element (span, div, ...)?

@mirka
Copy link
Member

mirka commented Dec 18, 2024

Does it mean we need to handle the onClick event when the user clicks on the not-label element (span, div, ...)?

Good point, I wasn't thinking about that. That handling would be necessary, and the upside doesn't seem worth it, at least for now.

Let's keep the label implementation as is, and start with just guidance in documentation. Something like:

diff --git a/packages/components/src/radio-control/stories/index.story.tsx b/packages/components/src/radio-control/stories/index.story.tsx
index 73123aa956..ddcf596d1a 100644
--- a/packages/components/src/radio-control/stories/index.story.tsx
+++ b/packages/components/src/radio-control/stories/index.story.tsx
@@ -95,8 +95,9 @@ WithOptionDescriptions.args = {
 };
 
 /**
- * When the label is not a string,
- * make sure that the element is accessibly labeled.
+ * When the label is not a string, make sure that the element is accessibly labeled.
+ * To ensure valid HTML and proper accessibility, only include [phrasing content](https://developer.mozilla.org/en-US/docs/Web/HTML/Content_categories#phrasing_content),
+ * but no descendant `label` elements. No [labelable](https://developer.mozilla.org/en-US/docs/Web/HTML/Content_categories#labelable) elements are allowed.
  */
 export const WithComponentLabels: StoryFn< typeof RadioControl > =
 	Template.bind( {} );
diff --git a/packages/components/src/radio-control/types.ts b/packages/components/src/radio-control/types.ts
index be42504d91..eb10112fda 100644
--- a/packages/components/src/radio-control/types.ts
+++ b/packages/components/src/radio-control/types.ts
@@ -20,6 +20,8 @@ export type RadioControlProps = Pick<
 		 * The label to be shown to the user.
 		 *
 		 * When the label is not a string, make sure that the element is accessibly labeled.
+		 * To ensure valid HTML and proper accessibility, only include [phrasing content](https://developer.mozilla.org/en-US/docs/Web/HTML/Content_categories#phrasing_content),
+		 * but no descendant `label` elements. No [labelable](https://developer.mozilla.org/en-US/docs/Web/HTML/Content_categories#labelable) elements are allowed.
 		 */
 		label: string | React.ReactElement;
 		/**

We'll keep an eye out for people ignoring it, and we can add more guardrails if it becomes problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow React Components as label in RadioControl Options for Enhanced Customization
4 participants