Skip to main content
twblazor
Home
  • Get started
  • Tailwind with Blazor
  • Why twblazor?
  • Theme
  • Class Merge
    • Configuring localization
    • RTL Provider
  • Breadcrumb
  • Card
  • Carousel
  • Collapse
  • Context Menu
  • Drawer
  • Footer
  • Menu
  • Navbar
  • On This Page
  • Separator
  • Sidebar
  • Stepper
  • Tabs
  • Tree List
  • Container
  • Flex
  • Grid
  • Text
  • Link
  • Button
  • Form
    • AutocompleteNew
    • Checkbox
    • Color Picker
    • Date Picker
    • Date Range Picker
    • Datetime Picker
    • Datetime Range Picker
    • File Upload
    • Input GroupNew
    • NumericNew
    • Masked InputNew
    • Radio Button
    • Range SliderNew
    • RatingNew
    • Select
    • Slider
    • Switch
    • Tag InputNew
    • Text EditorNew
    • Textfield
    • Time Picker
    • Time Range Picker
  • Toggle
  • Toggle Group
  • Alert
  • Avatar
  • Badge
  • Chip
  • Empty
  • Icon
  • Popover
  • Progress
  • Skeleton
  • Spinner
  • Tooltip
  • Data Table
  • SpreadsheetNew
  • Column Filter Editor
  • Pagination
  • Pick List
  • Barcode
  • QR Code
  • Calendar
  • Table
  • Attachment
  • Overview
    • Bar
    • Bullet
    • Column
    • Dot Plot
    • Dumbbell
    • Icon
    • Lollipop
    • Radial Bar
    • Range
    • Word Cloud
    • Area
    • Bump
    • Bump Area
    • Candlestick
    • Gantt
    • Horizon
    • Line
    • Slope
    • Small Multiples
    • Stream Graph
    • Funnel
    • Gauge
    • Marimekko
    • Nightingale
    • Pie
    • Treemap
    • Waffle
    • Waterfall
    • Circular Treemap
    • Convex Treemap
    • Dendrogram
    • Sunburst
    • Barcode
    • Beeswarm
    • Box Plot
    • Density Plot
    • Histogram
    • Radial Histogram
    • Ridgeline
    • Strip Plot
    • Violin Plot
    • Categorical Scatter
    • Contour
    • Hexagonal Binning
    • Quadrant
    • Scatter
    • Heatmap
    • Matrix
    • Parallel Coordinates
    • Radar
    • Arc Diagram
    • Chord Diagram
    • Flowchart
    • Network Diagram
    • Sankey
    • Euler Diagram
    • Venn Diagram
  • Bubble
  • Marker
  • Message
  • Dialog Service
  • Toast Service
API DocumentationAI Agents & Skills
Home Get Started

Services

TwToastProvider

TwToast is a Blazor toast notification service. Call ITwToastService.Show with a message, title, Color and icon, with duration, persistence and custom toasts.

Last updated 7 November 2026

Setup

The Toast service provides a simple way to display temporary notification messages to users. To use toasts, add the TwToastProvider component to your app and inject the ITwToastService.

1. Add TwToastProvider to your layout

Place the <TwToastProvider /> component in your main layout file (e.g., MainLayout.razor or App.razor):

HTML
<TwToastProvider />

2. Register the services

The toast service is registered automatically when you call AddTwBlazor() with your configured theme (see Get Started for the full theme setup):

CSHARP
builder.Services.AddTwBlazor(_ => { }, Theme.CreateDefaultTheme);

3. Inject the service in your component

CSHARP
@inject ITwToastService ToastService

Toast types

The toast service provides convenient methods for common notification types: Success, Error, Warning, and Info.
RAZOR
<div class="grid grid-cols-2 md:grid-cols-4 gap-3">
    <TwButton Label="Success" Color="Color.Success" OnClick="ShowSuccessToast" Class="w-full" />
    <TwButton Label="Error" Color="Color.Danger" OnClick="ShowErrorToast" Class="w-full" />
    <TwButton Label="Warning" Color="Color.Warning" OnClick="ShowWarningToast" Class="w-full" />
    <TwButton Label="Info" Color="Color.Primary" OnClick="ShowInfoToast" Class="w-full" />
</div>
CSHARP
@code {
    private void ShowSuccessToast()
    {
        ToastService.ShowSuccess("Success!", "Your operation completed successfully.");
    }
    
    private void ShowErrorToast()
    {
        ToastService.ShowError("Error", "Something went wrong.", 0);
    }
    
    private void ShowWarningToast()
    {
        ToastService.ShowWarning("Warning", "Please review your input.");
    }
    
    private void ShowInfoToast()
    {
        ToastService.ShowInfo("Information", "Here's some useful information.");
    }
}

Custom toasts

Create custom toasts with any color, icon, and duration using the Show() method, or build a ToastModel directly for full control.
RAZOR
<TwButton Label="Show Custom Toast" Color="Color.Accent" OnClick="ShowCustomToast" Class="w-full" />
CSHARP
@code {
    private void ShowCustomToast()
    {
        ToastService.Show("Custom Toast", "This is a custom toast with a heart icon!", Color.Accent, Icon.Heart, 3000);
    }
}
RAZOR
<TwButton Label="Show Manual Toast" Color="Color.Primary" OnClick="ShowManualToast" Class="w-full" />
CSHARP
@code {
    private void ShowManualToast()
    {
        ToastService.AddToast(new ToastModel
        {
            Title = "Manual Toast",
            Message = "Created with ToastModel directly",
            Color = Color.Primary,
            Icon = Icon.Bell,
            Duration = 5000
        });
    }
}

