Inputs
Toggle
BitToggle is a physical switch turned into a control: flip it to choose between two mutually exclusive options - “On/Off”, “Show/Hide” - and the choice takes effect right away, with no Save button in between. That immediacy is what separates it from a checkbox, which states an intention that a later submit carries out; a switch inside a long form that ends in a Submit button is the one place not to use it. The knob slides between the two ends of the track, so the state is carried by position as much as by color, and the slide collapses to a jump wherever reduced motion is asked for. It is built on a real focusable button with the switch role, so Tab reaches it, Space and Enter flip it, and screen readers announce it as a switch together with its state. Beyond the two plain states it can name each of them with a state text, put an icon or arbitrary content inside its track, explain itself in a description line, show a spinner for as long as the change is still in flight, refuse a change before it lands, be flipped from code, and place its label on any side of the knob.
Usage
Every example is live. Open its code to see exactly what produced the component running underneath.
Basic
Texts
Track content
Icons
Label
Label position
Reversed
FullWidth
Loading
Read-only & Required
Binding
Events
Validation
Accessibility
Description
Color
External Icons
Size
Style & Class
RTL
API
Every parameter, public member, sub-class and enum this component exposes.
BitToggle parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| AriaControls | string? | null | The id of the element the toggle controls, rendered as aria-controls on the switch. |
| AriaDescription | string? | null | Detailed description of the toggle for the benefit of screen readers, rendered as a visually hidden element that the switch points to via aria-describedby. |
| AriaDescribedby | string? | null | The id of an existing element that describes the toggle, added to the aria-describedby of the switch alongside its state text and its AriaDescription rather than replacing them. |
| AriaLabelledby | string? | null | The id of an existing element that labels the toggle, rendered as aria-labelledby on the switch. Takes precedence over the label of the toggle and over AriaLabel. |
| AutoFocus | bool | false | If true, the toggle automatically receives focus when the page renders (rendered as the autofocus attribute). |
| AutoLoading | bool | false | Turns the toggle busy on its own for as long as the awaited OnClick, OnChanging and OnChange callbacks behind a change are still running, without a loading flag having to be tracked outside the component. |
| Classes | BitToggleClassStyles? | null | Custom CSS classes for different parts of the toggle. |
| Color | BitColor? | null | The general color of the toggle, applied to the track of the checked state. |
| Description | string? | null | A visible explanation of what the toggle switches, rendered on a line of its own under it and announced after the name of the switch through aria-describedby. |
| DescriptionTemplate | RenderFragment? | null | Custom description of the toggle, replacing Description with arbitrary markup. |
| 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. |
| LabelPosition | BitLabelPosition? | null | The position of the label in regards to the knob of the toggle. Takes precedence over Inline and Reversed when set. |
| LabelTemplate | RenderFragment? | null | Custom label of the toggle. |
| Loading | bool | false | Renders a spinner in place of the knob's icon and suspends the toggle until the pending work behind the change is done. |
| OffContent | RenderFragment? | null | Content rendered inside the track of the toggle while it is OFF, next to the knob. |
| OffIcon | BitIconInfo? | null | The icon rendered inside the knob while the toggle is OFF, using custom CSS classes for external icon libraries. Takes precedence over OffIconName when both are set. |
| OffIconName | string? | null | The name of the built-in icon rendered inside the knob while the toggle is OFF. |
| OffText | string? | null | Text to display when toggle is OFF. |
| OnBlur | EventCallback<FocusEventArgs> | Callback for when the toggle loses focus. | |
| OnChanging | EventCallback<BitToggleChangeArgs> | Callback invoked before the state of the toggle changes, letting the change be cancelled by setting Cancel on the provided arguments. | |
| OnClick | EventCallback<MouseEventArgs> | Callback for when the toggle is clicked, invoked before the state changes. | |
| OnContent | RenderFragment? | null | Content rendered inside the track of the toggle while it is ON, next to the knob. |
| OnFocus | EventCallback<FocusEventArgs> | Callback for when the toggle receives focus. | |
| OnFocusIn | EventCallback<FocusEventArgs> | Callback for when focus moves into the toggle. | |
| OnFocusOut | EventCallback<FocusEventArgs> | Callback for when focus moves out of the toggle. | |
| OnIcon | BitIconInfo? | null | The icon rendered inside the knob while the toggle is ON, using custom CSS classes for external icon libraries. Takes precedence over OnIconName when both are set. |
| OnIconName | string? | null | The name of the built-in icon rendered inside the knob while the toggle is ON. |
| OnText | string? | null | Text to display when toggle is ON. |
| Reversed | bool | false | Reverses the positions of the label and input of the toggle. |
| Role | string? | switch | Denotes role of the toggle, default is switch. |
| Size | BitSize? | null | The size of the toggle. |
| StopPropagation | bool | false | If true, stops the click event from bubbling up to the parent elements. |
| Styles | BitToggleClassStyles? | null | Custom CSS styles for different parts of the toggle. |
| Text | string? | null | The default text used when the On or Off texts are null. |
| Title | string? | null | The native tooltip of the knob of the toggle, shown on hover. |
BitToggle public members
| Name | Type | Default value | Description |
|---|---|---|---|
| InputElement | ElementReference | The ElementReference of the switch button of the BitToggle - the element that holds the tab stop, not the hidden checkbox that carries the value into a form. | |
| FocusAsync | (bool preventScroll = false) => ValueTask | Gives focus to the switch button of the BitToggle. Pass true to keep the browser from scrolling the toggle into view along the way. | |
| ToggleAsync | () => Task | Flips the state of the BitToggle from code, as a click would: OnChanging still gets to cancel the change and OnChange still reports it. Unlike a click it is not blocked by the read-only or loading states, which only close the toggle to the user; a disabled toggle changes through neither. | |
| ToggleAsync | (bool value) => Task | Moves the BitToggle to a specific state from code, doing nothing when it is already in it. Otherwise identical to the parameterless overload. |
BitInputBase parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| DefaultValue | TValue? | null | The default value of the input to be used in uncontrolled mode (i.e. when the Value is not bound), typically used alongside the OnChange callback. |
| 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 | Gets or sets the accessible label for the component, used by assistive technologies. |
| Class | string? | null | Gets or sets the CSS class name(s) to apply to the rendered element. |
| Dir | BitDir? | null | Gets or sets the text directionality for the component's content. |
| ForceAnimation | bool | false | Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested. |
| HtmlAttributes | Dictionary<string, object> | new Dictionary<string, object>() | Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. |
| Id | string? | null | Gets or sets the unique identifier for the component's root element. |
| IsEnabled | bool | true | Gets or sets a value indicating whether the component is enabled and can respond to user interaction. |
| Style | string? | null | Gets or sets the CSS style string to apply to the rendered element. |
| TabIndex | string? | null | Gets or sets the tab order index for the component when navigating with the keyboard. |
| Visibility | BitVisibility | BitVisibility.Visible | Gets or sets the visibility state (visible, hidden, or collapsed) of the component. |
BitComponentBase public members
| Name | Type | Default value | Description |
|---|---|---|---|
| UniqueId | Guid | Guid.NewGuid() | Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed. |
| RootElement | ElementReference | Gets the reference to the root HTML element associated with this component. |
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. |
| Description | string? | null | Custom CSS classes/styles for the description 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. |
| Content | string? | null | Custom CSS classes/styles for the content rendered inside the track of the BitToggle. |
| OnContent | string? | null | Custom CSS classes/styles for the ON side of the content rendered inside the track of the BitToggle. |
| OffContent | string? | null | Custom CSS classes/styles for the OFF side of the content rendered inside the track of the BitToggle. |
| Thumb | string? | null | Custom CSS classes/styles for the thumb of the BitToggle. |
| Icon | string? | null | Custom CSS classes/styles for the icon rendered inside the thumb of the BitToggle. |
| Spinner | string? | null | Custom CSS classes/styles for the loading spinner of the BitToggle. |
| Text | string? | null | Custom CSS classes/styles for the text of the BitToggle. |
BitToggleChangeArgs properties
| Name | Type | Default value | Description |
|---|---|---|---|
| Value | bool | false | The state the toggle is about to move to (read-only). |
| Cancel | bool | false | Set to true to cancel the change and keep the current state of the toggle. |
BitColor enum
| Name | Value | Description |
|---|---|---|
| Primary | 0 | Primary general color. |
| Secondary | 1 | Secondary general color. |
| Tertiary | 2 | Tertiary general color. |
| Info | 3 | Info general color. |
| Success | 4 | Success general color. |
| Warning | 5 | Warning general color. |
| SevereWarning | 6 | SevereWarning general color. |
| Error | 7 | Error general color. |
| PrimaryBackground | 8 | Primary background color. |
| SecondaryBackground | 9 | Secondary background color. |
| TertiaryBackground | 10 | Tertiary background color. |
| PrimaryForeground | 11 | Primary foreground color. |
| SecondaryForeground | 12 | Secondary foreground color. |
| TertiaryForeground | 13 | Tertiary foreground color. |
| PrimaryBorder | 14 | Primary border color. |
| SecondaryBorder | 15 | Secondary border color. |
| TertiaryBorder | 16 | Tertiary border color. |
BitLabelPosition enum
| Name | Value | Description |
|---|---|---|
| Top | 0 | The label shows on the top of the toggle. |
| End | 1 | The label shows on the end of the toggle. |
| Bottom | 2 | The label shows on the bottom of the toggle. |
| Start | 3 | The label shows on the start of the toggle. |
BitSize enum
| Name | Value | Description |
|---|---|---|
| Small | 0 | The small size toggle. |
| Medium | 1 | The medium size toggle. |
| Large | 2 | The large size toggle. |
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
Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.