Skip to content

BbSwitch

Toggles two-state values with switch semantics.

import { BbSwitch } from 'bitboss-ui';

On this page

Props

NameTypeDefaultDescription
autofocusBooleanish | undefined-

Sets autofocus on page load.

checkedboolean | undefined-

Defines the input as checked.

Paints the control from state you own elsewhere and wins over modelValue until you unset it — so it is display-driven or bound, never both. Narrowed to a plain boolean in v3: the v2 checked="false" no longer compiles, where it used to turn the switch on.

See example
descriptionstring | undefined-

Descriptive text displayed below the label and above the input. Unlike the hint it is always visible, and it is linked to the input via aria-describedby (after any errors / warnings, before the hint).

Always visible and tied to the control through aria-describedby. On a settings row it is where the consequence of flipping the switch belongs.

See example
directionInputDirection | undefined"auto"

Direction of the layout of the component. Can either be a predefined value or a pattern separated by a space like xx xxxxx.

disabledboolean | undefinedfalse

Disables the component.

errorsstring | string[] | undefined-

Can be a string or an array of string containing the messages to display. They render in an aria-live="polite" region (announced when they appear) and, while the list is non-empty, are referenced FIRST from the control's aria-describedby, so the reason the field is invalid is re-read whenever the control regains focus.

falseValueany-

Value of the input when unchecked. It handles any kind of serializable object.

See example
hasErrorsboolean | undefinedfalse

Define if the component should be in an error state. It usually attaches a CSS class for styling purposes.

hasWarningsboolean | undefinedfalse

Define if the component should be in a warning state. Same chrome as errors with --bb-warn; errors take priority.

hideLabelboolean | undefinedfalse

Visually hides the label of the input while maintaining accessibility.

hintstring | undefined-

Text box to be displayed near the input, usually to indicate instructions.

idstring | undefined-

The identifier of the component.

indeterminateboolean | undefinedfalse

Sets the input in an indeterminate state.

Parks the thumb midway, for a master switch whose children disagree. Visual only — the model keeps its value, and the component re-asserts the state after clicks because the DOM resets it natively.

See example
inputPosition"left" | "center" | "right" | undefined"left"

Sets the alignment of the input. Since inputs are inline block they can be aligned just as text can.

labelrequiredstring-

Text content of the label of the element.

Required — it is the accessible name of the switch. Do not add ARIA of your own: the control already exposes role="switch" and its state.

See example
labelPosition"left" | "center" | "right" | undefined-

Sets the text alignment of the label.

modelValueany-

Used by v-model. Can be any serializable type.

namestring | undefined-

Defines the name of the input.

persistentHintboolean | undefinedfalse

Keeps the hint displayed.

ptPtMap<TogglePtPart, CheckablePtState> | undefined-

Passthrough, object form: the same keys as the pt:* attributes without the prefix. See the pt:<part> row.

pt:descriptionPtValue | undefined-
pt:description:checkedPtValue | undefined-
pt:description:disabledPtValue | undefined-
pt:description:errorsPtValue | undefined-
pt:description:focus-visiblePtValue | undefined-
pt:description:focusedPtValue | undefined-
pt:description:indeterminatePtValue | undefined-
pt:description:readonlyPtValue | undefined-
pt:description:warningsPtValue | undefined-
pt:hintPtValue | undefined-
pt:hint:checkedPtValue | undefined-
pt:hint:disabledPtValue | undefined-
pt:hint:errorsPtValue | undefined-
pt:hint:focus-visiblePtValue | undefined-
pt:hint:focusedPtValue | undefined-
pt:hint:indeterminatePtValue | undefined-
pt:hint:readonlyPtValue | undefined-
pt:hint:warningsPtValue | undefined-
pt:iconPtValue | undefined-
pt:icon:checkedPtValue | undefined-
pt:icon:disabledPtValue | undefined-
pt:icon:errorsPtValue | undefined-
pt:icon:focus-visiblePtValue | undefined-
pt:icon:focusedPtValue | undefined-
pt:icon:indeterminatePtValue | undefined-
pt:icon:readonlyPtValue | undefined-
pt:icon:warningsPtValue | undefined-
pt:labelPtValue | undefined-
pt:label:checkedPtValue | undefined-
pt:label:disabledPtValue | undefined-
pt:label:errorsPtValue | undefined-
pt:label:focus-visiblePtValue | undefined-
pt:label:focusedPtValue | undefined-
pt:label:indeterminatePtValue | undefined-
pt:label:readonlyPtValue | undefined-
pt:label:warningsPtValue | undefined-
pt:messagePtValue | undefined-
pt:message:checkedPtValue | undefined-
pt:message:disabledPtValue | undefined-
pt:message:errorsPtValue | undefined-
pt:message:focus-visiblePtValue | undefined-
pt:message:focusedPtValue | undefined-
pt:message:indeterminatePtValue | undefined-
pt:message:readonlyPtValue | undefined-
pt:message:warningsPtValue | undefined-
pt:rootPtValue | undefined-
pt:root:checkedPtValue | undefined-
pt:root:disabledPtValue | undefined-
pt:root:errorsPtValue | undefined-
pt:root:focus-visiblePtValue | undefined-
pt:root:focusedPtValue | undefined-
pt:root:indeterminatePtValue | undefined-
pt:root:readonlyPtValue | undefined-
pt:root:warningsPtValue | undefined-
readonlyboolean | undefinedfalse