Colors

Toasts support every color in the Color enum, sharing the same palette as TwAlert.
RAZOR
<div class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-2">
    @foreach (Color c in (Color[])Enum.GetValues(typeof(Color)))
    {
        <TwButton Label="@c.ToString()" Color="@c" OnClick="@(() => ShowColorToast(c))" Variant="ButtonVariant.Filled" Class="text-xs" />
    }
</div>
CSHARP
@code {
    private void ShowColorToast(Color color)
    {
        ToastService.Show($"{color} Toast", $"This is a {color.ToString().ToLower()} colored toast", color, Icon.Palette, 3000);
    }
}

Duration & persistence

By default, toasts auto-dismiss after 5 seconds. Customize the duration per toast, or set it to 0 to make it persistent until manually dismissed.
RAZOR
<div class="grid grid-cols-1 md:grid-cols-3 gap-3">
    <TwButton Label="3 Second Toast" Color="Color.Success" OnClick="@(() => ShowDurationToast(3000))" Class="w-full" />
    <TwButton Label="5 Second Toast (Default)" Color="Color.Primary" OnClick="@(() => ShowDurationToast(5000))" Class="w-full" />
    <TwButton Label="Persistent Toast" Color="Color.Accent" OnClick="ShowPersistentToast" Class="w-full" />
</div>
CSHARP
@code {
    private void ShowDurationToast(int duration)
    {
        var seconds = duration / 1000;
        ToastService.Show($"{seconds} Second Toast", $"This toast will dismiss after {seconds} seconds", Color.Success, Icon.Clock, duration);
    }
    
    private void ShowPersistentToast()
    {
        ToastService.Show("Persistent Toast", "This toast won't auto-dismiss. Click the X to close.", Color.Accent, Icon.Info_Circle, 0);
    }
}

Managing toasts

Clear all active toasts programmatically using the ClearAll() method.
RAZOR
<div class="flex gap-2 mb-2">
    <TwButton Label="Show Multiple Toasts" Color="Color.Primary" OnClick="ShowMultipleToasts" />
    <TwButton Label="Clear All Toasts" Color="Color.Danger" OnClick="@(() => ToastService.ClearAll())" Variant="ButtonVariant.Outlined" />
</div>
CSHARP
@code {
    private void ShowMultipleToasts()
    {
        ToastService.ShowSuccess("First Toast", "This is the first toast");
        ToastService.ShowInfo("Second Toast", "This is the second toast");
        ToastService.ShowWarning("Third Toast", "This is the third toast");
    }
}

Keyboard navigation

A toast is announced when it appears: errors and warnings at once, everything else when the screen reader is idle. Its timer pauses while it has focus or the pointer is over it, and toasts stay reachable while a dialog is open.
Key What it does
Tab Moves focus to a toast's close button.
Enter or Space Closes the toast.

Theme configuration

Styling is controlled by TwBlazorTheme.Components (a TwToastTheme), configured via the AddTwBlazor() options callback described on the Get Started page. Every property is a set of Tailwind classes, so changes are picked up by your own Tailwind build.
Property Type Controls
ToastRounded Rounded?
Container string
Toast string
HeaderClasses string
Title string
Message string
Timestamp string
IconContainer string
CloseButton string
ToastWidth string

Global Theme Configuration

These reuse shared design tokens configured once on TwBlazorTheme, rather than being unique to this component.
Property Type Controls
Colors TwBlazorPalette

For example, to move toasts to the top-right and make them fully rounded:

CSHARP
@code {
    builder.Services.AddTwBlazor(options =>
    {
        options.Theme.Toast.Container = "fixed top-0 right-0 p-4 z-50 flex flex-col gap-2 max-w-md"; // Move to top-right
        options.Theme.Toast.ToastRounded = Rounded.Full; // Make toasts fully rounded
        options.Theme.Toast.ToastWidth = "w-96 max-w-full"; // Wider toasts
    }, Theme.DefaultTheme);
}

Duration isn't themed - it's passed per call via Show(..., duration) or ToastModel.Duration (default 5000ms, 0 = persistent). See the Theme page for the full colour and design-token reference.

Full reference: TwToastTheme on the API docs.

On this page

  • Setup
  • Toast types
  • Custom toasts
  • Colors
  • Duration & persistence
  • Managing toasts
  • Keyboard navigation
  • Theme configuration
twblazor

The Tailwind CSS component library for Blazor. Accessible, themeable and MIT licensed.

Discord (opens in a new tab) GitHub(opens in a new tab)

Get started

  • Install guide
  • Theming
  • Class merging
  • Localization

Guides

  • Tailwind CSS with Blazor
  • Why twblazor?
  • Right to left
  • AI agents and skills

Resources

  • API documentation (opens in a new tab)
  • llms.txt (opens in a new tab)
  • MIT License (opens in a new tab)

Community

  • Contributing (opens in a new tab)
  • Code of conduct (opens in a new tab)
  • Report an issue (opens in a new tab)

twblazor is and always will be free and open source.

An unhandled error has occurred. Reload