A switch allows a user to turn a setting on or off.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.Selection
Use the isSelected or defaultSelected prop to set the selection state, and onChange to handle selection changes.
High power mode active.
import {Switch} from './Switch';
import {useState} from 'react';
function Example(props) {
let [selected, setSelection] = useState(false);
return (
<>
<Switch
{...props}
isSelected={selected}
onChange={setSelection}
>
Low power mode
</Switch>
<p>{selected ? 'Low' : 'High'} power mode active.</p>
</>
);
}
Forms
Use the name and value props to submit the switch to the server. Set the isRequired prop to validate the user selects the switch, or implement custom client or server-side validation. See the Forms guide to learn more.
import {Switch} from './Switch';
import {Button} from './Button';
import {Form} from './Form';
<Form>
<Switch
name="two-factor"
isRequired
description="Your organization requires two-factor authentication.">
Two-factor authentication
</Switch>
<Button type="submit" style={{marginTop: 8}}>Submit</Button>
</Form>
API
<SwitchField>
<SwitchButton>Label</SwitchButton>
<Text slot="description" />
<FieldError />
</SwitchField>
SwitchField
A switch allows a user to turn a setting on or off, with support for validation and help text.
| Name | Type | |
|---|---|---|
inputRef | RefObject | |
A ref for the HTML input element. | ||
defaultSelected | boolean | |
Whether the element should be selected (uncontrolled). | ||
isSelected | boolean | |
Whether the element should be selected (controlled). | ||
isDisabled | boolean | |
Whether the input is disabled. | ||
isReadOnly | boolean | |
Whether the input can be selected but not changed by the user. | ||
children | ChildrenOrFunction | |
The children of the component. A function may be provided to alter the children based on component state. | ||
onChange | | |
Handler that is called when the element's selection state changes. | ||
Default className: react-aria-SwitchField
| Render Prop | CSS Selector |
|---|---|
isSelected | CSS Selector: [data-selected]
|
| Whether the switch is selected. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the switch is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the switch is read only. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the switch invalid. | |
isRequired | CSS Selector: [data-required]
|
| Whether the switch is required. | |
state | CSS Selector: — |
| State of the switch. | |
SwitchButton
A switch button is the clickable area of a switch, including the indicator and label.
| Name | Type | |
|---|---|---|
children | ChildrenOrFunction | |
The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-SwitchButton
| Render Prop | CSS Selector |
|---|---|
isInvalid | CSS Selector: [data-invalid]
|
| Whether the switch invalid. | |
isRequired | CSS Selector: [data-required]
|
| Whether the switch is required. | |
state | CSS Selector: — |
| State of the switch. | |
isSelected | CSS Selector: [data-selected]
|
| Whether the switch is selected. | |
isHovered | CSS Selector: [data-hovered]
|
| Whether the switch is currently hovered with a mouse. | |
isPressed | CSS Selector: [data-pressed]
|
| Whether the switch is currently in a pressed state. | |
isFocused | CSS Selector: [data-focused]
|
| Whether the switch is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether the switch is keyboard focused. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the switch is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the switch is read only. | |