Skip to content
Form Switch

Switch

An on/off toggle built on a native checkbox, correct even without interactivity.

On this page

Examples

Standalone

One digest per week.

Notifications: off

Razor
<div class="flex flex-col gap-4">
    <KlySwitch @bind-Value="_notify" Label="Email notifications" Description="One digest per week." data-testid="notify" />
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="notify-state">Notifications: @(_notify ? "on" : "off")</p>
    <div class="flex flex-wrap items-center gap-4">
        @foreach (var size in Enum.GetValues<KlySize>())
        {
            <KlySwitch Size="size" Value="true" Label="@size.ToString()" Color="KlyColor.Success" />
        }
        <KlySwitch Disabled Label="Disabled" />
    </div>
</div>

@code {
    private bool _notify;
}

In an EditForm

Posts in static rendering too.

Razor
@using System.ComponentModel.DataAnnotations

<EditForm Model="Model" FormName="terms" OnValidSubmit="Save" class="flex flex-col items-start gap-3">
    <DataAnnotationsValidator />
    <KlySwitch @bind-Value="Model!.AcceptTerms" Label="I accept the terms" data-testid="terms" />
    <ValidationMessage For="() => Model!.AcceptTerms" class="text-sm text-(--klyroo-error-text)" />
    <KlyButton Type="ButtonType.Submit" Label="Save" />
    @if (_saved)
    {
        <p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="saved">Saved, terms accepted.</p>
    }
</EditForm>

@code {
    [SupplyParameterFromForm(FormName = "terms")]
    private Settings? Model { get; set; }

    private bool _saved;

    protected override void OnInitialized() => Model ??= new();

    private void Save() => _saved = true;

    private sealed class Settings
    {
        [Range(typeof(bool), "true", "true", ErrorMessage = "You must accept the terms.")]
        public bool AcceptTerms { get; set; }
    }
}

API

KlySwitch

On/off switch, a native checkbox with role="switch" . Its look follows the native checked state, so it is correct in static server rendering and after a form post.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "primary" Semantic color of the control: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else primary. Values: primary, secondary, success, info, warning, error, neutral.
Description string Text under the label of the switch.
Disabled bool false Disables the control: it cannot be focused or changed and is not submitted.
Id string The id of the native control. Defaults to the one of the enclosing KlyFormField , else a generated one.
Label string Text next to the switch. A KlyFormField label also names it.
Name string Form field name outside an EditForm . Inside one, the name comes from the bound expression.
Required bool false Marks the control as required, for the browser and for assistive technology.
Size KlySize? Defaults to the size of the enclosing KlyFormField , or medium. Values: Xs, Sm, Md, Lg, Xl.
Ui SwitchUi Per-instance class overrides of the slots of the switch, see SwitchUi .
Value (@bind-Value) bool false The value of the control. Bind it with @bind-Value .
ValueExpression Expression<Func<bool>> The expression of the bound field. The compiler sets it with @bind-Value , and validation uses it to find the field.

Ui slots

Ui takes a SwitchUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlySwitch , after the theme and the app configuration.
Container Classes merged into the container slot of KlySwitch , after the theme and the app configuration.
Input Classes merged into the input slot of KlySwitch , after the theme and the app configuration.
Thumb Classes merged into the thumb slot of KlySwitch , after the theme and the app configuration.
Wrapper Classes merged into the wrapper slot of KlySwitch , after the theme and the app configuration.
Label Classes merged into the label slot of KlySwitch , after the theme and the app configuration.
Description Classes merged into the description slot of KlySwitch , after the theme and the app configuration.