Switch

Switch is used to select a boolean value.

basic-demo

Installation#

npx shadcn@latest add @primereact/ui/switch

Usage#

import { Switch } from 'primereact/switch';
<Switch>
    <Switch.Control>
        <Switch.Thumb />
    </Switch.Control>
</Switch>

Examples#

Controlled#

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

controlled-demo
'use client';
import { Switch } from '@/components/ui/switch';
import { SwitchRootChangeEvent } from '@primereact/types/shared/switch';
import React from 'react';

export default function ControlledDemo() {
    const [checked, setChecked] = React.useState(true);

    return (
        <div className="flex justify-center items-center gap-2">
            <Switch inputId="mode" checked={checked} onCheckedChange={(event: SwitchRootChangeEvent) => setChecked(event.checked)} />
            <label htmlFor="mode">Airplane Mode</label>
        </div>
    );
}

Uncontrolled#

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

uncontrolled-demo
import { Switch } from '@/components/ui/switch';

export default function UncontrolledDemo() {
    return (
        <div className="flex justify-center">
            <Switch defaultChecked />
        </div>
    );
}

Template#

Switch.Thumb also allows displaying custom content inside itself.

template-demo
import { Switch } from '@/components/ui/switch';
import { Check } from '@primeicons/react/check';
import { Times } from '@primeicons/react/times';

export default function TemplateDemo() {
    return (
        <div className="flex justify-center">
            <Switch className="group">
                <Times className="text-[10px] absolute top-1/2 -translate-y-1/2 left-1/2 -translate-x-1/2 text-surface-400 dark:text-surface-600 group-data-checked:opacity-0 opacity-100 transition-opacity duration-200" />
                <Check className="text-[10px] absolute top-1/2 -translate-y-1/2 left-1/2 -translate-x-1/2 text-primary group-data-checked:opacity-100 opacity-0 transition-opacity duration-200" />
            </Switch>
        </div>
    );
}

Invalid#

Invalid state is displayed using the invalid prop to indicate a failed validation. You can use this style when integrating with form validation libraries.

invalid-demo
'use client';
import { Switch } from '@/components/ui/switch';
import { SwitchChangeEvent } from '@primereact/types/shared/switch';
import * as React from 'react';

export default function InvalidDemo() {
    const [checked, setChecked] = React.useState(false);

    return (
        <div className="flex justify-center">
            <Switch checked={checked} onCheckedChange={(event: SwitchChangeEvent) => setChecked(event.checked)} invalid={!checked} />
        </div>
    );
}

Disabled#

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

disabled-demo
import { Switch } from '@/components/ui/switch';

export default function DisabledDemo() {
    return (
        <div className="flex justify-center">
            <Switch disabled />
        </div>
    );
}

Accessibility#

Screen Reader#

Switch component uses a hidden native checkbox element with switch role internally that is only visible to screen readers. Value to describe the component can either be provided via label tag combined with id prop or using aria-labelledby, aria-label props.

<label htmlFor="switch1">Remember Me</label>
<Switch inputId="switch1" />
 
<span id="switch2">Remember Me</span>
<Switch aria-labelledby="switch2" />
 
<Switch aria-label="Remember Me" />

Keyboard Support#

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