Toast Service
Show, dismiss, and update toast notifications programmatically from C# code.
Quick Start
The ToastService is registered automatically when you call
AddLumeo() in your Program.cs.
You only need to place a <ToastProvider /> in your layout and then inject the service wherever you need it.
1. Register services
2. Add the provider to your layout
Place <ToastProvider /> once in your
MainLayout.razor. This is the container that renders all toast notifications.
3. Inject and use
Inject ToastService into any component and call one of its methods to show a toast.
Every method returns a string toast ID you can use to dismiss or update it later.
Variants
Five built-in variants cover the most common notification types. Each has a convenience method that sets the
ToastVariant for you.
Click each button to see the variant in action:
With Options
For full control, pass a ToastOptions object. This lets you set action buttons, durations, dismiss behavior, callbacks, and custom CSS.
Action Button
Add an action button to the toast with ActionLabel and
OnAction. Common use case: undo a destructive operation.
Non-dismissible (Persistent)
Set Dismissible = false to hide the close button and
Duration = 0 to prevent auto-dismiss. The toast stays until you call
Dismiss(id) or Update(id, ...) programmatically.
Custom Duration
Override the default 5-second duration. Set any value in milliseconds, or 0 for no auto-dismiss.
Custom CSS Class
Apply additional CSS classes to the toast element with the Class property.
OnDismiss Callback
Run code when a toast is dismissed (by the user or by timeout) using the OnDismiss callback.
Dismiss & Update
Every Show() call returns a unique toast ID. Use it to dismiss or update that toast later.
Dismiss by ID
Store the ID returned by Show() and pass it to
Dismiss(id) to remove a specific toast.
Dismiss All
Clear every active toast at once. Useful when navigating away from a page or resetting state.
Update an Existing Toast
Change the title, description, variant, or any other property of an active toast. The toast stays in place and its timer resets.
Real-world Pattern: Upload Progress
A common pattern: show a persistent loading toast, then update it to success or error when the operation completes.
Promise Toast
The Promise<T> method wraps an async operation.
It shows a loading toast, then automatically transitions to success or error based on the result. No manual ID tracking needed.
Async Success
The action returns a value, which you can use in the success toast options.
Async Error
If the action throws, the error callback receives the exception and displays a destructive toast.
Custom Content
When the built-in title/description layout isn't enough, pass a RenderFragment as
CustomContent to render arbitrary Razor markup inside the toast.
You can also use the Show(RenderFragment, variant?, duration?) overload for a shorter syntax:
Toast Positions
The ToastProvider accepts a
Position parameter that determines where toasts appear on screen.
This is set once in your layout and applies to all toasts.
| Position | CSS Placement |
|---|---|
| TopLeft | Fixed to top-left corner |
| TopCenter | Fixed to top-center |
| TopRight | Fixed to top-right corner |
| BottomLeft | Fixed to bottom-left corner |
| BottomCenter | Fixed to bottom-center |
| BottomRight | Fixed to bottom-right corner (default) |
Position is set on the ToastProvider in your layout, not on individual toasts.
All toasts share the same position. If you need multiple positions, add multiple providers (not recommended).
API Reference
ToastService Methods
All Show methods return a
string toast ID.
| Method | Returns | Description |
|---|---|---|
| Show(title, description?, variant?) | string | Show a toast with a title, optional description, and optional variant. |
| Show(ToastOptions) | string | Show a toast with full control over all options. |
| Show(RenderFragment, variant?, duration?) | string | Show a toast with custom Razor content instead of title/description. |
| Success(title, description?) | string | Shorthand for Show(title, description, ToastVariant.Success). |
| Error(title, description?) | string | Shorthand for Show(title, description, ToastVariant.Destructive). |
| Warning(title, description?) | string | Shorthand for Show(title, description, ToastVariant.Warning). |
| Info(title, description?) | string | Shorthand for Show(title, description, ToastVariant.Info). |
| Dismiss(toastId) | void | Remove a specific toast by its ID. |
| DismissAll() | void | Remove all active toasts at once. |
| Update(toastId, ToastOptions) | void | Replace all options on an existing toast. Resets the auto-dismiss timer. |
| Promise<T>(action, loading, success, error) | Task<string> | Show a loading toast, run the async action, then update to success or error. Returns the toast ID. |
ToastOptions Properties
| Property | Type | Default | Description |
|---|---|---|---|
| Title | string? | null | The main heading text displayed in the toast. |
| Description | string? | null | Secondary text displayed below the title. |
| Variant | ToastVariant | Default | Visual style of the toast. Determines border and background color. |
| Duration | int? | null (uses provider default) | Auto-dismiss time in milliseconds. Set to 0 for no auto-dismiss. When null, falls back to the provider's DefaultDuration. |
| Dismissible | bool | true | Whether the close (X) button is rendered. Set to false for toasts that must be dismissed programmatically. |
| ActionLabel | string? | null | Text for an action button rendered inside the toast (e.g., "Undo", "Retry"). |
| OnAction | Action? | null | Callback invoked when the action button is clicked. |
| OnDismiss | Action? | null | Callback invoked when the toast is dismissed (by user click or programmatically). |
| CustomContent | RenderFragment? | null | Custom Razor content that replaces the default title/description layout. When set, Title and Description are ignored. |
| Class | string? | null | Additional CSS classes appended to the toast element. |
ToastVariant Enum
| Value | Convenience Method | Description |
|---|---|---|
| Default | Show() | Neutral notification with card background and border. |
| Success | Success() | Green-tinted background indicating a successful operation. |
| Destructive | Error() | Red-tinted background indicating a failure or error. |
| Warning | Warning() | Amber/yellow-tinted background for cautionary messages. |
| Info | Info() | Blue-tinted background for informational messages and tips. |
ToastProvider Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| Position | ToastViewport.ToastPosition | BottomRight | Screen position where toasts appear. One of: TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight. |
| DefaultDuration | int | 5000 | Default auto-dismiss duration in milliseconds for toasts that don't specify their own Duration. |
| MaxToasts | int | 5 | Maximum number of toasts visible at once. When exceeded, the oldest non-persistent toast is evicted. |