Skip to content

Switch

Switches allow users to toggle between two states, off and on.

Import

import { Switch } from "@cfa/react-core";

Live Editor

Examples

Description

Use the description prop to add a description to the switch. The description will be displayed below the switch.

AutoFocus

Use the autoFocus prop to automatically focus the switch when it is mounted.

Readonly

The isReadOnly prop makes the switch read-only, preventing user interaction.

Disabled

The isDisabled prop disables the switch, preventing user interaction.

Default Selected

Use the defaultSelected prop to set the initial state of the switch. The switch will be checked by default.

Controlled

To use the Switch in a controlled state, you can use the combination of isSelected which is a boolean and onChange which accepts a function.

Label Position

The labelPosition prop allows you to change the position of the label. The default is right.

Native HTML Forms

The Switch component can be used in native HTML forms. The name prop is required for the switch to be submitted with the form.

Props

Visit the Documentation Site for the full list of props.

NameTypeDefaultDescription
childrenReactNode-The label of the switch.
descriptionstring-Helper text for the switch.
inputRefRefObject<HTMLInputElement>-React 19 only
labelPosition"left" | "right" | "top"--
refRef<HTMLLabelElement>-React 19 only
aria-controlsstring-Identifies the element (or elements) whose contents or presence are controlled by the current element.
aria-describedbystring-Identifies the element (or elements) that describes the object.
aria-detailsstring-Identifies the element (or elements) that provide a detailed, extended description for the object.
aria-labelstring-Defines a string value that labels the current element.
aria-labelledbystring-Identifies the element (or elements) that labels the current element.
autoFocusboolean-Whether the element should receive focus on render.
classNameClassNameOrFunction<SwitchRenderProps>'react-aria-Switch'The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state.
defaultSelectedboolean-Whether the Switch should be selected (uncontrolled).
dirstring--
excludeFromTabOrderboolean-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.
formstring-The `<form>` element to associate the input with. The value of this attribute must be the id of a `<form>` in the same document. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form).
hiddenboolean--
idstring-The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).
inertboolean--
isDisabledboolean-Whether the input is disabled.
isReadOnlyboolean-Whether the input can be selected but not changed by the user.
isSelectedboolean-Whether the Switch should be selected (controlled).
langstring--
namestring-The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname).
onAnimationEndAnimationEventHandler<HTMLLabelElement>--
onAnimationEndCaptureAnimationEventHandler<HTMLLabelElement>--
onAnimationIterationAnimationEventHandler<HTMLLabelElement>--
onAnimationIterationCaptureAnimationEventHandler<HTMLLabelElement>--
onAnimationStartAnimationEventHandler<HTMLLabelElement>--
onAnimationStartCaptureAnimationEventHandler<HTMLLabelElement>--
onAuxClickMouseEventHandler<HTMLLabelElement>--
onAuxClickCaptureMouseEventHandler<HTMLLabelElement>--
onBlur(e: FocusEvent<Element, Element>) => void-Handler that is called when the element loses focus.
onChange(isSelected: boolean) => void-Handler that is called when the Switch's selection state changes.
onClickCaptureMouseEventHandler<HTMLLabelElement>--
onContextMenuMouseEventHandler<HTMLLabelElement>--
onContextMenuCaptureMouseEventHandler<HTMLLabelElement>--
onDoubleClickMouseEventHandler<HTMLLabelElement>--
onDoubleClickCaptureMouseEventHandler<HTMLLabelElement>--
onFocus(e: FocusEvent<Element, Element>) => void-Handler that is called when the element receives focus.
onFocusChange(isFocused: boolean) => void-Handler that is called when the element's focus status changes.
onGotPointerCapturePointerEventHandler<HTMLLabelElement>--
onGotPointerCaptureCapturePointerEventHandler<HTMLLabelElement>--
onHoverChange(isHovering: boolean) => void-Handler that is called when the hover state changes.
onHoverEnd(e: HoverEvent) => void-Handler that is called when a hover interaction ends.
onHoverStart(e: HoverEvent) => void-Handler that is called when a hover interaction starts.
onKeyDown(e: KeyboardEvent) => void-Handler that is called when a key is pressed.
onKeyUp(e: KeyboardEvent) => void-Handler that is called when a key is released.
onLostPointerCapturePointerEventHandler<HTMLLabelElement>--
onLostPointerCaptureCapturePointerEventHandler<HTMLLabelElement>--
onMouseDownMouseEventHandler<HTMLLabelElement>--
onMouseDownCaptureMouseEventHandler<HTMLLabelElement>--
onMouseEnterMouseEventHandler<HTMLLabelElement>--
onMouseLeaveMouseEventHandler<HTMLLabelElement>--
onMouseMoveMouseEventHandler<HTMLLabelElement>--
onMouseMoveCaptureMouseEventHandler<HTMLLabelElement>--
onMouseOutMouseEventHandler<HTMLLabelElement>--
onMouseOutCaptureMouseEventHandler<HTMLLabelElement>--
onMouseOverMouseEventHandler<HTMLLabelElement>--
onMouseOverCaptureMouseEventHandler<HTMLLabelElement>--
onMouseUpMouseEventHandler<HTMLLabelElement>--
onMouseUpCaptureMouseEventHandler<HTMLLabelElement>--
onPointerCancelPointerEventHandler<HTMLLabelElement>--
onPointerCancelCapturePointerEventHandler<HTMLLabelElement>--
onPointerDownPointerEventHandler<HTMLLabelElement>--
onPointerDownCapturePointerEventHandler<HTMLLabelElement>--
onPointerEnterPointerEventHandler<HTMLLabelElement>--
onPointerLeavePointerEventHandler<HTMLLabelElement>--
onPointerMovePointerEventHandler<HTMLLabelElement>--
onPointerMoveCapturePointerEventHandler<HTMLLabelElement>--
onPointerOutPointerEventHandler<HTMLLabelElement>--
onPointerOutCapturePointerEventHandler<HTMLLabelElement>--
onPointerOverPointerEventHandler<HTMLLabelElement>--
onPointerOverCapturePointerEventHandler<HTMLLabelElement>--
onPointerUpPointerEventHandler<HTMLLabelElement>--
onPointerUpCapturePointerEventHandler<HTMLLabelElement>--
onScrollUIEventHandler<HTMLLabelElement>--
onScrollCaptureUIEventHandler<HTMLLabelElement>--
onTouchCancelTouchEventHandler<HTMLLabelElement>--
onTouchCancelCaptureTouchEventHandler<HTMLLabelElement>--
onTouchEndTouchEventHandler<HTMLLabelElement>--
onTouchEndCaptureTouchEventHandler<HTMLLabelElement>--
onTouchMoveTouchEventHandler<HTMLLabelElement>--
onTouchMoveCaptureTouchEventHandler<HTMLLabelElement>--
onTouchStartTouchEventHandler<HTMLLabelElement>--
onTouchStartCaptureTouchEventHandler<HTMLLabelElement>--
onTransitionCancelTransitionEventHandler<HTMLLabelElement>--
onTransitionCancelCaptureTransitionEventHandler<HTMLLabelElement>--
onTransitionEndTransitionEventHandler<HTMLLabelElement>--
onTransitionEndCaptureTransitionEventHandler<HTMLLabelElement>--
onTransitionRunTransitionEventHandler<HTMLLabelElement>--
onTransitionRunCaptureTransitionEventHandler<HTMLLabelElement>--
onTransitionStartTransitionEventHandler<HTMLLabelElement>--
onTransitionStartCaptureTransitionEventHandler<HTMLLabelElement>--
onWheelWheelEventHandler<HTMLLabelElement>--
onWheelCaptureWheelEventHandler<HTMLLabelElement>--
renderDOMRenderFunction<"label", SwitchRenderProps>-Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: * You must render the expected element type (e.g. if `<button>` is expected, you cannot render an `<a>`). * Only a single root DOM element can be rendered (no fragments). * You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
slotstring-A slot name for the component. Slots allow the component to receive props from a parent component. An explicit `null` value indicates that the local props completely override all props received from a parent.
styleStyleOrFunction<SwitchRenderProps>-The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.
translate"yes" | "no"--
valuestring-The value of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefvalue).