Introducing PrimeReact v11-alpha 🎉Discover Now
styledForm

ToggleSwitch

ToggleSwitch is used to select a boolean value.

preview

Usage#

import { ToggleSwitch } from '@primereact/ui/toggleswitch';
<ToggleSwitch.Root>
    <ToggleSwitch.Control>
        <ToggleSwitch.Thumb />
    </ToggleSwitch.Control>
</ToggleSwitch.Root>

Examples#

Basic#

Toggles a boolean setting between enabled and disabled states.

basic-demo

Controlled#

A controlled ToggleSwitch requires managing the checked state with a state variable and handling the change event manually. This allows for complete control over the ToggleSwitch's behavior.

controlled-demo

Uncontrolled#

For an uncontrolled ToggleSwitch component, defaultChecked is used to set the initial state, and the component manages its own state internally.

uncontrolled-demo

Template#

ToggleSwitch.Thumb also allows displaying custom content inside itself.

template-demo

Customization#

ToggleSwitch component supports customization through CSS classes. The appearance, including colors and other visual properties, can be modified by applying custom classes to the component.

customization-demo

Invalid#

Invalid state is displayed using the invalid prop to indicate a failed validation. This style is useful when integrating with form validation libraries.

invalid-demo

Disabled#

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

disabled-demo

API#

Sub-Components#

See ToggleSwitch Primitive for the full sub-component API.

Hooks#

See useToggleSwitch for the headless hook API.

Accessibility#

See ToggleSwitch Primitive for WAI-ARIA compliance details and keyboard support.