# Blazor Toast Notifications

URL: https://flexcoreui.com/demo/notifications  
Section: Components  
Controls: NotificationService, NotificationDisplayControl  
Keywords: toast, notification, snackbar, alert message

Tell users what happened without stopping them: Blazor toasts for info, success, warning and error, raised from any component.

## Documentation

### Service

| Name | Type | Description |
|---|---|---|
| Info(message, detail?, durationMs = 5000) | void | Blue toast. |
| Success(message, detail?, durationMs = 4000) | void | Green toast. |
| Warning(message, detail?, durationMs = 8000) | void | Yellow toast. |
| Error(message, detail?, durationMs = 10000) | void | Red toast. durationMs: 0 keeps it until dismissed. |
| Dismiss(Guid id) | void | Removes one toast, such as a progress message. The add methods return nothing, so read the new toast's Id from Notifications. |
| Clear() | void | Removes every toast. |
| Notifications | IReadOnlyList<Notification> | Current toasts, newest first, at most 20. |
| OnChange | event Action | Raised after every add, dismiss or clear. Auto-dismiss raises it from a timer thread; marshal with InvokeAsync. |

### Item

| Name | Type | Default | Description |
|---|---|---|---|
| Id | Guid |  | Key for Dismiss. |
| Level | NotificationLevel |  | Info, Success, Warning or Error. |
| Message | string |  | Bold first line. |
| Detail | string? | null | Smaller second line. |
| Timestamp | DateTime |  | When it was added. |
| DurationMs | int |  | Auto-dismiss delay; 0 means sticky. |

### Css

| Name | Description |
|---|---|
| fx-notification-container | Fixed 12px from the top-right, 320–420px wide, z-index 10000, column with 8px gaps. |
| fx-notification · .fx-notification-info / -success / -warning / -error | One toast; the level class sets the tinted background, text color and 4px left border. It slides in from the right over 0.25s. |
| fx-notification-icon · fx-notification-body · fx-notification-message · fx-notification-detail | Icon glyph, text column, bold message, 11px detail at 80% opacity. |
| fx-notification-close | The × button, 50% opacity until hovered. |

## Examples

### Levels

```razor
@using Fx.ControlKit

<div class="nt">
    <div class="demo-row">
        <ButtonControl CssClass="btn-primary" OnClick="@(() => Notifications.Info("Pick list PICK-0318 assigned", "To Hermione Ranger, 12 lines"))">Info</ButtonControl>
        <ButtonControl CssClass="btn-success" OnClick="@(() => Notifications.Success("Receipt for PO-2291 saved", "2 pallets, 48 cartons"))">Success</ButtonControl>
        <ButtonControl CssClass="btn-warning" OnClick="@(() => Notifications.Warning("Bin A-12-03 is nearly full", "Room for 6 cartons, check before putaway"))">Warning</ButtonControl>
        <ButtonControl CssClass="btn-danger" OnClick="@(() => Notifications.Error("Shipping label did not print", "Shipment SHP-0456 at Dock 4"))">Error</ButtonControl>
        <ButtonControl CssClass="btn-outline-primary" OnClick="@(() => Notifications.Error("Pickup for shipment SHP-0457 was canceled", "Stays until you close it", durationMs: 0))">Sticky error</ButtonControl>
        <ButtonControl OnClick="@(() => Notifications.Clear())">Clear all</ButtonControl>
    </div>
    <p class="result" role="status">@(_count == 0 ? "No toasts showing." : $"{_count} showing, newest on top.")</p>
</div>
```

```csharp
// Levels: the page listens to the service to count the toasts on show
// The page declares @inject NotificationService Notifications (namespace Fx.ControlKit.Notifications)
// and @implements IDisposable, so Dispose() below runs and unhooks OnChange.
private int _count;
protected override void OnInitialized()
{
    Notifications.OnChange += HandleChange;   // auto-dismiss fires off the render thread
    _count = Notifications.Notifications.Count;
}
private void HandleChange() => _ = InvokeAsync(() => { _count = Notifications.Notifications.Count; StateHasChanged(); });
public void Dispose() => Notifications.OnChange -= HandleChange;
```

### Task

```razor
@using Fx.ControlKit

<div class="nt">
    <div class="demo-row">
        <ButtonControl CssClass="btn-primary" IconCss="bi bi-save" Disabled="_saving" OnClick="SaveAsync">Save receipt</ButtonControl>
        <SwitchControl @bind-Checked="_failSaves" Label="Make saves fail" />
    </div>
    <p class="result" role="status">@_saved saved, @_failed failed</p>
</div>
```

```csharp
// A task: a sticky progress toast, replaced by the outcome
// Notifications is the injected NotificationService, as in the first example.
private bool _saving, _failSaves;
private Guid? _lastError;
private int _saved, _failed;
private async Task SaveAsync()
{
    _saving = true;
    if (_lastError is Guid old) Notifications.Dismiss(old);   // a new attempt replaces the last error
    _lastError = null;
    Notifications.Info("Saving receipt for PO-2291…", durationMs: 0);
    var progress = Notifications.Notifications.First(n => n.Message == "Saving receipt for PO-2291…").Id;
    await Task.Delay(1500);   // stands in for the call that saves
    Notifications.Dismiss(progress);
    if (_failSaves)
    {
        _failed++;
        Notifications.Error("Could not save receipt for PO-2291", "The warehouse system did not answer. Try again.", durationMs: 0);
        _lastError = Notifications.Notifications.Last(n => n.Message == "Could not save receipt for PO-2291").Id;
    }
    else
    {
        _saved++;
        Notifications.Success("Receipt for PO-2291 saved");
    }
    _saving = false;
}
```

