Keep focus in the list when a ComboboxSelect popup opens with nothing selected
Context
While testing #7614, which fixes #7612, a check of ComboboxSelect popups with a heading and a dismiss button before the list found that the popup can send its initial focus out of the list. A separate check confirmed the behavior on main in Chrome, Firefox, and Safari.
Problem
In a select without a ComboboxInput, ComboboxItem puts data-autofocus only on the last selected item (combobox-item.tsx#L115-L121, #L255-L272). When nothing is selected, no item has the marker, so Dialog focuses the first tabbable element in the popup (dialog.tsx#L520-L535). In a popup that renders ComboboxHeading and ComboboxDismiss before a nested ComboboxList, that element is the dismiss button. The dismiss button is outside the composite, so the arrow keys, typeahead, Home, and End do nothing.
Without a tabbable element before the list, Dialog focuses the list, and ComboboxList sends focus back to the select (combobox-list.tsx#L65-L84), so the arrow keys work.
Reproduction
import * as Ariakit from "@ariakit/react";
export default function Example() {
return (
<Ariakit.ComboboxProvider defaultSelectedValue="">
<Ariakit.ComboboxSelect aria-label="Fruit" />
<Ariakit.ComboboxPopover>
<Ariakit.ComboboxHeading>Fruits</Ariakit.ComboboxHeading>
<Ariakit.ComboboxDismiss>Dismiss fruits</Ariakit.ComboboxDismiss>
<Ariakit.ComboboxList>
{["Apple", "Banana", "Cherry"].map((value) => (
<Ariakit.ComboboxItem key={value} value={value} />
))}
</Ariakit.ComboboxList>
</Ariakit.ComboboxPopover>
</Ariakit.ComboboxProvider>
);
}- Click the select, or focus it and press Enter.
- Press ArrowDown.
Observed: the dismiss button has focus after the popup opens, no item is active, and ArrowDown does nothing. Enter activates the dismiss button. Shift+Tab goes back to the select, and ArrowDown works from there.
Expected: focus stays in the list, and ArrowDown activates Apple.
The result is the same with defaultSelectedValue={[]} and with virtualFocus={false}. When a value is selected, focus stays in the list. In a multi-select, unchecking the only selected item while the popup is still positioning has the same result when the popup is placed.
Pre-existing on main
Yes. It reproduces on main at 42ce68d in Chrome, Firefox, and Safari, with virtual and real focus. The reproductions used temporary sandboxes only. The legacy Select has the same symptom with an empty value and a SelectDismiss before the list. It is soft-deprecated (#6836), so this issue covers ComboboxSelect.
Why #7614 can finish without it
#7614 keeps a keyboard move made while the popup is positioning. This problem needs no move: it happens on every open when nothing is selected.
Expected outcome
When a ComboboxSelect popup without a ComboboxInput opens with nothing selected, focus stays in the list, and the arrow keys and typeahead move through the items.
Workaround
https://github.com/ariakit/ariakit/issues/7626#issuecomment-5808673036
Source: ariakit/ariakit