nimuli/app/Livewire/Components/Toaster.php

37 lines
822 B
PHP

<?php
namespace App\Livewire\Components;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
class Toaster extends Component
{
/** @var array<int, array<string, mixed>> */
public array $toasts = [];
#[On('toast')]
public function add(string $message, string $type = 'success', int $duration = 4000): void
{
$this->toasts[] = [
'id' => uniqid('t_', true),
'message' => $message,
'type' => $type,
'duration' => $duration,
];
}
public function dismiss(string $id): void
{
$this->toasts = array_values(
array_filter($this->toasts, fn ($t) => $t['id'] !== $id)
);
}
public function render(): View
{
return view('livewire.components.toaster');
}
}