Repository navigation
[checkbox][radio][switch] Render root as span instead of button - #3205
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
It "works" for me in the example, but the focus state doesn't sync properly. |
51cc3d8 to
4a41acc
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
4a41acc to
97256ad
Compare
span instead of button
|
On https://deploy-preview-3205--base-ui.netlify.app/experiments/forms/button-controls, |
|
@mj12albert yeah, |
77ea36e to
b397403
Compare
OK doign the same here but a gotcha with this is that |
| onKeyDown(event) { | ||
| if (nativeButton || event.defaultPrevented || readOnly || disabled) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
What's the reason for this handler? Shouldn't useButton already call onClick?
There was a problem hiding this comment.
useButton prevents default in onKeyDown + Enter key:
base-ui/packages/react/src/use-button/useButton.ts
Lines 130 to 132 in 56dee07
So the click in Switch doesn't fire because it returns if default prevented:
base-ui/packages/react/src/switch/root/SwitchRoot.tsx
Lines 146 to 149 in 56dee07
It's the same for checkbox, both of these are necessary though I think
There was a problem hiding this comment.
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)
2190766 to
c759722
Compare
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>
);
} |
Oh yeah it works, I didn't update this comment yet 😅 |
26cc06f to
730b21a
Compare
Is this more so an axe false positive? Without |
|
Alright, with VO the differences seem negligible anyway |
|
I have updated my project to use beta 5, but I am seeing a conflict because of this change. In my implementation, updating my custom props to extend either a button or a span both result in errors when passed into |

Breaking:
Checkbox.Root,Radio.RootandSwitch.Rootrenders<span>instead of<button>Labels can't wrap both a
buttonand the hiddeninputsince it's invalid HTMLWAVE also reports errors on the

button:Clicking the label to trigger the radio/checkbox/switch doesn't work if it's aactually worksspan, but this is added back viaField.Labelaria-requiredonrole="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