Skip to content

[checkbox][radio][switch] Render root as span instead of button - #3205

Merged
mj12albert merged 26 commits into
mui:masterfrom
mj12albert:span-button-controls
Nov 17, 2025
Merged

mj12albert merged 26 commits into
mui:masterfrom
mj12albert:span-button-controls

Conversation

@mj12albert

@mj12albert mj12albert commented Nov 13, 2025 •

Copy link
Copy Markdown
Member

Breaking: Checkbox.Root, Radio.Root and Switch.Root renders <span> instead of <button>

Labels can't wrap both a button and the hidden input since it's invalid HTML

Screenshot 2025-11-13 at 4 16 13 PM

WAVE also reports errors on the button:
Screenshot 2025-11-17 at 3 03 02 PM

  • Clicking the label to trigger the radio/checkbox/switch doesn't work if it's a span, but this is added back via Field.Label actually works
  • HTML validator flags aria-required on role="checkbox|switch" as invalid, but VO announces the required state correctly so I think it should be kept for checkbox and switch.

Demos:

Alternative to #3203

@mj12albert mj12albert added the component: radio group Changes related to the radio group component. label Nov 13, 2025
@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@3205
pnpm add https://pkg.pr.new/mui/base-ui/@base-ui-components/utils@3205

commit: 5815b16

@mui-bot

