Introducing PrimeReact v11-alpha 🎉Discover Now
tailwindMessages

Toast

Toast is a component that displays a message to the user.

preview

Installation#

npx shadcn@latest add https://v11.primereact.org/r/toast.json

Usage#

import { toast, Toaster } from '@/components/ui/toast';
<Toaster />

Trigger toasts from anywhere with toast(...) or one of the severity shortcuts (toast.success, toast.info, toast.warn, toast.error, toast.secondary, toast.contrast).

Examples#

Basic#

basic-demo

Position#

Use the position prop on <Toaster> to control where toasts appear.

position-demo

Action#

Add an interactive button to the toast with the action field.

action-demo

toast function#

toast(options) queues a toast and returns its id.

const id = toast({ severity: 'success', title: 'Saved' });

Severity shortcuts#

Each severity has a shortcut that sets severity automatically:

toast.success({ title: 'Saved' });
toast.info({ title: 'Heads up' });
toast.warn({ title: 'Check this' });
toast.error({ title: 'Failed' });
toast.secondary({ title: '...' });
toast.contrast({ title: '...' });

Methods#

toast.dismiss(id);          // dismiss specific toast
toast.dismiss();            // dismiss all
toast.update(id, options);  // merge options into existing toast
toast.promise(p, { loading, success, error });

Toast options#

Any of these fields can be passed to toast(...) or its shortcuts.

FieldTypeDescription
severity'info' | 'success' | 'warn' | 'error' | 'secondary' | 'contrast'Visual severity. Defaults to info.
titleReactNodeMain heading.
descriptionReactNodeSecondary text.
iconReactNodeCustom icon — overrides the matched <Toast.Icon match="..." /> for this toast.
actionButtonPropsProps forwarded to <ToastAction> (e.g. children, onClick).
renderReactElementReplaces the default body with custom JSX.
durationnumberAuto-dismiss delay in ms. Pass Infinity to keep open.
loadingbooleanMarks the toast as loading and disables auto-dismiss.
dismissiblebooleanIf false, hides the close button and disables swipe-to-dismiss.
groupstringScopes the toast to a specific <Toaster group="..." />.
dataRecord<string, unknown>Arbitrary metadata, available on the toast object and in callbacks.
onDismiss(toast) => voidCalled when dismissed by user (close button, swipe, or dismiss(id)).
onTimeout(toast) => voidCalled when duration elapses.

Accessibility#

Screen Reader#

Toast component use alert role that implicitly defines aria-live as "assertive" and aria-atomic as "true".