Sets the input in a readonly state.

The right state for a switch whose change is in flight, and for admin-locked settings. It sets no native disabled attribute, so do not target :disabled in CSS or tests — .bb-base-switch--readonly is the hook.

See example
requiredboolean | undefinedfalse

Sets the input as required.

reverseboolean | undefinedfalse

Reverses the layout: in every direction the order of the label and the input is swapped from the standard arrangement.

Its meaning inverted in v3 and the compiler cannot see it. The default rendering is unchanged, but a v2 :reverse="false" becomes a bare reverse — which is the label-first settings row, so most switches that set it are affected.

submitWhenFalseboolean | undefinedfalse

Will submit "falseValue" if the input is not checked. Otherwise "trueValue" will be submitted.

Only for classic form posts: an unchecked switch otherwise submits nothing. The full value contract is shared with BbCheckbox.

trueValueany-

Value of the input when checked. It handles any kind of serializable object.

Point it and false-value at the two members of an enum and the model stops needing a translation layer on either side of the request.

See example
warningsstring | string[] | undefined-

Warning messages to display beneath the field, in their own aria-live region and amber (--bb-text-warn). A string or array; a non-empty list implies the warning state. Errors suppress warnings when both are set. While displayed they are also referenced from the control's aria-describedby, after any errors and before the description/hint.

v-model

Values kept in sync through v-model.

NameTypeDescription
modelValueany

Carries the mapped true-value / false-value. Use :model-value with @update:model-value instead of v-model when a failed save has to put the previous value back.

See example

Events

NameTypeDescription
blur(event: "blur", event: FocusEvent): void
change(event: "change", event: Event): void
click(event: "click", event: MouseEvent): void
focus(event: "focus", event: FocusEvent): void
input(event: "input", event: Event): void
keydown(event: "keydown", event: KeyboardEvent): void
mousedown(event: "mousedown", event: MouseEvent): void
mouseup(event: "mouseup", event: MouseEvent): void
update:modelValue(event: "update:modelValue", value: any): void

The hook for a live switch that saves itself: write optimistically here, hold the control readonly while the request is in flight, and restore the previous value on failure.

See example

Slots

The listed properties are the ones exposed to the slot scope.

NameTypeDescription
descriptionBbSwitchDescriptionSlotProps

Replaces the description text. Provided alone (no description prop) it still renders the description region and wires it into aria-describedby.

iconBbCheckboxIconSlotProps

Replaces the default toggle track visual inside the switch. Receives the switch state plus the label text.

Replaces the entire track and thumb, not a glyph inside them — a bare icon here leaves no track at all. Keep it when only the visual is wrong; when the whole row is wrong, drop to BbBaseSwitchIcon.

See example
labelBbSwitchLabelSlotProps

Replaces the default label text rendered next to the switch.

Changes from v2

Props removed or renamed in 3.0. If you are coming from v2, the answer is here.

  • colorremovedfails silently
  • hasWarninghasWarningsfails silently