Appearance
Using Toast vs Snackbar
This guide shows you how to use the useToast and useSnackbar composables in your Vue 3 projects.
Overview
Both Toast and Snackbar provide notification systems, but they serve different purposes:
| Feature | Toast | Snackbar |
|---|---|---|
| Multiple notifications | ✅ Yes - can show multiple at once | ❌ No - shows one at a time |
| Position | 6 positions (top-left, top, top-right, bottom-left, bottom, bottom-right) | 6 positions (same as Toast, default: bottom center) |
| Use case | Multiple notifications, system alerts | Brief messages, undo actions |
| Auto-dismiss | Configurable timeout | Configurable timeout |
Setup
First, you need to add the Toast and Snackbar components to your app root (App.vue or Provider.vue). Import OToast and OSnackbar components and add them to your template.
Using Toast
Basic Usage
Import useToast from the composable and call methods like success(), error(), warning(), and info() to show different types of notifications.
Advanced Options
Use showToast() for custom notifications with full control over options like position, timeout, and messages. You can also configure global settings using methods like setPosition(), setDefaultTimeout(), and setMaxNotifications().
Toast Methods
| Method | Description |
|---|---|
toast.success(message, options?) | Show success notification |
toast.error(message, options?) | Show error notification (persistent by default) |
toast.warning(message, options?) | Show warning notification |
toast.info(message, options?) | Show info notification |
toast.showToast(options) | Show custom notification with full options |
toast.removeNotification(notification) | Remove specific notification |
toast.clearAll() | Remove all notifications |
toast.setPosition(position) | Set default position |
toast.setOffset(x, y) | Set default offset in rem units |
toast.setDefaultTimeout(timeout) | Set default auto-dismiss timeout |
toast.setMaxNotifications(max) | Set maximum number of notifications |
Using Snackbar
Basic Usage
Import useSnackbar from the composable and use methods like success(), error(), warning(), and info() to show snackbar notifications.
Snackbar with Action Button
Use showSnackbar() with buttonConfig option to add action buttons like "Undo" for user interactions.
Snackbar Methods
| Method | Description |
|---|---|
snackbar.success(message, options?) | Show success snackbar |
snackbar.error(message, options?) | Show error snackbar |
snackbar.warning(message, options?) | Show warning snackbar |
snackbar.info(message, options?) | Show info snackbar |
snackbar.showSnackbar(options) | Show custom snackbar |
snackbar.removeNotification() | Remove current notification |
snackbar.clearAll() | Clear notification |
snackbar.setPosition(position) | Set default position |
snackbar.setDefaultTimeout(timeout) | Set default auto-dismiss timeout |
Using Toast vs Snackbar
Use Toast When:
- You need to show multiple notifications simultaneously
- You want notifications in different positions
- You need system-wide alerts
- You want notifications to stack
Use Snackbar When:
- You need a brief, single message
- You want to show an undo action
- You prefer bottom-center positioning
- You need a simple, non-intrusive notification
Use Cases
Toast Use Cases
1. Form Submission Feedback
- Show success toast when form is submitted successfully
- Show error toast if validation fails
- Show multiple error toasts for different field errors
2. API Response Notifications
- Success toast when data is saved
- Error toast when API call fails
- Warning toast for partial success scenarios
3. System Alerts
- Multiple system notifications at once
- Network status changes
- Background task completions
4. User Action Confirmations
- File upload progress and completion
- Bulk operations status
- Import/export operation results
Snackbar Use Cases
1. Undo Actions
- Delete item with undo option
- Move item with undo
- Archive item with restore option
2. Brief Status Messages
- Item added to cart
- Settings saved
- Profile updated
3. Simple Confirmations
- Action completed successfully
- Quick feedback for user interactions
- Non-critical information messages
Best Practices
- Use Toast for multiple notifications - When you need to show several alerts at once
- Use Snackbar for single actions - When you need one brief message with optional undo
- Set appropriate timeouts - Success messages: 3-5s, Errors: persistent or longer timeout
- Position strategically - Top-right for toasts, bottom-center for snackbars
- Provide context - Include clear, actionable messages
- Handle errors gracefully - Always show user-friendly error messages
📖 Related Documentation: