useToast
@volvo-cars/react-messages v0.5.4
Connects a ToastManager to a popover container. It returns the toasts to render and the props for their container. Use it to build a custom toast renderer. For the standard renderer, use ToastsRenderer on the Toast Message page.
import { type BaseToastType, ToastMessage, type ToastManager, useToast,} from '@volvo-cars/react-messages';
function Toasts({ toastManager,}: { toastManager: ToastManager<BaseToastType>;}) { const { visibleToasts, toastContainerProps } = useToast<BaseToastType>({ toastManager, maxVisibleToasts: 2, });
return ( <section {...toastContainerProps}> {visibleToasts.map((item) => ( <ToastMessage {...item} key={item.id} onClose={() => { toastManager.remove(item.id); item.onClose?.(); }} /> ))} </section> );}Spread toastContainerProps on the container element. It carries the popover, focus and timer handlers.
useToast accepts an options object with the following properties:
toastManager– theToastManagerthat holds the toast queue (required)maxVisibleToasts– number of toasts visible at the same time; defaults to1
The hook returns an object with:
visibleToasts– the most recent toasts, up tomaxVisibleToaststoastContainerProps– props to spread on the container element
Behavior
Section titled “Behavior”- The container opens as a manual popover while there are visible toasts and closes when there are none
- Hovering or focusing the container pauses the auto-dismiss timers. Leaving it resumes them
- Pressing F6 moves keyboard focus to the container while toasts exist
- When an interactive toast, one without a timeout, is removed, focus moves to the container if another toast is visible
- When the last toast is removed, focus returns to the element that was focused before the first toast appeared
- The container has
role="region"andtabIndex={-1}so it can receive focus