I'm closer to how it should be, being able to call Livewire.dispatch('toast', { message: 'Message' }) and it displaying properly. But I now have duplication between the Blade and the Class.
Blade:
Livewire.on('toast', ({message}) => {
$wire.body = message;
let toastEl = document.getElementById('wireToast');
new bootstrap.Toast(toastEl, { delay: 3000 })
.show();
});
Class:
public function toast($message): void
{
$this->body = $message;
$this->js(<<<'JS'
let toastEl = document.getElementById('wireToast');
new bootstrap.Toast(toastEl, { delay: 3000 })
.show();
JS
);
}
Any ideas how to get rid of the duplication while still being able to show existing messages as well as ones dispatched from the Livewire event?