-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add escape event handling for better a11y #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d12939d
3b47b49
6d93e00
bc940e6
044ca94
bfbaef8
db063a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ yarn.lock | |
| package-lock.json | ||
| pnpm-lock.yaml | ||
| .doc | ||
| .vscode | ||
|
|
||
| # dumi | ||
| .dumi/tmp | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { useEffect, useMemo } from 'react'; | ||
| import { type EscCallback } from './Portal'; | ||
| import useId from '@rc-component/util/lib/hooks/useId'; | ||
| import { useEvent } from '@rc-component/util'; | ||
|
|
||
| let stack: string[] = []; | ||
|
|
||
| export default function useEscKeyDown(open: boolean, onEsc?: EscCallback) { | ||
| const id = useId(); | ||
|
|
||
| const handleEscKeyDown = useEvent((event: KeyboardEvent) => { | ||
| if (event.key === 'Escape') { | ||
| const top = stack[stack.length - 1] === id; | ||
| onEsc?.({ top, event }); | ||
| } | ||
| }); | ||
|
|
||
| useMemo(() => { | ||
| if (open && !stack.includes(id)) { | ||
| stack.push(id); | ||
aojunhao123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if (!open) { | ||
| stack = stack.filter(item => item !== id); | ||
| } | ||
| }, [open, id]); | ||
|
Comment on lines
+18
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 严重问题: 存在两个关键问题:
建议的修复方案:将 stack 操作移至 🔎 推荐的修复方案- useMemo(() => {
- if (open && !stack.includes(id)) {
- stack.push(id);
- } else if (!open) {
- stack = stack.filter(item => item !== id);
- }
- }, [open, id]);
-
useEffect(() => {
- if (!open) {
- return;
- }
+ if (open && !stack.includes(id)) {
+ stack.push(id);
+ }
- window.addEventListener('keydown', handleEscKeyDown);
+ if (open) {
+ window.addEventListener('keydown', handleEscKeyDown);
+ }
return () => {
+ stack = stack.filter(item => item !== id);
window.removeEventListener('keydown', handleEscKeyDown);
};
}, [open, id]);🤖 Prompt for AI Agents |
||
|
|
||
| useEffect(() => { | ||
| if (!open) { | ||
| return; | ||
| } | ||
|
|
||
| window.addEventListener('keydown', handleEscKeyDown); | ||
|
||
|
|
||
| return () => { | ||
| window.removeEventListener('keydown', handleEscKeyDown); | ||
| }; | ||
| }, [open, id]); | ||
aojunhao123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
onEscprop lacks documentation. Consider adding a JSDoc comment to describe its purpose, parameters, and usage. For example, explaining that the callback receives an object withisTop(boolean indicating if this portal is the topmost) andevent(the keyboard event).