skip to content

In a music-streaming web app's search dropdown, where should focus stay as users arrow through suggestions, and how do screen readers learn the active one?

level: middleimportance: must knowfreq 52%

answer

  1. two kinds of focus
  2. typing must keep working
  3. the input points at an option id
  4. ids stable per catalogue item
  5. scroll it into view yourself

basics

~20 s

DOM focus stays in the text input; the input's aria-activedescendant names the id of the active option, so screen readers announce it while typing, the caret and the phone keyboard keep working. Keep option ids stable across refreshes.

solid answer

~50 s

DOM focus stays in the input the whole time, and only the active option changes. The input has `role="combobox"`, `aria-controls` pointing at the listbox, `aria-expanded`, and `aria-activedescendant` set to the id of the option the arrow keys reached; the browser then reports that option to assistive technology as if it had focus. Keeping focus in the input means the user can keep refining the text, the caret and the input method still work, and the phone's on-screen keyboard does not close. For this product I would derive option ids from the catalogue item, so when remote results re-render the list the attribute still points at the same artist or track; if that item leaves the list, I remove the attribute rather than leave it dangling. The active option needs `aria-selected="true"`, the same visual highlight, and scrolling into view, which the attribute does not do.

code

html · 19 lines
html
<label for="q">Search artists, songs and podcasts</label>
<input id="q" type="text" role="combobox"
       aria-autocomplete="list" aria-expanded="true"
       aria-controls="sugg" aria-activedescendant="opt-artist-118">
<ul id="sugg" role="listbox" aria-label="Suggestions">
  <li role="presentation">
    <ul role="group" aria-labelledby="grp-artists">
      <li role="presentation" id="grp-artists">Artists</li>
      <li role="option" id="opt-artist-118" aria-selected="true">Radio Lanterns</li>
      <li role="option" id="opt-artist-904" aria-selected="false">Radial Coast</li>
    </ul>
  </li>
  <li role="presentation">
    <ul role="group" aria-labelledby="grp-tracks">
      <li role="presentation" id="grp-tracks">Songs</li>
      <li role="option" id="opt-track-5521" aria-selected="false">Radiant Hours</li>
    </ul>
  </li>
</ul>

go deeper

for a junior

Remember the rule: focus stays in the text box, and the active suggestion is pointed at with aria-activedescendant rather than focused directly.

for a middle

Explain why DOM focus stays put (typing, caret, input method, phone keyboard) and walk through the attributes on the input, the listbox and each option.

for a senior

Show what breaks as the list changes: index-based ids, dangling references, highlight and attribute out of step. Say when rows with a second action call for the grid popup.

for a principal

Decide what the shared search component guarantees every team: one focus model, id rules and an accessibility test that runs on refreshes, not just the first render.

## Two kinds of focus A search dropdown separates two ideas that usually coincide: - **DOM focus** is where keystrokes go: the element the browser treats as focused. - **Assistive-technology focus** is what a screen reader presents as the current item. The combobox pattern keeps **DOM focus on the combobox** while assistive-technology focus moves inside the popup through **`aria-activedescendant`**. The browser keeps real focus on the input, and reports the referenced element to assistive technology as if that element had focus. The attribute does not move focus itself: the page's script sets and updates it. ## Why focus must stay in the input In a **music-streaming web app's** global search, the user arrows down to "Radio Lanterns", realises they wanted the live album, and types " live". That only works if focus never left the input. - **Typing continues.** Printable characters land in the text, so refining the query does not require leaving the list. - **Caret and selection** keep working: Home, End, selecting a word to replace it. - **Input-method composition** stays attached to the text field. - **The phone's on-screen keyboard stays open.** Moving focus to a non-editable option closes it on many phones. - **Escape** simply closes the popup and the user is already where they started. Moving real focus into the options with a roving tab index suits a standalone listbox or menu, where nothing is being typed. It does not suit a box the user is still typing into: every refinement would first need a trip back to the input, and on a phone each trip reopens the keyboard and shifts the page under the user. ## The wiring | Element | Attribute | Purpose | |---|---|---| | input | `role="combobox"` | marks the text field as the combobox | | input | `aria-controls` | names the popup it controls | | input | `aria-expanded` | `true` while the popup shows, `false` otherwise | | input | `aria-autocomplete="list"` | suggestions relate to the typed text | | input | `aria-activedescendant` | id of the active option; absent when none is active | | popup | `role="listbox"` | the suggestions container, with a label | | row | `role="option"` with `aria-selected="true"` on the active one | each suggestion | The grouped dropdown, with artists, songs, albums, playlists and podcasts, puts options into labelled groups; the group labels are not options, and the arrow keys skip them. ## What this product adds 1. **Stable ids per catalogue item.** Derive each option's id from its type and catalogue id. When the remote half of the list lands and the list re-renders, the attribute still names the same artist, so the screen reader and the next Enter agree. 2. **Never point at a missing element.** If the active item leaves the list on a new keystroke, remove the attribute, or move it deliberately to a sensible neighbour. The value must refer to an option inside the controlled popup. 3. **Scroll the active option into view.** The attribute neither creates focus nor scrolls; the page keeps the active option visible. 4. **One source of truth for "active".** The visual highlight, `aria-selected` and `aria-activedescendant` change together, whether the keyboard or the pointer moved them. 5. **Rows with a second action.** If each row also offers "play preview" or "go to artist", one option per row no longer fits; the grid form of the popup lets a row hold cells while DOM focus still stays in the input. What Enter or leaving the field commits is a separate component decision, and so are the meaning of each role and the full listbox keyboard model. ## Common failure modes - Focus moved into the list, so typing stops working and the phone keyboard drops. - Ids generated from the row index, so after a refresh the attribute names a different item from the one highlighted. - A highlight that moves visually while the attribute stays put, so sighted and screen-reader users are on different rows. - The active option scrolled out of sight in a long grouped list. - The popup closed by a refresh while `aria-expanded` still says `true`, so a screen reader describes a list that is not there. ## Summary Keep DOM focus in the input, point at the active option with `aria-activedescendant`, and make that pointer survive the list changing: stable ids, cleared when the item goes, scrolled into view, and kept in step with the visual highlight.

  • Why derive option ids from catalogue ids rather than row positions?
    Because the list changes under the user. When remote results land or a group grows, row three may become a different artist. An id built from the position would keep pointing at row three, so the screen reader would announce one item while Enter opened another. An id built from the item follows it wherever it moves.
  • Does aria-activedescendant also make the browser scroll the active option into view?
    No. It only tells assistive technology which descendant to treat as focused; it creates neither focus nor scrolling. The page has to keep the active option visible itself, for example by scrolling it into the nearest visible position each time the arrow keys change it.

  • Move DOM focus into each option so the screen reader announces it.
  • aria-activedescendant also scrolls the active option into view.
  • Option ids can be the row index; they only need to be unique.
  • Leave aria-activedescendant set after its option leaves the list; readers ignore it.
  • Group headings like Artists should be options so the arrow keys land on them.