Skip to content

Commit

Permalink
Merge pull request #2079 from tijlleenders/vin/1684/remove-toast-when…
Browse files Browse the repository at this point in the history
…-clicked-outside

feat: destroy toast when clicked outside it
  • Loading branch information
Tushar-4781 authored Nov 16, 2024
2 parents 35991a6 + af10701 commit 4baa3a9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,38 @@ const App = () => {
}`,
});
};
const handleClick = () => {
api.destroy();

const handleOutsideClick = (e: MouseEvent) => {
const notifications = document.getElementsByClassName("ant-notification-notice");
let clickedInside = false;

Array.from(notifications).forEach((notificationElement) => {
if (notificationElement.contains(e.target as Node)) {
clickedInside = true;
}
});

if (!clickedInside) {
setTimeout(() => {
api.destroy();
}, 0);
}
};

useEffect(() => {
if (showToast.open) {
openNotification();
setShowToast({ ...showToast, open: false });
}
document.addEventListener("click", handleClick);
}, [showToast]);

useEffect(() => {
document.addEventListener("click", handleOutsideClick, true);
return () => {
document.removeEventListener("click", handleClick);
document.removeEventListener("click", handleOutsideClick, true);
};
}, [showToast]);
}, []);

return (
<div className={`${darkModeEnabled ? "dark" : "light"}-theme${theme[darkModeEnabled ? "dark" : "light"]}`}>
<div className={`App-${darkModeEnabled ? "dark" : "light"}`}>
Expand Down

0 comments on commit 4baa3a9

Please sign in to comment.