Skip to content

Commit 153a257

Browse files
committed
Split into two separate useEffect() Hooks
They have different areas of concern
1 parent 7361be1 commit 153a257

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/use-dropdown-menu.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,22 @@ export default function useDropdownMenu(itemCount: number) {
2222
itemRefs.current[itemIndex].current?.focus();
2323
};
2424

25-
// Run whenever the menu opens/closes
25+
// Focus the first item when the menu opens
2626
useEffect(() => {
27-
// Stop if the ref currently points to a null value
28-
if (!itemRefs.current[0].current || !buttonRef.current) {
29-
return;
30-
}
31-
3227
// Stop if this is the first fire of the Hook, and update the ref
3328
if (firstRun.current) {
3429
firstRun.current = false;
3530
return;
3631
}
37-
32+
3833
// If the menu is currently open focus on the first item in the menu
3934
if (isOpen) {
4035
moveFocus(0);
4136
}
42-
37+
}, [isOpen]);
38+
39+
// Handle listening for clicks and auto-hiding the menu
40+
useEffect(() => {
4341
// This function is designed to handle every click
4442
const handleEveryClick = (event: MouseEvent) => {
4543
// Ignore if the menu isn't open

0 commit comments

Comments
 (0)