ToggleButton

ToggleButton is used to select a boolean value using a button.


import ToggleButton from 'primevue/togglebutton';

Two-way binding to a boolean property is defined using the standard v-model directive.

No


<ToggleButton v-model="checked" />

Icons and Labels can be customized using onLabel, offLabel, onIcon and offIcon properties.

I reject


<ToggleButton v-model="checked1" onLabel="I confirm" offLabel="I reject"
    onIcon="pi pi-check" offIcon="pi pi-times" class="w-9rem" />

When disabled is present, the element cannot be edited and focused.

No


<ToggleButton v-model="checked" disabled onIcon="pi pi-check" offIcon="pi pi-times"
    class="w-full sm:w-10rem" aria-label="Confirmation" />

Compatibility with popular Vue form libraries.

VeeValidate is a popular library for handling forms in Vue.

No
 


<div class="card flex justify-content-center">
    <form @submit="onSubmit" class="flex flex-column gap-2">
        <ToggleButton v-model="value" :class="['w-8rem', { 'p-invalid': errorMessage }]" aria-describedby="text-error" />
        <small id="text-error" class="p-error">{{ errorMessage || '&nbsp;' }}</small>
        <Button type="submit" label="Submit" />
    </form>
</div>

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-togglebuttonContainer element
p-button-iconIcon element.
p-button-textLabel element.

Screen Reader

ToggleButton component uses a hidden native checkbox element as the switch element internally that is only visible to screen readers. Value to describe the component can be defined with aria-labelledby or aria-label props, it is highly suggested to use either of these props as the component changes the label displayed which will result in screen readers to read different labels when the component receives focus. To prevent this, always provide an aria label that does not change related to state.



<span id="rememberme">Remember Me</span>
<ToggleButton aria-labelledby="rememberme" />

<ToggleButton aria-label="Remember Me" />

Keyboard Support

Keyboard interaction is derived from the native browser handling of checkboxs in a group.

KeyFunction
tabMoves focus to the button.
spaceToggles the checked state.