Blazor Toast Application


Any application that does any interactions work with a user needs to provide some form of user feedback to them to let the user know the application is busy doing work, running a log process like uploading a file or that action succeeded or failed.  Every UI framework has some form of feedback from a simple alert box to styled message boxes to toasts. 

Like them or not, toast popups are a nice way to let the user when something happens, warn them about an action they are about to perform or when an error happened.


As with many things with Blazor, there is a component for that.  All the free UI frameworks, Blazorise , Radzen or any of the paid for packages, Telerik, DevExpress and SyncFusion contain toast messages.  I found a nice one from Chris Sainty.  It is a toast without any JavaScript, just C#, HTML and CSS.  Which means you can restyle it to anything you would like.  We will be exploring how to implement Chris’s Toast component.


You can get the component either as a Nuget package or from the git hub repository listed above.  We will be using the  source code from Chris’s GitHub and linking the project.
The toast component is a service that we will inject into our application then simply call the service when we need to display out toast message.  It is that simple.

Once we have the new project created, Toast Sample, we will need to:
1.      Link in the Toast Project
2.      Add the toast project as a dependency in the Blazor project
3.      Add our CSS to _Host.cshtml or Index.html (depending on the Blazor configuration you are building)

    <link href="_content/Blazored.Toast/blazored-toast.css" rel="stylesheet" />

4.   Add the toast service in the Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddBlazoredToast();
            services.AddSingleton<CityWeatherService>();
    }

       5.       Add our using statements to _Imports.razor

       @using Blazored.Toast
      @using Blazored.Toast.Services

6.       Add the configuration to MainLayout.       

           @using Blazored.Toast.Configuration

       <BlazoredToasts Position="ToastPosition.BottomRight"
                Timeout="5"
                SuccessClass="success-toast-override"
                SuccessIconClass="fa fa-thumbs-up"
                ErrorIconClass="fa fa-bug" />

Notice this is where the toast service gives you a place you can customize how your toast show up.  You can adjust how long the toast is displayed by the timeout. You can adjust the CSS classes and the icons.

We are now ready to start using the service.  We will be adding a notification on the Counter page; the level of the message will be based on the value of the counter.  We will also add a notification to the Fetched data page to show when we are done loading the data.

On the Counter Page:

      1.       We need to inject the IToastService into the page

@inject IToastService toastService

      2.       We need to update the IncrementCount Method to check for the value of currentCount and sow the correct toast type.  Notice we are passing in the toast message and the title of the toast.  The title is an optional parameter.

    void IncrementCount()
    {
        currentCount++;
        if (currentCount < 3)
        {
            toastService.ShowInfo($"Count is set to {currentCount}", "Counter was Clicked");
        }
        else if (currentCount > 2 && currentCount < 5)
        {
            toastService.ShowWarning($"Count is set to {currentCount}", "Counter was Clicked");
        }
        else
        {
            toastService.ShowError($"Count is set to {currentCount}", "Counter was Clicked");
        }
          }

On the FetchData Page:

        1.       Injected the Toast Service

@inject IToastService toastService

        2.       Added the call to display the toast once we got the data

 protected override async Task OnInitializedAsync()
    {
        forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
        toastService.ShowInfo($"Found {forecasts.Length} records", "Fetch the Forecast");
    }

Now we have some nice user feedback that is very simple to implement in our application.  If you like the Toast user experience, then you should add this into your UI component library.

The fact that Blazor is component based and that the Blazor community is so awesome, thanks again to Chris Sainty, makes building cool Blazor apps so much fun.

Piggybacked on https://chrissainty.com/announcing-blazored-and-blazored-toast/

Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor - Cool Blur Loading Page