Toast
Toasts provide brief notifications to the user. They appear temporarily and shouldn't interrupt the user experience.
When to use
- To confirm an action (saved, deleted, updated)
- To show non-critical errors or warnings
- To notify about background processes
- For system updates or status changes
- To provide feedback after user actions
Key features
- Multiple variants - Default, success, warning, error, and info
- Auto-dismiss - Configurable duration or persistent
- Action buttons - Optional actions like undo or retry
- Progress indicator - Shows time remaining
- Queueing - Multiple toasts stack nicely
- Programmatic control - Show, close, and manage via JavaScript
- Accessible - Screen reader announcements
Anatomy
A toast consists of:
- Container - The toast wrapper
- Title - Primary message
- Description - Additional details (optional)
- Close button - Manual dismiss option
- Action - Optional button for actions
- Progress bar - Visual countdown for auto-dismiss
Toast Variants
Toast Features
Common Patterns
Toast Management
Installation
npm install @pm7/core
CSS Classes
Class | Description |
---|---|
.pm7-toast-viewport |
Container for all toasts |
.pm7-toast |
Individual toast container |
.pm7-toast--default |
Default toast variant |
.pm7-toast--success |
Success toast variant |
.pm7-toast--warning |
Warning toast variant |
.pm7-toast--destructive |
Error/destructive toast variant |
.pm7-toast--info |
Info toast variant |
.pm7-toast-header |
Toast header container |
.pm7-toast-title |
Toast title |
.pm7-toast-description |
Toast description |
.pm7-toast-close |
Close button |
.pm7-toast-action |
Action button container |
.pm7-toast-progress |
Progress bar for auto-dismiss |
Data Attributes
Attribute | Description |
---|---|
data-state |
Toast visibility state: 'open' or 'closed' |
data-toast-id |
Unique identifier for each toast instance |
Basic Usage
import { showToast } from '@pm7/core';
// Show a simple toast
showToast({
title: 'Success!',
description: 'Your changes have been saved.'
});
JavaScript API
showToast(options)
Option | Type | Default | Description |
---|---|---|---|
title |
string | '' | Toast title |
description |
string | '' | Toast description |
variant |
string | 'default' | Toast variant: 'default', 'success', 'warning', 'destructive', 'info' |
duration |
number | 5000 | Auto-dismiss duration in ms. Use 0 for persistent |
action |
string | null | HTML string for action buttons |
onClose |
function | null | Callback when toast is closed |
Toast Variants
// Default toast
showToast({
title: 'Notification',
description: 'This is a default toast message.'
});
// Success toast
showToast({
title: 'Success!',
description: 'Operation completed successfully.',
variant: 'success'
});
// Warning toast
showToast({
title: 'Warning',
description: 'Please review your input.',
variant: 'warning'
});
// Error/Destructive toast
showToast({
title: 'Error',
description: 'Something went wrong.',
variant: 'destructive'
});
// Info toast
showToast({
title: 'Information',
description: 'New updates are available.',
variant: 'info'
});
Advanced Options
// Custom duration
showToast({
title: 'Quick message',
duration: 3000 // 3 seconds
});
// Persistent toast
showToast({
title: 'Action required',
description: 'Please complete your profile.',
duration: 0 // Won't auto-dismiss
});
// Toast with action
showToast({
title: 'File deleted',
description: 'document.pdf has been deleted.',
action: '<button class="pm7-button pm7-button--sm pm7-button--primary">Undo</button>'
});
// Toast with callback
showToast({
title: 'Saved',
description: 'Your preferences have been saved.',
onClose: () => {
console.log('Toast closed');
}
});
Manual Control
import { showToast, closeToast, closeAllToasts } from '@pm7/core';
// Show toast and get ID
const toastId = showToast({
title: 'Processing...',
duration: 0 // Don't auto-dismiss
});
// Close specific toast
setTimeout(() => {
closeToast(toastId);
}, 5000);
// Close all toasts
closeAllToasts();
React Usage
import { useToast } from '@pm7/react';
function MyComponent() {
const { toast } = useToast();
const handleSave = () => {
// Save logic...
toast({
title: 'Saved successfully',
description: 'Your changes have been saved.',
variant: 'success'
});
};
return (
<button onClick={handleSave}>Save</button>
);
}
Accessibility
- Toasts appear in a fixed position (top-right corner)
- Close button includes
aria-label="Close"
for screen readers - Focus remains on the trigger element
- Toast actions are keyboard accessible
- Each toast has a unique ID for programmatic control
Best Practices
- Keep it brief - Toast messages should be concise
- Non-blocking - Don't use toasts for critical information
- Appropriate variant - Use the correct variant for the message type
- Timing - Default 5 seconds is good for most messages
- Actionable - Include an action button when appropriate
Copy this link that can be used as documentation for working with this component:
🤖 AI-Optimized Documentation
This link contains complete Markdown documentation that is perfectly readable by AI assistants, developers, and other automated systems. Directly accessible on GitHub - including all essential PM7 Toast implementation guidance.
Perfect for ChatGPT, Claude, and other AI code assistants