Escape closes the Dropdown but does not return focus to the trigger

未关闭
#272 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript

调研方向

Start with es/hooks/useAccessibility.js and its handleKeyDown and handleCloseMenuAndReturnFocus functions, then read es/index.js and the portal Escape listener described in the issue to trace listener ordering and cleanup. Reproduce with a real Escape key press in a browser, since the issue says scripted dispatchEvent may not expose the behavior. Done means closing the menu with Escape returns focus to the trigger; the issue names no test file.

由索引模型根据 Issue 内容生成。

描述

Versions

  • @rc-component/dropdown 1.0.3
  • @rc-component/portal 2.2.1
  • @rc-component/trigger 3.10.1
  • antd 6.6.6
  • react and react-dom 19.3.0
  • Chrome 155 on macOS. Checked with real clicks and key presses on a bare page holding only the snippet below.

Steps

import { Button, Dropdown } from 'antd';

export default function App() {
    return (
        <Dropdown menu={{ items: [{ key: 'a', label: 'Rename' }] }} trigger={['click']}>
            <Button>Actions</Button>
        </Dropdown>
    );
}
  1. Click "Actions". The menu opens.
  2. Press Tab. Focus moves into the menu.
  3. Press Escape.

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:

  1. @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).
  2. @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.

主要语言
TypeScript
星标
181
派生
112
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

react-component/dropdown 的其他 Issue

查看 react-component/dropdown 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。