- Add the registry
- Add toast
Usage
Wrap your app in one ToastProvider, near its root. Toasts render at the end of the page, or into the
element or shadow root you pass as the ToastProvider's container.
toast.add returns an ID you can pass to toast.update and toast.close. type sets the icon:
success, info, warning, error or loading.
Screen readers announce toasts politely, after what they're reading. Set priority: "high" on a
toast that needs attention right away, such as an error that stopped someone's work, to announce it
at once. onClose runs when a toast starts to close, and onRemove once it has animated out.
data holds anything else you want to keep with a toast, for a toast list of your own to render.
toast.update takes the changes, or a function that gets the toast and returns them, as in
toast.update(id, (prev) => ({ data: { count: prev.data.count + 1 } })). toast.close() with no
ID closes every toast.
The ToastProvider shows up to three toasts at once and closes each after 5 seconds. Change these with
its limit and timeout props, or give a toast its own timeout, where 0 keeps it open until
someone closes it. Hovering the toasts or focusing one pauses their timers, and F6 moves focus to
them.
Examples
With an action
actionProps adds an action button, such as Undo. Give the toast a longer timeout so users
have time to use it.
From a promise
toast.promise shows a loading toast that becomes a success or error toast when the promise
settles.
Repeated
Adding a toast with the same id as one on screen updates that toast instead of stacking a copy.
It also restarts the toast's timer and gives it a small bump. toast.update does the same.
Position
position on the ToastProvider places the toasts in a corner or at the center of the top or bottom
edge. The stack grows away from that edge, and someone can swipe a toast toward the edge, or toward
the side it's on, to dismiss it. start and end follow the reading direction, so bottom-end is
the bottom left on a right-to-left page.
This example renders a second ToastProvider with its own manager from createToastManager, so it can
move its toasts without moving the rest of the page's.
Anchored
anchoredToast places a toast beside an element rather than in the stack, as a confirmation of a
small action, such as copying. Pass the element as positionerProps.anchor to add(), along with
any of a popup's placement props, such as side, align and sideOffset. The ToastProvider renders
these toasts too, since it renders anchoredToast's alongside toast's.
Props
The other parts let you build your own toast list with useToastManager. Toast places its toast
beside an element when the toast has positionerProps or you pass anchor. ToastIcon shows the
icon for the toast's type. The ToastProvider's labels replace the words it gives its parts, for
other languages.