# Blazor Button Component

URL: https://flexcoreui.com/demo/buttons  
Section: Components  
Controls: ButtonControl, SaveButtonControl, CancelButtonControl, RoundedButtonControl  
Keywords: button, dropdown button, icon button, submit button, loading button, button variants

A Blazor button for actions, form submits and dropdowns: color variants, icons and sizes, plus Save and Cancel buttons with a busy state.

## Documentation

### Parameters

| Name | Type | Default | Description |
|---|---|---|---|
| ChildContent | RenderFragment? |  | The label: text or any markup, after the icon. |
| CssClass | string? | null | Added after fx-button: a color class such as btn-primary, and btn-sm or btn-lg. |
| IconCss | string? | null | Classes for an icon before the label, such as bi bi-save; any icon font works. |
| Disabled | bool | false | Native disabled: no clicks, no hover or pressed look, drawn at 60% opacity. |
| ButtonType | string | "button" | Native type: button, submit or reset. The default never submits a form. |
| Title | string? | null | Native tooltip. An icon-only button also needs aria-label. |
| Style | string? | null | Inline style; handy for setting --fx-btn-* variables on one button. |
| TabIndex | int? | null | Native tabindex. |
| AutoFocus | bool | false | Focuses the button once, on the first render where it is enabled. |
| BareStyle | bool | false | Leaves out fx-button, so only your CssClass styles the button. |
| StopPropagation | bool | false | Stops the click reaching ancestors that also handle clicks. |
| PreventMouseDownDefault | bool | false | Keeps focus where it was when the button is pressed, so an editor keeps its selection. |
| DropDownContent | RenderFragment? | null | Makes the button a dropdown trigger: a click shows this content in a panel beneath it, and a click outside closes it. |
| DropDownCssClass | string? | null | Extra class on the dropdown panel. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the button, such as id, name, form, accesskey, aria-* and data-*. The button's own class, type, disabled, title, tabindex and style win. |

### Events

| Name | Type | Description |
|---|---|---|
| OnClick | MouseEventArgs | Raised on a click, or on Enter or Space. Not raised while Disabled. In dropdown mode it runs before the panel opens or closes. |

### Methods

| Name | Type | Description |
|---|---|---|
| FocusAsync() | ValueTask | Moves focus to the button without scrolling; get the component with @ref. |
| CloseDropDown() | void | Closes the dropdown panel, for example after an item is picked. |

### Presets

| Name | Type | Default | Description |
|---|---|---|---|
| SaveButtonControl.IsSaving | bool | false | True while a save runs: disables the button and shows "Saving…" as its text unless Label or ChildContent is set, and as its tooltip unless Title is set. |
| SaveButtonControl.Label | string? | null | Text after the disk icon. Null shows "Save" ("Saving…" while saving); an empty string leaves the icon alone. ChildContent wins. |
| CancelButtonControl.Label | string? | null | Text after the cross icon. Null shows "Cancel"; an empty string leaves the icon alone. ChildContent wins. |
| SaveButtonControl.ShowIcon · CancelButtonControl.ShowIcon | bool | true | Draws the built-in disk or cross icon. |
| SaveButtonControl.Title · CancelButtonControl.Title | string? | null | Tooltip; when null, "Save" ("Saving…") or "Cancel". |
| RoundedButtonControl.Variant | string | "muted" | Pill colors: accent, muted, success or danger, in any case; any other value gives muted. |

### Css

| Name | Description |
|---|---|
| fx-button | Base class of every ButtonControl: 4px by 16px padding, 75px minimum width, colors from --fx-btn-*. BareStyle leaves it out. |
| btn-primary · btn-secondary · btn-success · btn-warning · btn-danger | Filled colors, each from its own --fx-btn-color-* variables. |
| btn-outline-primary | Blue border and text on a clear fill; fills blue on hover. |
| btn-sm · btn-lg | Smaller (11px) or larger (16px) text, with matching padding. |
| fx-button-classic | Win32 3D bevel, MS Sans Serif, square corners. For migrated VB6 forms. |
| mini-btn · fx-dialog-btn | Compact grid-cell button; dialog footer padding. |
| fx-button-dd-host · fx-button-dd-panel · fx-button-dd-backdrop | Dropdown mode: the wrapper the panel is placed against, the panel (2px below the button, bordered, with a shadow) and the clear full-screen layer that closes it. |
| fx-save-button · fx-save-button--saving · fx-cancel-button | The Save and Cancel presets: pill-shaped with a clear fill; Save pulses while IsSaving. |
| fx-rounded-button · fx-rounded-button--accent · --muted · --success · --danger | The pill: fully rounded, small bold text, tinted fill per Variant. |

