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
Basic usage
Bind the state with
@bind-Pressed. A toggle that shows only an icon needs an AriaLabel.
Not 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.
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.
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.