Skip to content

CdsToggle

A toggle is an on/off switch for a setting that applies immediately, without a "Save" button: notifications, auto-renewal, profile visibility. It is built on CdsInput: it can do a label and a description, messages and errors, loading and the indeterminate state.

Storybook GitLab Figma
Test coverage: 100% — good coverage
API in specs
69 / 69 props, events, slots and exposed members with a test
Test cases
66 unit · 0 browser · 4 planned
Code coverage
100% lines · 96.7% branches measured in v4.0.0-alpha.13
A11y (axe)
2 recommendations in 13 stories measured in v4.0.0-alpha.11

A toggle changes the setting instantly. If the choice is applied together with the rest of the form by a button, take CdsCheckbox, and to pick one option out of a set — CdsRadioGroup.

Anatomy ​

On the left is the track with the thumb: their position and color show the state. On the right are the label and the description of the setting, at the bottom is the messages block. A click on the label toggles the switch exactly like a click on the track.

Variations ​

Label and description ​

label names the setting, description explains what turning it on leads to. Phrase the label so that it is clear what happens in the on position.

Sizes ​

Five sizes on the size scale — from xs to xl, md by default.

States ​

disabled blocks the toggle and lowers the contrast, readonly forbids changing the value, error paints the track with the error color. indeterminate is the undefined state: the thumb stops in the middle while the value is unknown — when only some of the nested options are on, for example. The first toggle removes it.

Loading and icons ​

loading shows an indicator inside the thumb — handy while the setting is being saved on the server; a string value sets the color of the loader. true-icon and false-icon add an icon to the thumb for each position.

Colors ​

color paints the track of a switched-on toggle, base-color — of a switched-off one, thumb-color — the thumb. The values are the names of the palette colors.

Your own values ​

By default the model is true/false. true-value and false-value bind the toggle to your own values — to an API field with a string status, for example.

Messages ​

messages shows a hint under the toggle, error-messages — a ready error message together with the error state. hide-details removes the bottom block entirely.

Slots ​

When the text props are not enough, parts of the component are replaced by slots.

Slot nameDescription
labelLabel content (replaces label)
descriptionDescription content (replaces description)
thumbThumb content; receives the current icon and the model
track-true / track-falseTrack content in the on / off position
loaderCustom loading indicator
prepend / appendElements before and after the control
messages / detailsContent of the messages block / the whole bottom block

In the demo the thumb slot changes the icon of the thumb depending on the value, and track-true and track-false show icons on the track.

Guidelines ​

Scenarios ​

Notification settings ​

The main toggle controls the whole group: while notifications are off, the channels are blocked through disabled. The choice of channels is kept though — after switching back on the group returns to its previous values.

Saving on the server ​

The setting is saved right after the switch: while the request is in flight the toggle shows loading and does not accept clicks because of readonly. If the server rejected the change, the value comes back and the reason is shown in error-messages. Try switching the toggle off.

Every toggle adds an option to the plan, and the total is recalculated at once — the user sees the consequences of the choice before paying.

API ​

Props

