You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Input's aria-activedescendant is absent at mount and goes stale after filtering, so it can name a row other than the selected one #413
In cmdk 1.1.1, Command.Input never exposes a usable aria-activedescendant: it is absent at a pristine mount and, once set, goes stale after the filter changes — so it can name a row other than the one aria-selected marks and other than the one Enter activates.
Environment
cmdk 1.1.1, React 19, Chromium (headless, via Playwright), measured 2026-09-04.
A plain Command with an Input, a List, two Groups of Items ("Open jobs", "New job" / "Profile", "Sign out"), uncontrolled value, default filtering. No disablePointerSelection, no custom filter.
Steps to reproduce
Mount the palette and read the input's attributes before touching it.
Observed: the first item carries aria-selected="true", and the input has noaria-activedescendant attribute at all.
Type a query that leaves one match (sign).
Observed: still no aria-activedescendant.
Clear the query, press ArrowDown until "Profile" is selected, type pro, then clear the query again.
Observed: aria-activedescendant now names "Profile" while aria-selected="true" is on "Open jobs" (the first item, re-selected on clear).
Press Enter at that state.
Observed: cmdk-item-select dispatches on "Open jobs".
So after step 3 the row a screen reader is told about (aria-activedescendant → "Profile") is not the row that is selected or the row Enter runs ("Open jobs"). In two runs the attribute named an item id that was no longer in the DOM at all.
Expected
aria-activedescendant on the input tracks the selected item on every change — including the initial selection and re-selection after a filter — so it always names the same item aria-selected="true" is on and the one Enter would activate. Per the ARIA combobox/listbox pattern, the input's aria-activedescendant is what assistive technology uses to announce the current option; a stale or missing value means a screen-reader user hears the wrong row or none.
Why a consumer cannot work around it
Command.Input spreads incoming props before its own attributes and then writes aria-activedescendant from useCommandState(m => m.selectedItemId), so a value passed from outside is overwritten by the library. (The same ordering means role passed to Command.Separator is discarded — a separate, smaller issue.) Patching the package or re-implementing the input inside the cmdk root are the remaining options, neither of which a consumer should need.
Where the id seems to go stale
selectedItemId is read from the store, but the store's selected id is derived from value and the item registry; when the filter unmounts the selected item and the first visible item is re-selected, the id the input renders lags one update behind (or points at an unmounted node), and at first mount nothing sets it at all. I have not traced the exact line and may be wrong about the mechanism; the observations above are what we measured.
Context
Found while auditing keyboard and screen-reader behaviour of a design system that composes Select and Menu components on cmdk. Happy to test a fix or a canary build against the same driven sequence.
I traced the mechanism behind this (and #373, which is the same bug). It matches the observations above, and we now ship a patch for it.
Why the id is missing or stale
selectedItemId is written by a callback that reads the selected row back out of the DOM (cmdk/src/index.tsx L251):
schedule(7,()=>{state.current.selectedItemId=getSelectedItem()?.id// [aria-selected="true"] in the DOMstore.emit()})
setState('value') usually runs inside another scheduled callback: selectFirstItem from schedule(3) at mount, or from schedule(1) after a search change. useScheduleLayoutEffect runs those with fns.current.forEach(...) (L1051), and Map.prototype.forEach visits entries added while it iterates. So schedule(7) runs in the same flush, before any item has re-rendered with the new aria-selected. It finds no row (at mount, after typing) or the previous one (after a clear re-selects the first row).
An arrow key works because setState('value') is then called from an event handler, and schedule(7) waits for a real render.
The fix we apply
We derive the id from the same state Item uses for aria-selected, at emit time, instead of reading the DOM:
/** The first rendered item whose value is the selected value - the same test `Item` uses for `aria-selected`. */functionselectedItemIdFromValue(){constvalue=state.current.valueif(!value)returnundefinedfor(const[id,item]ofids.current){if(item.value!==value||allGroups.current.has(id))continueconstrendered=!allItems.current.has(id)||// forceMount!state.current.search||propsRef.current.shouldFilter===false||state.current.filtered.items.get(id)>0if(rendered)returnid}}
Every path that changes the selection, the filter or the item registry already ends in emit(), including the controlled-value effect. So the input, the list and the rows all read one state in the same render.
Two details that matter for #411, which takes a similar approach (a lookup by value in ids):
Skip groups.Group registers its heading in ids through useValue (L744). A lookup by value alone returns the group's id when a heading and an item share text, for example a "Profile" group containing a "Profile" item.
Only rendered items. A filtered-out item keeps its entry in ids. Without the visibility check, the id can point at a node that is not in the DOM, which is the "id no longer in the DOM" case above.
We verified it against the sequence in this issue: at mount, while typing, a clear after an arrow-key move, no matches, a controlled value (set at mount, from outside and by keyboard), shouldFilter={false} with rows remounting on every keystroke, and Enter running the named row. It ran in Vitest and in Playwright against a production build, on React 19. Happy to open a PR with this and the tests if it would help.
We hit this in 1.1.1 too and traced the stale/missing value to the order of updates:
When value changes, setState schedules selectedItemId = getSelectedItem()?.id (schedule(7, ...), cmdk/src/index.tsx ~L251). getSelectedItem() looks up [aria-selected="true"] in the DOM.
That scheduled callback runs in the scheduler's layout effect, before the items have re-rendered with the new aria-selected, so it finds the previously selected row, or none. That's why it's missing after a search and can name the old row after the filter changes.
When value is controlled, the props.value effect sets the value but never schedules the selectedItemId update, so only arrow keys and hover refresh it.
What fixed it for us, patched locally:
// derive the id from the value instead of the DOM's aria-selectedschedule(7,()=>{state.current.selectedItemId=getValidItems().find((item)=>item.getAttribute(VALUE_ATTR)===state.current.value,)?.idstore.emit()})
…and scheduling the same update from the props.value effect. With that, a screen reader announces the highlighted row after a search and on controlled value changes, and arrow keys, Home/End and hover behave as before. Happy to open a PR if that's useful.
Activity
dimitribee commentedon Sep 29, 2026
I traced the mechanism behind this (and #373, which is the same bug). It matches the observations above, and we now ship a patch for it.
Why the id is missing or stale
selectedItemIdis written by a callback that reads the selected row back out of the DOM (cmdk/src/index.tsxL251):setState('value')usually runs inside another scheduled callback:selectFirstItemfromschedule(3)at mount, or fromschedule(1)after a search change.useScheduleLayoutEffectruns those withfns.current.forEach(...)(L1051), andMap.prototype.forEachvisits entries added while it iterates. Soschedule(7)runs in the same flush, before any item has re-rendered with the newaria-selected. It finds no row (at mount, after typing) or the previous one (after a clear re-selects the first row).An arrow key works because
setState('value')is then called from an event handler, andschedule(7)waits for a real render.The fix we apply
We derive the id from the same state
Itemuses foraria-selected, at emit time, instead of reading the DOM:} else if (key === 'value') { ... - schedule(7, () => { - state.current.selectedItemId = getSelectedItem()?.id - store.emit() - })emit: () => { + state.current.selectedItemId = selectedItemIdFromValue() listeners.current.forEach((l) => l()) },Every path that changes the selection, the filter or the item registry already ends in
emit(), including the controlled-valueeffect. So the input, the list and the rows all read one state in the same render.Two details that matter for #411, which takes a similar approach (a lookup by value in
ids):Groupregisters its heading inidsthroughuseValue(L744). A lookup by value alone returns the group's id when a heading and an item share text, for example a "Profile" group containing a "Profile" item.ids. Without the visibility check, the id can point at a node that is not in the DOM, which is the "id no longer in the DOM" case above.We verified it against the sequence in this issue: at mount, while typing, a clear after an arrow-key move, no matches, a controlled
value(set at mount, from outside and by keyboard),shouldFilter={false}with rows remounting on every keystroke, and Enter running the named row. It ran in Vitest and in Playwright against a production build, on React 19. Happy to open a PR with this and the tests if it would help.fix(dashboard): keep command pickers' active option announced (w4/252)
feat(ui): rebuild the command palette on shadcn Command (#49528)
timmothy17 commentedon Nov 20, 2026
We hit this in 1.1.1 too and traced the stale/missing value to the order of updates:
valuechanges,setStateschedulesselectedItemId = getSelectedItem()?.id(schedule(7, ...),cmdk/src/index.tsx~L251).getSelectedItem()looks up[aria-selected="true"]in the DOM.aria-selected, so it finds the previously selected row, or none. That's why it's missing after a search and can name the old row after the filter changes.valueis controlled, theprops.valueeffect sets the value but never schedules theselectedItemIdupdate, so only arrow keys and hover refresh it.What fixed it for us, patched locally:
…and scheduling the same update from the
props.valueeffect. With that, a screen reader announces the highlighted row after a search and on controlledvaluechanges, and arrow keys, Home/End and hover behave as before. Happy to open a PR if that's useful.