Skip to content

Commit 80594a3

Browse files
authored
Merge pull request #48 from sparksuite/fix-capturing-keys
Fix capturing keys we shouldn't
2 parents 7b5e928 + d4fdeb3 commit 80594a3

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/use-dropdown-menu.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ export default function useDropdownMenu(itemCount: number) {
6363

6464
// Create a function that handles menu logic based on keyboard events that occur on menu items
6565
const itemListener = (e: React.KeyboardEvent<HTMLAnchorElement>) => {
66-
// Create mutable value that initializes as the currentFocusIndex value
67-
let newFocusIndex = currentFocusIndex.current;
68-
6966
// Destructure the key property from the event object
7067
const { key } = e;
7168

72-
// Prevent default browser behavior except in cases where maintaining the natural tab order is desired
73-
if (key !== 'Tab' && key !== 'Shift' && key !== 'Enter') {
74-
e.preventDefault();
69+
// Ignore keys that we shouldn't handle
70+
if (!['Tab', 'Shift', 'Enter', 'Escape', 'ArrowUp', 'ArrowDown'].includes(key)) {
71+
return;
7572
}
7673

74+
// Create mutable value that initializes as the currentFocusIndex value
75+
let newFocusIndex = currentFocusIndex.current;
76+
7777
// Controls whether the menu is open or closed, if the button should regain focus on close, and if a handler function should be called
7878
if (key === 'Escape') {
7979
setIsOpen(false);

0 commit comments

Comments
 (0)