# Blazor Toggle Button and Button Group

URL: https://flexcoreui.com/demo/toggle-buttons  
Section: Components  
Controls: ToggleButtonControl, ButtonGroupControl, SegmentedControl  
Keywords: toggle button, button group, segmented control, select bar

Blazor toggle buttons, button groups and a segmented bar: turn options on and off, or pick one value or several from a row of buttons.

## Documentation

### Toggle

| Name | Type | Default | Description |
|---|---|---|---|
| Selected | bool | false | Pressed state; two-way with @bind-Selected. |
| ChildContent | RenderFragment? | null | The label: text or any markup, after the icon. |
| IconCss | string? | null | Classes for an icon before the label, such as bi bi-journal-text. |
| Enabled (base) | bool | true | False disables the button and keeps its pressed look. There is no Disabled parameter. |
| Title (base) | string? | null | Native tooltip. An icon-only toggle also needs aria-label. |
| Visible (base) | bool | true | False hides the button with display:none. |
| CssClass / Style (base) | string? | null | On the inner button. |
| Id / Tag / Data / Parent (base) |  | null | Accepted but not rendered. A written id binds to Id, so it does not reach the button. |
| (unmatched attributes) | Dictionary<string,object> |  | Passed to the inner button, such as aria-label and data-*. |

### Group

| Name | Type | Default | Description |
|---|---|---|---|
| Items | IReadOnlyList<SelectionItem> | empty | The buttons, in order, each with its text, icon and disabled state. Values must be unique, or the control throws. |
| Values | IReadOnlyList<string> | empty | The selected values; two-way with @bind-Values. |
| Selection | ChoiceSelectionMode | Single | Single works like radio buttons: a click selects that button alone. Multiple works like checkboxes: a click adds or removes it. |
| AllowEmpty | bool | false | Lets a click clear the last selected button. When false, one stays selected once picked. |
| Orientation | FlexOrientation | Horizontal | Horizontal is one row; Vertical stacks the buttons. |
| AriaLabel | string | "Button group" | Accessible name of the group; give each group its own. |
| Enabled (base) | bool | true | False disables every button. |
| Visible (base) | bool | true | False renders nothing. |
| CssClass / Style (base) | string? | null | On the group element. |
| Id / Title / Tag / Data / Parent (base) · unmatched attributes |  | null | Accepted but not rendered. |

### Segmented

| Name | Type | Default | Description |
|---|---|---|---|
| Items | IReadOnlyList<SelectionItem> | empty | The segments: Value, Text and Disabled are used; IconCss is not drawn. |
| Value | string | "" | Value of the chosen segment; two-way with @bind-Value. |
| Orientation | FlexOrientation | Horizontal | Vertical stacks the segments. |
| AriaLabel | string | "Segments" | Accessible name of the radio group. |
| Enabled / Visible (base) | bool | true | Enabled false disables every segment; Visible false hides the bar. |
| CssClass / Style (base) | string? | null | On the fx-segmented wrapper. |
| Id / Title / Tag / Data / Parent (base) · unmatched attributes |  | null | Accepted but not rendered. |

### Events

| Name | Type | Description |
|---|---|---|
| ToggleButtonControl.SelectedChanged | bool | After each press, with the new state. |
| ToggleButtonControl.OnClick | MouseEventArgs | After the toggle and after SelectedChanged. |
| ButtonGroupControl.ValuesChanged | IReadOnlyList<string> | After each click on an enabled button, with the whole selection, even when the click changed nothing. |
| SegmentedControl.ValueChanged | string | When a segment is chosen, by click or arrow key. |

### Item

| Name | Type | Default | Description |
|---|---|---|---|
| Value | string |  | Key reported in Values or Value; unique within a group. |
| Text | string |  | The label. Left empty, a group button shows only its icon and has no accessible name, since there is no title field. |
| Disabled | bool | false | Drawn disabled and skipped by the arrow keys. |
| IconCss | string? | null | Classes for an icon before the text; drawn by ButtonGroupControl only. |
| ctor |  |  | new(value, text, disabled, iconCss); a positional record, so name the later arguments, as in IconCss: "bi bi-eye". |

### Methods

| Name | Type | Description |
|---|---|---|
| ButtonGroupControl.SelectAsync(string value) | Task | Same as a click on that button: applies the selection mode and raises ValuesChanged. Ignored when the group or the item is disabled, or the value is unknown. |

### Css

| Name | Description |
|---|---|
| fx-toggle-host · fx-toggle-button · .selected | The wrapper span and the toggle's button; selected adds the accent fill, white text and an inset shadow. |
| fx-button-group · .horizontal · .vertical | Inline row of buttons with no gap; vertical stacks them. |
| fx-selected | A selected button inside a group: accent fill, white text. |
| fx-button-group button | Square corners, 28px minimum height, 2px focus ring on keyboard focus. |
| fx-segmented | The segmented bar: light blue-gray fill and border, rounded, 3px padding. The checked segment takes the accent fill, a keyboard-focused one a 2px outline; the radio circles are hidden. |

### Vars

| Name | Type | Description |
|---|---|---|
| --fx-accent | #1c6ebf (toggle) · #2465b9 (group) · #2768bd (segmented) | Pressed or chosen fill. Each stylesheet falls back to its own blue. |
| --fx-btn-* | — | Resting look of toggle and group buttons, from ButtonControl; see Buttons. |

