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 a later submit carries out, so a switch inside a form that ends in a Submit button is the one place not to use it. It is built on a real focusable button carrying the switch role: Tab reaches it, Space and Enter flip it, and screen readers announce it as a switch with its state. The knob carries that state by position as much as by color, and its slide collapses to a jump wherever reduced motion is asked for. Beyond the two plain states it can name each of them with a state text, carry content inside its track or its knob, explain itself in a description line, say why a state was rejected in an error line, show a spinner while a change is still in flight, refuse a change before it lands, be flipped from code, and place its label on any side. A BitParams ancestor sets the defaults of every toggle in a subtree, and a tier of public --bit-Toggle-* CSS variables restyles the default geometry, palette and motion, which an explicit Color or Size still wins over.
Usage
Every example is live. Open its code to see exactly what produced the component running underneath.
Basic
Texts
Track content
Knob content
Label & layout
Loading
Read-only & Required
Binding
Events
Description
Validation
Accessibility
Cascading parameters
Color
External Icons
Size
Style & Class
:root or on any ancestor re-skins every toggle below it, and
one on the Style of an instance re-skins that one and any toggle nested in it. They restyle the
default, not a choice: an explicit Color or Size wins over the variables for what it sets. The
geometry ones belong on the root - the knob and the lane held open for it are measured there - which is what
the Root slot above and the Style parameter below both are.
RTL
CSS variables
The public custom properties this component reads off its root, for what no parameter covers.
Every variable is read with a fallback and never declared by the component, so it inherits like any other custom property:
set one on :root (or in a [bit-theme] block) to restyle every instance, on any ancestor to restyle the ones inside it,
or on the Style of one instance to restyle it - and any instance nested inside it, which inherits the value too; a parameter changes one alone,
as does the real CSS property on the instance's Style / Styles when that property does not inherit (opacity: 1 keeps
one disabled instance undimmed without undimming a disabled one inside it), while an inherited one such as color or
font-family also reaches the nested instances that do not set it themselves. Leave a variable unset and the component
falls back to the theme token beside it.
BitToggle CSS variables
| Name | Default value | Description |
|---|---|---|
| --bit-Toggle-track-background | $clr-bg-pri | Track fill while the toggle is off. |
| --bit-Toggle-track-border-color | $clr-brd-pri | Track stroke while the toggle is off. |
| --bit-Toggle-thumb-color | $clr-fg-sec | The knob while the toggle is off. |
| --bit-Toggle-hover-track-background | As the track fill at rest | Track fill while hovered and off (pointer devices only). |
| --bit-Toggle-hover-track-border-color | $clr-brd-pri-hover | Track stroke while hovered and off (pointer devices only). |
| --bit-Toggle-hover-thumb-color | $clr-fg-sec-hover | The knob while hovered and off (pointer devices only). |
| --bit-Toggle-checked-background | $clr-pri | Track fill while the toggle is on. The Color parameter wins over it. |
| --bit-Toggle-checked-border-color | transparent | Track stroke while the toggle is on. |
| --bit-Toggle-checked-thumb-color | $clr-pri-text | The knob while the toggle is on, hover included. The Color parameter wins over it. |
| --bit-Toggle-checked-hover-background | $clr-pri-hover | Track fill while hovered and on (pointer devices only). The Color parameter wins over it. |
| --bit-Toggle-icon-color | The track fill behind the knob | The glyph inside the knob, in both states - drawn in the color of the track, so it reads as cut out of the knob. |
| --bit-Toggle-spinner-color | As the glyph it replaces | The moving arc of the loading spinner; the ring it travels on is the same color at 30%. |
| --bit-Toggle-content-color | $clr-fg-sec while off, $clr-pri-text while on | The OnContent and OffContent carried inside the track. The Color parameter wins over it while on. |
| --bit-Toggle-label-color | $clr-fg-pri | The label beside the knob. |
| --bit-Toggle-text-color | inherit | The state text beside the knob. |
| --bit-Toggle-description-color | $clr-fg-sec | The description line under the toggle. |
| --bit-Toggle-disabled-color | $clr-bg-dis / $clr-brd-dis / $clr-fg-dis | Track fill, track stroke and knob when disabled. |
| --bit-Toggle-disabled-text-color | $clr-fg-dis | Label, state text, description and track content when disabled, and the focus ring of a disabled toggle that AllowDisabledFocus keeps in the tab order. |
| --bit-Toggle-error-color | $clr-err | Track stroke, checked fill, state text, error message and focus ring while the state is invalid. |
| --bit-Toggle-required-color | $clr-req | The asterisk of a required toggle. |
| --bit-Toggle-focus-color | $clr-pri-focus | Focus ring color. The Color parameter wins over it. While it is unset, the focus ring is the library's own --bit-shd-focus-ring, so replacing that token re-shapes it too. |
| --bit-Toggle-track-width | $siz-switch-w-md | Smallest width of the track; a track carrying content still grows to fit it. Stepped up to the roomier icon geometry when the knob holds a glyph, unless this variable names a width of its own. Set it on the root, not on the track. The Size parameter wins over it. |
| --bit-Toggle-track-height | $siz-switch-h-md | Height of the track. Stepped up when the knob holds a glyph, unless set. Set it on the root, not on the track. The Size parameter wins over it. |
| --bit-Toggle-track-radius | Half the track height | Corner radius of the track and of its focus ring; the default draws a pill. |
| --bit-Toggle-border-width | $shp-border-width | Thickness of the track stroke. |
| --bit-Toggle-thumb-size | $siz-switch-thumb-md | Diameter of the knob. Stepped up when it holds a glyph, unless set. Set it on the root, not on the track. The Size parameter wins over it. |
| --bit-Toggle-thumb-radius | 50% | Corner radius of the knob; the default draws a circle. |
| --bit-Toggle-thumb-inset | Derived from the track height and the knob | Room between the knob and the stroke, on every side of it. A negative value lets the knob overhang the track. |
| --bit-Toggle-thumb-shadow | none | Elevation of the knob. |
| --bit-Toggle-thumb-active-scale | 0.9 | How far the knob dips while pressed. Set it to 1 to drop the dip. |
| --bit-Toggle-icon-size | 3/5 of the knob it sits in | The glyph inside the knob. |
| --bit-Toggle-spinner-size | 4/5 of the knob it lands in | Diameter of the loading spinner. |
| --bit-Toggle-spinner-width | $siz-spinner-stroke | Thickness of the ring the spinner is drawn on. |
| --bit-Toggle-gap | spacing(1) | Room between the parts of the toggle: the track and the label, the track and the state text, and the toggle and the lines under it. |
| --bit-Toggle-content-padding | spacing(0.375) | Inset of the track content from the ends of the track. The Size parameter wins over it. |
| --bit-Toggle-label-font-size | $tg-fs-sm | The label. The Size parameter wins over it. |
| --bit-Toggle-label-font-weight | $tg-field-label-font-weight | The label. |
| --bit-Toggle-text-font-size | inherit | The state text. The Size parameter wins over it. |
| --bit-Toggle-description-font-size | $tg-fs-xs | The description line. The Size parameter wins over it. |
| --bit-Toggle-error-font-size | $tg-fs-xs | The error message line. The Size parameter wins over it. |
| --bit-Toggle-content-font-size | $tg-fs-xs | The content carried inside the track. The Size parameter wins over it. |
| --bit-Toggle-transition-duration | $mot-duration-short | How long the knob takes to cross the track, and every color with it. Reduced motion collapses it - the default and a value set here alike - unless ForceAnimation is set. |
API
Every parameter, public member, sub-class and enum this component exposes.
BitToggle parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| AllowDisabledFocus | bool | false | Keeps a disabled toggle focusable and discoverable by assistive technologies, conveying the disabled state through aria-disabled rather than the native disabled attribute. The toggle still refuses every change. |
| 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. An explicit value wins over the --bit-Toggle-* color variables of the checked state and the focus color; left unset, the toggle is primary unless they say otherwise. |
| 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. |
| ErrorMessage | string? | null | A line under the toggle saying why its state was rejected, which marks it invalid in the same way Invalid does and is announced by a live region the moment it shows up. |
| ErrorMessageTemplate | RenderFragment? | null | Custom content of the error message, replacing the plain ErrorMessage text and marking the toggle invalid in the same way. Only the plain ErrorMessage is announced. |
| 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. |
| Invalid | bool | false | Marks the state of the toggle as invalid, giving a state rejected by something other than the cascading EditContext the same look and the same aria-invalid attribute a failing data annotation gives it. |
| Label | string? | null | Label of the toggle. |
| LabelPlacement | BitPlacement? | null | The position of the label in regards to the knob of the toggle. Takes precedence over Inline and Reversed when set. Only Top, Bottom, Start and End are honoured; any other value leaves the layout as it is with this unset. |
| LabelTemplate | RenderFragment? | null | Custom label of the toggle. |
| Loading | bool | false | Renders a spinner in place of whatever the knob carries 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. An explicit value wins over the --bit-Toggle-* size variables (track width and height, knob size, content padding, font sizes); left unset, the toggle is medium unless they say otherwise. |
| 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. |
| ThumbTemplate | RenderFragment<bool>? | null | Arbitrary content rendered inside the knob, receiving the current state of the toggle, in place of the glyph OnIconName and OffIconName would have drawn there. The knob takes the roomier geometry a glyph asks for, and the spinner still takes it back while the toggle is busy. |
| Title | string? | null | The native tooltip of the toggle, rendered on its root so a hover anywhere on it - the label included - brings the tooltip up. |
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. |
| Disabled | bool | false | Gets or sets a value indicating whether the component is disabled and cannot respond to user interaction. |
| 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. An attribute written on the component wins over an entry of the same name. |
| Id | string? | null | Gets or sets the unique identifier for the component's root element. |
| 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. |
| ErrorMessageContainer | string? | null | Custom CSS classes/styles for the line carrying the error message of the BitToggle. |
| ErrorMessage | string? | null | Custom CSS classes/styles for the error message of the BitToggle. Ignored when an ErrorMessageTemplate replaces the plain message. |
| 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. |
BitPlacement enum
| Name | Value | Description |
|---|---|---|
| Top | 0 | The top edge. |
| Bottom | 1 | The bottom edge. |
| Start | 2 | The edge the reading direction starts from - the left in LTR, the right in RTL. On the vertical axis, which does not turn around, it is the top. |
| End | 3 | The edge the reading direction ends at - the right in LTR, the left in RTL. On the vertical axis, which does not turn around, it is the bottom. |
| Left | 4 | The left edge, in both reading directions. |
| Right | 5 | The right edge, in both reading directions. |
| Center | 6 | The middle of the axis, against neither edge. |
| TopAndBottom | 7 | Both edges of the block axis at once. |
| StartAndEnd | 8 | Both edges of the inline axis at once, following the reading direction the way Start and End do. |
BitSize enum
| Name | Value | Description |
|---|---|---|
| Small | 0 | The small size. |
| Medium | 1 | The medium size. |
| Large | 2 | The large size. |
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.