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.



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

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="Yes" offLabel="No"></p-toggleButton>
</form>

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



<p-toggleButton [(ngModel)]="checked" onLabel="I confirm" offLabel="I reject" onIcon="pi pi-check" offIcon="pi pi-times" [style]="{ width: '10em' }"></p-toggleButton>

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

NameElement
p-knobContainer element.
p-knob-textText element.
p-knob-valueValue element.
p-knob-textText element.
Accessibility guide documents the specification of this component based on WCAG guidelines, the implementation is in progress.

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 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>
<p-toggleButton aria-labelledby="rememberme"></p-toggleButton>

<p-toggleButton aria-label="Remember Me"></p-toggleButton>

Keyboard Support

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