Skip to content

Commit e5a777f

Browse files
authored
Merge pull request #61 from sparksuite/use-two-useeffects
Split into two separate `useEffect()` Hooks
2 parents 7361be1 + 0c3a8ef commit e5a777f

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/use-dropdown-menu.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ 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;
@@ -39,7 +34,10 @@ export default function useDropdownMenu(itemCount: number) {
3934
if (isOpen) {
4035
moveFocus(0);
4136
}
37+
}, [isOpen]);
4238

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)