ToggleButton
Since 0.1.0-alpha.22A button that stays on or off: a real checkbox drawn as a button, so it toggles with JavaScript off, shows its state from :checked and posts with a form like any checkbox.
Toggle buttons
A toggle button turns something on and off, such as a filter. Bind it with @bind-Value, and keep its text the same whether it's on or off. It's a checkbox drawn as a button, so screen readers hear "checked" or "not checked".
Always Interactive
Preview
5 of 12 messages
Code
@* Filters that stay on or off: @bind-Value on each, and screen readers hear "checked". *@
<Stack Gap="3" Align="Align.Center">
<Row Gap="2" Wrap Justify="Justify.Center">
<ToggleButton Icon="Lucide.Star" Size="ControlSize.Sm" @bind-Value="_starred">Starred</ToggleButton>
<ToggleButton Icon="Lucide.Paperclip" Size="ControlSize.Sm" @bind-Value="_attachments">Has attachments</ToggleButton>
<ToggleButton Icon="Lucide.Mail" Size="ControlSize.Sm" @bind-Value="_unread">Unread</ToggleButton>
</Row>
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Shown.Count() of Messages.Length messages</TextBlock>
</Stack>
{
private bool _starred;
private bool _attachments;
private bool _unread = true;
private IEnumerable<Message> Shown => Messages.Where(m => (!_starred || m.Starred) && (!_attachments || m.Attachments) && (!_unread || m.Unread));
}Add this example to a page with the izzy CLI:
izzy add component ToggleButton --at <file>:<line>Icon only
To show only the icon, set IconOnly and Label: the label is its name for screen readers and its tooltip. To show the state in the icon, such as a muted microphone, set CheckedIcon, shown while it's checked in place of Icon; the label stays the same.
Always Interactive
Preview
Muted · Camera on · Captions off
Code
@* Call controls: icons alone, each named by its Label, which stays the same as it toggles; CheckedIcon
shows its state. *@
<Stack Gap="3" Align="Align.Center">
<Row Gap="2">
<ToggleButton Icon="Lucide.Mic" CheckedIcon="Lucide.MicOff" Label="Mute" IconOnly @bind-Value="_muted" />
<ToggleButton Icon="Lucide.Video" CheckedIcon="Lucide.VideoOff" Label="Turn off camera" IconOnly @bind-Value="_cameraOff" />
<ToggleButton Icon="Lucide.CaptionsOff" CheckedIcon="Lucide.Captions" Label="Captions" IconOnly @bind-Value="_captions" />
</Row>
<TextBlock Size="TextSize.Sm" Color="Color.Muted">Status</TextBlock>
</Stack>
{
private bool _muted = true;
private bool _cameraOff;
private bool _captions;
private string Status => string.Join(" · ", new[]
{
_muted ? "Muted" : "Mic on",
_cameraOff ? "Camera off" : "Camera on",
_captions ? "Captions on" : "Captions off",
});
}Applying at once
On static pages a toggle changes nothing until its form posts. To post the form as it changes, set SubmitOnChange. With JavaScript off, give the form a submit button, here in a noscript.
Static
Preview
Code
@* A filter that applies as it's toggled, on static pages too: SubmitOnChange posts the form. With
JavaScript off, Apply posts it. *@
<Form Model="Model" FormName="toggle-button-submit-on-change" Grow>
<Row Gap="2" Align="Align.Center">
<ToggleButton @bind-Value="Model!.ShowArchived" Icon="Lucide.Archive" Size="ControlSize.Sm" SubmitOnChange>Show archived</ToggleButton>
<noscript><Button Submit Size="ControlSize.Sm" Variant="ButtonVariant.Ghost">Apply</Button></noscript>
</Row>
<List>
foreach (var project in Projects.Where(p => Model!.ShowArchived || !p.Archived))
{
<ListItem>project.Name@(project.Archived ? " (archived)" : "")</ListItem>
}
</List>
</Form>
{
[SupplyParameterFromForm(FormName = "toggle-button-submit-on-change")]
private Filter? Model { get; set; }
protected override void OnInitialized() => Model ??= new();
public sealed class Filter
{
public bool ShowArchived { get; set; }
}
}Static: Toggling posts the form, and the list comes back filtered.
Details
Bound to a model property in a Form it posts like a Checkbox: a hidden "false" follows it, so an off toggle posts "false". On interactive pages ValueChanged runs C# at once; on static pages it changes nothing until the form posts, unless SubmitOnChange posts it as the toggle changes. Several toggles that belong together go in a ToggleGroup; in a Toolbar, use ToolbarToggleButton.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| Toggles, shows its state and posts with JavaScript off | Yes | Yes |
| Binds a bool, in a Form or on its own | Yes | Yes |
| Posts its form as it changes (SubmitOnChange) | Yes | Yes |
| ValueChanged runs C# at once | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off.
- Posts its form as it changes (SubmitOnChange): waits for the script
Accessibility
- It renders a native checkbox inside a
labeldrawn as the button: Space toggles it, clicking anywhere on it does too, and screen readers hear "checkbox, checked" or "not checked". - Its text names it. With
IconOnly,Labelnames it, hidden from sight, and is its tooltip. - The icon is hidden from screen readers.
Parameters
| Prop | Type | Default |
|---|---|---|
ChildContentThe text, such as "Starred". | RenderFragment? | |
IconAn icon before the text, such as Lucide.Mic; while it's off, when CheckedIcon is set. | IconData? | |
CheckedIconThe icon while it's on, in place of Icon, such as Lucide.MicOff on a mute toggle. | IconData? | |
IconOnlyShows only the Icon; Label stays as its name for screen readers and its tooltip. | bool | false |
SubmitOnChangePosts the form around it as it changes, such as a filter in a GET form on a static page. | bool | false |
AccentIts color. | Accent? | computed |
VariantIts look. | ButtonVariant? | computed |
@bind-ValueThe value, bound with @bind-Value. | bool | false |
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed. | string? |
Plus 28 field props.
Full APISee also
- ToggleGroup: joins toggle buttons that any of can be on, such as text styles.
- Checkbox: shows a checkbox styled to match the fields.
- Switch: shows an on/off switch that posts like a checkbox.
- SegmentedControl: lets the user pick one of a few choices from joined buttons.