Skip to content

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

Open

Description

@aseutin

Summary

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

  1. Mount the palette and read the input's attributes before touching it.
    • Observed: the first item carries aria-selected="true", and the input has no aria-activedescendant attribute at all.
  2. Type a query that leaves one match (sign).
    • Observed: still no aria-activedescendant.
  3. 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).
  4. 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.

Activity

  1. dimitribee commented on Sep 29, 2026

    @dimitribee

    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 DOM
      store.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:

           } else if (key === 'value') {
             ...
    -        schedule(7, () => {
    -          state.current.selectedItemId = getSelectedItem()?.id
    -          store.emit()
    -        })
           emit: () => {
    +        state.current.selectedItemId = selectedItemIdFromValue()
             listeners.current.forEach((l) => l())
           },
    /** The first rendered item whose value is the selected value - the same test `Item` uses for `aria-selected`. */
    function selectedItemIdFromValue() {
      const value = state.current.value
      if (!value) return undefined
      for (const [id, item] of ids.current) {
        if (item.value !== value || allGroups.current.has(id)) continue
        const rendered =
          !allItems.current.has(id) || // forceMount
          !state.current.search ||
          propsRef.current.shouldFilter === false ||
          state.current.filtered.items.get(id) > 0
        if (rendered) return id
      }
    }

    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.

  2. timmothy17 commented on Nov 20, 2026

    @timmothy17

    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-selected
    schedule(7, () => {
      state.current.selectedItemId = getValidItems().find(
        (item) => item.getAttribute(VALUE_ATTR) === state.current.value,
      )?.id
      store.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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions