diff --git a/components/ClickAwayHandler.tsx b/components/ClickAwayHandler.tsx index 63378a1..e8e8878 100644 --- a/components/ClickAwayHandler.tsx +++ b/components/ClickAwayHandler.tsx @@ -8,19 +8,38 @@ type Props = { onMount?: (rect: DOMRect) => void; }; +function getZIndex(element: HTMLElement): number { + let zIndex = 0; + while (element) { + const zIndexStyle = window + .getComputedStyle(element) + .getPropertyValue("z-index"); + const numericZIndex = Number(zIndexStyle); + if (zIndexStyle !== "auto" && !isNaN(numericZIndex)) { + zIndex = numericZIndex; + break; + } + element = element.parentElement as HTMLElement; + } + return zIndex; +} + function useOutsideAlerter( ref: RefObject, onClickOutside: Function ) { useEffect(() => { - function handleClickOutside(event: Event) { - if ( - ref.current && - !ref.current.contains(event.target as HTMLInputElement) - ) { - onClickOutside(event); + function handleClickOutside(event: MouseEvent) { + const clickedElement = event.target as HTMLElement; + if (ref.current && !ref.current.contains(clickedElement)) { + const refZIndex = getZIndex(ref.current); + const clickedZIndex = getZIndex(clickedElement); + if (clickedZIndex <= refZIndex) { + onClickOutside(event); + } } } + document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); diff --git a/pages/_app.tsx b/pages/_app.tsx index b965941..0ece19d 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -5,7 +5,8 @@ import { SessionProvider } from "next-auth/react"; import type { AppProps } from "next/app"; import Head from "next/head"; import AuthRedirect from "@/layouts/AuthRedirect"; -import { Toaster } from "react-hot-toast"; +import toast from "react-hot-toast"; +import { Toaster, ToastBar } from "react-hot-toast"; import { Session } from "next-auth"; import { isPWA } from "@/lib/client/utils"; @@ -61,7 +62,31 @@ export default function App({ className: "border border-sky-100 dark:border-neutral-700 dark:bg-neutral-800 dark:text-white", }} - /> + > + {(t) => ( + + {({ icon, message }) => ( +
+ {icon} + {message} + {t.type !== "loading" && ( + + )} +
+ )} +
+ )} +