Toggle
Switch
Switch
A toggle represents a physical switch that allows someone to choose between two mutually exclusive options. For example, “On/Off”, “Show/Hide”. Choosing an option should produce an immediate result.
Usage
Basic
<BitToggle Label="Basic" />
<BitToggle Label="Disabled" IsEnabled="false" />Texts
Customize BitToggle On and Off texts, providing more context for its state.
<BitToggle Label="Text" Text="This is a toggle!" />
<BitToggle Label="OnText & OffText" OnText="Toggle is On" OffText="Toggle is Off" />Label
Use inline labels and custom label templates in BitToggle.
Inline:
LabelTemplate:
<BitToggle Label="This is an inline label" Inline />
<BitToggle>
<LabelTemplate>
<div style="display:flex;align-items:center;gap:10px">
<BitLabel Style="color:green">This is custom Label</BitLabel>
<BitIcon IconName="@BitIconName.Filter" />
</div>
</LabelTemplate>
</BitToggle>Reversed
Reverse the label and input position of BitToggle.
Default:
Inline:
<BitToggle Label="This is a reversed label" Reversed />
<BitToggle Label="This is a reversed inline label" Reversed Inline />FullWidth
Full width BitToggle in Inline mode.
Default:
Reversed:
<BitToggle Label="This is a full-width toggle" FullWidth Inline />
<BitToggle Label="This is a reversed full-width toggle" Reversed FullWidth Inline />Binding
Bind value one-way and two-way in BitToggle
<BitToggle Label="One-way" Value="oneWayValue" /> <BitToggleButton @bind-IsChecked="oneWayValue" OnText="On" OffText="Off" /> <BitToggle Label="Two-way" @bind-Value="twoWayValue" /> <BitToggleButton @bind-IsChecked="twoWayValue" OnText="On" OffText="Off" />@code { private bool oneWayValue; private bool twoWayValue; }
Style & Class
Customize the appearance of BitToggle using styles and CSS classes.
<style>
.custom-thumb {
background: #fff;
width: 30px;
height: 30px;
}
.custom-button {
padding: 0;
width: 52px;
height: 22px;
border: none;
background: #ccc;
border-radius: 11px;
}
.custom-check .custom-thumb {
background: #ff6868;
}
.custom-check .custom-button {
background: #ffcece;
}
.custom-check .custom-button:hover .custom-thumb {
background: #ff6868;
}
</style>
<BitToggle Label="Styles"
Styles="@(new() { Root = "--toggle-background: lightgray;", Checked = "--toggle-background: #2ecc71;",
Thumb = "background: whitesmoke; height: 28px; width: 28px;",
Button = "background: var(--toggle-background); border: none; border-radius: 60px; padding: 0; height: 30px; width: 50px;" } )" />
<BitToggle Label="Classes"
Classes="@(new() { Thumb = "custom-thumb",
Button = "custom-button",
Checked = "custom-check" } )" />Validation
Use BitToggle within a form and validate its state.
<style> .validation-message { color: red; } </style> <EditForm Model="validationModel" OnValidSubmit="HandleValidSubmit" OnInvalidSubmit="HandleInvalidSubmit"> <DataAnnotationsValidator /> <BitToggle Label="Terms and conditions" Text="I agree." @bind-Value="validationModel.TermsAgreement" /> <ValidationMessage For="@(() => validationModel.TermsAgreement)" /> <BitButton ButtonType="BitButtonType.Submit">Submit</BitButton> </EditForm>@code { public class BitToggleValidationModel { [Range(typeof(bool), "true", "true", ErrorMessage = "You must agree to the terms and conditions.")] public bool TermsAgreement { get; set; } = true; } public BitToggleValidationModel validationModel { get; set; } = new(); private async Task HandleValidSubmit() { } private void HandleInvalidSubmit() { } }
RTL
Use BitToggle in right-to-left (RTL).
<BitToggle Label="این یک تاگل است" Dir="BitDir.Rtl" OnText="روشن" OffText="خاموش" />
<BitToggle Label="این یک تاگل خطی است" Dir="BitDir.Rtl" Inline />
<BitToggle Label="این یک تاگل خطی برعکس است" Dir="BitDir.Rtl" Reversed Inline />API
BitToggle parameters
Name |
Type |
Default value |
Description |
|---|---|---|---|
| Classes | BitToggleClassStyles? | null | Custom CSS classes for different parts of the toggle. |
| DefaultValue | bool? | null | The default value of the toggle when the value parameter has not been assigned. |
| FullWidth | bool | false | Renders the toggle in full width of its container while putting space between the label and the knob. |
| Inline | bool | false | Renders the label and the knob in a single line together. |
| Label | string? | null | Label of the toggle. |
| LabelTemplate | RenderFragment? | null | Custom label of the toggle. |
| OffText | string? | null | Text to display when toggle is OFF. |
| OnText | string? | null | Text to display when toggle is ON. |
| Role | string | switch | Denotes role of the toggle, default is switch. |
| Styles | BitToggleClassStyles? | null | Custom CSS styles for different parts of the toggle. |
| Text | string? | null | Default text used when the On or Off texts are null. |
BitInputBase parameters
Name |
Type |
Default value |
Description |
|---|---|---|---|
| DisplayName | string? | null | Gets or sets the display name for this field. |
| InputHtmlAttributes | IReadOnlyDictionary<string, object>? | null | Gets or sets a collection of additional attributes that will be applied to the created element. |
| Name | string? | null | Gets or sets the name of the element. Allows access by name from the associated form. |
| NoValidate | bool | false | Disables the validation of the input. |
| OnChange | EventCallback<TValue?> | Callback for when the input value changes. | |
| ReadOnly | bool | false | Makes the input read-only. |
| Required | bool | false | Makes the input required. |
| Value | TValue? | null | Gets or sets the value of the input. This should be used with two-way binding. |
BitInputBase public members
Name |
Type |
Default value |
Description |
|---|---|---|---|
| InputElement | ElementReference | The ElementReference of the input element. | |
| FocusAsync() | () => ValueTask | Gives focus to the input element. | |
| FocusAsync(bool preventScroll) | (bool preventScroll) => ValueTask | Gives focus to the input element. |
BitComponentBase parameters
Name |
Type |
Default value |
Description |
|---|---|---|---|
| AriaLabel | string? | null | The aria-label of the control for the benefit of screen readers. |
| Class | string? | null | Custom CSS class for the root element of the component. |
| Dir | BitDir? | null | Determines the component direction. |
| HtmlAttributes | Dictionary<string, object> | new Dictionary<string, object>() | Capture and render additional attributes in addition to the component's parameters. |
| Id | string? | null | Custom id attribute for the root element. if null the UniqueId will be used instead. |
| IsEnabled | bool | true | Whether or not the component is enabled. |
| Style | string? | null | Custom CSS style for the root element of the component. |
| Visibility | BitVisibility | BitVisibility.Visible | Whether the component is visible, hidden or collapsed. |
BitComponentBase public members
Name |
Type |
Default value |
Description |
|---|---|---|---|
| UniqueId | Guid | Guid.NewGuid() | The readonly unique id of the root element. it will be assigned to a new Guid at component instance construction. |
| RootElement | ElementReference | The ElementReference of the root element. |
BitToggleClassStyles properties
Name |
Type |
Default value |
Description |
|---|---|---|---|
| Root | string? | null | Custom CSS classes/styles for the root element of the BitToggle. |
| Label | string? | null | Custom CSS classes/styles for the label of the BitToggle. |
| Container | string? | null | Custom CSS classes/styles for the container of the BitToggle. |
| Button | string? | null | Custom CSS classes/styles for the button of the BitToggle. |
| Checked | string? | null | Custom CSS classes/styles for the checked state of the BitToggle. |
| Thumb | string? | null | Custom CSS classes/styles for the thumb of the BitToggle. |
| Text | string? | null | Custom CSS classes/styles for the text of the BitToggle. |
BitVisibility enum
Name |
Value |
Description |
|---|---|---|
| Visible | 0 | The content of the component is visible. |
| Hidden | 1 | The content of the component is hidden, but the space it takes on the page remains (visibility:hidden). |
| Collapsed | 2 | The component is hidden (display:none). |
BitDir enum
Name |
Value |
Description |
|---|---|---|
| Ltr | 0 | Ltr (left to right) is to be used for languages that are written from the left to the right (like English). |
| Rtl | 1 | Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic). |
| Auto | 2 | Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element. |
Feedback
You can give us your feedback through our GitHub repo by filing a new Issue or starting a new Discussion.
Or you can review / edit this page on GitHub.
Or you can review / edit this component on GitHub.
- On this page