larshg / Toast

A JavaScript free toast library for Blazor and Razor Component applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blazored Toast

This is a JavaScript free toast implementation for Blazor and Razor Components applications.

Build Status

Nuget

Important Notice For Blazor server-side Apps

There is currently an issue with Blazor server-side apps (not Blazor). They are unable to import static assets from component libraries such as this one.

You can still use this package, however, you will need to manually add the CSS to your apps wwwroot folder. You will then need to add a reference to it in the head tag of your apps index.html page.

Alternatively, there is a great package by Mister Magoo which offers a solution to this problem without having to manually copy files.

Getting Setup

You can install the package via the nuget package manager just search for Blazored.Toast. You can also install via powershell using the following command.

Install-Package Blazored.Toast

Or via the dotnet CLI.

dotnet add package Blazored.Toast

1. Register Services

First, you will need to register the Blazored Toast service in your applications Startup.ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddBlazoredToast(options => {
        options.Timeout = 10; // default: 5
        options.Position = ToastPosition.BottomRight; // default: ToastPosition.TopRight
    });
}

2. Add Imports

Second, add the following to your _Imports.razor

@using Blazored
@using Blazored.Toast.Services

3. Register Toasts Component

Third and finally you will need to register the <BlazoredToasts /> component in your applications MainLayout.cshtml.

Usage

In order to show a toast you have to inject the IToastService into the component or service you want to trigger a toast. You can then call one of the following methods depending on what kind of toast you want to display, passing in a message and an optional heading.

  • ShowInfo
  • ShowSuccess
  • ShowWarning
  • ShowError
@page "/toastdemo"
@inject IToastService toastService

<h1>Toast Demo</h1>

To show a toast just click one of the buttons below.

<button class="btn btn-info" onclick="@(() => toastService.ShowInfo("I'm an INFO message"))">Info Toast</button>
<button class="btn btn-success" onclick="@(() => toastService.ShowSuccess("I'm a SUCCESS message with a custom title", "Congratulations!"))">Success Toast</button>
<button class="btn btn-warning" onclick="@(() => toastService.ShowWarning("I'm a WARNING message"))">Warning Toast</button>
<button class="btn btn-danger" onclick="@(() => toastService.ShowError("I'm an ERROR message"))">Error Toast</button>

About

A JavaScript free toast library for Blazor and Razor Component applications

License:MIT License


Languages

Language:C# 72.7%Language:CSS 18.3%Language:HTML 8.9%