Switches allow users to turn an individual option on or off. They are usually used to activate or deactivate a specific setting.
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 '@react-spectrum/s2/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 '@react-spectrum/s2/Switch';
import {Button} from '@react-spectrum/s2/Button';
import {Form} from '@react-spectrum/s2/Form';
<Form>
<Switch
name="two-factor"
isRequired
description="Your organization requires two-factor authentication.">
Two-factor authentication
</Switch>
<Button type="submit">Submit</Button>
</Form>
API
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
inputRef | Ref | Default: — |
A ref for the HTML input element. | ||
defaultSelected | boolean | Default: — |
Whether the element should be selected (uncontrolled). | ||
isSelected | boolean | Default: — |
Whether the element should be selected (controlled). | ||
isDisabled | boolean | Default: — |
Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
Whether the input can be selected but not changed by the user. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the Switch. | ||
isEmphasized | boolean | Default: — |
Whether the Switch should be displayed with an emphasized style. | ||
onChange | | Default: — |
Handler that is called when the element's selection state changes. | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the input value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
description | ReactNode | Default: — |
A description for the field. Provides a hint such as specific requirements for what to choose. | ||
errorMessage | ReactNode | | Default: — |
An error message for the field. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
onPress | | Default: — |
Handler that is called when the press is released over the target. | ||
onPressStart | | Default: — |
Handler that is called when a press interaction starts. | ||
onPressEnd | | Default: — |
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. | ||
onPressChange | | Default: — |
Handler that is called when the press state changes. | ||
onPressUp | | Default: — |
Handler that is called when a press is released over the target, regardless of whether it started on the target or not. | ||
onClick | | Default: — |
Not recommended – use | ||
name | string | Default: — |
The name of the input element, used when submitting an HTML form. See MDN. | ||
value | string | Default: — |
The value of the input element, used when submitting an HTML form. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
excludeFromTabOrder | boolean | Default: — |
Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. | ||
aria-controls | string | Default: — |
Identifies the element (or elements) whose contents or presence are controlled by the current element. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
aria-errormessage | string | Default: — |
Identifies the element that provides an error message for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||