Don't render for attribute on Label for non-labelable controls - #3884
Open
Mohith26 wants to merge 2 commits into
Open
Don't render for attribute on Label for non-labelable controls#3884Mohith26 wants to merge 2 commits into
for attribute on Label for non-labelable controls#3884Mohith26 wants to merge 2 commits into
Conversation
The `for` attribute is only valid on labelable elements (https://html.spec.whatwg.org/#category-label). Components such as `Radio` and `Checkbox` render a `span` under the hood, which is not labelable, so pointing the `Label`'s `for` attribute at it results in invalid HTML. We now verify that the element the `for` attribute points to is a labelable element, and drop the attribute otherwise. Clicking the `Label` still activates and focuses the control because the click proxying now relies on the resolved id instead of the rendered `for` attribute. Fixes tailwindlabs#3883
|
@Mohith26 is attempting to deploy a commit to the Tailwind Labs Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3883, reported by @fischi20.
When a
Labelsits next to aRadioorCheckboxinside aField, Headless UI rendersfor="headlessui-control-..."pointing at the control, but those components render a<span role="radio">, and the HTML spec only allowsforto reference labelable elements (button/input/select/textarea and friends). The result is invalid HTML that Chrome DevTools flags.The fix makes
Labelverify after mount that its target is actually labelable (newisLabelableElement()util, styled after the existingisInteractiveElement()) and omit theforattribute otherwise. Accessibility is unchanged (aria-labelledbystill links the pair), and label-click proxying now resolves the control via the id closure instead of the rendered attribute, so clicking the label still activates and focuses the radio. Native controls likeInputkeep theirfor, including during SSR.CHANGELOG entry added under Unreleased. Three regression tests:
forkept forInput,fordropped forRadiowitharia-labelledbyintact, and label-click still focuses the radio. Full @headlessui-react suite: 1071 passed, zero new failures. Note: @headlessui-vue likely has the same issue; happy to port the fix there if you want it in this PR.