Mantle Native Radio Group Migration Guide
RadioGroup and SegmentedControl no longer use Headless UI. Each item (RadioGroup.Item, RadioGroup.ListItem, RadioGroup.Card, and SegmentedControl.Item) is still a <div>, and it now holds a native <input type="radio"> that covers it. The browser owns the one tab stop, Space, and the form contract, and the root moves the arrow keys. The accessibility tree and the look stay the same. The keyboard behavior stays the same in Chromium and Firefox; Safari differs, as the edge cases below say. What changes is the element that carries the state: the radio inside the item, not the item itself. A class on an item that reads aria-checked, aria-disabled, or focus-visible no longer matches.
TL;DR: In the className of a radio item, replace aria-checked: with has-[>input:checked]:, aria-disabled: with has-[>input:disabled]:, and focus-visible: with has-[>input:focus-visible]:. From a child of the item, replace group-aria-checked/radio: with group-has-[>input:checked]/radio:. Rename onChange to onValueChange on RadioGroup.Root, RadioGroup.List, and SegmentedControl.Root. Remove as and by from radio parts, and read only checked and disabled from RadioGroup.Indicator render props.
Codemod
From a mantle checkout:
In a project that installs @ngrok/mantle:
The codemod renames onChange to onValueChange on the roots and removes as from radio items and by from the roots (step 5). Do the other steps by hand: each one is a class string, often built in a constant away from the item, or a render-prop field.
You are an agent migrating a codebase. Do this:
- Find every file that imports
RadioGroupfrom@ngrok/mantle/radio-grouporSegmentedControlfrom@ngrok/mantle/segmented-control, under any local name. - In those files, find the class strings that reach
RadioGroup.Item,RadioGroup.ListItem,RadioGroup.Card, orSegmentedControl.Item: theclassNameprop itself and any constant orcx(...)call that feeds it. Rewrite each variant:aria-checked:→has-[>input:checked]:not-aria-checked:→not-has-[>input:checked]:aria-disabled:→has-[>input:disabled]:not-aria-disabled:→not-has-[>input:disabled]:focus-visible:→has-[>input:focus-visible]:data-checked:,data-focus:,data-hover:, anddata-disabled:(Headless UI attributes) → the samehas-[>input:…]forms, andhover:fordata-hover:
- In elements inside an item, rewrite
group-aria-checked/radio:togroup-has-[>input:checked]/radio:andgroup-aria-disabled/radio:togroup-has-[>input:disabled]/radio:. Leave agroup/radiothat your own element sets alone. - Leave
aria-checked:alone on every other element, such as aSwitchor aDropdownMenu.CheckboxItem. - Rename
onChangetoonValueChangeonRadioGroup.Root,RadioGroup.List, andSegmentedControl.Root. The handler still receives the value string.onChangeon a root is now the DOMchangeevent of the<div>, the same name every selection component in mantle uses. Remove theasprop from radio items and thebyprop fromRadioGroup.RootandSegmentedControl.Root. Values are strings, sobycompared strings only. The codemod does this step. - In a
RadioGroup.Indicatorrender-props function, keepcheckedanddisabled. Replacefocuswith agroup-has-[>input:focus-visible]/radio:class,hoverwith agroup-hover/radio:class, and dropautofocus. - In tests,
getByRole("radio")now returns the<input>. Query the item withradio.parentElementwhen the test needs the item'sdata-slot, class, or layout. - After editing, run the project's formatter, linter, type checker, and tests.
Why native radios? A native radio group needs no JavaScript for the tab stop, Space, or form submission. Headless UI also patched
HTMLElement.prototype.focusand added document listeners on every page that used a radio. TheRadioGroupentry is about 8.6 KB smaller (gzip), and a page of radios hydrates in less than half the time.
Before → after
The value handler
A checked and disabled style on an item
A style on a child that reads the item
A custom indicator
Edge cases & gotchas
- A group with no
name, or an empty one, submits nothing, as before. Its radios share a generated name, so the arrow keys still move between them. requiredandformare new onRadioGroup.RootandSegmentedControl.Root. They go to each radio, and they need aname: a group with no name has no form owner.- A form reset returns the group to
defaultValueand callsonValueChangewith it. A controlled group keeps itsvalueuntil the parent changes it. - An
onClickonRadioGroup.InputSandboxruns first. Callevent.preventDefault()in it to keep the radio unchecked. - Put one
Choicein each radio item. Its title and description take the ids that the radio reads. - Safari moves Tab to radios only when "Press Tab to highlight each item" is on, or with Option-Tab. This is the same rule Safari applies to every native button and checkbox.
- With no checked item, Shift+Tab into the group lands on the last radio, the native behavior. Tab still lands on the first.
- A
Choice.Title, aChoice.Label, or a<label htmlFor>with the item'sidnames the radio alone. Without one, all of the item's text is the radio's name, as before.
How to verify
- Run
rg 'aria-checked:|aria-disabled:|group-aria-checked/radio|data-headlessui' <path>in the files from step 1, and confirm that each remaining match is on an element that is not a radio item. - Check a checked, a disabled, and a keyboard-focused item in the browser in both themes.
- Run the type checker: it reports each removed
asorbyprop, each removed render-prop field, and each rootonChangewhose handler takes a string.