Toggle Group

A set of toggles where one, or several, can be on at a time.

ToggleGroup is new in v0.4. It holds ToggleGroupItems and tracks which are pressed: one value with @bind-Value, or a list with Multiple and @bind-Values.

Installation

bash
shellui add toggle-group

This also installs ToggleGroupItem.

Usage

Single selection

Bind a string? with @bind-Value. Pressing the active item again clears the value.

Alignment: center

<ToggleGroup @bind-Value="align">
    <ToggleGroupItem Value="left">Left</ToggleGroupItem>
    <ToggleGroupItem Value="center">Center</ToggleGroupItem>
    <ToggleGroupItem Value="right">Right</ToggleGroupItem>
</ToggleGroup>

<p class="mt-3 text-sm text-muted-foreground">Alignment: @(align ?? "none")</p>

@code {
    private string? align = "center";
}

Multiple selection

Set Multiple="true" and bind a List<string> with @bind-Values.

Formats: bold

<ToggleGroup Multiple="true" @bind-Values="formats">
    <ToggleGroupItem Value="bold"><span class="font-bold">B</span></ToggleGroupItem>
    <ToggleGroupItem Value="italic"><span class="italic">I</span></ToggleGroupItem>
    <ToggleGroupItem Value="underline"><span class="underline">U</span></ToggleGroupItem>
</ToggleGroup>

<p class="mt-3 text-sm text-muted-foreground">
    Formats: @(formats.Count == 0 ? "none" : string.Join(", ", formats))
</p>

@code {
    private List<string> formats = new() { "bold" };
}

Disabled

Disable the whole group, or single items.

<div class="flex flex-col gap-4">
    <ToggleGroup Value="week" Disabled="true">
        <ToggleGroupItem Value="day">Day</ToggleGroupItem>
        <ToggleGroupItem Value="week">Week</ToggleGroupItem>
        <ToggleGroupItem Value="month">Month</ToggleGroupItem>
    </ToggleGroup>

    <ToggleGroup @bind-Value="view">
        <ToggleGroupItem Value="list">List</ToggleGroupItem>
        <ToggleGroupItem Value="board">Board</ToggleGroupItem>
        <ToggleGroupItem Value="gantt" Disabled="true">Gantt</ToggleGroupItem>
    </ToggleGroup>
</div>

@code {
    private string? view = "list";
}

API Reference

ToggleGroup

Parameter Type Default Description
Multiple bool false Allow more than one pressed item
Value string? null The pressed item in single mode
ValueChanged EventCallback<string?> - Fires when the single value changes
Values List<string> empty The pressed items when Multiple is set
ValuesChanged EventCallback<List<string>> - Fires when the list changes
Disabled bool false Disables every item
Class string? null Additional CSS classes

ToggleGroupItem

Parameter Type Default Description
Value string "" The value this item represents
Disabled bool false Disables this item
ChildContent RenderFragment? null Item content
Class string? null Additional CSS classes

Accessibility

  • Single mode renders role="radiogroup" with role="radio" items; multiple mode renders role="group" with role="checkbox" items
  • aria-checked and data-state (on / off) reflect each item's state
  • Items are native buttons, so Tab, Enter and Space work

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.