-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
base: trunk
Are you sure you want to change the base?
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this 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 😄
*/ | ||
label: string; | ||
label: string | React.ReactNode; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 beboolean
ornull
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?
There was a problem hiding this comment.
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.
Never stop learning. Thanks a bunch for your :-) feedback |
Co-authored-by: Lena Morita <lena@jaguchi.com>
Co-authored-by: Lena Morita <lena@jaguchi.com>
Co-authored-by: Lena Morita <lena@jaguchi.com>
Flaky tests detected in c6b9c9e. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12055287484
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing from my end 👍 |
The internals need to be altered to stop using |
Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com>
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. |
What?
This PR allows using a
ReactNode
element for label in the RadioControl componentWhy?
Because sometimes it's desired to render not only text for the radio control label
How?
ReactNode
for radio labelTesting Instructions
Testing Instructions for Keyboard
Screenshots or screencast