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

Forms

TwSwitch

TwSwitch is a Blazor toggle switch component for on and off settings. Bind Value and set Color, Name, and disabled or readonly states.

Last updated 22 October 2026

Basic usage

A toggle switch for binary on/off state, bound with @bind-Value.

Notifications are on

RAZOR
<TwSwitch @bind-Value="notificationsEnabled" Label="Notifications" AriaLabel="Toggle notifications" />
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Notifications are <strong>@(notificationsEnabled ? "on" : "off")</strong></p>
CSHARP
@code {
    private bool notificationsEnabled = true;
}

Colors

Use Color to give a switch meaning at a glance - here each toggle drives an independent, bound setting.
RAZOR
<div>
    @foreach (Color c in ((Color[])Enum.GetValues(typeof(Color))).Take(10))
    {
        @if (c != Color.Light && c != Color.Dark)
        {
            <div class="p-3">
                <TwSwitch @bind-Value="exampleSwitch" Label="@c.ToString()" Color="@c" AriaLabel="@($"{@c.ToString()} switch")" />
            </div>
        }
        else
        {
            <div class="@(c == Color.Light ? "bg-gray-900 dark:bg-transparent p-3 rounded-lg" : "dark:bg-white p-3 rounded-lg")">
                <TwSwitch @bind-Value="exampleSwitch" Label="@c.ToString()" Color="@c" LabelClass="@(c == Color.Light ? "text-white dark:text-white" : "text-gray-900 dark:text-gray-900")" AriaLabel="@($"{@c.ToString()} switch")" />
            </div>
        }
    }
</div>

Disabled & readonly

Disabled dims and locks the switch; ReadOnly keeps its state and color but blocks changes.
RAZOR
<div class="space-y-3">
    <TwSwitch @bind-Value="disabledValue" Label="Disabled Switch" Disabled Color="Color.Primary" AriaLabel="Disabled switch" />
    <TwSwitch @bind-Value="readonlyValue" Label="Readonly Switch" ReadOnly Color="Color.Primary" AriaLabel="Readonly switch" />
</div>
CSHARP
@code {
    private bool disabledValue = false;
    private bool readonlyValue = true;
}

Settings panel

A practical example - each switch binds to its own field, and a summary below reflects the combined state.

Active settings:

  • Email notifications
RAZOR
<div class="space-y-3">
    <TwSwitch @bind-Value="emailNotifications" Label="Email notifications" AriaLabel="Toggle email notifications" />
    <TwSwitch @bind-Value="smsNotifications" Label="SMS notifications" AriaLabel="Toggle SMS notifications" />
    <TwSwitch @bind-Value="marketingEmails" Label="Marketing emails" AriaLabel="Toggle marketing emails" />
</div>

<div class="mt-4 p-3 rounded border border-gray-200 dark:border-gray-600">
    <p class="text-sm text-gray-600 dark:text-gray-400">Active settings:</p>
    @if (activeSettings.Count > 0)
    {
        <ul class="mt-1 text-sm list-disc list-inside">
            @foreach (var setting in activeSettings)
            {
                <li>@setting</li>
            }
        </ul>
    }
    else
    {
        <p class="mt-1 text-sm text-gray-500 dark:text-gray-400 italic">None enabled</p>
    }
</div>
CSHARP
@code {
    private bool emailNotifications = true;
    private bool smsNotifications = false;
    private bool marketingEmails = false;
    
    private List<string> activeSettings
    {
        get
        {
            var settings = new List<string>();
            if (emailNotifications) settings.Add("Email notifications");
            if (smsNotifications) settings.Add("SMS notifications");
            if (marketingEmails) settings.Add("Marketing emails");
            return settings;
        }
    }
}

Keyboard navigation

A switch is announced as a switch that is on or off. A read-only switch can be focused but not changed.
Key What it does
Tab Moves focus to the switch.
Space Turns it on or off.

Theme configuration

Styling is controlled by TwBlazorTheme.Components (a TwSwitchTheme), 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
Switch string
Track string
WrapperSize string
Base string
LabelBase string
LabelInteractiveCursor string
LabelNonInteractiveCursor string
LabelDisabled 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

Full reference: TwSwitchTheme on the API docs.

On this page

  • Basic usage
  • Colors
  • Disabled & readonly
  • Settings panel
  • 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