Toggle Buttons Free Pro
FormKit Pro Quick Installation Guide 🚀
Toggle Buttons
To output multiple toggle buttons use the options prop. Options can be specified 3 ways:
- An array of strings
- An object of value/label pairs
- An array of objects with
labelandvalueproperties (the same as the select and radio inputs).- Additional parameters can be passed to set
disabled,help,onLabel, and/oroffLabelon a per option basis.
- Additional parameters can be passed to set
<FormKit type="togglebuttons" name="transportation" label="Travel Preference" :options="['Car', 'Airplane', 'Train', 'Hoverboard']"/><FormKit type="togglebuttons" name="shirt_size" label="T-shirt Size" :options="{ sm: 'Small', md: 'Medium', lg: 'Large', xl: 'X-Large', }"/><FormKit type="togglebuttons" name="pizza_toppings" label="Pizza Toppings" :options="[ 'Pepperoni', 'Sausage', 'Mushrooms', { value: 'Pineapple', label: 'Pineapple', disabled: true, help: 'Sorry, pineapple doesn\'t belong on pizza', }, ]"/>Behavioral props
Multiple
The togglebuttons input, by default, only allows one option to be selected. You can change this behavior by setting the multiple prop which will then allow the selection of multiple options:
<FormKit type="togglebuttons" name="pizza_toppings" label="Pizza Toppings" :options="['Pepperoni', 'Sausage', 'Mushroom', 'Olives']" multiple/>Enforced
By default togglebuttons will allow you to select and deselect allowing for an off state. With the enforced prop enabled once a user has interacted with the input, one value will always be set much like that of a radio input.
<FormKit type="togglebuttons" name="shirt_size" label="T-shirt Size" enforced :options="['Small', 'Medium', 'Large', 'X-Large']"/>Vertical
Assuming you are using the default FormKit styles, the vertical prop applies dataset attributes and styling to stack toggle buttons in a vertical orientation.
<FormKit type="togglebuttons" name="pizza_toppings" label="Pizza Toppings" :options="['Pepperoni', 'Sausage', 'Mushroom', 'Olives']" vertical/>More prop examples and combinations:
<FormKit type="togglebuttons" name="shirt_size" label="T-shirt Size" :options="['Small', 'Medium', 'Large', 'X-Large']" :vertical="value.orientation"/><FormKit type="togglebuttons" label="Size Toggle Orientation" name="orientation" enforced :value="false" :multiple="false" :options="[ { label: 'Horizontal', value: false }, { label: 'Vertical', value: true }, ]"/>Standalone Toggle
Standalone basic example
The togglebuttons input offers choices between one or more values; it's a great option when you want the user to toggle a feature on or off, between choices or allow multiple selections.
<FormKit type="togglebuttons" name="toggle" label="Toggle Me" />Standalone Toggle On/Off Values
The values for the toggle buttons 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':
<FormKit type="togglebuttons" name="airplane_mode" label="Airplane mode" on-value="active" off-value="inactive"/>Standalone On/Off labels
Additionally, you may specify secondary labels by setting the on-label and the off-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 input label position above the toggle when value labels are used:
<FormKit type="togglebuttons" name="mode" off-label="Light Mode" on-label="Dark Mode"/>Slots
Toggle Button Slots
For multiple options you can use the default slot to set the toggle's content.
<FormKit type="togglebuttons" name="social_network" label="Preferred social network" :options="['Facebook', 'Twitter', 'Instagram']"> <template #default="context"> <div class="w-5 h-5"> <FormKitIcon :icon="context.option.value.toLowerCase()" /> </div> </template></FormKit>Standalone Toggle Slots
For a single toggle you can use the default slot for setting the toggle content. Also available are on and off slots to change the content based on whether the toggle is activated or not.
<FormKit type="togglebuttons"> Airplane mode </FormKit><FormKit type="togglebuttons"> <template #on><span class="text-xl">☑</span> Enabled </template> <template #off><span class="text-xl">☐</span> Disabled </template></FormKit>Props & Attributes
| Prop | Type | Default | Description |
|---|---|---|---|
off-value | any | false | The value when the toggle is unchecked. For use with standalone toggle buttons only. |
on-value | any | true | The value when the toggle is checked. For use with standalone toggle buttons only. |
off-label | String | undefined | The text of the button label when the toggle is unchecked. For use with standalone toggle buttons only. |
on-label | String | undefined | The text of the button label when the toggle is checked. For use with standalone toggle buttons only. |
options | Array/Object | [] | An object of value/label pairs or an array of strings, or an array of objects that must contain a label and value property. Additional props include: disabled, help, onLabel and offLabel |
multiple | Boolean | false | Allows for selecting multiple options. For use with multiple toggle buttons only. |
enforced | Boolean | false | Makes sure at least one selection remains selected once field is activated. For use with multiple toggle buttons only. |
vertical | Boolean | false | Applies data attributes and styling to stack toggle buttons in a vertical orientation. For use with multiple toggle buttons only. |
| Show Universal props | |||
config | Object | {} | Configuration options to provide to the input’s node and any descendent node of this input. |
delay | Number | 20 | Number of milliseconds to debounce an input’s value before the commit hook is dispatched. |
dirtyBehavior | string | touched | Determines how the "dirty" flag of this input is set. Can be set to touched or compare — touched (the default) is more performant, but will not detect when the form is once again matching its initial state. |
errors | Array | [] | Array of strings to show as error messages on this field. |
help | String | '' | Text for help text associated with the input. |
id | String | input_{n} | The unique id of the input. Providing an id also allows the input’s node to be globally accessed. |
ignore | Boolean | false | Prevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values. |
index | Number | undefined | Allows 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. |
label | String | '' | Text for the label element associated with the input. |
name | String | input_{n} | The name of the input as identified in the data object. This should be unique within a group of fields. |
parent | FormKitNode | contextual | By default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node. |
prefix-icon | String | '' | Specifies an icon to put in the prefixIcon section. |
preserve | Boolean | false | Preserves the value of the input on a parent group, list, or form when the input unmounts. |
preserve-errors | Boolean | false | By 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-schema | Object | {} | An object of section keys and schema partial values, where each schema partial is applied to the respective section. |
suffix-icon | String | '' | Specifies an icon to put in the suffixIcon section. |
type | String | text | The type of input to render from the library. |
validation | String, Array | [] | The validation rules to be applied to the input. |
validation-visibility | String | blur | Determines when to show an input's failing validation rules. Valid values are blur, dirty, and live. |
validation-label | String | {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-rules | Object | {} | Additional custom validation rules to make available to the validation prop. |
value | Any | undefined | Seeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists.. |
Sections & slot data
The toggle buttons input has a different construction depending on if it's a single or multiple configuration.
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.
Single toggle button
A single toggle button without options.
Click on a section to lock focus to the chosen section. Click locked section again to unlock.
Multiple toggle buttons
Toggle buttons with multiple options.
Click on a section to lock focus to the chosen section. Click locked section again to unlock.
| Section-key | Description |
|---|---|
singleToggle | Button wrapper for the single toggle button |
multiToggle | Button wrapper for a toggle button when multiple toggle buttons exist |
inputInner | Button inner text for a toggle button |
option | Responsible for the wrapper around each item in the options. |
options | Responsible for the wrapper element around all of the option items. |
| Show Universal section keys | |
outer | The outermost wrapping element. |
wrapper | A wrapper around the label and input. |
label | The label of the input. |
prefix | Has no output by default, but allows content directly before an input element. |
prefixIcon | An element for outputting an icon before the prefix section. |
inner | A wrapper around the actual input element. |
suffix | Has no output by default, but allows content directly after an input element. |
suffixIcon | An element for outputting an icon after the suffix section. |
input | The input element itself. |
help | The element containing help text. |
messages | A wrapper around all the messages. |
message | The 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 markup
- ARIA attributes
- Keyboard accessibility
- Focus indicators
- Color contrast with the provided theme
- Accessible labels, help text, and errors
Accessibility attributes
Single toggle button
| Section Key | Attribute | Value | Description |
|---|---|---|---|
singleToggle | aria-label | Provides an accessible name. | |
aria-pressed | Indicates the pressed state of toggle button. | ||
role | checkbox | Indicates to assistive technologies that this element functions as a checkbox. | |
| Universal Accessibility Attributes | |||
label | label | for | Associates a label to an input element. Users can click on the label to focus the input or to toggle between states. |
input | input | disabled | Disables an HTML element, preventing user interaction and signaling a non-interactive state. |
aria-describedby | Associates an element with a description, aiding screen readers. | ||
aria-required | Added when input validation is set to required. | ||
icon | icon | for | Links icon to input element when icon in rendered as a label. |
Multiple toggle buttons
| Section Key | Attribute | Value | Description |
|---|---|---|---|
multiToggle | aria-label | Provides an accessible name. | |
aria-pressed | Indicates the pressed state of toggle button. | ||
role | checkbox or radio | Indicates to assistive technologies that this element functions as a checkbox if has options or radio if not. | |
options | role | group | Indicates to assistive technologies that this element functions as a group. |
aria-labelledby | Associates this element as a label for the input. | ||
option | role | listitem | Indicates to assistive technologies that this element functions as listitem. |
| Universal Accessibility Attributes | |||
label | label | for | Associates a label to an input element. Users can click on the label to focus the input or to toggle between states. |
input | input | disabled | Disables an HTML element, preventing user interaction and signaling a non-interactive state. |
aria-describedby | Associates an element with a description, aiding screen readers. | ||
aria-required | Added when input validation is set to required. | ||
icon | icon | for | Links icon to input element when icon in rendered as a label. |
Keyboard Interactions
| Keyboard Event | Description |
|---|---|
| Space | Changes the state of the currently focused toggle button. |
| Universal Keyboard Events | |
| Tab | Moves the focus to the next focusable element on the page. |
| Shift+Tab | Moves the focus to the previous focusable element on the page. |