### Vars

| Name | Type | Description |
|---|---|---|
| --fx-btn-bg / --fx-btn-color | #f7f7f7 / #333 | Resting fill and text. |
| --fx-btn-border / --fx-btn-border-color | 1px solid #c9c9c9 / text color | Border shorthand, then a color for each side (for a bevel). Left unset, the color is the text color, not the shorthand's gray. |
| --fx-btn-radius | 4px | Corner radius. |
| --fx-btn-hover-bg / --fx-btn-hover-border / --fx-btn-hover-border-color | #e6e6e6 / --fx-btn-border / text color | Hover fill, border and border color; the color falls back to the text color. |
| --fx-btn-active-bg | #b6b6b6 | Pressed fill. |
| --fx-btn-shadow | none | Box shadow. |
| --fx-btn-primary-bg · -color · -border-color | #0d6efd · #fff · #0d6efd | Fill, text and border of btn-primary. Secondary, success, warning and danger have the same three. |
| --fx-btn-primary-hover-bg · -hover-border-color · -active-bg · -active-border-color | #0b5ed7 · #0a58ca · #0a58ca · #0a53be | Hover and pressed fill and border of btn-primary; the same four exist for the other colors. |
| --fx-btn-outline-primary-color · -border-color · -hover-bg · -hover-color · -hover-border-color · -active-bg · -active-border-color | #0d6efd · #0d6efd · #0d6efd · #fff · #0d6efd · #0a58ca · #0a53be | btn-outline-primary: text and border at rest; fill, text and border on hover; fill and border when pressed. |
| --fx-surface-color / --fx-border-color / --fx-dialog-shadow | #ffffff / #8a8a8a / 2px 2px 6px rgba(0, 0, 0, 0.3) | Dropdown panel fill, border and shadow, shared with other controls. |
| --fx-font-size × --fx-zoom | 13px × 1 | Font size of the button; --fx-zoom multiplies it, as in the other controls that read the variable. |

## Examples

### Actions

```razor
@using Fx.ControlKit

<SwitchControl @bind-Checked="_small" Label="Small buttons" />

<div class="lr">
    <div class="lr-head">
        <span class="lr-icon bi bi-calendar-week" aria-hidden="true"></span>
        <div class="lr-text">
            <div class="lr-title">LR-0107 · Vacation, Dec 14 to 18</div>
            <div class="lr-sub">Hermione Ranger, Reservations, 5 working days</div>
        </div>
        <BadgeControl Text="@_status" Variant="@StatusVariant" />
    </div>
    <div class="demo-row">
        <ButtonControl CssClass="@Size("btn-success")" IconCss="bi bi-check2-circle" Disabled="@(_status is "Approved" or "Declined")"
                       OnClick='() => Set("Approved", "Leave approved")'>Approve</ButtonControl>
        <ButtonControl CssClass="@Size("btn-primary")" IconCss="bi bi-envelope" Disabled="@(_status == "Declined")"
                       OnClick='() => Log("Coverage question emailed to the manager")'>Ask manager</ButtonControl>
        <ButtonControl CssClass="@Size("btn-warning")" IconCss="bi bi-pause-circle" Disabled="@(_status != "Pending")"
                       OnClick='() => Set("On hold", "On hold until coverage is confirmed")'>Hold</ButtonControl>
        <ButtonControl CssClass="@Size("btn-outline-primary")" OnClick='() => Log("Team calendar opened")'>Team calendar <span class="bi bi-arrow-right"></span></ButtonControl>
        <ButtonControl CssClass="@Size("")" IconCss="bi bi-printer" Title="Print" aria-label="Print" OnClick='() => Log("Sent to the printer")' />
        <ButtonControl CssClass="@Size("btn-danger")" IconCss="bi bi-x-circle" Disabled="@(_status == "Declined")"
                       OnClick='() => Set("Declined", "Leave declined")'>Decline</ButtonControl>
        <ButtonControl CssClass="@Size("btn-secondary")" IconCss="bi bi-arrow-counterclockwise" Disabled="@(_status == "Pending")"
                       OnClick='() => Set("Pending", "Reopened for review")'>Reopen</ButtonControl>
    </div>
    <p class="result" role="status"><span class="bi bi-clock-history" aria-hidden="true"></span>Last action: @_last</p>
</div>
```

