Skip to content

Fix for possible race conditions #268

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

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-accessible-dropdown-menu-hook",
"version": "2.2.1",
"version": "2.2.2",
"description": "A simple Hook for creating fully accessible dropdown menus in React",
"main": "dist/use-dropdown-menu.js",
"types": "dist/use-dropdown-menu.d.ts",
Expand Down
20 changes: 9 additions & 11 deletions src/use-dropdown-menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import React, { useState, useRef, createRef, useEffect } from 'react';
import React, { useState, useRef, createRef, useEffect, useMemo } from 'react';

// Create interface for button properties
interface ButtonProps
Expand Down Expand Up @@ -32,12 +32,10 @@ export default function useDropdownMenu(itemCount: number): DropdownMenuResponse

// Create refs
const buttonRef = useRef<HTMLButtonElement>(null);
const itemRefs = useRef<React.RefObject<HTMLAnchorElement>[]>([]);

// Initialize refs and update them when the item count changes
useEffect(() => {
itemRefs.current = Array.from({ length: itemCount }, () => createRef<HTMLAnchorElement>());
}, [itemCount]);
const itemRefs = useMemo<React.RefObject<HTMLAnchorElement>[]>(
() => Array.from({ length: itemCount }, () => createRef<HTMLAnchorElement>()),
[itemCount]
);

// Create type guard
const isKeyboardEvent = (e: React.KeyboardEvent | React.MouseEvent): e is React.KeyboardEvent =>
Expand All @@ -46,7 +44,7 @@ export default function useDropdownMenu(itemCount: number): DropdownMenuResponse
// Handles moving the focus between menu items
const moveFocus = (itemIndex: number): void => {
currentFocusIndex.current = itemIndex;
itemRefs.current[itemIndex].current?.focus();
itemRefs[itemIndex].current?.focus();
};

// Focus the first item when the menu opens
Expand Down Expand Up @@ -175,10 +173,10 @@ export default function useDropdownMenu(itemCount: number): DropdownMenuResponse
newFocusIndex += 1;
}

if (newFocusIndex > itemRefs.current.length - 1) {
if (newFocusIndex > itemRefs.length - 1) {
newFocusIndex = 0;
} else if (newFocusIndex < 0) {
newFocusIndex = itemRefs.current.length - 1;
newFocusIndex = itemRefs.length - 1;
}
}

Expand All @@ -203,7 +201,7 @@ export default function useDropdownMenu(itemCount: number): DropdownMenuResponse
onKeyDown: itemListener,
tabIndex: -1,
role: 'menuitem',
ref: itemRefs.current[index],
ref: itemRefs[index],
}));

// Return a listener for the button, individual list items, and the state of the menu
Expand Down