Escape closes the Dropdown but does not return focus to the trigger
還沒有人認領這個 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>
);
}
- Click "Actions". The menu opens.
- Press Tab. Focus moves into the menu.
- 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:
@rc-component/portal,es/useEscKeyDown.js:onGlobalKeyDowncallsonEscfor the popup on top of the stack. In@rc-component/trigger,es/index.js,onEsccallstriggerOpen(false).@rc-component/dropdown,es/hooks/useAccessibility.js:handleKeyDownonESCcallshandleCloseMenuAndReturnFocus(), 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,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
react-component/dropdown 的其他 Issue
-
Escape inside a focused submenu closes the entire Dropdown instead of returning to its parent item未關閉
難度 3/5 1-2 天 新手友好度 67/100
react-component/dropdown#274 ·
-
難度 3/5 1-2 天 新手友好度 45/100
react-component/dropdown#245 ·
-
難度 3/5 1-2 天 新手友好度 35/100
react-component/dropdown#242 ·
-
難度 5/5 一週以上 新手友好度 15/100
react-component/dropdown#241 ·
-
難度 5/5 一週以上 新手友好度 20/100
react-component/dropdown#231 ·
查看 react-component/dropdown 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 2 天內回覆
-
reproduced
難度 2/5 1-3 小時 新手友好度 74/100
anomalyco/opencode#62052 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
strapi/design-system#2084 ·
維護者通常 3 天內回覆
-
check:passed streams:remove
難度 1/5 1 小時以內 新手友好度 67/100
維護者通常 1 天內回覆
-
OAuth documentation for GET /api/v2/users/me/ appears inconsistent with required global read scope未關閉
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