ToggleButton

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


import { ToggleButton } from 'primeng/togglebutton';

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


<p-togglebutton [(ngModel)]="checked" onLabel="On" offLabel="Off" styleClass="w-24" />

ToggleButton can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.


<form [formGroup]="formGroup">
    <p-togglebutton formControlName="checked" onLabel="On" offLabel="Off" />
</form>

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


<p-togglebutton [(ngModel)]="checked" onLabel="Locked" offLabel="Unlocked" onIcon="pi pi-check" offIcon="pi pi-times" onIcon="pi pi-lock" offIcon="pi pi-lock-open" styleClass="w-36" ariaLabel="Do you confirm" />

ToggleButton provides small and large sizes as alternatives to the base.


<p-togglebutton [(ngModel)]="value1" onLabel="On" offLabel="Off" size="small" styleClass="min-w-16" />
<p-togglebutton [(ngModel)]="value2" onLabel="On" offLabel="Off" styleClass="min-w-20" />
<p-togglebutton [(ngModel)]="value3" onLabel="On" offLabel="Off" size="large" styleClass="min-w-24" />

Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.


<p-togglebutton [(ngModel)]="checked" onIcon="pi pi-check" offIcon="pi pi-times" class="ng-invalid ng-dirty" styleClass="w-full sm:w-40" aria-label="Confirmation" />

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


<p-togglebutton disabled="true" onIcon="pi pi-check" offIcon="pi pi-times" [(ngModel)]="checked" onLabel="Yes" offLabel="No" styleClass="w-full sm:w-40" ariaLabel="Confirmation" />

Screen Reader

ToggleButton component uses an element with button role and updates aria-pressed state for screen readers. Value to describe the component can be defined with ariaLabelledBy or ariaLabel 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>
<p-togglebutton ariaLabelledBy="rememberme" />

<p-togglebutton ariaLabel="Remember Me" />

Keyboard Support

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