Pro installation quickstart 🚀

Basic example

The toggle input — sometimes called a "switch" — offers a choice between one of two values; it's a great option when you want the user to toggle a feature on or off:

Load live example

Values

The values for the toggle are undefined if not-interacted with, true if checked, and false if unchecked. You can change these values by passing the onValue and offValue props. In this example, we will set the onValue to the string 'active' and the offValue to the string 'inactive':

Load live example

Labels

Primary label

By default, the toggle input supports a primary label like the label prop on any other FormKit input. The primary label is displayed to the right of the toggle unless you use a Value label, which will then move it above the toggle:

Load live example

Alt label position

If you would like the label to be displayed above the toggle, you can use the alt-label-position prop:

Load live example

Value label

Additionally, you may specify secondary labels by setting the on-value-label and the off-value-label props. These values are conditionally shown based on the on/off state of the toggle. The value labels render to the right of the toggle input. The "primary label" will be moved to the alternative label position above the toggle when value labels are used:

Load live example

Inner value labels

Additionally, you can set the value-label-display prop to inner to render the value labels inside the toggle. The Primary label will return to being displayed to the right of the toggle:

Load live example

Icons

The thumb-icon prop allows you to insert an icon into the thumb section of the toggle input. This prop accepts a string that's passed to FormKit's icon plugin:

Load live example

Colors

The toggle input supports color props for its various sections for easy styling. Each prop contains on and off states that are applied depending on whether the toggle is on or off. The following example uses some of these color props. See the Props & Attributes section below for a full list of these props:

Load live example

The toggle input is built atop HTML's native checkbox input.

Props & Attributes

PropTypeDefaultDescription
alt-label-positionBooleanundefinedMoves the label above the toggle.
off-valueanyfalseThe value when the toggle is unchecked.
on-valueanytrueThe value when the toggle is checked.
off-value-labelStringundefinedThe text of the Value label when the toggle is unchecked.
on-value-labelStringundefinedThe text of the Value label when the toggle is checked.
value-label-displayStringundefinedThe `on` and `off` value labels will always default to being displayed outside and to the right of the toggle. Other accepted values include `inner` to set the value labels inside of the toggle, and `hidden` which hides the value labels.
value-label-color-offStringundefinedUsed to set the color of the value label when the toggle is unchecked.
value-label-color-onStringundefinedUsed to set the color of the value label when the toggle is checked.
thumb-iconStringundefinedThis prop is used to set the icon that will be placed inside the thumb section.
thumb-color-offStringundefinedSets the background color of the thumb section when the toggle is unchecked.
thumb-color-onStringundefinedSets the background color of the thumb section when the toggle is checked.
icon-color-offStringundefinedThe color the icon should be set to when `toggle-icon` prop is set and the toggle is unchecked.
icon-color-onStringundefinedThe color the icon should be set to when `toggle-icon` prop is set and the toggle is checked.
track-color-offStringundefinedSets the background color of the `track` section when the toggle is unchecked.
track-color-onStringundefinedSets the background color of the `track` section when toggle is checked.
Show Universal props
configObject{}Configuration options to provide to the input’s node and any descendent node of this input.
delayNumber20Number of milliseconds to debounce an input’s value before the commit hook is dispatched.
dirtyBehaviorstringtouchedDetermines how the "dirty" flag of this input is set. Can be set to touched or comparetouched (the default) is more performant, but will not detect when the form is once again matching its initial state.
errorsArray[]Array of strings to show as error messages on this field.
helpString''Text for help text associated with the input.
idStringinput_{n}The unique id of the input. Providing an id also allows the input’s node to be globally accessed.
ignoreBooleanfalsePrevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values.
indexNumberundefinedAllows an input to be inserted at the given index if the parent is a list. If the input’s value is undefined, it inherits the value from that index position. If it has a value it inserts it into the lists’s values at the given index.
labelString''Text for the label element associated with the input.
nameStringinput_{n}The name of the input as identified in the data object. This should be unique within a group of fields.
parentFormKitNodecontextualBy default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node.
prefix-iconString''Specifies an icon to put in the prefixIcon section.
preservebooleanfalsePreserves the value of the input on a parent group, list, or form when the input unmounts.
preserve-errorsbooleanfalseBy default errors set on inputs using setErrors are automatically cleared on input, setting this prop to true maintains the error until it is explicitly cleared.
sections-schemaObject{}An object of section keys and schema partial values, where each schema partial is applied to the respective section.
suffix-iconString''Specifies an icon to put in the suffixIcon section.
typeStringtextThe type of input to render from the library.
validationString, Array[]The validation rules to be applied to the input.
validation-visibilityStringblurDetermines when to show an input's failing validation rules. Valid values are blur, dirty, and live.
validation-labelString{label prop}Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop.
validation-rulesObject{}Additional custom validation rules to make available to the validation prop.
valueAnyundefinedSeeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists..

Sections

You can target a specific section of an input using that section's "key", allowing you to modify that section's classes, HTML (via :sections-schema, or content (via slots)). Read more about sections here.

⚫️
Sound effects
Turn sound effects on and off.
Something wrong happened.
Section-keyDescription
trackThe track section is the background of the toggle (the area that the thumb slides on).
thumbThe thumb section is the element that slides on the track.
thumbIconThe thumbIcon section is the icon that is rendered inside the thumb section (when thumb-icon prop is set).
valueLabelAn additional label element, rendered when value-label props are used. Displayed to the right of the toggle.
altLabelA label element for the label. This label is rendered when value-label props are being used.
innerLabelA label element used when value-label props are provided and value-label-display is set to inner.
Show Universal section keys
outerThe outermost wrapping element.
wrapperA wrapper around the label and input.
labelThe label of the input.
prefixHas no output by default, but allows content directly before an input element.
prefixIconAn element for outputting an icon before the prefix section.
innerA wrapper around the actual input element.
suffixHas no output by default, but allows content directly after an input element.
suffixIconAn element for outputting an icon after the suffix section.
inputThe input element itself.
helpThe element containing help text.
messagesA wrapper around all the messages.
messageThe element (or many elements) containing a message — most often validation and error messages.

Accessibility

All FormKit inputs are designed with the following accessibility considerations in mind. Help us continually improve accessibility for all by filing accessibility issues here:

Semantic markupAria attributesKeyboard accessibleFocus indicatorsColor contrast with the provided themeAccessible labels, help text, and errors
Section KeyAttributeDefaultDescription
altLabelforAssociates the label to an input element. Users can click on the label to focus the input or to toggle between states.
valueLabelforAssociates the label to an input element. Users can click on the label to focus the input or to toggle between states.
innerforAssociates the label to an input element. Users can click on the label to focus the input or to toggle between states.
Show Universal section key
labellabelforAssociates the label to an input element. Users can click on the label to focus the input or to toggle between states.
inputinputdisabledDisables an HTML element, preventing user interaction and signaling a non-interactive state.
aria-describedbyAssociates an element with a description, aiding screen readers.
aria-requiredAdds this attribute when validation is required.
iconiconforWhenever icon is defined as a label it links it to an input element.

Keyboard Interactions

Keyboard EventDescription
TabMoves the focus to the next focusable input on the page.
Shift + TabMoves the focus to the previous focusable input on the page.