52 lines
1.2 KiB
Plaintext
52 lines
1.2 KiB
Plaintext
@inject ToastService ToastService
|
|
@implements IDisposable
|
|
@rendermode InteractiveServer
|
|
|
|
<div class="toast-stack">
|
|
@foreach (var toast in toasts)
|
|
{
|
|
<div class="toast-item @(toast.Dismissing ? "toast-dismiss" : "")">
|
|
<span class="toast-icon">⚠</span>
|
|
<span class="toast-message">@toast.Message</span>
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private readonly List<ToastItem> toasts = [];
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
ToastService.OnShow += ShowToast;
|
|
}
|
|
|
|
private async void ShowToast(string message)
|
|
{
|
|
var toast = new ToastItem { Message = message };
|
|
toasts.Add(toast);
|
|
await InvokeAsync(StateHasChanged);
|
|
_ = DismissAfterDelay(toast);
|
|
}
|
|
|
|
private async Task DismissAfterDelay(ToastItem toast)
|
|
{
|
|
await Task.Delay(3500);
|
|
toast.Dismissing = true;
|
|
await InvokeAsync(StateHasChanged);
|
|
await Task.Delay(300);
|
|
toasts.Remove(toast);
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
ToastService.OnShow -= ShowToast;
|
|
}
|
|
|
|
private sealed class ToastItem
|
|
{
|
|
public string Message { get; set; } = "";
|
|
public bool Dismissing { get; set; }
|
|
}
|
|
}
|