You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Escape closes the Dropdown but does not return focus to the trigger #272
Expected: the menu closes and focus goes back to the "Actions" button, as useAccessibility means it to (handleCloseMenuAndReturnFocus calls triggerRef.current.focus()).
Actual: the menu closes and document.activeElement is document.body. A keyboard user has lost their place on the page.
Cause, as far as we can tell
Two listeners on window handle the same Escape key press:
@rc-component/portal, es/useEscKeyDown.js: onGlobalKeyDown calls onEsc for the popup on top of the stack. In @rc-component/trigger, es/index.js, onEsc calls triggerOpen(false).
@rc-component/dropdown, es/hooks/useAccessibility.js: handleKeyDown on ESC calls handleCloseMenuAndReturnFocus(), which focuses the trigger.
The portal's listener is added first (the Portal is a child, so its effect runs before the Dropdown's), so it runs first. It closes the popup. React renders that close before the next listener runs, and the Dropdown's effect cleanup removes its own keydown listener. A listener removed during dispatch is not called, so the focus return never happens.
Note: a test that calls dispatchEvent from script may not show this, because microtasks do not run between listeners then, so React does not render the close in between. A real key press in a browser shows it.
Workaround we use
We pass our own onOpenChange that adds a capture-phase keydown listener on window while the menu is open, notes that Escape was pressed, and on close focuses the element with the ant-dropdown-open class. We would like to remove it once this is fixed here.
Activity