A Toast displays a brief, temporary notification of actions, errors, or other events in an application.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.Content
Title and description
Use the "title" and "description" slots within <ToastContent> to provide structured content for the toast. The title is required, and description is optional.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.import {queue} from './Toast';
import {Button} from './Button';
function Example() {
return (
<Button
onPress={() => queue.add({
title: 'Update available',
description: 'A new version is ready to install.'
})}
>
Check for updates
</Button>
);
}
Close button
Include a <Button slot="close"> to allow users to dismiss the toast manually. This is important for accessibility.
Accessibility
<ToastContent> rather than inside it, so that screen readers announce the toast content without the close button first.Dismissal
Use the timeout option to automatically dismiss toasts after a period of time. For accessibility, toasts should have a minimum timeout of 5 seconds. Timers automatically pause when the user focuses or hovers over a toast.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.import {queue} from './Toast';
import {Button} from './Button';
function Example() {
return (
<Button
onPress={() => queue.add(
{title: 'File has been saved!'},
{timeout: 5000}
)}
>
Save file
</Button>
);
}
Accessibility
Programmatic dismissal
Toasts can be programmatically dismissed using the key returned from queue.add(). This is useful when a toast becomes irrelevant before the user manually closes it.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.import {queue} from './Toast';
import {Button} from './Button';
import {useState} from 'react';
function Example() {
let [toastKey, setToastKey] = useState<string | null>(null);
return (
<Button
onPress={() => {
if (!toastKey) {
setToastKey(queue.add(
{title: 'Processing...'},
{onClose: () => setToastKey(null)}
));
} else {
queue.close(toastKey);
}
}}
>
{toastKey ? 'Cancel' : 'Process'}
</Button>
);
}
Accessibility
Toast regions are landmark regions that can be navigated using F6 to move forward and Shift + F6 to move backward. This provides an easy way for keyboard users to jump to toasts from anywhere in the app.
When a toast is closed, focus moves to the next toast if any. When the last toast is closed, focus is restored to where it was before.
API
<ToastRegion>
{({toast}) => (
<Toast toast={toast}>
<ToastContent>
<Text slot="title" />
<Text slot="description" />
</ToastContent>
<Button slot="close" />
</Toast>
)}
</ToastRegion>
ToastRegion
A ToastRegion displays one or more toast notifications.
| Name | Type | |
|---|---|---|
queue | ToastQueue | |
| The queue of toasts to display. | ||
children | ReactNode | | |
A function to render each toast, or children containing a <ToastList>. | ||
Default className: react-aria-ToastRegion
Toast
A Toast displays a brief, temporary notification of actions, errors, or other events in an application.
| Name | Type | |
|---|---|---|
toast | QueuedToast | |
| The toast object. | ||
children | ChildrenOrFunction | |
| The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-Toast
ToastContent
<ToastContent> renders the main content of a toast, including the title and description slots. It accepts all HTML attributes.
ToastQueue
A ToastQueue manages the state for a <ToastRegion>. The state is stored outside React so you can trigger toasts from anywhere in your application.
Properties
| Name | Type | |
|---|---|---|
visibleToasts | QueuedToast | |
| The currently visible toasts. | ||
Methods
constructor | ||
subscribe | ||
| Subscribes to updates to the visible toasts. | ||
add | ||
| Adds a new toast to the queue. | ||
close | ||
| Closes a toast. | ||
pauseAll | ||
| Pauses the timers for all visible toasts. | ||
resumeAll | ||
| Resumes the timers for all visible toasts. | ||
clear | ||