NameTypeDefaultDescription
appendIconIconValue | undefined—Icon in the append slot, after the content
baseColorstring | undefined—Color of the component in its inactive state
centerAffixboolean | undefinedtrueVertically center the appendInner, prependInner, clearIcon and label elements.
colorstring | undefined—Component color
defaultsTargetstring | undefined—Name of the child component the group prop values are passed to as defaults
descriptionstring | undefined—Description of the component, shown under the label
direction"horizontal" | "vertical" | undefined'horizontal'Switches the component between horizontal and vertical layout.
disabledboolean | null | undefinednullDisabled state
errorboolean | undefined—Puts the component into the visual error state
errorMessagesstring | readonly string[] | null | undefined[]Puts the component into the error state and passes custom error messages. They are merged with any checks defined in the rules prop. This field does not trigger validation.
falseIconIconValue | undefined—Icon of the unselected state
falseValueany—Value for the false state.
focusedboolean | undefined—Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior.
hideDetailsboolean | "auto" | undefined—Hides the details block (messages and hints). With the 'auto' value the block is hidden when there are no messages
hideMessagesboolean | undefined—Hide the messages under the input
hintstring | undefined—Hint text shown under the input
iconColorstring | boolean | undefined—Icon color. Accepts a boolean or a color string
idstring | undefined—Unique identifier of the component
indeterminateboolean | undefined—Indeterminate state of the toggle.
inlineboolean | undefined—Renders the control as an inline element — the container does not stretch to the full width
labelstring | undefined—Label text of the component
loadingstring | boolean | undefinedfalseLoading indicator. Accepts a boolean or a string with the color of the loader.
maxErrorsstring | number | undefined1Maximum number of validation errors shown
maxWidthstring | number | undefined—Maximum component width
messagesstring | readonly string[] | undefined[]Messages shown under the input
minWidthstring | number | undefined—Minimum component width
modelValueany—Value of the component (v-model). In a group it is the value of the selected control, with multiple — an array of the selected values
multipleboolean | null | undefinednullAllows selecting several values — the model becomes an array. Turns on automatically when an array is passed to modelValue
namestring | undefined—The name attribute of the native input element. In a group one name joins all the nested controls
persistentHintboolean | undefined—Always show the hint, not only on focus
prependIconIconValue | undefined—Icon in the prepend slot, before the content
readonlyboolean | null | undefinednullRead-only state
rulesreadonly ValidationRule[] | undefined[]Accepts an array of items of different types: functions, booleans and strings. Functions take the input value as an argument and must return either true / false, or a string with an error message. The input goes into the error state if a function returns false (or any value in the array is false) or is a string.
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
themestring | undefined—Component theme
thumbColorstring | undefined—Color of the thumb.
trueIconIconValue | undefined—Icon of the selected state
trueValueany—Value for the true state.
typestring | undefined—Type of the native input element — radio or checkbox, for example
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
validationValueany—Value used when applying the validation rules
valueany—Value used when the component is selected inside a group. When it is not set, a unique identifier is used.
valueComparatorValueComparator | undefinedtrueFunction comparing the model value with the value of the control. Deep comparison by default
widthstring | number | undefined—Component width

Events

NameTypeDescription
update:focused[focused: boolean]Fires when the focus state of the component changes.
update:indeterminate[value: boolean]Fires when the indeterminate state changes.
update:modelValue[value: any]Fires when the model of the component changes.

Slots

NameTypeDescription
appendCdsInputSlotAdds an element after the input
default{ backgroundColorClasses: Ref<string[], string[]>; backgroundColorStyles: Ref<CSSProperties, CSSProperties>; }Default slot
description{ description: string | undefined; props: Record<string, any>; }Default slot of the CdsDescription component
detailsCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
inputSelectionControlSlotSlot for the default input element.
label{ label: string | undefined; props: Record<string, any>; }Default slot of the CdsLabel component
loaderLoaderSlotPropsSlot for a custom loading indicator.
messageCdsMessageSlotSlot for customizing the content of a message
messagesCdsInputSlotSlot for custom content that lets you customize how the messages are displayed
prependCdsInputSlotAdds an element before the input
thumb{ icon: IconValue | undefined; } & CdsToggleSlotSlot for the content of the thumb.
track-falseCdsToggleSlotSlot for the content of the off part of the track.
track-trueCdsToggleSlotSlot for the content of the on part of the track.

Exposed

NameTypeDescription
isDirtybooleanWhether the input is dirty
isDisabledbooleanWhether the input is disabled
isPurebooleanWhether the input is pure
isReadonlybooleanWhether the input is read-only
isValidboolean | nullWhether the input is valid
isValidatingbooleanWhether the input is being validated
reset() => Promise<void>Resets the input to its initial state
resetValidation() => Promise<void>Resets the validation of the input
validate(silent?: boolean) => Promise<string[]>Validates the input