A switch is an input widget that allows users to choose one of two values: on or off. Switches are similar to checkboxes and toggle buttons, which can also serve as binary inputs. One difference, however, is that switches can only be used for binary input.
The switch field has enough unique behaviors and use-cases that justify it having its own composable. The binary state of a switch means it shouldn’t be used to represent “required” inputs where the switch needs to be “on”. It is a user preference that can be turned off.
Features
You can use an input[type="checkbox"] element as a base or any custom element.
Labeling, descriptions, and error message displays are automatically linked to input and label elements with aria-* attributes.
Support for custom on and off values.
Validation support with native HTML constraint validation or Standard Schema validation.
Support for v-model binding.
Supported keyboard features:
Key
Description
⎵Space
Toggles the switch on/off.
↩Enter
Toggles the switch on/off.
Anatomy
Input
Label
Label
Something went wrong
Error Message
Building a Switch component
The useSwitch composable provides the necessary props and methods to build a switch component. It returns binding objects for the elements shown in the anatomy. You will use v-bind to bind them to the corresponding DOM elements.
There are two ways to build a switch component:
With an input[type="checkbox"] as a base element.
Without an input element, like a div or a button.
We will review the two ways to build a switch component in the following examples.
With input[type="checkbox"] base element
We will add some styling to the next example because switches don’t look like one unless we style them. Otherwise, it would just look like a checkbox.
Without input elements
Similar to the previous example, we can achieve the same look and behavior with a div element or a button element.
In this example, we will use an svg element to closely resemble a switch. We will borrow those SVG paths from Phosphor Icons.
Validation
HTML Constraints
While ideally, the switch field should not be validatable, you can still use the required attribute if you choose to use an input[type="checkbox"] as a base element. We make no assumptions about how you want to use the switch field.
Name
Type
Description
required
boolean
Whether the number field is required.
Standard Schemas
useSwitch also supports Standard Schema validation through the schema prop. This includes multiple providers like Zod, Valibot, Arktype, and more.
Mixed Validation
While it is unlikely that you need both HTML constraints and Standard Schemas to validate a switch, Formwerk supports mixed validation, which means you can use both HTML constraints and Standard Schemas to validate the switch, and they will work together.
Note that HTML constraints are validated first, so any errors from the HTML constraints will be displayed first. Then, once all HTML constraints are satisfied, the Standard Schema is validated.
If you need to disable the native validation, you can do so by setting the disableHtmlValidation prop to true. You can also disable it globally for all fields. For more information, check out the Validation guide.
Usage
Disabled
Use disabled to mark the switch as non-interactive. Disabled switches are not validated and will not be submitted.
If you need to prevent the user from interacting with the field while still allowing it to submit, consider using readonly instead.
Readonly
Readonly switches are validated and submitted, but they do not accept user input. The switch is still focusable, and the value is copyable. For more info, check the MDN.
Custom On/Off values
You can customize the on and off values of the switch component by passing the trueValue and falseValue props. They can be anything you want.
API
Props
These are the properties that can be passed to the useSwitch composable.
Name
Type
Description
disabled
Whether the switch is disabled.
disableHtmlValidation
Whether to disable HTML5 validation.
falseValue
The value to use when the switch is unchecked.
label
The label text for the switch.
modelValue
The v-model value of the switch.
name
The name attribute for the switch input.
readonly
Whether the switch is readonly.
required
Whether the switch is required.
schema
Schema for switch validation.
trueValue
The value to use when the switch is checked.
Returns
These are the properties in the object returned by the useSwitch composable.