Provides the behavior and accessibility implementation for a switch component. A switch is similar to a checkbox, but represents on/off values as opposed to selection.
import {Switch} from './Switch';
<Switch>Low power mode</Switch>
API
<SwitchField>
<SwitchButton>Label</SwitchButton>
<Text slot="description" />
<FieldError />
</SwitchField>
useToggleState (props: ToggleStateOptions ): ToggleStateuseSwitch(
props: AriaSwitchProps,
state: ToggleState,
ref: RefObject <HTMLInputElement | null >
): SwitchAriaToggleState
Properties
| Name | Type | |
|---|---|---|
isSelected | boolean | |
Whether the toggle is selected. | ||
defaultSelected | boolean | |
Whether the toggle is selected by default. | ||
Methods
setSelected | ||
| Updates selection state. | ||
toggle | ||
| Toggle the selection state. | ||
AriaSwitchProps
| Name | Type | |
|---|---|---|
children | ReactNode | |
The label for the 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. | ||
onChange | | |
Handler that is called when the element's selection state changes. | ||
SwitchAria
| Name | Type | |
|---|---|---|
labelProps | LabelHTMLAttributes | |
Props for the label wrapper element. | ||
inputProps | InputHTMLAttributes | |
Props for the input element. | ||
descriptionProps | | |
Props for the switch description element, if any. | ||
errorMessageProps | DOMAttributesWithRef | |
Props for the switch error message element, if any. | ||
isSelected | boolean | |
Whether the switch is selected. | ||
isPressed | boolean | |
Whether the switch is in a pressed state. | ||
isDisabled | boolean | |
Whether the switch is disabled. | ||
isReadOnly | boolean | |
Whether the switch is read only. | ||
validationDetails | ValidityState | |
The native validation details for the input. | ||