Skip to content

Commit

Permalink
Merge pull request #217 from sparksuite/fix-portal-bug
Browse files Browse the repository at this point in the history
Fix portal bug in React v17
  • Loading branch information
WesCossick authored Nov 25, 2020
2 parents 69a25ca + b272c46 commit 284c3b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
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.1.1",
"version": "2.1.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
15 changes: 9 additions & 6 deletions src/use-dropdown-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export default function useDropdownMenu(itemCount: number) {

// Handle listening for clicks and auto-hiding the menu
useEffect(() => {
// Ignore if the menu isn't open
if (!isOpen) {
return;
}

// This function is designed to handle every click
const handleEveryClick = (event: MouseEvent) => {
// Ignore if the menu isn't open
if (!isOpen) {
return;
}

// Make this happen asynchronously
setTimeout(() => {
// Type guard
Expand All @@ -80,7 +80,10 @@ export default function useDropdownMenu(itemCount: number) {
};

// Add listener
document.addEventListener('click', handleEveryClick);
// -> Force it to be async to fix: https://github.com/facebook/react/issues/20074
setTimeout(() => {
document.addEventListener('click', handleEveryClick);
}, 1);

// Return function to remove listener
return () => document.removeEventListener('click', handleEveryClick);
Expand Down

0 comments on commit 284c3b0

Please sign in to comment.