Installation:
composer require theabhishekin/livewire-toast
Add Toast Container:
Place @livewire('toast-container') in your main layout (e.g., resources/views/layouts/app.blade.php), typically near the <body> opening tag for optimal visibility.
Import JavaScript:
Add the toast script to your entry file (e.g., resources/js/app.js):
import '../../vendor/theabhishekin/livewire-toast/resources/js/toast';
First Toast: Dispatch a toast from a Livewire component:
$this->dispatch('toast', [
'title' => 'Success!',
'type' => 'success',
]);
Trigger a success toast after a form submission:
public function save()
{
$this->validate([...]);
// Save logic...
$this->dispatch('toast', [
'title' => 'Saved successfully!',
'type' => 'success',
'description' => 'Your changes have been saved.',
]);
return redirect()->to('/');
}
Component Integration:
$this->dispatch('toast', [...]) in Livewire methods (e.g., mount(), updatedProperty(), or event handlers).if ($this->isValid()) {
$this->dispatch('toast', ['title' => 'Valid!', 'type' => 'success']);
}
Dynamic Toasts:
dispatch:
$this->dispatch('toast', [
'title' => 'User ' . $user->name . ' created',
'type' => 'info',
]);
Action Buttons:
$this->dispatch('toast', [
'title' => 'Action required',
'type' => 'warning',
'actions' => [
['text' => 'Retry', 'url' => route('retry')],
['text' => 'Dismiss', 'event' => 'toast.dismiss'],
],
]);
Positioning:
$this->dispatch('toast', [
'title' => 'Alert',
'position' => 'bottom-right',
]);
Auto-Dismiss:
config/livewire-toast.php or per-toast:
$this->dispatch('toast', [
'title' => 'Temporary',
'duration' => 3000, // 3 seconds
]);
data-[dark] attributes (handled automatically via Alpine).maxVisible in config (default: 5).document.addEventListener('toast.show', (e) => {
console.log('Toast shown:', e.detail);
});
.livewire-toast-container) in your CSS.JavaScript Loading Order:
toast.js is imported after Alpine.js (if not using Vite/Laravel Mix auto-ordering).app.js or use defer.Duplicate Toasts:
duration: 0 for persistent toasts or debounce dispatches:
if (!$this->hasToastShown) {
$this->dispatch('toast', [...]);
$this->hasToastShown = true;
}
Alpine Conflicts:
// In toast.js, replace `Alpine.store` with:
window.Alpine.store('toast', { ... });
Livewire Re-renders:
#app).Config Publishing:
php artisan vendor:publish --tag=livewire-toast-config) means using defaults only.Toast Not Showing?:
@livewire('toast-container') exists in the DOM).Styling Issues:
.livewire-toast) in your CSS:
.livewire-toast { --tw-bg-opacity: 0.95; } /* Example: Adjust backdrop */
Event Listeners:
window.Alpine.on('toast.show', (e) => console.log(e.detail));
Custom Toast Types:
types array in config to add themes (e.g., ['custom'])..livewire-toast-custom { --tw-bg-color: #3b82f6; }
Global Toast State:
const toasts = Alpine.store('toast').toasts;
Server-Side Logic:
protected $listeners = ['toast.trigger' => 'showToast'];
public function showToast($data) {
$this->dispatch('toast', $data);
}
Testing:
$this->dispatchBrowserEvent('toast', ['title' => 'Test']);
$this->assertLivewireDispatches('toast');
Performance:
$this->dispatch('toast.batch', [
['title' => 'Update 1'],
['title' => 'Update 2', 'type' => 'info'],
]);
How can I help you explore Laravel packages today?