import { XIcon } from '@primer/octicons-react'; import { AriaToastProps, useToast, useToastRegion, } from '@react-aria/toast'; import { ToastQueue, ToastState, useToastQueue, } from '@react-stately/toast'; import { ReactNode, useRef } from 'react'; import { Button } from 'react-aria-components'; import { createPortal } from 'react-dom'; import { ClientOnly } from 'remix-utils/client-only'; import { cn } from '~/utils/cn'; type ToastProps = AriaToastProps & { readonly state: ToastState; }; function Toast({ state, ...properties }: ToastProps) { const reference = useRef(null); const { toastProps, titleProps, closeButtonProps } = useToast( properties, state, reference, ); return (
{properties.toast.content}
); } const toasts = new ToastQueue({ maxVisibleToasts: 5, }); export function toast(text: string) { return toasts.add(text, { timeout: 5000 }); } export function Toaster() { const reference = useRef(null); const state = useToastQueue(toasts); const { regionProps } = useToastRegion({}, state, reference); return ( { () => createPortal( state.visibleToasts.length >= 0 ? (
{state.visibleToasts.map((toast) => ( ))}
) : undefined, document.body, ) }
); }