Skip to content
This repository was archived by the owner on Dec 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion samples/BlazorServer/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/"
@using Blazored.Toast.Configuration;
@inject IToastService toastService
@inject NavigationManager NavigationManager

Expand All @@ -22,7 +23,7 @@

<h1>Blazored Toasts - Custom Component</h1>

<button class="btn btn-primary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(settings => { settings.Timeout = 5; settings.ShowProgressBar = false; }))">Custom Toast</button>
<button class="btn btn-primary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(settings => { settings.Timeout = 5; settings.ShowProgressBar = true; }))">Custom Toast</button>
<button class="btn btn-secondary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(_toastParameters, settings => { settings.Timeout = 5; settings.ShowProgressBar = false; }))">Custom Toast with parameters</button>
<hr />

Expand All @@ -41,9 +42,29 @@
<button class="btn btn-warning" @onclick="ClearQueueWarnings">Clear queued Warning Toasts</button>
<button class="btn btn-info" @onclick="ClearQueueInfos">Clear queued Info Toasts</button>

<hr />
<h1>Blazored Toasts - Close toast with instance.Close()</h1>
<div>
<button class="btn btn-primary" @onclick="AddNewToast">Add new toast</button>
<button class="btn btn-primary" @onclick="AddNewCustomToast">Add new custom toast</button>
@if (toastInstanceList.Any())
{
@foreach (var item in toastInstanceList.OrderBy(x => x.Key))
{
<div class="d-flex m-2">
<span class="col-3 align-self-center border-bottom">@item.Value.Id</span>
<button class="btn btn-danger btn-sm col-1 border-bottom" @onclick="@(() => CloseInstance(item))">Close @item.Key</button>
</div>
}
}
</div>

@code
{
private ToastParameters _toastParameters;
private Dictionary<int, IToastInstance> toastInstanceList { get; set; } = new();
private int toastNumber;


protected override void OnInitialized()
{
Expand All @@ -59,6 +80,34 @@
toastService.ShowToast(ToastLevel.Info, message);
}

private void AddNewToast()
{
toastNumber++;
var instance = toastService.ShowInfo($"#{toastNumber} You can close me from code behind", settings => { settings.DisableTimeout = true; });
toastInstanceList.Add(toastNumber, instance);

InvokeAsync(StateHasChanged);
}

private void AddNewCustomToast()
{
toastNumber++;
var _toastParams = new ToastParameters();
_toastParams.Add(nameof(MyToastComponent.Title), $"#{toastNumber} I'm a custom toast component with parameters");
var instance = toastService.ShowToast<MyToastComponent>(_toastParams, settings => { settings.DisableTimeout = true; });
toastInstanceList.Add(toastNumber, instance);

InvokeAsync(StateHasChanged);
}

private void CloseInstance(KeyValuePair<int, IToastInstance> item)
{
item.Value.Close();
toastInstanceList.Remove(item.Key);

InvokeAsync(StateHasChanged);
}

private void ClearAll()
=> toastService.ClearAll();

Expand Down
12 changes: 10 additions & 2 deletions src/Blazored.Toast.TestExtensions/InMemoryToast.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using Blazored.Toast.Services;
using Blazored.Toast.Configuration;
using Blazored.Toast.Services;
using Microsoft.AspNetCore.Components;
using System;

namespace Blazored.Toast.TestExtensions
{
public class InMemoryToast
public class InMemoryToast: IToastInstance
{
public Type ToastType { get; set; }
public ToastLevel ToastLevel { get; }
public RenderFragment? Message { get; }

public Guid Id => Guid.NewGuid();

public InMemoryToast(Type toastType, ToastLevel toastLevel, RenderFragment message)
{
ToastType = toastType;
Expand All @@ -21,5 +24,10 @@ public InMemoryToast(Type toastType)
{
ToastType = toastType;
}

public void Close()
{
throw new NotImplementedException();
}
}
}
74 changes: 47 additions & 27 deletions src/Blazored.Toast.TestExtensions/InMemoryToastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,76 @@ public class InMemoryToastService : IToastService
{
private readonly List<InMemoryToast> _toasts = new();
public IReadOnlyList<InMemoryToast> Toasts => _toasts;
public event Action<Type, ToastParameters?, Action<ToastSettings>?>? OnShowComponent;
public event Action<ToastLevel, RenderFragment, Action<ToastSettings>?>? OnShow;

public event Func<Type, ToastParameters?, Action<ToastSettings>?, IToastInstance>? OnShowComponent;
public event Func<ToastLevel, RenderFragment, Action<ToastSettings>?, IToastInstance>? OnShow;
public event Action? OnClearAll;
public event Action<ToastLevel>? OnClearToasts;
public event Action? OnClearCustomToasts;
public event Action? OnClearQueue;
public event Action<ToastLevel>? OnClearQueueToasts;

public void ShowToast<TComponent>() where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(ToastParameters parameters) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(Action<ToastSettings>? settings) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(ToastParameters parameters, Action<ToastSettings>? settings) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowError(string message, Action<ToastSettings>? settings = null)
public IToastInstance ShowToast<TComponent>() where TComponent : IComponent
{
var instance = new InMemoryToast(typeof(TComponent));
_toasts.Add(instance);
return instance;
}

public IToastInstance ShowToast<TComponent>(ToastParameters parameters) where TComponent : IComponent
{
var instance = new InMemoryToast(typeof(TComponent));
_toasts.Add(instance);
return instance;
}

public IToastInstance ShowToast<TComponent>(Action<ToastSettings>? settings) where TComponent : IComponent
{
var instance = new InMemoryToast(typeof(TComponent));
_toasts.Add(instance);
return instance;
}

public IToastInstance ShowToast<TComponent>(ToastParameters parameters, Action<ToastSettings>? settings) where TComponent : IComponent
{
var instance = new InMemoryToast(typeof(TComponent));
_toasts.Add(instance);
return instance;
}

public IToastInstance ShowError(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Error, message, settings);

public void ShowError(RenderFragment message, Action<ToastSettings>? settings = null)
public IToastInstance ShowError(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Error, message, settings);

public void ShowInfo(string message, Action<ToastSettings>? settings = null)
public IToastInstance ShowInfo(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Info, message, settings);

public void ShowInfo(RenderFragment message, Action<ToastSettings>? settings = null)
public IToastInstance ShowInfo(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Info, message, settings);

public void ShowSuccess(string message, Action<ToastSettings>? settings = null)
public IToastInstance ShowSuccess(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Success, message, settings);

public void ShowSuccess(RenderFragment message, Action<ToastSettings>? settings = null)
public IToastInstance ShowSuccess(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Success, message, settings);

public void ShowToast(ToastLevel level, string message, Action<ToastSettings>? settings = null)
public IToastInstance ShowToast(ToastLevel level, string message, Action<ToastSettings>? settings = null)
=> ShowToast(level, builder => builder.AddContent(0, message), settings);

public void ShowToast(ToastLevel level, RenderFragment message, Action<ToastSettings>? settings = null)
=> _toasts.Add(new InMemoryToast(typeof(ToastInstance), level, message));
public IToastInstance ShowToast(ToastLevel level, RenderFragment message, Action<ToastSettings>? settings = null)
{
var instance = new InMemoryToast(typeof(IToastInstance), level, message);
_toasts.Add(instance);
return instance;
}

public void ShowWarning(string message, Action<ToastSettings>? settings = null)
public IToastInstance ShowWarning(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Warning, message, settings);

public void ShowWarning(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Warning, message, settings);
public IToastInstance ShowWarning(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Warning, message, settings);

public void ClearAll()
=> _toasts.Clear();
Expand Down
Loading