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

TwToggle

TwToggle is a two-state button that switches a formatting option, feature or mode on and off. Use Pressed, Variant, Size, Color, Icon and Disabled.

Last updated 9 November 2026

Basic usage

Bind the state with @bind-Pressed. A toggle that shows only an icon needs an AriaLabel.
Not bookmarked
RAZOR
<div class="flex items-center gap-4">
    <TwToggle @bind-Pressed="bookmarked" Icon="Icon.Bookmark" AriaLabel="Bookmark" />
    <span class="text-sm">@(bookmarked ? "Bookmarked" : "Not bookmarked")</span>
</div>
CSHARP
@code {
    private bool bookmarked;
}

Variants

ToggleVariant.Text (the default) has no border or background until it is hovered or pressed. ToggleVariant.Outlined adds a border.
RAZOR
<div class="flex items-center gap-4">
    <TwToggle Icon="Icon.Type_Italic" AriaLabel="Italic" />
    <TwToggle Icon="Icon.Type_Italic" AriaLabel="Italic" Variant="ToggleVariant.Outlined" />
    <TwToggle Icon="Icon.Type_Italic" AriaLabel="Italic" Variant="ToggleVariant.Outlined" Pressed="true" />
</div>

With text

Set Label next to an Icon, or pass any ChildContent.
RAZOR
<div class="flex items-center gap-4">
    <TwToggle Icon="Icon.Wifi" Label="Wi-Fi" Variant="ToggleVariant.Outlined" Pressed="true" />
    <TwToggle Icon="Icon.Bell" Label="Notifications" Variant="ToggleVariant.Outlined" />
</div>

Sizes

Set Size to Size.Sm, Size.Md (the default) or Size.Lg. The icon grows with the toggle.
RAZOR
<div class="flex items-center gap-4">
    <TwToggle Icon="Icon.Type_Bold" AriaLabel="Bold" Size="Size.Sm" Variant="ToggleVariant.Outlined" />
    <TwToggle Icon="Icon.Type_Bold" AriaLabel="Bold" Size="Size.Md" Variant="ToggleVariant.Outlined" />
    <TwToggle Icon="Icon.Type_Bold" AriaLabel="Bold" Size="Size.Lg" Variant="ToggleVariant.Outlined" />
</div>

Colors

A pressed toggle is drawn in a neutral tint. Set Color to tint it with one of the theme colors instead.
RAZOR
<div class="flex flex-wrap items-center gap-4">
    <TwToggle Icon="Icon.Star" Label="Neutral" Pressed="true" />
    <TwToggle Icon="Icon.Star" Label="Primary" Color="Color.Primary" Pressed="true" />
    <TwToggle Icon="Icon.Heart" Label="Danger" Color="Color.Danger" Pressed="true" />
    <TwToggle Icon="Icon.Pin" Label="Success" Color="Color.Success" Pressed="true" />
</div>

Disabled

A Disabled toggle cannot be focused or switched, and does not raise PressedChanged.
RAZOR
<div class="flex items-center gap-4">
    <TwToggle Icon="Icon.Type_Underline" AriaLabel="Underline" Disabled="true" />
    <TwToggle Icon="Icon.Type_Underline" AriaLabel="Underline" Disabled="true" Pressed="true" />
</div>

Custom styling

The pressed state is exposed as data-state="on", so a Class can restyle it with Tailwind's data-[state=on]: variant. Your classes are merged last and win over the theme's.
RAZOR
<TwToggle Icon="Icon.Heart" Label="Favourite" Pressed="true" Class="data-[state=on]:bg-pink-100 data-[state=on]:text-pink-700 dark:data-[state=on]:bg-pink-500/25 dark:data-[state=on]:text-pink-100" />

Formatting toolbar

Independent toggles, each with its own state, make a text formatting toolbar.

The quick brown fox jumps over the lazy dog.

RAZOR
<div class="flex flex-col gap-3">
    <div class="flex items-center gap-1" role="toolbar" aria-label="Text formatting">
        <TwToggle @bind-Pressed="bold" Icon="Icon.Type_Bold" AriaLabel="Bold" />
        <TwToggle @bind-Pressed="italic" Icon="Icon.Type_Italic" AriaLabel="Italic" />
        <TwToggle @bind-Pressed="underline" Icon="Icon.Type_Underline" AriaLabel="Underline" />
        <TwToggle @bind-Pressed="strikethrough" Icon="Icon.Type_Strikethrough" AriaLabel="Strikethrough" />
    </div>
    <p class="@previewClasses">The quick brown fox jumps over the lazy dog.</p>
</div>
CSHARP
@code {
    private bool bold = true;
    private bool italic;
    private bool underline;
    private bool strikethrough;
    
    private string previewClasses => string.Join(' ',
        "text-sm",
        bold ? "font-bold" : string.Empty,
        italic ? "italic" : string.Empty,
        TextDecorationClass());
    
    private string TextDecorationClass()
    {
        if (underline && strikethrough)
        {
            return "underline line-through";
        }
    
        if (underline)
        {
            return "underline";
        }
    
        if (strikethrough)
        {
            return "line-through";
        }
    
        return string.Empty;
    }
}

Accessibility

A toggle is a native <button> with aria-pressed, so screen readers announce it as a toggle button and say whether it is pressed. Keep its label the same in both states (for example "Bold", not "Make bold" and "Remove bold"), because the pressed state is announced separately. Icon-only toggles need an AriaLabel. In forced colors (Windows high contrast) mode a pressed toggle is drawn in the system highlight color.

Keyboard navigation

A toggle is a native button that announces whether it is pressed. A disabled toggle is not a Tab stop.
Key What it does
Tab Moves focus to the toggle.
Enter or Space Presses or releases the toggle.
Escape Hides the toggle's tooltip, if it has one.

Theme configuration

Styling is controlled by TwBlazorTheme.Components (a TwToggleTheme), 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.

Inherited from TwToggle - this component reuses that theme rather than defining its own.

Property Type Controls
ToggleRounded Rounded?
DefaultVariant ToggleVariant?
Base string
Sm string
Md string
Lg string
IconSm string
IconMd string
IconLg string
Unpressed string
Hover string
Pressed string
GroupPressed string
PressedForcedColors string
Outlined string
Disabled string
GroupBase string
GroupHorizontal string
GroupVertical string
GroupText string
GroupOutlined 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: TwToggleTheme on the API docs.

On this page

  • Basic usage
  • Variants
  • With text
  • Sizes
  • Colors
  • Disabled
  • Custom styling
  • Formatting toolbar
  • Accessibility
  • 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