Merge pull request #570 from QAComet/qacomet/add-toast-button

Add close button and data-testids to toast messages
This commit is contained in:
Daniel 2024-04-20 10:49:30 -04:00 committed by GitHub
commit 389db59b28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 8 deletions

View File

@ -8,19 +8,38 @@ type Props = {
onMount?: (rect: DOMRect) => void; 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( function useOutsideAlerter(
ref: RefObject<HTMLElement>, ref: RefObject<HTMLElement>,
onClickOutside: Function onClickOutside: Function
) { ) {
useEffect(() => { useEffect(() => {
function handleClickOutside(event: Event) { function handleClickOutside(event: MouseEvent) {
if ( const clickedElement = event.target as HTMLElement;
ref.current && if (ref.current && !ref.current.contains(clickedElement)) {
!ref.current.contains(event.target as HTMLInputElement) const refZIndex = getZIndex(ref.current);
) { const clickedZIndex = getZIndex(clickedElement);
onClickOutside(event); if (clickedZIndex <= refZIndex) {
onClickOutside(event);
}
} }
} }
document.addEventListener("mousedown", handleClickOutside); document.addEventListener("mousedown", handleClickOutside);
return () => { return () => {
document.removeEventListener("mousedown", handleClickOutside); document.removeEventListener("mousedown", handleClickOutside);

View File

@ -5,7 +5,8 @@ import { SessionProvider } from "next-auth/react";
import type { AppProps } from "next/app"; import type { AppProps } from "next/app";
import Head from "next/head"; import Head from "next/head";
import AuthRedirect from "@/layouts/AuthRedirect"; 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 { Session } from "next-auth";
import { isPWA } from "@/lib/client/utils"; import { isPWA } from "@/lib/client/utils";
@ -61,7 +62,31 @@ export default function App({
className: className:
"border border-sky-100 dark:border-neutral-700 dark:bg-neutral-800 dark:text-white", "border border-sky-100 dark:border-neutral-700 dark:bg-neutral-800 dark:text-white",
}} }}
/> >
{(t) => (
<ToastBar toast={t}>
{({ icon, message }) => (
<div
className="flex flex-row"
data-testid="toast-message-container"
data-type={t.type}
>
{icon}
<span data-testid="toast-message">{message}</span>
{t.type !== "loading" && (
<button
className="btn btn-xs outline-none btn-circle btn-ghost"
data-testid="close-toast-button"
onClick={() => toast.dismiss(t.id)}
>
<i className="bi bi-x"></i>
</button>
)}
</div>
)}
</ToastBar>
)}
</Toaster>
<Component {...pageProps} /> <Component {...pageProps} />
</AuthRedirect> </AuthRedirect>
</SessionProvider> </SessionProvider>