Introducing PrimeReact v11-alpha 🎉Discover Now

Toast

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

preview

Installation#

npx shadcn@latest add @primereact/toast

Usage#

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

Examples#

Basic#

basic-demo

Variants#

Use toast function to create a toast with different variants.

variants-demo

Position#

Use position prop to change the position of the toast.

position-demo

Rich Colors#

Use richColors prop to enable rich colors for the toast.

rich-colors-demo

Action#

Use the action prop to add an interactive button to the toast.

action-demo

toast function#

remove method#

The remove method removes a toast by its ID by animating it out. If id is not provided, it will remove all toasts.

toast.remove(id);

delete method#

The delete method removes a toast by its ID without animating it out. If id is not provided, it will remove all toasts.

toast.delete(id);

update method#

The update method updates a toast by its ID. It accepts an object with the properties to update.

const id = toast.loading({
    title: 'Please wait.',
    description: 'This will take a moment'
    // ...
});

Change the toast options by its ID.

toast.update(id, {
    title: 'Success',
    description: 'The operation was successful',
    variant: 'success'
    // ...
});

promise method#

The promise method is a wrapper around the toast function that creates a toast based on the result of a promise. Each state has its own toast configuration.

toast.promise(Promise, {
    loading: {
        title: '',
        description: ''
        // ...
    },
    success: {
        title: '',
        description: ''
        // ...
    },
    error: {
        title: '',
        description: ''
        // ...
    }
});

custom method#

The custom method creates a toast that is based on a custom component. Also, it accepts other props from toast function.

toast.custom(<div>Hello</div>, {
    duration: 1000
    // ...
});

action prop#

The action prop accepts all ToastAction component properties - any props passed will be directly forwarded to the underlying ToastAction component, including children, onClick, className, and other Button-specific properties.

toast({
    title: '',
    description: '',
    action: {
        children: '',
        onClick: () => {},
        className: ''
        // ...
    }
});

duration prop#

The duration prop is the duration of the toast in milliseconds. Passing Infinity keeps the toast visible until it is removed manually.

toast({
    duration: 1000
    // ...
});

icon prop#

The icon prop accepts a React node or a function that returns a React node.

toast({
    icon: <CheckIcon />
    // ...
});

close callbacks#

onDismiss and onTimeout callbacks are called when the toast is dismissed or timed out.

toast({
    onDismiss: () => {},
    onTimeout: () => {}
    // ...
});

group prop#

Add group to the toast to group it with other toasts.

toast({
    group: 'group-1'
    // ...
});

Accessibility#

Screen Reader#

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