static/app/components/core/toast/toast.mdx
import {Toast} from '@sentry/scraps/toast';
import * as Storybook from 'sentry/stories';
export const documentation = import('!!type-loader!@sentry/scraps/toast');
<Toast> is a notification component that displays temporary messages to provide feedback for user actions. Toasts appear briefly to confirm actions, report errors, show loading states, or offer undo functionality.
In practice, you won't typically render <Toast> components directly. Instead, use the action creators addSuccessMessage and addErrorMessage from static/app/actionCreators/indicator.tsx to display toasts.
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
// Show success toast
addSuccessMessage('Project settings saved successfully');
// Show error toast
addErrorMessage('Failed to save settings. Please try again.');
Toasts come in four types, each serving a different purpose:
<Storybook.Demo> <Toast indicator={{ type: 'success', message: 'Operation completed successfully', id: 'success', options: {}, }} onDismiss={() => {}} /> <Toast indicator={{ type: 'error', message: 'An error occurred', id: 'error', options: {}, }} onDismiss={() => {}} /> <Toast indicator={{ type: 'loading', message: 'Processing...', id: 'loading', options: {}, }} onDismiss={() => {}} /> <Toast indicator={{ type: 'undo', message: 'Item deleted', id: 'undo', options: {}, }} onDismiss={() => {}} /> </Storybook.Demo>
// Success
addSuccessMessage('Settings saved');
// Error
addErrorMessage('Failed to load data');
// Loading
addLoadingMessage('Uploading file...');
// Undo
addSuccessMessage('Item deleted', {undo: handleUndo});
For reversible actions, provide an undo callback in the options. This displays an "Undo" button in the toast.
<Storybook.Demo> <Storybook.SideBySide> <Toast indicator={{ type: 'success', message: 'Item deleted', id: 'undo-success', options: { undo: () => {}, }, }} onDismiss={() => {}} /> <Toast indicator={{ type: 'undo', message: 'Changes reverted', id: 'undo-undo', options: { undo: () => {}, }, }} onDismiss={() => {}} /> </Storybook.SideBySide> </Storybook.Demo>
addSuccessMessage('Project deleted', {
undo: () => {
restoreProject(projectId);
},
});
All toasts are dismissible by default—users can click the "×" button to close them. Toasts also auto-dismiss after a few seconds (except loading toasts).
addSuccessMessage('Your changes have been saved');
// Toast appears and auto-dismisses after ~4 seconds
Control how long toasts remain visible using the duration option:
// Short duration (2 seconds)
addSuccessMessage('Copied to clipboard', {duration: 2000});
// Longer duration (10 seconds)
addErrorMessage('Important error message', {duration: 10000});
// Persistent (no auto-dismiss)
addLoadingMessage('Processing large file...', {duration: Infinity});
Confirm successful operations:
const handleSave = async () => {
try {
await saveProject(data);
addSuccessMessage('Project saved successfully');
} catch (error) {
addErrorMessage('Failed to save project');
}
};
Inform users of errors with actionable guidance:
try {
await uploadFile(file);
addSuccessMessage(`${file.name} uploaded`);
} catch (error) {
addErrorMessage(`Failed to upload ${file.name}. File may be too large.`);
}
Allow users to undo destructive actions:
const handleDelete = async id => {
const backup = items.find(item => item.id === id);
await deleteItem(id);
addSuccessMessage('Item deleted', {
undo: async () => {
await restoreItem(backup);
addSuccessMessage('Item restored');
},
});
};
Show progress for long-running operations:
const handleExport = async () => {
addLoadingMessage('Generating export...');
try {
await generateExport();
addSuccessMessage('Export complete');
} catch (error) {
addErrorMessage('Export failed');
}
};
Message Content
Frequency
Timing
Toasts use the WAI-ARIA Alert pattern for announcing messages to screen readers:
Message Clarity
Timing
Frequency Control
Undo Actions
For more information, see the WAI-ARIA Alert practices.