```csharp
// Leave request actions
private bool _small;
private string _status = "Pending";
private string _last = "none yet";
private BadgeVariant StatusVariant => _status switch
{
    "Approved" => BadgeVariant.Success,
    "On hold" => BadgeVariant.Warning,
    "Declined" => BadgeVariant.Danger,
    _ => BadgeVariant.Secondary,
};
private string Size(string variant) => _small ? $"{variant} btn-sm" : variant;
private void Log(string action) => _last = action;
private void Set(string status, string action)
{
    _status = status;
    _last = action;
}
```

### Form

```razor
@using Fx.ControlKit
@using Fx.ControlKit.Editor

<form class="cv" @onsubmit="AddCoverAsync">
    <div class="cv-label"><span class="bi bi-person-plus" aria-hidden="true"></span>Add a colleague to cover for Hermione Ranger</div>
    <div class="demo-row">
        <TextBoxControl @bind-Value="_name" UpdateOnInput="true" Placeholder="Colleague's full name" Width="240px" aria-label="Colleague name" />
        <SaveButtonControl ButtonType="submit" IsSaving="_saving" Disabled="@string.IsNullOrWhiteSpace(_name)" />
        <CancelButtonControl Disabled="@(_saving || string.IsNullOrEmpty(_name))" OnClick="() => _name = null" />
    </div>
    <div class="demo-row mt" id="pill">
        @foreach (var colleague in _covering)
        {
            <RoundedButtonControl Variant="accent" IconCss="bi bi-x" Title="@($"Remove {colleague}")" OnClick="() => _covering.Remove(colleague)"><span class="sr">Remove </span>@colleague</RoundedButtonControl>
        }
        <RoundedButtonControl Variant="@(_covering.Count > 0 ? "success" : "danger")" Disabled="true">@_covering.Count covering</RoundedButtonControl>
    </div>
</form>
```

```csharp
// Form actions
private string? _name;
private bool _saving;
private readonly List<string> _covering = new() { "Ron Measly", "Ginny Measly" };
private async Task AddCoverAsync()
{
    if (_saving || string.IsNullOrWhiteSpace(_name)) return;
    var name = _name.Trim();   // read before the await: the box stays editable during the save
    _saving = true;
    await Task.Delay(900);   // stands in for the call that saves
    _covering.Add(name);
    _name = null;
    _saving = false;
}
```

### Dropdown

```razor
@using Fx.ControlKit

<div class="demo-row dd">
    <ButtonControl @ref="_export" CssClass="btn-primary" IconCss="bi bi-download" DropDownCssClass="menu">
        <ChildContent>Export leave report <span class="bi bi-caret-down-fill dd-caret"></span></ChildContent>
        <DropDownContent>
            @foreach (var (fmt, icon) in new[] { ("Excel (.xlsx)", "bi-file-earmark-excel"), ("CSV", "bi-filetype-csv"), ("PDF", "bi-file-earmark-pdf") })
            {
                <ButtonControl BareStyle="true" CssClass="menu-item" IconCss="@($"bi {icon}")" OnClick="@(() => Pick(fmt))">@fmt</ButtonControl>
            }
        </DropDownContent>
    </ButtonControl>
    <span class="result" role="status">@(_picked is null ? "Nothing exported yet" : $"Leave report exported as {_picked}")</span>
</div>
```

```csharp
// Dropdown
private ButtonControl? _export;
private string? _picked;
private void Pick(string format)
{
    _picked = format;
    _export?.CloseDropDown();
}
```

