useSwitch

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.

Example
Switch.tsx
Switch.css
import {Switch} from './Switch';

<Switch>Low power mode</Switch>

API

Shows a switch component with labels pointing to its parts, including the input, and label elements.Low power modeInputLabel
<SwitchField>
  <SwitchButton>Label</SwitchButton>
  <Text slot="description" />
  <FieldError />
</SwitchField>
useToggleState(props: ): useSwitch( props: , state: , ref: <HTMLInputElementnull> ):

ToggleState

Properties

NameType
isSelectedboolean

Whether the toggle is selected.

defaultSelectedboolean

Whether the toggle is selected by default.

Methods

setSelected(isSelected: boolean): void
Updates selection state.
toggle(): void
Toggle the selection state.

AriaSwitchProps

NameType
childrenReactNode

The label for the element.

defaultSelectedboolean

Whether the element should be selected (uncontrolled).

isSelectedboolean

Whether the element should be selected (controlled).

isDisabledboolean

Whether the input is disabled.

isReadOnlyboolean

Whether the input can be selected but not changed by the user.

onChange(isSelected: boolean) => void

Handler that is called when the element's selection state changes.

SwitchAria

NameType
labelPropsLabelHTMLAttributes<HTMLLabelElement>

Props for the label wrapper element.

inputPropsInputHTMLAttributes<HTMLInputElement>

Props for the input element.

descriptionProps<HTMLElement>

Props for the switch description element, if any.

errorMessageProps<HTMLElement>

Props for the switch error message element, if any.

isSelectedboolean

Whether the switch is selected.

isPressedboolean

Whether the switch is in a pressed state.

isDisabledboolean

Whether the switch is disabled.

isReadOnlyboolean

Whether the switch is read only.

validationDetailsValidityState

The native validation details for the input.