Repository navigation
[accordion][combobox][dialog][field][fieldset][menu][meter][popover][progress][select][slider][tabs][toast] Register the rendered id of labels, titles, and descriptions - #6358
Conversation
A part that gets its id from a `render` element now registers that id with its owner, so `aria-labelledby` and `aria-describedby` point to an element that exists. When the render element removes the id, the owner gets no reference. Co-Authored-By: Claude Opus 5.5 <[email protected]>
commit: |
Bundle size
PerformanceNo regressions in 36 benchmarks · precise to ±10.3% (±19.9% at worst) · ran 3m 54s · details 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. |
- Field.Error resolves the render id only while mounted, so a pending lazy render element does not suspend the field. - An id-less label or toast part releases only its own id and does not clear the id of another part. - Field.Label uses its id prop, not the id that it registered before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Dialog.Title, Dialog.Description, Popover.Title, and Popover.Description now register their id through useRegisteredLabelId. An older part that loses its id or unmounts no longer clears the id of a newer part. Co-Authored-By: Claude Opus 5.5 <[email protected]>
GPT Astra 6.2 reviewI would approve this head. No remaining code findings in the documented component configurations. All eight new Dialog and Popover regression tests failed on the preceding commit a1950bd and passed on 14df40e. They cover retaining the newer accessible name or description when an older part removes its ID or unmounts. The earlier Field.Error, Field.Label, Select.GroupLabel and Toast.Title reproductions also pass. The two-trigger Accordion case still reproduces outside the documented one-trigger-per-item configuration. |
michaldudak
left a comment
There was a problem hiding this comment.
The registered id matches the DOM id in every changed part. Two non-blocking nits:
1. 🟡 Field.Label stops labelling the control after a sibling label unmounts
packages/react/src/field/label/FieldLabel.tsx:41
const labelProps = useLabel({
id: idProp, // before: labelId ?? idProp
render,
native: nativeLabel,
});On master, Field.Label took its id from the context labelId, so when another label released its id, the surviving label re-rendered with a new id and registered again. Now its id no longer depends on context, so the registration effect never re-runs after the last-registered label unmounts.
<Field.Root>
<Field.Label id="a">A</Field.Label>
{second && <Field.Label id="b">B</Field.Label>}
<Field.Control />
</Field.Root>| State | master | this PR |
|---|---|---|
| Both labels mounted | aria-labelledby="b", both labels have id="b" |
aria-labelledby="b", labels have a and b |
After second becomes false |
aria-labelledby="a" |
no aria-labelledby |
It needs two label parts in one Field.Root with the last-registered one unmounting, so it is narrow, and master's duplicate id in the same setup was wrong too. A native <label htmlFor> still names a native control, so it only matters for nativeLabel={false} labels and controls named through aria-labelledby. The same applies to a Field.Label next to a Select.Label, Combobox.Label or Slider.Label in one field.
Either register again when the context labelId becomes undefined while the label still has an id, or accept "last label wins, no fallback" as Meter, Progress and Fieldset already do.
2. 🟡 Four copies of the same store setter adapter
packages/react/src/dialog/title/DialogTitle.tsx:23 (also DialogDescription.tsx, PopoverTitle.tsx, PopoverDescription.tsx)
const setTitleId = useStableCallback((action: React.SetStateAction<string | undefined>) => {
store.set(
'titleElementId',
typeof action === 'function' ? action(store.state.titleElementId) : action,
);
});
const id = useRegisteredLabelId(componentProps, setTitleId);Each of the four parts adds a useStableCallback and an identical updater shim that differs only in the store key; Combobox.Label and Select.Label carry the same shim for labelId. A shared helper that takes the store and key would cover all of them. If two titles or descriptions in one popup are not worth supporting, store.useSyncedValueWithCleanup('titleElementId', id) with the resolved id is smaller and already handles the id-less case.
Dialog and Popover titles and descriptions, Select.Label, and Combobox.Label each had a copy of the same updater shim. The new useStoreIdSetter helper replaces all six copies. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
When a part gets its id from a
renderelement (render={<div id="custom" />}), the DOM element hasid="custom", but the part registered its generated id with the owner. The owner'saria-labelledbyoraria-describedbythen pointed to an id that no element has.The parts now resolve their id with the existing
resolveRenderedIdhelper (added in #5527), so the owner gets the id that the DOM element has. When the render element setsid={undefined}, the element has no id, and the owner gets no ARIA reference.Fixed parts:
Accordion.Trigger(panelaria-labelledby)Combobox.GroupLabel,Menu.GroupLabel,Select.GroupLabel(grouparia-labelledby)Combobox.Label,Select.Label(triggeraria-labelledby)Dialog.Title,Dialog.Description, and theAlertDialogandDrawerparts that reuse themPopover.Title,Popover.DescriptionField.Label,Field.Description,Field.ErrorFieldset.Legend,Meter.Label,Progress.Label,Slider.LabelTabs.Tab(panelaria-labelledby)Toast.Title,Toast.DescriptionNotes:
useRegisteredLabelIdanduseLabeltake therenderprop. The three group labels useuseRegisteredLabelIdinstead of a copy of its effect. The Dialog and Popover titles and descriptions also use it, so a part that unmounts or loses its id releases only its own id.id=""no longer gives the owneraria-labelledby=""oraria-describedby="".Accordion.Triggerkeeps its old behavior and uses the generated id.Field.Labelnow uses itsidprop. Before, it kept the first id that it registered and ignored a lateridchange.id={undefined}behavior from [dialog][popover][drawer] Keep generated ids when id is undefined #6343 does not change: the part keeps its generated id.aria-controlsare not in this PR.This lets Material UI remove the
Menu2GroupLabelworkaround from mui/material-ui#48823.Generated with Claude Code