Skip to content

[docs] Fix incorrect usage of label and Field.Label in Checkbox, Radio, and Switch - #3203

Closed
battaglr wants to merge 14 commits into
mui:masterfrom
battaglr:docs/checkboxs-and-radios
Closed

battaglr wants to merge 14 commits into
mui:masterfrom
battaglr:docs/checkboxs-and-radios

Conversation

@battaglr

@battaglr battaglr commented Nov 13, 2025 •

Copy link
Copy Markdown
Contributor

Description

The current examples and guidelines show wrapping components in a <label>, which is invalid for components that render both a <button> and an <input> (as Checkbox, Radio, and Switch do).

Note

The HTML spec for label defines its content model as:

Phrasing content, but with no descendant labelable elements unless it is the element's labeled control, and no descendant label elements.

That's why the example below fails the W3C Markup Validator:

<label>
  <button>Text</button>
  <input type="checkbox" />
</label>

This PR includes:

  • Adding a usage-guideline bullet clarifying that these components must not be wrapped in a <label>.
  • Updating the recent Form integrations page to remove references to wrapper labels.
  • Updating examples in Checkbox, Radio, and Form to use independent labels.
  • Adding a new demo for Radio + Form.

Caution

These changes make the bug described in #2691 more obvious, since labels associated with <Checkbox parent /> don’t work. The good news is that PR #2896 appears to address that issue.

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2025

Copy link
Copy Markdown

vite-css-base-ui-example

pnpm add https://pkg.pr.new/mui/base-ui/@base-ui-components/react@3203
pnpm add https://pkg.pr.new/mui/base-ui/@base-ui-components/utils@3203

commit: 30a63e0

@netlify

netlify Bot commented Nov 13, 2025

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 30a63e0
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/691572e276b1c8000855d30a
😎 Deploy Preview https://deploy-preview-3203--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks

atomiks commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

I wonder if these components should render <span> by default instead; the implicit labeling was an intentional feature we added support for since it works neatly without Field.

@mj12albert

mj12albert commented Nov 13, 2025 •

Copy link
Copy Markdown
Member

I wonder if these components should render <span> by default instead

If we did this it could render

<span role="checkbox">
  <input aria-hidden="true" />
</span>

Which would also solve: #3143

And the only breaking change would be button->span; however when explicitly labelled and the label doesn't wrap span role="checkbox", the ability to trigger the control by clicking the label is lost

@mj12albert

Copy link
Copy Markdown
Member
  • Adding a usage-guideline bullet clarifying that these components must not be wrapped in a <label>

And since this is natively supported I think we can't explicitly forbid it whether <label> or <Field.Label> is involved @battaglr

@battaglr

Copy link
Copy Markdown
Contributor Author

@atomiks

I wonder if these components should render <span> by default instead

Absolutely! That would address the root issue.

@mj12albert

And since this is natively supported I think we can't explicitly forbid it whether <label> or <Field.Label> is involved

Totally agree!

@mj12albert

If we did this it could render: [span>input]

I like that. A 1:1 output for each component is a more predictable API and gives implementers better control.


My intention with this PR was to avoid invalid HTML based on the current implementation, but updating the implementation itself would definitely be the ideal fix.

Would you prefer I close this and open an issue outlining the problem?

@battaglr

Copy link
Copy Markdown
Contributor Author

@mj12albert

however when explicitly labelled and the label doesn't wrap span role="checkbox", the ability to trigger the control by clicking the label is lost

I missed that. I don’t think we should settle for losing either implicit or explicit label behavior.

Hmm… could we forward the user-defined id from any of these components to the underlying input and somehow capture whatever focus state it receives? 🤔

@zannager zannager added the docs Improvements or additions to the documentation. label Nov 13, 2025
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 17, 2025
@battaglr

Copy link
Copy Markdown
Contributor Author

Closing in favor of #3205.

@battaglr battaglr closed this Nov 17, 2025
@Dakkers

Dakkers commented Dec 5, 2025 •

Copy link
Copy Markdown

however when explicitly labelled and the label doesn't wrap span role="checkbox", the ability to trigger the control by clicking the label is lost

is there a workaround for this as it stands? my markup looks like this (some css/style omitted for brevity)

Screenshot 2025-12-05 at 12 35 03

markup for just the radio button:

<label for="_r_8_" id="_r_8_-label" style="">
  <span data-unchecked="" role="radio" tabindex="-1" aria-checked="false"
    aria-labelledby="_r_8_-label" id="base-ui-_r_9_" style="">
    <div style="">
      <div style=""></div>
    </div>
  </span>
  <input id="_r_8_" tabindex="-1" aria-hidden="true" type="radio" style="">
  <div style="">Item 3</div>
</label>

@mj12albert

Copy link
Copy Markdown
Member

when explicitly labelled and the label doesn't wrap span role="checkbox"

This still works after the change, there's a test here:

it('associates explicitly', async () => {
const changeSpy = spy((newValue) => newValue);
await render(
<RadioGroup onValueChange={changeSpy}>
<div>
<label data-testid="label" htmlFor="RadioA">
Apple
</label>
<Radio.Root value="apple" id="RadioA" />
</div>
<div>
<label data-testid="label" htmlFor="RadioB">
Banana
</label>
<Radio.Root value="banana" id="RadioB" />
</div>
</RadioGroup>,
);
const [label1, label2] = screen.getAllByTestId('label');
fireEvent.click(label1);
expect(changeSpy.callCount).to.equal(1);
expect(changeSpy.lastCall.returnValue).to.equal('apple');
fireEvent.click(label2);
expect(changeSpy.callCount).to.equal(2);
expect(changeSpy.lastCall.returnValue).to.equal('banana');
});
});

Your markup looks more or less the same as the example in https://base-ui.com/react/handbook/forms

Maybe you're using Field.* components and left out Field.Item? @Dakkers

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

Labels

docs Improvements or additions to the documentation. PR: out-of-date The pull request has merge conflicts and can't be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants