The Sonner component provides a toast notification system that displays temporary messages to users. It's designed for success messages, error notifications, and other temporary feedback.
import { Sonner } from '@dashflowx/core'export default function SonnerExample() {return (<Sonner />)}
Preview:
The main sonner component with toast functionality.
The underlying sonner implementation.
import { Sonner } from '@dashflowx/core'export default function BasicToast() {return (<Sonner />)}
Preview:
import { Sonner } from '@dashflowx/core'export default function CustomStyledToast() {return (<Sonner className="border border-blue-500 rounded-lg p-4" />)}
Preview:
import { toast } from 'sonner'export default function SuccessToast() {return (<button onClick={() => toast.success('Operation completed successfully!')}>Show Success Toast</button>)}
import { toast } from 'sonner'export default function ErrorToast() {return (<button onClick={() => toast.error('Something went wrong!')}>Show Error Toast</button>)}
import { toast } from 'sonner'export default function WarningToast() {return (<button onClick={() => toast.warning('Please check your input!')}>Show Warning Toast</button>)}
import { toast } from 'sonner'export default function InfoToast() {return (<button onClick={() => toast.info('New update available!')}>Show Info Toast</button>)}
const successNotification = (<button onClick={() => toast.success('Data saved successfully!')}>Save Data</button>);
const errorNotification = (<button onClick={() => toast.error('Failed to save data!')}>Save Data</button>);
const formValidation = (<button onClick={() => toast.error('Please fill in all required fields!')}>Submit Form</button>);
const actionConfirmation = (<button onClick={() => toast.success('Item deleted successfully!')}>Delete Item</button>);
Use clear and concise toast messages.
// ✅ Good - Clear messagetoast.success('Data saved successfully!')// ❌ Avoid - Unclear messagetoast.success('Done')
Set appropriate duration for different types of messages.
// ✅ Good - Appropriate durationtoast.success('Quick action completed!', { duration: 2000 })toast.error('Important error!', { duration: 5000 })// ❌ Avoid - Too short or too longtoast.success('Message', { duration: 100 })toast.error('Error', { duration: 30000 })
Maintain consistent styling across toast notifications.
// ✅ Good - Consistent stylingtoast.success('Message', { className: 'standard-toast-style' })// ❌ Avoid - Inconsistent stylingtoast.success('Message', { className: 'random-styles' })
Ensure toast notifications are accessible to all users.
// ✅ Good - Accessibletoast.success('Message', {description: 'Additional context for screen readers'})// ❌ Avoid - Not accessibletoast.success('Message')
Use appropriate positioning for toast notifications.
// ✅ Good - Appropriate positioningtoast.success('Message', { position: 'top-right' })// ❌ Avoid - Poor positioningtoast.success('Message', { position: 'bottom-left' })
You can customize the toast appearance using CSS classes:
toast.success('Message', {className: 'border border-gray-300 rounded-lg p-4'})
Use different themes for different contexts:
// Light themetoast.success('Message', { className: 'bg-white border-gray-200' })// Dark themetoast.success('Message', { className: 'bg-gray-900 border-gray-700 text-white' })// Brand themetoast.success('Message', { className: 'bg-blue-50 border-blue-200' })
Use different sizes for different emphasis levels:
// Small toasttoast.success('Message', { className: 'text-sm' })// Standard toasttoast.success('Message', { className: 'text-base' })// Large toasttoast.success('Message', { className: 'text-lg' })
Create dynamic toast content:
const showDynamicToast = (userName) => {toast.success(`Welcome back, ${userName}!`);};
Add actions to toast notifications:
toast.success('File uploaded successfully!', {action: {label: 'View',onClick: () => navigate('/files')}});
Create custom toast components:
const CustomToast = ({ message, type }) => {return (<div className={`toast toast-${type}`}><span>{message}</span></div>);};toast.custom(<CustomToast message="Custom message" type="success" />);
- Toast: For toast notifications
- Alert: For alert messages
- Notification: For notifications
- Button: For toast triggers
- Icon: For toast icons
For the complete API reference and advanced usage patterns, see the Sonner API documentation.
