Switch

Switch is a UI element that let users choose between two states.

Give FeedbackWAI-ARIABundle Size

Installation

Base UI components are all available as a single package.

npm install @base-ui-components/react

Once you have the package installed, import the component.

import { Switch } from '@base-ui-components/react/switch';

Anatomy

Switch is composed of two components:

<Switch.Root>
  <Switch.Thumb />
</Switch.Root>

Overriding default components

Use the render prop to override the root or thumb component:

<Switch.Root render={(props) => <MyCustomSwitch {...props} />}>
  <Switch.Thumb render={(props) => <MyCustomThumb {...props} />} />
</Switch.Root>

Accessibility

Ensure the Switch has an accessible name via a <label> element.

<Switch.Root id="my-switch">
  <Switch.Thumb />
</Switch.Root>
<label htmlFor="my-switch">
  My label
</label>

API Reference

SwitchRoot

The foundation for building custom-styled switches.

PropTypeDefaultDescription
checkedboolIf true, the switch is checked.
classNameunionClass names applied to the element or a function that returns them based on the component's state.
defaultCheckedboolfalseThe default checked state. Use when the component is uncontrolled.
disabledboolfalseIf true, the component is disabled and can't be interacted with.
inputRefcustomRef to the underlying input element.
namestringName of the underlying input element.
onCheckedChangefuncCallback fired when the checked state is changed.
readOnlyboolfalseIf true, the component is read-only. Functionally, this is equivalent to being disabled, but the assistive technologies will announce this differently.
renderunionA function to customize rendering of the component.
requiredboolfalseIf true, the switch must be checked for the browser validation to pass.

SwitchThumb

PropTypeDefaultDescription
classNameunionClass names applied to the element or a function that returns them based on the component's state.
renderunionA function to customize rendering of the component.

Contents