Skip to content

Conversation

@RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Dec 19, 2025

The <label for="…"> requires that the target element is labelable. The spec defines labelable elements as:

Some elements, not all of them form-associated, are categorized as labelable elements. These are elements that can be associated with a label element.

button, input (if the type attribute is not in the Hidden state), meter, output, progress, select, textarea, form-associated custom elements

https://html.spec.whatwg.org/multipage/forms.html#category-label

It doesn't say anything about custom implementations where a role="…" is being used (even though it works as expected).

This PR fixes the issue shown in devtools, by using a span without the for attribute in case we are pointing to a non-labelable element.

Unfortunately, we only know what we are pointing to at runtime, so during SSR we will use a span as well, but once everything is hydrated, a proper label will be used if possible.

In a perfect world, we do use the native <label> and switch to span if we know it's invalid, but then the devtools warning still shows since I believe it checks the HTML coming from the server.

Fixes: tailwindlabs/tailwind-plus-issues#1792

Test plan

  1. Existing tests still pass
  2. Added a test to make sure that the behavior with a span works
  3. The issue in devtools is now gone as well:

Before:
image

After:
image

This ensures that the DOM is valid, otherwise using a native `<label
for="…">` with an ID that points to a non-labelable element could result
in an issue.

A labelable element is defined here:

> button, input (if the type attribute is not in the Hidden state)
meter, output, progress, select, textarea, form-associated custom
elements
>
> — https://html.spec.whatwg.org/multipage/forms.html#category-label

But it doesn't say anything about custom implementations where you
implement the behavior yourself. E.g.: components with `role="checkbox"`

With this implementation, we fallback to a simple inline `span` which is
not focusable, but is clickable, mimicking the native `<label>`.

We still use the `aria-labelledby` attribute so tools like VoiceOver
work as expected.

Last but not least, the `for` (`htmlFor`) attribute is removed when
dealing with a `span` instead of a native `label`.
We already figured out that we only ever use a real `<label>` when we
target a real labelable element. This also means that we can entirely
rely on the native browser behavior for labels.

However, when we are _not_ dealing with a labelable element, we use a
span and therefore we have to perform that `click` logic ourselves
(which we conveniently already had).
`el.role` is undefined in jsdom, but `el.getAttribute('role')` is not.
So we can fallback to that, but short-circuit if `el.target` is
available.
@vercel
Copy link

vercel bot commented Dec 19, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
headlessui-react Ready Ready Preview, Comment Dec 19, 2025 4:47pm
headlessui-vue Ready Ready Preview, Comment Dec 19, 2025 4:47pm

@thecrypticace
Copy link
Contributor

thecrypticace commented Dec 19, 2025

tests need to be tweaked now that a <span> is used instead of a <label> when server rendering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catalyst Checkbox/Label - Incorrect use of <label for=FORM_ELEMENT>

3 participants