ToggleButton

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


import { ToggleButtonModule } from 'primeng/togglebutton';

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

Off

<p-toggleButton 
    [(ngModel)]="checked" 
    onLabel="On" 
    offLabel="Off" />

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

Off

<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.

Unlocked

<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-9rem"
    ariaLabel="Do you confirm" />

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

No

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

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

NameElement
p-togglebuttonContainer element.
p-button-icon-leftText element.
p-button-icon-rightValue element.

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.