Skip to content

Commit

Permalink
added bug fix for unregistered event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinAguda committed Jun 18, 2021
1 parent 86beaaf commit 937c588
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
37 changes: 19 additions & 18 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,28 @@ const UnopDropdown: React.FC<UnopDropdownProps> = ({
element.style.visibility = 'visible';
}, []);

const handler = (e: MouseEvent) => {
const path = e.composedPath();
if (show && closeOnClickOut && !path.includes(dropdownMenuRef.current!)) {
handleAction(e);
} else {
if (
show &&
closeOnDropdownClicked &&
path.includes(dropdownMenuRef.current!)
) {
handleAction(e);
}
}
};

useEffect(() => {
if (closeOnClickOut || closeOnDropdownClicked) {
window.addEventListener('click', (e) => {
const path = e.composedPath();
if (
show &&
closeOnClickOut &&
!path.includes(dropdownMenuRef.current!)
) {
handleAction(e);
} else {
if (
show &&
closeOnDropdownClicked &&
path.includes(dropdownMenuRef.current!)
) {
handleAction(e);
}
}
});
window.addEventListener('click', handler);
}
return () => {
window.removeEventListener('click', handler);
};
}, [show]);

const displayMenuItem = (e: CustomMouseEvent) => {
Expand Down

0 comments on commit 937c588

Please sign in to comment.