## Examples

### Toggle

```razor
@using Fx.ControlKit

<div class="estimate">
    <div class="est-head"><span class="est-icon bi bi-kanban" aria-hidden="true"></span><span class="lbl">Website relaunch for Green Fork Cafe: extras</span></div>
    <div class="demo-row">
        <ToggleButtonControl Selected="true" Enabled="false" IconCss="bi bi-window">Core pages included</ToggleButtonControl>
        <ToggleButtonControl @bind-Selected="_ordering" IconCss="bi bi-bag">Online ordering</ToggleButtonControl>
        <ToggleButtonControl @bind-Selected="_blog" IconCss="bi bi-journal-text">Blog</ToggleButtonControl>
        <ToggleButtonControl @bind-Selected="_photos" IconCss="bi bi-camera">Photo shoot</ToggleButtonControl>
        <ToggleButtonControl @bind-Selected="_flagged" IconCss="@(_flagged ? "bi bi-flag-fill" : "bi bi-flag")"
                             Title="Flag for review" aria-label="Flag for review" />
    </div>
    <p class="price" role="status">@Total.ToString("C0") <span class="result">estimate@(_flagged ? ", flagged for review" : "")</span><span class="extras">Includes @Extras</span></p>
</div>
```

```csharp
// Toggle
private bool _ordering = true, _blog, _photos, _flagged;
private decimal Total => 21000 + (_ordering ? 3000 : 0) + (_blog ? 1800 : 0) + (_photos ? 1200 : 0);
private string Extras => string.Join(", ", new[] { "core pages", _ordering ? "online ordering" : "", _blog ? "blog" : "", _photos ? "photo shoot" : "" }.Where(x => x != ""));
```

### Group

```razor
@using Fx.ControlKit

<SwitchControl @bind-Checked="_stackLabels" Label="Stack the labels" />

<div class="demo-row top panel">
    <div class="stack">
        <span class="lbl">Status</span>
        <ButtonGroupControl Items="_statusItems" @bind-Values="_status" Selection="ChoiceSelectionMode.Single" AriaLabel="Status" />
    </div>
    <div class="stack">
        <span class="lbl">Labels</span>
        <ButtonGroupControl Items="_labelItems" @bind-Values="_labels" Selection="ChoiceSelectionMode.Multiple" AllowEmpty="true"
                            Orientation="@(_stackLabels ? FlexOrientation.Vertical : FlexOrientation.Horizontal)" AriaLabel="Labels" />
    </div>
    <div class="stack res">
        <span class="lbl" role="status">@Matches.Length of @_tasks.Length open tasks match</span>
        <ul class="tasks">
            @foreach (var t in Matches)
            {
                <li><span class="bi bi-card-checklist" aria-hidden="true"></span>@t.Title<span class="task-label">@t.Label</span></li>
            }
        </ul>
    </div>
</div>
```

```csharp
// Group
private bool _stackLabels;
private readonly SelectionItem[] _statusItems =
{
    new("todo", "To do", IconCss: "bi bi-circle"),
    new("progress", "In progress", IconCss: "bi bi-hourglass-split"),
    new("review", "In review", IconCss: "bi bi-eye"),
};
private IReadOnlyList<string> _status = new[] { "progress" };
private readonly SelectionItem[] _labelItems =
{
    new("Design", "Design"), new("Frontend", "Frontend"), new("Backend", "Backend"), new("Content", "Content"), new("QA", "QA", Disabled: true),
};
private IReadOnlyList<string> _labels = new[] { "Frontend", "Backend" };
private sealed record TaskItem(string Title, string Label, string Status);
private readonly TaskItem[] _tasks =
{
    new("WEB-112 Menu page layout", "Design", "progress"), new("WEB-115 Navigation bar", "Frontend", "progress"),
    new("WEB-117 Order form", "Frontend", "review"), new("WEB-118 Order emails", "Backend", "progress"),
    new("WEB-120 Payment setup", "Backend", "todo"), new("WEB-121 Menu copy", "Content", "review"),
    new("WEB-123 Photo gallery", "Frontend", "progress"), new("WEB-124 Allergen icons", "Design", "todo"),
};
private TaskItem[] Matches => _tasks.Where(t => t.Status == _status[0] && (_labels.Count == 0 || _labels.Contains(t.Label))).ToArray();
```

### Segmented

```razor
@using Fx.ControlKit

<div class="estimate">
    <div class="demo-row">
        <span class="est-icon bi bi-cash-coin" aria-hidden="true"></span>
        <span class="lbl">Build milestone, 4 sprints, 8 weeks</span>
        <SegmentedControl Items="_budgetItems" @bind-Value="_budgetView" AriaLabel="Budget view" />
    </div>
    <p class="price" role="status">@Budget.ToString("C0") <span class="result">@_budgetItems.First(i => i.Value == _budgetView).Text.ToLowerInvariant()</span></p>
</div>
```

```csharp
// Segmented
private readonly SelectionItem[] _budgetItems = { new("sprint", "Per sprint"), new("total", "In total"), new("week", "Per week") };
private string _budgetView = "sprint";
private decimal Budget => _budgetView switch { "total" => 12000, "week" => 12000 / 8m, _ => 3000 };
```

