Switch
On/off control with label.
Examples
Default
Demo.tsxtsx
import { Switch } from '@var-ui/react';
<Switch>Enable notifications</Switch>Props
Accessibility
Built on React Aria Components where applicable.
On/off control with label.
On/off control with label.
import { Switch } from '@var-ui/react';
<Switch>Enable notifications</Switch>Built on React Aria Components where applicable.
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Label rendered beside the switch control. |
| Prop | Type | Default | Description |
|---|---|---|---|
aria-controls | string | undefined | — | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-describedby | string | undefined | — | Identifies the element (or elements) that describes the object. |
aria-details | string | undefined | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
aria-errormessage | string | undefined | — | Identifies the element that provides an error message for the object. |
aria-label | string | undefined | — | Defines a string value that labels the current element. |
aria-labelledby | string | undefined | — | Identifies the element (or elements) that labels the current element. |
autoFocus | boolean | undefined | — | Whether the element should receive focus on render. |
className | ClassNameOrFunction<SwitchRenderProps> | undefined | '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. |
dir | string | undefined | — | — |
excludeFromTabOrder | boolean | undefined | — | 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. |
form | string | undefined | — | 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). |
hidden | boolean | undefined | — | — |
id | string | undefined | — | The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id). |
inert | boolean | undefined | — | — |
inputRef | RefObject<HTMLInputElement | null> | undefined | — | A ref for the HTML input element. |
isDisabled | boolean | undefined | — | Whether the input is disabled. |
isReadOnly | boolean | undefined | — | Whether the input can be selected but not changed by the user. |
lang | string | undefined | — | — |
name | string | undefined | — | 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). |
onAnimationEnd | AnimationEventHandler<HTMLLabelElement> | undefined | — | — |
onAnimationEndCapture | AnimationEventHandler<HTMLLabelElement> | undefined | — | — |
onAnimationIteration | AnimationEventHandler<HTMLLabelElement> | undefined | — | — |
onAnimationIterationCapture | AnimationEventHandler<HTMLLabelElement> | undefined | — | — |
onAnimationStart | AnimationEventHandler<HTMLLabelElement> | undefined | — | — |
onAnimationStartCapture | AnimationEventHandler<HTMLLabelElement> | undefined | — | — |
onAuxClick | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onAuxClickCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onBlur | ((e: FocusEvent<Element, Element>) => void) | undefined | — | Handler that is called when the element loses focus. |
onClick | ((e: MouseEvent<FocusableElement, MouseEvent>) => void) | undefined | — | **Not recommended – use `onPress` instead.** `onClick` is an alias for `onPress` provided for compatibility with other libraries. `onPress` provides additional event details for non-mouse interactions. |
onClickCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onContextMenu | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onContextMenuCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onDoubleClick | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onDoubleClickCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onFocus | ((e: FocusEvent<Element, Element>) => void) | undefined | — | Handler that is called when the element receives focus. |
onFocusChange | ((isFocused: boolean) => void) | undefined | — | Handler that is called when the element's focus status changes. |
onGotPointerCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onGotPointerCaptureCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onHoverChange | ((isHovering: boolean) => void) | undefined | — | Handler that is called when the hover state changes. |
onHoverEnd | ((e: HoverEvent) => void) | undefined | — | Handler that is called when a hover interaction ends. |
onHoverStart | ((e: HoverEvent) => void) | undefined | — | Handler that is called when a hover interaction starts. |
onKeyDown | ((e: KeyboardEvent) => void) | undefined | — | Handler that is called when a key is pressed. |
onKeyUp | ((e: KeyboardEvent) => void) | undefined | — | Handler that is called when a key is released. |
onLostPointerCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onLostPointerCaptureCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseDown | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseDownCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseEnter | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseLeave | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseMove | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseMoveCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseOut | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseOutCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseOver | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseOverCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseUp | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onMouseUpCapture | MouseEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerCancel | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerCancelCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerDown | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerDownCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerEnter | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerLeave | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerMove | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerMoveCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerOut | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerOutCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerOver | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerOverCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerUp | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPointerUpCapture | PointerEventHandler<HTMLLabelElement> | undefined | — | — |
onPress | ((e: PressEvent) => void) | undefined | — | Handler that is called when the press is released over the target. |
onPressChange | ((isPressed: boolean) => void) | undefined | — | Handler that is called when the press state changes. |
onPressEnd | ((e: PressEvent) => void) | undefined | — | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
onPressStart | ((e: PressEvent) => void) | undefined | — | Handler that is called when a press interaction starts. |
onPressUp | ((e: PressEvent) => void) | undefined | — | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
onScroll | UIEventHandler<HTMLLabelElement> | undefined | — | — |
onScrollCapture | UIEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchCancel | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchCancelCapture | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchEnd | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchEndCapture | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchMove | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchMoveCapture | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchStart | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTouchStartCapture | TouchEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionCancel | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionCancelCapture | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionEnd | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionEndCapture | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionRun | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionRunCapture | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionStart | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onTransitionStartCapture | TransitionEventHandler<HTMLLabelElement> | undefined | — | — |
onWheel | WheelEventHandler<HTMLLabelElement> | undefined | — | — |
onWheelCapture | WheelEventHandler<HTMLLabelElement> | undefined | — | — |
render | DOMRenderFunction<"label", SwitchRenderProps> | undefined | — | 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. |
slot | string | null | undefined | — | 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. |
style | StyleOrFunction<SwitchRenderProps> | undefined | — | 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" | undefined | — | — |
| Prop | Type | Default | Description |
|---|---|---|---|
defaultSelected | boolean | undefined | — | Whether the element should be selected (uncontrolled). |
isSelected | boolean | undefined | — | Whether the element should be selected (controlled). |
onChange | ((isSelected: boolean) => void) | undefined | — | Handler that is called when the element's selection state changes. |
value | string | undefined | — | 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). |
No dedicated style recipe table for switch. Check the HTML demo markup and @var-ui/core recipes, or see Customize for
theme overrides.