Restore the right value on Escape after a ComboboxSelect opens with an arrow key
Context
While investigating alternative designs for #7614, which fixes #7612, a design review suggested that resetOnEscape records the wrong value when a ComboboxSelect opens with an arrow key. A separate check confirmed the behavior on main in Chrome, Firefox, and Safari.
Problem
ComboboxPopover records the selected value that Escape restores until the user moves through the items. When the popup opens, it stops recording as soon as the store's moves count is not zero (combobox-popover.tsx#L126-L128), before it records the value for that open (#L130). An arrow key that opens a closed ComboboxSelect moves the active item before the popup opens (combobox-select.tsx#L140-L160), so moves is not zero when the popup opens, and the recorded value stays at the one from an earlier open.
With selectOnMove, which resetOnEscape follows by default, Escape then restores that stale value. The resetOnEscape documentation says: "Selection changes made before any item movement become part of the selected value Escape restores."
Reproduction
import * as Ariakit from "@ariakit/react";
export default function Example() {
return (
<Ariakit.ComboboxProvider defaultSelectedValue="Apple" selectOnMove>
<Ariakit.ComboboxSelect aria-label="Fruit" />
<Ariakit.ComboboxPopover>
{["Apple", "Banana", "Cherry", "Grape"].map((value) => (
<Ariakit.ComboboxItem key={value} value={value} />
))}
</Ariakit.ComboboxPopover>
</Ariakit.ComboboxProvider>
);
}- Focus the select and press ArrowDown to open it.
- Press ArrowDown. The value is
Banana. - Press Enter. The popup closes with
Bananaselected. - Press ArrowDown to open it again.
- Press Escape.
Observed: the select shows Apple. Expected: the select shows Banana.
The same happens when the value changes while the popup is closed, for example with setSelectedValue("Grape"), and the popup then opens with an arrow key: Escape restores Apple instead of Grape. Opening with Enter restores the right value. With unmountOnHide, the popover records the value again on mount, so it also restores the right value.
Pre-existing on main
Yes. It reproduces on main at 42ce68d in Chrome, Firefox, and Safari. The reproductions used temporary sandboxes only. The existing resetOnEscape tests open the popup with a click, so they don't cover this path.
Why #7614 can finish without it
#7614 changes where the popup puts its initial focus after positioning. It does not change how ComboboxPopover records the value that Escape restores.
Expected outcome
Escape restores the value that was selected when the popup opened, whether a click, Enter, or an arrow key opened it.
Notes for the fix
ComboboxSelect already records the moves count each time the popup opens, keyed by the select element (useTrackComboboxSelectPresentation), and that count includes the arrow key that opened the popup. After #7614, ComboboxPopover reads the same baseline for its initial focus. Comparing with it, instead of checking for any move, may be enough for a select. A combobox with an input has no select element, so it needs its own rule.
Workaround
The unmountOnHide workaround and current-main verification keep the selected value when Escape closes the popup.
Source: ariakit/ariakit