Toast
Transient notifications sent from anywhere in the app through a shared manager.
Usage guidelines
- Use a toast for short-lived feedback on something the user just did or that just happened in the background, such as a save, an export or a failed request with no other place on the page. A status that stays true while the user works on the page is an
Alert, and a decision the user must answer is anAlertDialog. - Do not use a toast for a form rejection. A field error goes to that field through
Formerrors, and a rejection that belongs to no field stays inside the form as anAlert. - Create one manager in a module with
createToastManager()and exportcreateToast(manager)from it. The returnedtoastis a plain function, so event handlers, mutation callbacks and code outside React all call it the same way. - Render one
Toasterfor that manager near the application root, inside the isolated root element.Toasterincludes the provider and portals its viewport todocument.body, so the app needs no other wrapper. It sets the defaulttimeout(5000 ms) and the visible stacklimit(3). - Create another manager with its own
Toasteronly when a part of the app needs a separate stack, for example a test or an embedded surface. Toasts never cross between managers. - The title is the message and names the notification for assistive technology; add a
descriptiononly for detail. Every toast has a type that sets its icon and halo. - A toast closes on its own, so its action must not be the only way to reach that outcome. Use
timeout: 0for a toast that stays until the user or the code closes it.
Anatomy
Toaster renders the cards itself: icon, title, description, an optional action button and a close button. Call sites pass content through the toast API rather than composing parts.
The demos on this page share one manager from a notifications.ts module like the one above. The hero demo renders its Toaster, standing in for the application root, and the other demos only call toast.
Examples
Types
toast.success, toast.info, toast.warning and toast.error fix the type. The callable form toast(title, { type }) takes it as an option.
Action
actionProps renders one Button in the card and accepts its variant. The caller closes the original toast when the action completes.
Promise
toast.promise shows a loading toast and updates the same card to success or error when the promise settles. Each state takes a title string or an options object, and the result states can derive it from the resolved value or the error. It returns the original promise, so handle its rejection, and keep the trigger in loading until it settles so the action cannot start twice.
Updating and dismissing
Every call returns the toast ID. Pass it to toast.update to change the card in place, or to toast.dismiss to close it; toast.dismiss() without an ID closes every toast of the manager. A call with an existing id option also updates that card instead of adding another. The queued toast below uses timeout: 0 and stays; the update passes a timeout so the finished card closes on its own.
Keyboard
| Key | Action |
|---|---|
| F6 | Moves focus to the notifications region while a toast is shown. |
| Tab | Moves focus from the region into the front toast, then to its action and close buttons. |
| Shift+Tab | Returns focus from the region to the element that was focused before F6. |
| EnterorSpace | Activates the focused action or close button. |
| Escape | Closes the toast that contains focus and returns focus to the element focused before F6. |
API reference
Toaster
Provides the toast context, portaled viewport, and stacked notification cards. Renders a portaled <div> viewport.
Pass a manager from createToastManager() and trigger notifications via createToast(manager). Users can move focus to the notifications with F6.
| Prop | Type | Default | Details |
|---|---|---|---|
toastManager required | ToastManager | No default | |
offset | { right?: Property.Right<string | number> | undefined; top?: Property.Top<string | number> | undefined; } | No default | |
timeout | number | 5000 | |
limit | number | 3 | |
createToast
Creates the toast API for a manager. Call it with a title and a type, or use a shortcut such as toast.success(title).
| Parameter | Type | Description |
|---|---|---|
manager required | ToastManager | No description |
Returns
ToastApiCall signatures
(title: React.ReactNode, options: ToastOptions) => string
| Member | Type | Description |
|---|---|---|
success required | (title: React.ReactNode, options?: TypedToastOptions) => string | Shows a success toast and returns its ID. |
error required | (title: React.ReactNode, options?: TypedToastOptions) => string | Shows an error toast and returns its ID. |
warning required | (title: React.ReactNode, options?: TypedToastOptions) => string | Shows a warning toast and returns its ID. |
info required | (title: React.ReactNode, options?: TypedToastOptions) => string | Shows an info toast and returns its ID. |
dismiss required | (toastId?: string) => void | Closes the toast with the given ID, or every toast when no ID is passed. |
update required | (toastId: string, options: ToastUpdateOptions) => void | Updates the toast with the given ID. |
promise required | <Value>(promiseValue: Promise<Value>, options: ToastPromiseOptions<Value>) => Promise<Value> | Shows a loading toast that becomes a success or error toast when the promise settles. Returns a promise that settles with the same result. |
createToastManager
Creates a toast manager to pass to both Toaster and createToast.
Returns
ToastManager