#7625·ariakit

Keep focus inside a popover when it positions again while open

Author: diegohazCreated Sep 24, 2026Updated Sep 26, 2026
Labelsbughas workarounda11yp2hard

Context

While investigating alternative designs for #7614, which fixes #7612, a prototype that changed how Popover releases its initial focus showed that a popup takes its initial focus again after every positioning pass that runs while it is open. A separate check confirmed the behavior on main in Chrome, Firefox, and Safari.

Problem

Popover marks each positioning pass as unplaced (popover.tsx#L331-L340) and passes autoFocusOnShow: positioned && autoFocusOnShow to Dialog (popover.tsx#L642). When a pass ends, the value turns back on, so the Dialog auto-focus effect (dialog.tsx#L506-L588) runs again and focuses the popup's initial target. The effect keeps focus only when it has left the popup, so focus that the user moved inside the popup goes back to the initial target.

A pass runs again when a dependency of the positioning effect changes (popover.tsx#L510-L536). Examples are store.render(), the anchor element, placement, an arrow element that mounts, and props such as gutter or sameWidth. A viewport resize, a page scroll, or content growth goes through Floating UI's autoUpdate and does not run the effect again, so focus stays.

Reproduction

typescript
import * as Ariakit from "@ariakit/react";

export default function Example() {
  const menu = Ariakit.useMenuStore();
  return (
    <>
      <Ariakit.MenuButton store={menu}>Actions</Ariakit.MenuButton>
      <Ariakit.Menu store={menu}>
        <Ariakit.MenuItem>Item one</Ariakit.MenuItem>
        <Ariakit.MenuItem>Item two</Ariakit.MenuItem>
        <Ariakit.MenuItem>Item three</Ariakit.MenuItem>
      </Ariakit.Menu>
    </>
  );
}
  1. Focus the Actions button and press Enter. Item one has focus.
  2. Press ArrowDown twice. Item three has focus.
  3. While focus stays in the menu, call menu.render() from outside the menu, or change its placement or gutter.

Observed: focus goes back to Item one. Expected: focus stays on Item three.

The menu-placing-pass sandbox shows the same result with its Reposition button. Other popups behave the same way:

  • A menu opened with a click sends focus back to the menu container and clears the active item.
  • A Popover moves focus from its second button back to its first one.
  • A Hovercard opened through HovercardDisclosure moves focus from its second link back to its first one.
  • A ComboboxSelect makes the selected item active again. #7614 covers this case for a keyboard move.

Pre-existing on main

Yes. It reproduces on main at 42ce68d in Chrome, Firefox, and Safari with Menu, Popover, Hovercard, and ComboboxSelect. The reproductions used temporary sandboxes only.

Why #7614 can finish without it

#7614 keeps a keyboard move in a ComboboxSelect popup, including one made before a later positioning pass, by passing the moved item as the initial focus. The repeated initial focus comes from Popover and affects every popover, so a fix for it is independent.

Expected outcome

A popup takes its initial focus once each time it opens. A later positioning pass does not move focus that is already inside the popup.

Notes for the fix

  • The menu-placing-pass tests already describe this model: "an already open popup has no initial focus left to take" (test-browser.ts#L214). The test that repositions an open menu checks only the scroll position after the release, not the focused item.
  • A prototype that released the initial focus once for each open request fixed the Menu case. The same prototype also dropped the initial focus when focus entered the popup while it was positioning. That second part broke autoFocus and initialFocus for ComboboxSelect popups with unmountOnHide, because the composite focuses the active item as soon as the items mount. Keep the two parts separate.

Workaround

https://github.com/ariakit/ariakit/issues/7625#issuecomment-5808703341