mui-bot commented Nov 13, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui-components/react 🔺+266B(+0.07%) 🔺+49B(+0.04%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Nov 13, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 5815b16
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/691ae79d1c201100087068b8
😎 Deploy Preview https://deploy-preview-3205--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

Clicking the label to trigger the radio doesn't work if it's a span, but this can be added back via FieldItem

It "works" for me in the example, but the focus state doesn't sync properly. onFocus() on the hidden input to focus the <span> seems like it'd be a workaround (similar to <Select.Trigger> behavior)

@mj12albert

This comment was marked as off-topic.

@mj12albert mj12albert changed the title [radio] Render span instead of button [radio][switch] Render span instead of button Nov 13, 2025
@mj12albert mj12albert added component: switch Changes related to the switch component. component: checkbox Changes related to the checkbox component. labels Nov 13, 2025
@mj12albert mj12albert changed the title [radio][switch] Render span instead of button [checkbox][radio][switch] Render span instead of button Nov 14, 2025
@mj12albert mj12albert added the breaking change Introduces changes that are not backward compatible. label Nov 14, 2025
@mj12albert mj12albert changed the title [checkbox][radio][switch] Render span instead of button [checkbox][radio][switch] Render root as span instead of button Nov 14, 2025
@atomiks

atomiks commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

On https://deploy-preview-3205--base-ui.netlify.app/experiments/forms/button-controls,
Clicking the label with the mouse doesn't set focus on the radio <span> control, so using ArrowUp/ArrowDown doesn't work. (There's also a mismatch in the focus style when tabbing into it and using the controls)

@mj12albert

Copy link
Copy Markdown
Member Author

There's also an issue where the label's for must match the input's id instead of the id of the root element, in which case it should be labelled with aria-labelledby="(label id)"

Screenshot 2025-11-14 at 6 45 42 PM

@atomiks

atomiks commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

@mj12albert yeah, Select forwards the id to the hidden input. Clicking the label focuses the hidden input, so onFocus is used to immediately relocate it to the trigger. I think that's the correct solution when using non-<button> triggers (although Select was recently changed to be a native <button>)

@mj12albert
mj12albert force-pushed the span-button-controls branch 4 times, most recently from 77ea36e to b397403 Compare November 14, 2025 19:35
@mj12albert

mj12albert commented Nov 14, 2025 •

Copy link
Copy Markdown
Member Author

Select forwards the id to the hidden input.

OK doign the same here but a gotcha with this is that <Select.Trigger id="MyTrigger" /> id's the input and not the actual trigger, though I suppose manual ids are an edge case in 2025...

@mj12albert
mj12albert marked this pull request as ready for review November 14, 2025 20:32
Comment on lines +158 to +161
onKeyDown(event) {
if (nativeButton || event.defaultPrevented || readOnly || disabled) {
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the reason for this handler? Shouldn't useButton already call onClick?

@mj12albert mj12albert Nov 16, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

useButton prevents default in onKeyDown + Enter key:

if (shouldClick) {
if (isSpaceKey || isEnterKey) {
event.preventDefault();

So the click in Switch doesn't fire because it returns if default prevented:

onClick(event) {
if (event.defaultPrevented || readOnly) {
return;
}

It's the same for checkbox, both of these are necessary though I think

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's the same for checkbox, both of these are necessary though I think

I think these were added before .preventBaseUIHandler was a thing. I think it's generally better to not check event.preventDefault as much as possible (at least we seemed to rarely do it in later components)

@atomiks

atomiks commented Nov 17, 2025

Copy link
Copy Markdown
Contributor

Clicking a plain sibling (even with for/id set correctly) is unable to toggle span role="checkbox"

It seems to work with a minimal repro:

export default function Checkbox() {
  const ref = React.useRef<HTMLSpanElement>(null);
  const [checked, setChecked] = React.useState(false);
  return (
    <div>
      <label htmlFor="checkbox">Checkbox</label>
      <span
        ref={ref}
        role="checkbox"
        aria-checked={checked}
        style={{
          display: 'inline-block',
          width: 20,
          height: 20,
          background: checked ? 'blue' : 'gray',
        }}
        onClick={() => setChecked(!checked)}
        tabIndex={0}
      />
      <input
        id="checkbox"
        onFocus={() => {
          ref.current?.focus();
        }}
        onClick={() => {
          ref.current?.click();
        }}
        tabIndex={-1}
        aria-hidden
      />
    </div>
  );
}

@mj12albert

Copy link
Copy Markdown
Member Author

Clicking a plain sibling (even with for/id set correctly) is unable to toggle span role="checkbox"

It seems to work with a minimal repro:

Oh yeah it works, I didn't update this comment yet 😅
Originally I thought it wasn't working because there is a weird behavior where an identical test for Switch fails for Checkbox because it's flaky when using user.click

@atomiks

atomiks commented Nov 17, 2025

Copy link
Copy Markdown
Contributor

in the demos messes up the accName:

Is this more so an axe false positive? Without aria-label, the enclosing label announces its text when focus is on the <span> checkbox. If so, I think it's okay since there are workarounds to satisfy it if necessary

@mj12albert

Copy link
Copy Markdown
Member Author

Alright, with VO the differences seem negligible anyway

@mj12albert
mj12albert merged commit 7a41931 into mui:master Nov 17, 2025
@mj12albert
mj12albert deleted the span-button-controls branch November 17, 2025 09:24
@hsimpson270

Copy link
Copy Markdown

I have updated my project to use beta 5, but I am seeing a conflict because of this change. CheckboxRootProps was updated to extend BaseUIComponentProps<'span', CheckboxRoot.State>, but CheckboxRoot was not updated as well, so it still typed as React.ForwardRefExoticComponent<CheckboxRootProps & React.RefAttributes<HTMLButtonElement>>. This causes an issue because CheckboxRoot is expected to extend a button, but its props are expected to extend a span.

In my implementation, updating my custom props to extend either a button or a span both result in errors when passed into CheckboxRoot. I was using button previously, which now generates a "Types of property 'onCopy' are incompatible" error (I'm assuming because that's the first native prop that conflicts), but if I update to use span instead I get a "Types of property 'ref' are incompatible." error. This error completely breaks the current implementation, so the only work around I can think of for now would be to not extend a native HTML element in my props at all.

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

Labels

breaking change Introduces changes that are not backward compatible. component: checkbox Changes related to the checkbox component. component: radio group Changes related to the radio group component. component: switch Changes related to the switch component.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants